apache / apache/lucene

Optimize transition lookup in CompiledAutomaton.addTail using binary search

Open Beginner friendly
#16,360 3 comments 0 reactions 0 assignees View on GitHub
type:enhancement
Dominant language
Java
Stars
3.6k
Forks
1.4k
Avg merge
2d 11h
Merged PRs (30d)
88

Description

### Description

Description:

### Description
In `CompiledAutomaton.java` within the `addTail` method, a linear scan $O(N)$ is currently used to find the largest transition index where the transition's minimum label is less than the `leadLabel`:
```java
// Find biggest transition that's < label
// TODO: use binary search here
int maxIndex = -1;
int numTransitions = automaton.initTransition(state, transition);
for (int i = 0; i < numTransitions; i++) {
automaton.getNextTransition(transition);
if (transition.min < leadLabel) {
maxIndex = i;
} else {
// Transitions are always sorted
break;
}
}
```

Since the automaton transitions are always sorted by minimum label first, we can optimize this transition lookup to run in $O(\log N)$ time by implementing a binary search using random access (`automaton.getTransition(state, index, transition)`).

Contributor guide

Open the contributing guide

Research direction

Start in CompiledAutomaton.java at the addTail method and inspect how automaton.getTransition(state, index, transition) provides random access. Replace the sorted-transition scan with a binary search for the largest minimum label below leadLabel, preserving the existing maxIndex behavior. Done means the lookup is logarithmic while selecting the same transition boundary.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
search
Issue type
Refactor
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
78/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.