Skip to content

Commit

Permalink
Speedup OrderIntervalsSource some more (#13937)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
original-brownbear authored Oct 21, 2024
1 parent 66f22fa commit d1e017f
Showing 1 changed file with 12 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,29 +127,31 @@ public int nextInterval() throws IOException {
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;
}
if (currentIndex == subIterators.size()) {
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) {
Expand All @@ -161,8 +163,10 @@ public int nextInterval() throws IOException {
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();
Expand Down

0 comments on commit d1e017f

Please sign in to comment.