-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Conversation
a0129cb
to
a316b78
Compare
/cc @Aneurysm9 |
Codecov Report
@@ 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
Continue to review full report at Codecov.
|
a316b78
to
a6a1c12
Compare
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.
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.
*am.orig = make([]otlpcommon.KeyValue, 0, capacity) | ||
copy(*am.orig, oldOrig) |
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.
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.
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.
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) |
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.
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.
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.
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.
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.
In contrib I plan to replace with Clear + EnsureCapacity so I don't have to manually check all usages.
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.
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.
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.
Ok, just to make the reviewer happy :)
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.
:-)
This PR was marked stale due to lack of activity. It will be closed in 7 days. |
a6a1c12
to
0d17736
Compare
Signed-off-by: Bogdan Drutu <[email protected]>
0d17736
to
2bfee7e
Compare
Signed-off-by: Bogdan Drutu <[email protected]>
Updates #2488
Signed-off-by: Bogdan Drutu [email protected]