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

Cache ImmutableKeyValuePairs#hashCode #5307

Merged
merged 1 commit into from
Mar 18, 2023

Conversation

jack-berg
Copy link
Member

The metrics hot path involves finding the relevant AggregationHandle in a ConcurrentHashmap for a given measurement based on its Attributes. We recommend that callers save references to common Attributes to prevent extra allocations on each measurement. In this case, callers can benefit from caching Attributes#hashCode so that it doesn't need to be recomputed each time we record a measurement and do a lookup in the ConcurrentHashmap.

I've added benchmarks to AttributesBenchmark, which shows a ~15x improvement in time to compute hashCode (not surprising). But its more interesting to see the performance improvement in an actual metrics scenario. I've extended MetricsBenchmark to include a case where there's enough cardinality to make the map lookup not completely trivial (the other benchmarks only have a single attributes entry). The performance gains are modest since there's a lot more going on when performing a measurement besides computing a hashcode, but non-zero. Check out the before and after for more details.

@jack-berg jack-berg requested a review from a team March 16, 2023 20:26
@codecov
Copy link

codecov bot commented Mar 16, 2023

Codecov Report

Patch coverage: 100.00% and no project coverage change.

Comparison is base (302f889) 90.97% compared to head (c07c5aa) 90.98%.

Additional details and impacted files
@@            Coverage Diff            @@
##               main    #5307   +/-   ##
=========================================
  Coverage     90.97%   90.98%           
- Complexity     4907     4908    +1     
=========================================
  Files           552      552           
  Lines         14489    14492    +3     
  Branches       1372     1373    +1     
=========================================
+ Hits          13181    13185    +4     
  Misses          907      907           
+ Partials        401      400    -1     
Impacted Files Coverage Δ
...telemetry/api/internal/ImmutableKeyValuePairs.java 98.94% <100.00%> (+0.03%) ⬆️

... and 1 file with indirect coverage changes

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@@ -32,6 +32,7 @@
@Immutable
public abstract class ImmutableKeyValuePairs<K, V> {
private final Object[] data;
private int hashcode;
Copy link
Contributor

Choose a reason for hiding this comment

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

do we need to mark as volatile for thread safety?

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 dont think so. Reading volatiles isn't free, and its not the end of the world if the hashcode gets computed multiple times.

@jack-berg jack-berg merged commit 3581d6e into open-telemetry:main Mar 18, 2023
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