Skip to content

Commit

Permalink
Use IntUnaryOperator in Gradient2Limit for queueSize
Browse files Browse the repository at this point in the history
Use IntUnaryOperator in Gradient2Limit for queueSize to avoid boxing. Also, avoid excessive
volatile reads and avoid boxing when debug logging is disabled.
  • Loading branch information
kilink committed Sep 18, 2024
1 parent daebd31 commit c4ec2c4
Showing 1 changed file with 16 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.function.IntUnaryOperator;

/**
* Concurrency limit algorithm that adjusts the limit based on the gradient of change of the current average RTT and
Expand Down Expand Up @@ -76,7 +77,7 @@ public static class Builder {
private int maxConcurrency = 200;

private double smoothing = 0.2;
private Function<Integer, Integer> queueSize = concurrency -> 4;
private IntUnaryOperator queueSize = concurrency -> 4;
private MetricRegistry registry = EmptyMetricRegistry.INSTANCE;
private int longWindow = 600;
private double rttTolerance = 1.5;
Expand Down Expand Up @@ -129,8 +130,15 @@ public Builder queueSize(int queueSize) {
* latencies remain low as a function of the current limit.
* @param queueSize
* @return Chainable builder
* @deprecated use {@link #queueSizeFunction(IntUnaryOperator)}
*/
@Deprecated
public Builder queueSize(Function<Integer, Integer> queueSize) {
this.queueSize = queueSize::apply;
return this;
}

public Builder queueSizeFunction(IntUnaryOperator queueSize) {
this.queueSize = queueSize;
return this;
}
Expand Down Expand Up @@ -231,7 +239,7 @@ public static Gradient2Limit newDefault() {

private final int minLimit;

private final Function<Integer, Integer> queueSize;
private final IntUnaryOperator queueSize;

private final double smoothing;

Expand Down Expand Up @@ -262,10 +270,11 @@ private Gradient2Limit(Builder builder) {

@Override
public int _update(final long startTime, final long rtt, final int inflight, final boolean didDrop) {
final double queueSize = this.queueSize.apply((int)this.estimatedLimit);
double estimatedLimit = this.estimatedLimit;
final double queueSize = this.queueSize.applyAsInt((int) estimatedLimit);

this.lastRtt = rtt;
final double shortRtt = (double)rtt;
final double shortRtt = (double) rtt;
final double longRtt = this.longRtt.add(rtt).doubleValue();

shortRttSampleListener.addSample(shortRtt);
Expand Down Expand Up @@ -293,7 +302,7 @@ public int _update(final long startTime, final long rtt, final int inflight, fin
newLimit = estimatedLimit * (1 - smoothing) + newLimit * smoothing;
newLimit = Math.max(minLimit, Math.min(maxLimit, newLimit));

if ((int)estimatedLimit != newLimit) {
if ((int) estimatedLimit != newLimit && LOG.isDebugEnabled()) {
LOG.debug("New limit={} shortRtt={} ms longRtt={} ms queueSize={} gradient={}",
(int)newLimit,
getLastRtt(TimeUnit.MICROSECONDS) / 1000.0,
Expand All @@ -302,9 +311,9 @@ public int _update(final long startTime, final long rtt, final int inflight, fin
gradient);
}

estimatedLimit = newLimit;
this.estimatedLimit = newLimit;

return (int)estimatedLimit;
return (int) newLimit;
}

public long getLastRtt(TimeUnit units) {
Expand Down

0 comments on commit c4ec2c4

Please sign in to comment.