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

Avoid allocating lambda in PercentileTimer.computeIfAbsent #1128

Merged
merged 2 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ subprojects {
profilers = ['stack', 'gc']
includeTests = false
duplicateClassesStrategy = DuplicatesStrategy.WARN
includes = ['.*Caching.*']
includes = ['.*PercentileTimers.*']
}

checkstyle {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2019 Netflix, Inc.
* Copyright 2014-2024 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -22,7 +22,6 @@
import com.netflix.spectator.api.Measurement;
import com.netflix.spectator.api.Registry;
import com.netflix.spectator.api.Statistic;
import com.netflix.spectator.api.Utils;
import com.netflix.spectator.api.patterns.IdBuilder;
import com.netflix.spectator.api.patterns.TagsBuilder;

Expand Down Expand Up @@ -66,8 +65,20 @@ public final class PercentileDistributionSummary implements DistributionSummary
*/
private static PercentileDistributionSummary computeIfAbsent(
Registry registry, Id id, long min, long max) {
Object summary = Utils.computeIfAbsent(
registry.state(), id, i -> new PercentileDistributionSummary(registry, id, min, max));
// Inlined:
// Object summary = Utils.computeIfAbsent(
// registry.state(), id, i -> new PercentileDistributionSummary(registry, id, min, max));
//
// The lambda needs to capture the parameters for registry, min, and max which can lead to
// a high allocation rate for the lambda instance.
Object summary = registry.state().get(id);
if (summary == null) {
PercentileDistributionSummary newSummary = new PercentileDistributionSummary(registry, id, min, max);
summary = registry.state().putIfAbsent(id, newSummary);
if (summary == null) {
return newSummary;
}
}
return (summary instanceof PercentileDistributionSummary)
? ((PercentileDistributionSummary) summary).withRange(min, max)
: new PercentileDistributionSummary(registry, id, min, max);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2023 Netflix, Inc.
* Copyright 2014-2024 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -23,7 +23,6 @@
import com.netflix.spectator.api.Registry;
import com.netflix.spectator.api.Statistic;
import com.netflix.spectator.api.Timer;
import com.netflix.spectator.api.Utils;
import com.netflix.spectator.api.patterns.IdBuilder;
import com.netflix.spectator.api.patterns.TagsBuilder;

Expand Down Expand Up @@ -72,8 +71,20 @@ 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));
// Inlined:
// Object timer = Utils.computeIfAbsent(
// registry.state(), id, i -> new PercentileTimer(registry, id, min, max));
//
// The lambda needs to capture the parameters for registry, min, and max which can lead to
// a high allocation rate for the lambda instance.
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
Loading