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

capacity-limiter-api: avoid division in EMA #3030

Merged
merged 1 commit into from
Aug 5, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public double observe(long timestampNs, final long latency) {
*/
final class EMA implements LatencyTracker {

private final double tau;
private final double invTau;
@Nullable
private final LatencyTracker calmTracker;
@Nullable
Expand All @@ -75,7 +75,7 @@ final class EMA implements LatencyTracker {
* @param halfLifeNs The decay window of the EMA.
*/
EMA(final long halfLifeNs) {
this.tau = halfLifeNs / log(2);
this.invTau = 1.0 / (halfLifeNs / log(2));
this.calmTracker = null;
this.calmRatio = null;
}
Expand All @@ -94,7 +94,7 @@ final class EMA implements LatencyTracker {
EMA(final long halfLifeNs, final LatencyTracker calmTracker,
final BiFunction<Double, Double, Float> calmRatio) {
NumberUtils.ensurePositive(halfLifeNs, "halfLifeNs");
this.tau = halfLifeNs / log(2);
this.invTau = 1.0 / (halfLifeNs / log(2));
this.calmTracker = requireNonNull(calmTracker);
this.calmRatio = requireNonNull(calmRatio);
}
Expand All @@ -112,7 +112,7 @@ public double observe(long timestampNs, final long latency) {
}
}

final double tmp = (timestampNs - lastTimeNanos) / tau;
final double tmp = (timestampNs - lastTimeNanos) * invTau;
final double w = exp(-tmp);
ewma = ewma * w + latency * (1d - w);
lastTimeNanos = timestampNs;
Expand Down
Loading