Skip to content

Commit

Permalink
Avoid allocating lambda in PercentileTimer.computeIfAbsent
Browse files Browse the repository at this point in the history
PercentileTimer.computeIfAbsent would delegate to Utils.computeIfAbsent, which actually
eagerly calls get and then putIfAbsent if the timer is not in the underlying map. Just directly
do that to avoid needing to allocate a lambda in the case the timer is present.
  • Loading branch information
kilink committed Mar 20, 2024
1 parent 0195a20 commit 143814a
Showing 1 changed file with 8 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,14 @@ public final class PercentileTimer implements Timer {
* site.
*/
private static PercentileTimer computeIfAbsent(Registry registry, Id id, long min, long max) {
Object timer = Utils.computeIfAbsent(
registry.state(), id, i -> new PercentileTimer(registry, id, min, max));
Object timer = registry.state().get(id);
if (timer == null) {
PercentileTimer newTimer = new PercentileTimer(registry, id, min, max);
timer = registry.state().putIfAbsent(id, newTimer);
if (timer == null) {
return newTimer;
}
}
return (timer instanceof PercentileTimer)
? ((PercentileTimer) timer).withRange(min, max)
: new PercentileTimer(registry, id, min, max);
Expand Down

0 comments on commit 143814a

Please sign in to comment.