-
Notifications
You must be signed in to change notification settings - Fork 5
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
Introduce CachedToStringMetricKey and CachedToStringMetricName for Improved Performance #127
Introduce CachedToStringMetricKey and CachedToStringMetricName for Improved Performance #127
Conversation
d6088f0
to
334ce0e
Compare
4ed7daa
to
429075a
Compare
429075a
to
a64ed43
Compare
public String toString() { | ||
return stepName() + ":" + metricName(); | ||
} | ||
|
||
public static MetricKey create(@Nullable String stepName, MetricName metricName) { | ||
return new AutoValue_MetricKey(stepName, metricName); | ||
return new CachedToStringMetricKey(new AutoValue_MetricKey(stepName, metricName)); |
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.
ha, I thought that we will create a CachedToStringMetricKey
, that replaces the AutoValue_MetricKey instead of wrapping the AutoValue_MetricKey. But either way should be fine.
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.
Not a fan of autovalue. :)
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.
Haha, I see what you mean! Initially, I also thought about directly creating a CachedToStringMetricKey to replace AutoValue_MetricKey, but I realized that AutoValue provides its own implementations of .equals and .hashcode, which I wanted to preserve. That’s why it evolved into the wrapper approach—my goal was to minimize changes while keeping the core behavior intact.
public String toString() { | ||
return getNamespace() + ":" + getName(); | ||
} | ||
|
||
public static MetricName named(String namespace, String name) { | ||
checkArgument(!Strings.isNullOrEmpty(namespace), "Metric namespace must be non-empty"); | ||
checkArgument(!Strings.isNullOrEmpty(name), "Metric name must be non-empty"); | ||
return new AutoValue_MetricName(namespace, name); | ||
return new CachedToStringMetricName(new AutoValue_MetricName(namespace, name)); |
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.
ditto
} | ||
|
||
public static MetricName named(Class<?> namespace, String name) { | ||
checkArgument(namespace != null, "Metric namespace must be non-null"); | ||
checkArgument(!Strings.isNullOrEmpty(name), "Metric name must be non-empty"); | ||
return new AutoValue_MetricName(namespace.getName(), name); | ||
return new CachedToStringMetricName(new AutoValue_MetricName(namespace.getName(), name)); |
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.
ditto
PR Description:
Introduce
CachedToStringMetricKey
andCachedToStringMetricName
for Improved PerformanceSummary:
This PR introduces two new classes,
CachedToStringMetricKey
andCachedToStringMetricName
, which cache the result of thetoString()
method to improve performance in scenarios wheretoString()
is frequently called.Key Changes:
MetricKey
that caches thetoString()
result upon construction, reducing overhead from repeatedtoString()
calls in performance-sensitive areas.CachedToStringMetricKey
, this class wraps aroundMetricName
and caches itstoString()
value for efficiency, while still delegating all other methods to the originalMetricName
.equals()
andhashCode()
Overrides: To ensure consistency in collections,equals()
andhashCode()
were overridden to delegate these comparisons to the underlyingMetricName
andMetricKey
instances. This ensures correct behavior when these objects are used as keys in maps.@Nullable
Parameters: Updated theequals()
method to handle@Nullable
parameters correctly, following Java’s contract forequals()
and ensuring compatibility with null comparisons.@Memoized
inMetricKey
andMetricName
with the new cached classes, removing the overhead caused by the@Memoized
pattern.Motivation:
The use of
@Memoized
for thetoString()
method in the auto-generatedMetricKey
andMetricName
classes introduces unnecessary overhead due to its thread-safe memoization pattern, which includes additional synchronization logic. This pattern creates atransient volatile String toString
field and performs a synchronized check with everytoString()
call. This can degrade performance, especially whentoString()
is frequently invoked in performance-sensitive parts of the metrics system.By caching the
toString()
value at construction, we eliminate this overhead, leading to significant performance improvements. Benchmarks have shown that even directly computingtoString()
is faster than the memoized implementation. The new approach optimizes for frequenttoString()
invocations without the cost of synchronization.Changes:
@Memoized
annotations fromMetricKey
andMetricName
.MetricKey.create()
to useCachedToStringMetricKey
andMetricName
construction to useCachedToStringMetricName
.toString()
and ensured consistent usage across the codebase to avoid potential issues with mixed types in collections.Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:
addresses #123
), if applicable. This will automatically add a link to the pull request in the issue. If you would like the issue to automatically close on merging the pull request, commentfixes #<ISSUE NUMBER>
instead.CHANGES.md
with noteworthy changes.See the Contributor Guide for more tips on how to make review process smoother.
To check the build health, please visit https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md
GitHub Actions Tests Status (on master branch)
See CI.md for more information about GitHub Actions CI.