Skip to content

Commit

Permalink
improve average performance of long task timer for out-of-order start…
Browse files Browse the repository at this point in the history
…/stop of many tasks
  • Loading branch information
fogninid committed Oct 16, 2024
1 parent 6b73a9f commit 44f147a
Showing 1 changed file with 3 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ConcurrentSkipListSet;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
Expand All @@ -55,8 +54,6 @@ public class DefaultLongTaskTimer extends AbstractMeter implements LongTaskTimer
*/
private final NavigableSet<SampleImpl> activeTasks = new ConcurrentSkipListSet<>();

private final AtomicLong taskId = new AtomicLong();

private final Clock clock;

private final TimeUnit baseTimeUnit;
Expand Down Expand Up @@ -98,7 +95,7 @@ public DefaultLongTaskTimer(Id id, Clock clock, TimeUnit baseTimeUnit,

@Override
public Sample start() {
SampleImpl sample = new SampleImpl(taskId.incrementAndGet());
SampleImpl sample = new SampleImpl();
activeTasks.add(sample);
return sample;
}
Expand Down Expand Up @@ -231,12 +228,9 @@ class SampleImpl extends Sample implements Comparable<SampleImpl> {

private final long startTime;

private final long id;

private volatile boolean stopped;

private SampleImpl(long id) {
this.id = id;
private SampleImpl() {
this.startTime = clock.monotonicTime();
}

Expand Down Expand Up @@ -268,7 +262,7 @@ public String toString() {
public int compareTo(DefaultLongTaskTimer.SampleImpl o) {
int startCompare = Long.compare(startTime, o.startTime);
if (startCompare == 0) {
return Long.compare(id, o.id);
return Long.compare(hashCode(), o.hashCode());
}
return startCompare;
}
Expand Down

0 comments on commit 44f147a

Please sign in to comment.