Skip to content

Commit c16bb67

Browse files
committed
resolve no commits
1 parent 86cb8a6 commit c16bb67

File tree

1 file changed

+18
-14
lines changed
  • lucene/core/src/java/org/apache/lucene/codecs/lucene90/blocktree

1 file changed

+18
-14
lines changed

lucene/core/src/java/org/apache/lucene/codecs/lucene90/blocktree/TrieBuilder.java

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.io.IOException;
2020
import java.util.ArrayDeque;
2121
import java.util.Deque;
22+
import java.util.Iterator;
2223
import java.util.LinkedList;
2324
import java.util.List;
2425
import java.util.ListIterator;
@@ -60,8 +61,12 @@ private static class Node {
6061
// children listed in order by their utf8 label
6162
private final LinkedList<Node> children;
6263
private Output output;
63-
// Used during saving, -1 means the node has not been saved.
64+
65+
// Vars used during saving:
66+
67+
// -1 means the node has not been saved.
6468
private long fp = -1;
69+
private Iterator<Node> childrenIterator;
6570

6671
Node(int label, Output output, LinkedList<Node> children) {
6772
this.label = label;
@@ -102,11 +107,16 @@ void absorb(TrieBuilder trieBuilder) {
102107
if (status != Status.BUILDING || trieBuilder.status != Status.BUILDING) {
103108
throw new IllegalStateException("tries should be unsaved");
104109
}
105-
absorb(this.root, trieBuilder.root);
110+
// Use a simple stack to avoid recursion.
111+
Deque<Runnable> stack = new ArrayDeque<>();
112+
stack.add(() -> absorb(this.root, trieBuilder.root, stack));
113+
while (!stack.isEmpty()) {
114+
stack.pop().run();
115+
}
106116
trieBuilder.status = Status.DESTROYED;
107117
}
108118

109-
private static void absorb(Node n, Node add) {
119+
private static void absorb(Node n, Node add, Deque<Runnable> stack) {
110120
assert n.label == add.label;
111121
if (add.output != null) {
112122
n.output = add.output;
@@ -118,8 +128,7 @@ private static void absorb(Node n, Node add) {
118128
while (iter.hasNext()) {
119129
Node nChild = iter.next();
120130
if (nChild.label == addChild.label) {
121-
// NO COMMIT: avoid this recursive impl.
122-
absorb(nChild, addChild);
131+
stack.push(() -> absorb(nChild, addChild, stack));
123132
continue outer;
124133
}
125134
if (nChild.label > addChild.label) {
@@ -204,16 +213,11 @@ void saveNodes(IndexOutput index) throws IOException {
204213
continue;
205214
}
206215

207-
Node unSaved = null;
208-
// NO COMMIT: this makes it a O(n^2) operation, we should avoid it.
209-
for (Node child : node.children) {
210-
if (child.fp == -1) {
211-
unSaved = child;
212-
break;
213-
}
216+
if (node.childrenIterator == null) {
217+
node.childrenIterator = node.children.iterator();
214218
}
215-
if (unSaved != null) {
216-
stack.push(unSaved);
219+
if (node.childrenIterator.hasNext()) {
220+
stack.push(node.childrenIterator.next());
217221
continue;
218222
}
219223

0 commit comments

Comments
 (0)