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

Replace InitEmptyWithCapacity with EnsureCapacity and Clear #2845

Merged
merged 2 commits into from
Apr 15, 2021

Conversation

bogdandrutu
Copy link
Member

Updates #2488

Signed-off-by: Bogdan Drutu [email protected]

@bogdandrutu bogdandrutu requested a review from a team March 30, 2021 00:21
@bogdandrutu bogdandrutu force-pushed the ensurecapacity branch 2 times, most recently from a0129cb to a316b78 Compare March 30, 2021 00:25
@bogdandrutu
Copy link
Member Author

/cc @Aneurysm9

@codecov
Copy link

codecov bot commented Mar 30, 2021

Codecov Report

Merging #2845 (2bfee7e) into main (03c7bf9) will decrease coverage by 0.00%.
The diff coverage is 94.28%.

❗ Current head 2bfee7e differs from pull request most recent head 13f453f. Consider uploading reports for the commit 13f453f to get more accurate results
Impacted file tree graph

@@            Coverage Diff             @@
##             main    #2845      +/-   ##
==========================================
- Coverage   91.66%   91.66%   -0.01%     
==========================================
  Files         312      312              
  Lines       15317    15308       -9     
==========================================
- Hits        14041    14032       -9     
  Misses        870      870              
  Partials      406      406              
Impacted Files Coverage Δ
...eceiver/internal/scraper/processscraper/process.go 80.00% <77.77%> (-4.62%) ⬇️
consumer/pdata/common.go 98.93% <100.00%> (+0.01%) ⬆️
processor/resourceprocessor/resource_processor.go 100.00% <100.00%> (ø)
translator/internaldata/oc_to_metrics.go 91.24% <100.00%> (ø)
translator/internaldata/oc_to_resource.go 100.00% <100.00%> (ø)
translator/internaldata/oc_to_traces.go 90.21% <100.00%> (ø)
translator/trace/jaeger/jaegerproto_to_traces.go 90.67% <100.00%> (ø)
translator/trace/jaeger/jaegerthrift_to_traces.go 89.79% <100.00%> (ø)
translator/trace/zipkin/zipkinv2_to_traces.go 89.91% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 03c7bf9...13f453f. Read the comment docs.

Copy link
Member

@tigrannajaryan tigrannajaryan left a comment

Choose a reason for hiding this comment

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

Please check carefully if change of semantic affects the code. If it does not affect it change the function signatures to make it clear that we are dealing with new slices and they are already empty. Otherwise the code looks wrong.

Comment on lines +401 to +408
*am.orig = make([]otlpcommon.KeyValue, 0, capacity)
copy(*am.orig, oldOrig)
Copy link
Member

Choose a reason for hiding this comment

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

Append may be faster to increase the capacity, assuming memory manager has a way to extend the allocated block without reallocation (but I did not verify): https://github.com/golang/go/wiki/SliceTricks#extend
It may be worth benchmarking.

Copy link
Member Author

Choose a reason for hiding this comment

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

We can play with the internal implementation.

@@ -250,7 +250,7 @@ func setLabelsMap(ocLabelsKeys []*ocmetrics.LabelKey, ocLabelValues []*ocmetrics
lablesCount = len(ocLabelValues)
}

labelsMap.InitEmptyWithCapacity(lablesCount)
labelsMap.EnsureCapacity(lablesCount)
Copy link
Member

Choose a reason for hiding this comment

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

Change in behvaior if labelsMap was not empty before. Is this guaranteed? If this is the case I suggest changing setLabelsMap to return labelsMap pdata.StringMap instead of accepting it as a parameter.

Copy link
Member Author

Choose a reason for hiding this comment

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

Returning is not a good pattern for the way we want users to use our pdata. Returning means I need to have a SetLablesMap which we don't have because we want users to construct things top-down to avoid extra memory allocation when copying to the parent struct.

In this case, as in all the other cases we start with an empty object during conversion, so there is no problem with having previous values.

Copy link
Member Author

Choose a reason for hiding this comment

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

In contrib I plan to replace with Clear + EnsureCapacity so I don't have to manually check all usages.

Copy link
Member

Choose a reason for hiding this comment

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

It is not apparent from setLabelsMap signature that labelsMap is empty. By looking at setLabelsMap code only it is not possible to tell if the code is correct. To know that the code is correct you have to analyse the call site where setLabelsMap.

What do you think about calling Clear() here too (like you want to do in contrib), or declare the requirement in setLabelsMap doccomment that labelsMap must be empty? I think calling Clear() is fine, it should have very little cost.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok, just to make the reviewer happy :)

Copy link
Member

Choose a reason for hiding this comment

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

:-)

translator/internaldata/oc_to_metrics.go Outdated Show resolved Hide resolved
translator/internaldata/oc_to_resource.go Show resolved Hide resolved
translator/internaldata/oc_to_traces.go Outdated Show resolved Hide resolved
@github-actions
Copy link
Contributor

github-actions bot commented Apr 9, 2021

This PR was marked stale due to lack of activity. It will be closed in 7 days.

@github-actions github-actions bot added the Stale label Apr 9, 2021
@bogdandrutu bogdandrutu removed the Stale label Apr 12, 2021
@bogdandrutu bogdandrutu changed the title Replace InitEmptyWithCapacity with EnsureCapacity Replace InitEmptyWithCapacity with EnsureCapacity and Clear Apr 14, 2021
Signed-off-by: Bogdan Drutu <[email protected]>
@bogdandrutu bogdandrutu merged commit 9ff58cb into open-telemetry:main Apr 15, 2021
@bogdandrutu bogdandrutu deleted the ensurecapacity branch April 15, 2021 17:15
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.

2 participants