Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve slice usage from pooled objects #160

Merged
merged 9 commits into from
Sep 26, 2023

Conversation

kruskall
Copy link
Member

@kruskall kruskall commented Sep 22, 2023

Followup to #158

Part 3 of #152

Objects retrieved from the vtpool have preallocated empty slices. We should reuse them instead of creating new one.

Related to #130

@kruskall kruskall requested a review from a team as a code owner September 22, 2023 20:27
Copy link
Contributor

@marclop marclop left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

were you able to run any benchmarks with this change?

@@ -45,18 +45,18 @@ func HTTPHeadersToStructPb(h http.Header) *structpb.Struct {
return nil
}

func HTTPHeadersToModelpb(h http.Header) []*modelpb.HTTPHeader {
func HTTPHeadersToModelpb(h http.Header, out []*modelpb.HTTPHeader) []*modelpb.HTTPHeader {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem right, we're passing out and also returning out?

Copy link
Member Author

@kruskall kruskall Sep 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If out is not big enough we might append to it, so we need to return a slice because they might not match

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find this API a bit confusing, I think it'd be best to do one or the other. If we want to accept the out as an argument, then we can pass out *[]*modelpb.HTTPHeader

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this signature looks similar to s = append(s, struct{}), so that's an argument for this pattern. But we'll have to remember to use the return value.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But for conversion purposes like this specific one, I think @marclop 's comment makes sense. It will be clearer.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit conflicted. This method was inspired by the slices package in the stdlib (https://pkg.go.dev/slices)

There are several methods/API there that returns the result (modified slice) despite changing the argument (Compact, Grow, Replace, Clip, etc.). I assume they might in turn have copied the signature of append.

I think this is a common pattern in go ecosystem but I don't feel strongly about it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for chiming in. I was unaware of the widespread apdoption of this pattern in the current Go stdlib. I still think it's clearer and simpler to accept a single argument. If we leave it as is, I would prefer if we documented the behaviour since there are currently no godocs for these functions.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

func ToKv(m map[string]any) []*modelpb.KeyValue {
nm := normalizeMap(m)
if len(nm) == 0 {
func ToKv(m map[string]any, out []*modelpb.KeyValue) []*modelpb.KeyValue {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

@kruskall kruskall merged commit e5607a5 into elastic:main Sep 26, 2023
5 checks passed
@kruskall kruskall deleted the feat/vtpool-slice branch September 26, 2023 16:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants