-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
[chore] preallocate slice #24956
[chore] preallocate slice #24956
Conversation
4f27357
to
6ecd931
Compare
@@ -200,8 +200,8 @@ func withExtractAnnotations(annotations ...FieldExtractConfig) option { | |||
} | |||
|
|||
func extractFieldRules(fieldType string, fields ...FieldExtractConfig) ([]kube.FieldExtractionRule, error) { | |||
var rules []kube.FieldExtractionRule | |||
for _, a := range fields { | |||
rules := make([]kube.FieldExtractionRule, len(fields)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rules := make([]kube.FieldExtractionRule, len(fields)) | |
rules := make([]kube.FieldExtractionRule, 0, len(fields)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After merging a few PRs like this, I'm not 100% sure about introducing this linter. In some cases (like this one), where the list is usually small and the iteration is often not triggered due to an empty (or nil) list after range
, it's beneficial and more performant to not pre-allocate the slice and keep it nil
.
I'd personally vote for not having this linter, but I'm ok with introducing it if @open-telemetry/collector-contrib-approvers think otherwise
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd also vote not to use it. Some of the PRs I reviewed seemed to decrease readability slightly, which I'd only find worthwhile if the code is in a hot path. I think it's a simple enough thing to decide and/or change case by case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree on letting this be a per-component choice. Hopefully we can use benchmarks/profiling to find areas where this is truly useful.
Closing this PR as this particular optimization is rejected since it ends up creating additional objects when nil may be returned. |
No description provided.