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

[chore] preallocate slice #24956

Closed
wants to merge 2 commits into from
Closed

Conversation

atoulme
Copy link
Contributor

@atoulme atoulme commented Aug 5, 2023

No description provided.

@atoulme atoulme requested a review from a team August 5, 2023 22:21
@github-actions github-actions bot added the processor/k8sattributes k8s Attributes processor label Aug 5, 2023
@@ -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))
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
rules := make([]kube.FieldExtractionRule, len(fields))
rules := make([]kube.FieldExtractionRule, 0, len(fields))

Copy link
Member

@dmitryax dmitryax Aug 8, 2023

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

Copy link
Member

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.

Copy link
Member

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.

@atoulme
Copy link
Contributor Author

atoulme commented Aug 14, 2023

Closing this PR as this particular optimization is rejected since it ends up creating additional objects when nil may be returned.

@atoulme atoulme closed this Aug 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
processor/k8sattributes k8s Attributes processor
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants