You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by ab...@apache.org on 2024/10/21 14:38:17 UTC

(lucene) branch main updated: Speedup OrderIntervalsSource some more (#13937)

This is an automated email from the ASF dual-hosted git repository.

abr pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/lucene.git


The following commit(s) were added to refs/heads/main by this push:
     new d1e017f1927 Speedup OrderIntervalsSource some more (#13937)
d1e017f1927 is described below

commit d1e017f1927e2539ead23e36e7136bf6bdf3993a
Author: Armin Braun <me...@obrown.io>
AuthorDate: Mon Oct 21 16:38:12 2024 +0200

    Speedup OrderIntervalsSource some more (#13937)
    
    Follow-up to #13871, getting another speedup from relatively trivial changes:
    
    * avoid redundant `end()` call by directly storing the end value for sub-iterator that we don't use for anything else
        * also save most `get(...)` calls for this sub-iterator
    * avoid redundant `start()` call by grabbing `start()` directly from `nextInterval`
    * replace `getFirst()` with `get(0)`, it looks nice but has needless overhead in my testing (not sure why, but profiling clearly shows it to be slower, maybe just a result of having `get()`'s code hot in the cache with a higher likelihood or something esoteric like that)
    * avoid using an iterator for loop for a random access list, this is probably the biggest win in this PR
---
 .../queries/intervals/OrderedIntervalsSource.java    | 20 ++++++++++++--------
 1 file changed, 12 insertions(+), 8 deletions(-)

diff --git a/lucene/queries/src/java/org/apache/lucene/queries/intervals/OrderedIntervalsSource.java b/lucene/queries/src/java/org/apache/lucene/queries/intervals/OrderedIntervalsSource.java
index c855d61d051..fc1588c8bdf 100644
--- a/lucene/queries/src/java/org/apache/lucene/queries/intervals/OrderedIntervalsSource.java
+++ b/lucene/queries/src/java/org/apache/lucene/queries/intervals/OrderedIntervalsSource.java
@@ -127,9 +127,9 @@ class OrderedIntervalsSource extends MinimizingConjunctionIntervalsSource {
       final var subIterators = this.subIterators;
       int currentIndex = i;
       while (true) {
+        int prevEnd = subIterators.get(currentIndex - 1).end();
         while (true) {
-          var prev = subIterators.get(currentIndex - 1);
-          if (prev.end() >= lastStart) {
+          if (prevEnd >= lastStart) {
             i = currentIndex;
             return start;
           }
@@ -137,19 +137,21 @@ class OrderedIntervalsSource extends MinimizingConjunctionIntervalsSource {
             break;
           }
           final IntervalIterator current = subIterators.get(currentIndex);
-          if (minimizing && (current.start() > prev.end())) {
+          if (minimizing && (current.start() > prevEnd)) {
             break;
           }
+          int currentStart;
           do {
             if (current.end() >= lastStart
-                || current.nextInterval() == IntervalIterator.NO_MORE_INTERVALS) {
+                || (currentStart = current.nextInterval()) == IntervalIterator.NO_MORE_INTERVALS) {
               i = currentIndex;
               return start;
             }
-          } while (current.start() <= prev.end());
+          } while (currentStart <= prevEnd);
           currentIndex++;
+          prevEnd = current.end();
         }
-        var first = subIterators.getFirst();
+        var first = subIterators.get(0);
         final int start = first.start();
         this.start = start;
         if (start == NO_MORE_INTERVALS) {
@@ -161,8 +163,10 @@ class OrderedIntervalsSource extends MinimizingConjunctionIntervalsSource {
         final int end = last.end();
         this.end = end;
         int slop = end - start + 1;
-        for (IntervalIterator subIterator : subIterators) {
-          slop -= subIterator.width();
+        // use indexed loop since this is always a random access capable list to avoid allocations
+        // in a hot nested loop
+        for (int j = 0, n = subIterators.size(); j < n; j++) {
+          slop -= subIterators.get(j).width();
         }
         this.slop = slop;
         onMatch.onMatch();