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`.
This commit is contained in:
Adrien Grand 2024-07-24 14:58:53 +02:00 committed by GitHub
parent 97d066dd6b
commit acbd714140
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 11 additions and 2 deletions

View File

@ -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() {
if (thresholdReached == false) {
globalHitCount.increment();
}
}
@Override
boolean isThresholdReached() {
return globalHitCount.longValue() > getHitsThreshold();
if (thresholdReached) {
return true;
}
return thresholdReached = globalHitCount.longValue() > getHitsThreshold();
}
@Override