Apache Lucene open-source search software
reuse a single BytesReader in NodeHash
Michael McCandlessf30dd532ec45
diff --git a/lucene/core/src/java/org/apache/lucene/util/fst/Builder.java b/lucene/core/src/java/org/apache/lucene/util/fst/Builder.javaindex 19ee5412943..812b3e74706 100644--- a/lucene/core/src/java/org/apache/lucene/util/fst/Builder.java+++ b/lucene/core/src/java/org/apache/lucene/util/fst/Builder.java@@ -160,7 +160,7 @@ public class Builder<T> {this.acceptableOverheadRatio = acceptableOverheadRatio;fst = new FST<T>(inputType, outputs, doPackFST, acceptableOverheadRatio, allowArrayArcs);if (doShareSuffix) {- dedupHash = new NodeHash<T>(fst);+ dedupHash = new NodeHash<T>(fst, fst.bytes.getReverseReader(false));} else {dedupHash = null;}diff --git a/lucene/core/src/java/org/apache/lucene/util/fst/BytesStore.java b/lucene/core/src/java/org/apache/lucene/util/fst/BytesStore.javaindex 1c4415fd9d7..6a3d104d178 100644--- a/lucene/core/src/java/org/apache/lucene/util/fst/BytesStore.java+++ b/lucene/core/src/java/org/apache/lucene/util/fst/BytesStore.java@@ -374,7 +374,11 @@ class BytesStore extends DataOutput {}public FST.BytesReader getReverseReader() {- if (blocks.size() == 1) {+ return getReverseReader(true);+ }++ FST.BytesReader getReverseReader(boolean allowSingle) {+ if (allowSingle && blocks.size() == 1) {return new ReverseBytesReader(blocks.get(0));}return new FST.BytesReader() {diff --git a/lucene/core/src/java/org/apache/lucene/util/fst/FST.java b/lucene/core/src/java/org/apache/lucene/util/fst/FST.javaindex a47398fe22c..54a9ec3643c 100644--- a/lucene/core/src/java/org/apache/lucene/util/fst/FST.java+++ b/lucene/core/src/java/org/apache/lucene/util/fst/FST.java@@ -146,7 +146,7 @@ public final class FST<T> {// produces this outputT emptyOutput;- private final BytesStore bytes;+ final BytesStore bytes;private int startNode = -1;diff --git a/lucene/core/src/java/org/apache/lucene/util/fst/NodeHash.java b/lucene/core/src/java/org/apache/lucene/util/fst/NodeHash.javaindex 3c01dfc0ea2..d469288ef71 100644--- a/lucene/core/src/java/org/apache/lucene/util/fst/NodeHash.java+++ b/lucene/core/src/java/org/apache/lucene/util/fst/NodeHash.java@@ -27,14 +27,16 @@ final class NodeHash<T> {private int mask;private final FST<T> fst;private final FST.Arc<T> scratchArc = new FST.Arc<T>();+ private final FST.BytesReader in;- public NodeHash(FST<T> fst) {+ public NodeHash(FST<T> fst, FST.BytesReader in) {table = new int[16];mask = 15;this.fst = fst;+ this.in = in;}- private boolean nodesEqual(Builder.UnCompiledNode<T> node, int address, FST.BytesReader in) throws IOException {+ private boolean nodesEqual(Builder.UnCompiledNode<T> node, int address) throws IOException {fst.readFirstRealTargetArc(address, scratchArc, in);if (scratchArc.bytesPerArc != 0 && node.numArcs != scratchArc.numArcs) {return false;@@ -87,7 +89,6 @@ final class NodeHash<T> {// hash code for a frozen nodeprivate int hash(int node) throws IOException {final int PRIME = 31;- final FST.BytesReader in = fst.getBytesReader(0);//System.out.println("hash frozen node=" + node);int h = 0;fst.readFirstRealTargetArc(node, scratchArc, in);@@ -111,7 +112,6 @@ final class NodeHash<T> {public int add(Builder.UnCompiledNode<T> nodeIn) throws IOException {// System.out.println("hash: add count=" + count + " vs " + table.length);- final FST.BytesReader in = fst.getBytesReader(0);final int h = hash(nodeIn);int pos = h & mask;int c = 0;@@ -128,7 +128,7 @@ final class NodeHash<T> {rehash();}return node;- } else if (nodesEqual(nodeIn, v, in)) {+ } else if (nodesEqual(nodeIn, v)) {// same node is already herereturn v;}