From acbd7141404d503e7ca89bec520a175482d41bce Mon Sep 17 00:00:00 2001 From: Adrien Grand Date: Wed, 24 Jul 2024 14:58:53 +0200 Subject: [PATCH] Further reduce the search concurrency overhead. (#13606) This iterates on #13546 to further reduce the overhead of search concurrency by caching whether the hit count threshold has been reached: once the threshold has been reached, it cannot get "un-reached" again, so we don't need to pay the cost of `LongAdder#longValue`. --- .../apache/lucene/search/HitsThresholdChecker.java | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/lucene/core/src/java/org/apache/lucene/search/HitsThresholdChecker.java b/lucene/core/src/java/org/apache/lucene/search/HitsThresholdChecker.java index 4232af53fe6..43ff4fecdbb 100644 --- a/lucene/core/src/java/org/apache/lucene/search/HitsThresholdChecker.java +++ b/lucene/core/src/java/org/apache/lucene/search/HitsThresholdChecker.java @@ -24,6 +24,10 @@ abstract class HitsThresholdChecker { /** Implementation of HitsThresholdChecker which allows global hit counting */ private static class GlobalHitsThresholdChecker extends HitsThresholdChecker { private final LongAdder globalHitCount = new LongAdder(); + // Cache whether the threshold has been reached already. It is not volatile or synchronized on + // purpose to contain the overhead of reading the value similarly to what String#hashCode() + // does. This does not affect correctness. + private boolean thresholdReached = false; GlobalHitsThresholdChecker(int totalHitsThreshold) { super(totalHitsThreshold); @@ -32,12 +36,17 @@ abstract class HitsThresholdChecker { @Override void incrementHitCount() { - globalHitCount.increment(); + if (thresholdReached == false) { + globalHitCount.increment(); + } } @Override boolean isThresholdReached() { - return globalHitCount.longValue() > getHitsThreshold(); + if (thresholdReached) { + return true; + } + return thresholdReached = globalHitCount.longValue() > getHitsThreshold(); } @Override