Apache Solr open-source search software
LUCENE-5610: optimization: just use already allocated/copied PendingTerm to for min/maxTerm
Michael McCandless0bfaf5cdfcbf
diff --git a/lucene/codecs/src/java/org/apache/lucene/codecs/blocktreeords/OrdsBlockTreeTermsWriter.java b/lucene/codecs/src/java/org/apache/lucene/codecs/blocktreeords/OrdsBlockTreeTermsWriter.javaindex 4975155a651..afa6ea2229a 100644--- a/lucene/codecs/src/java/org/apache/lucene/codecs/blocktreeords/OrdsBlockTreeTermsWriter.java+++ b/lucene/codecs/src/java/org/apache/lucene/codecs/blocktreeords/OrdsBlockTreeTermsWriter.java@@ -239,21 +239,15 @@ public final class OrdsBlockTreeTermsWriter extends FieldsConsumer {TermsEnum termsEnum = terms.iterator(null);TermsWriter termsWriter = new TermsWriter(fieldInfos.fieldInfo(field));- BytesRef minTerm = null;- BytesRef maxTerm = new BytesRef();while (true) {BytesRef term = termsEnum.next();if (term == null) {break;}- if (minTerm == null) {- minTerm = BytesRef.deepCopyOf(term);- }- maxTerm.copyBytes(term);termsWriter.write(term, termsEnum);}- termsWriter.finish(minTerm, minTerm == null ? null : maxTerm);+ termsWriter.finish();}}@@ -464,13 +458,16 @@ public final class OrdsBlockTreeTermsWriter extends FieldsConsumer {// Pending stack of terms and blocks. As terms arrive (in sorted order)// we append to this stack, and once the top of the stack has enough- // terms starting with a common prefix, write write a new block with+ // terms starting with a common prefix, we write a new block with// those terms and replace those terms in the stack with a new block:private final List<PendingEntry> pending = new ArrayList<>();// Reused in writeBlocks:private final List<PendingBlock> newBlocks = new ArrayList<>();+ private PendingTerm firstPendingTerm;+ private PendingTerm lastPendingTerm;+/** Writes the top count entries in pending, using prevTerm to compute the prefix. */void writeBlocks(int prefixLength, int count) throws IOException {@@ -812,6 +809,10 @@ public final class OrdsBlockTreeTermsWriter extends FieldsConsumer {PendingTerm term = new PendingTerm(BytesRef.deepCopyOf(text), state);pending.add(term);numTerms++;+ if (firstPendingTerm == null) {+ firstPendingTerm = term;+ }+ lastPendingTerm = term;}}@@ -853,7 +854,7 @@ public final class OrdsBlockTreeTermsWriter extends FieldsConsumer {}// Finishes all terms in this field- public void finish(BytesRef minTerm, BytesRef maxTerm) throws IOException {+ public void finish() throws IOException {if (numTerms > 0) {// if (DEBUG) System.out.println("BTTW.finish pending.size()=" + pending.size());@@ -881,6 +882,12 @@ public final class OrdsBlockTreeTermsWriter extends FieldsConsumer {// w.close();// }+ assert firstPendingTerm != null;+ BytesRef minTerm = new BytesRef(firstPendingTerm.termBytes);++ assert lastPendingTerm != null;+ BytesRef maxTerm = new BytesRef(lastPendingTerm.termBytes);+fields.add(new FieldMetaData(fieldInfo,((PendingBlock) pending.get(0)).index.getEmptyOutput(),numTerms,diff --git a/lucene/core/src/java/org/apache/lucene/codecs/blocktree/BlockTreeTermsWriter.java b/lucene/core/src/java/org/apache/lucene/codecs/blocktree/BlockTreeTermsWriter.javaindex b645bf7d5fe..259f2eb0f2a 100644--- a/lucene/core/src/java/org/apache/lucene/codecs/blocktree/BlockTreeTermsWriter.java+++ b/lucene/core/src/java/org/apache/lucene/codecs/blocktree/BlockTreeTermsWriter.java@@ -373,21 +373,15 @@ public final class BlockTreeTermsWriter extends FieldsConsumer {TermsEnum termsEnum = terms.iterator(null);TermsWriter termsWriter = new TermsWriter(fieldInfos.fieldInfo(field));- BytesRef minTerm = null;- BytesRef maxTerm = new BytesRef();while (true) {BytesRef term = termsEnum.next();if (term == null) {break;}- if (minTerm == null) {- minTerm = BytesRef.deepCopyOf(term);- }- maxTerm.copyBytes(term);termsWriter.write(term, termsEnum);}- termsWriter.finish(minTerm, minTerm == null ? null : maxTerm);+ termsWriter.finish();}}@@ -566,13 +560,16 @@ public final class BlockTreeTermsWriter extends FieldsConsumer {// Pending stack of terms and blocks. As terms arrive (in sorted order)// we append to this stack, and once the top of the stack has enough- // terms starting with a common prefix, write write a new block with+ // terms starting with a common prefix, we write a new block with// those terms and replace those terms in the stack with a new block:private final List<PendingEntry> pending = new ArrayList<>();// Reused in writeBlocks:private final List<PendingBlock> newBlocks = new ArrayList<>();+ private PendingTerm firstPendingTerm;+ private PendingTerm lastPendingTerm;+/** Writes the top count entries in pending, using prevTerm to compute the prefix. */void writeBlocks(int prefixLength, int count) throws IOException {@@ -903,6 +900,10 @@ public final class BlockTreeTermsWriter extends FieldsConsumer {PendingTerm term = new PendingTerm(text, state);pending.add(term);numTerms++;+ if (firstPendingTerm == null) {+ firstPendingTerm = term;+ }+ lastPendingTerm = term;}}@@ -944,7 +945,7 @@ public final class BlockTreeTermsWriter extends FieldsConsumer {}// Finishes all terms in this field- public void finish(BytesRef minTerm, BytesRef maxTerm) throws IOException {+ public void finish() throws IOException {if (numTerms > 0) {// if (DEBUG) System.out.println("BTTW: finish prefixStarts=" + Arrays.toString(prefixStarts));@@ -973,6 +974,11 @@ public final class BlockTreeTermsWriter extends FieldsConsumer {w.close();}*/+ assert firstPendingTerm != null;+ BytesRef minTerm = new BytesRef(firstPendingTerm.termBytes);++ assert lastPendingTerm != null;+ BytesRef maxTerm = new BytesRef(lastPendingTerm.termBytes);fields.add(new FieldMetaData(fieldInfo,((PendingBlock) pending.get(0)).index.getEmptyOutput(),diff --git a/lucene/sandbox/src/java/org/apache/lucene/codecs/idversion/VersionBlockTreeTermsWriter.java b/lucene/sandbox/src/java/org/apache/lucene/codecs/idversion/VersionBlockTreeTermsWriter.javaindex b11af85931f..3bc4ad5bd18 100644--- a/lucene/sandbox/src/java/org/apache/lucene/codecs/idversion/VersionBlockTreeTermsWriter.java+++ b/lucene/sandbox/src/java/org/apache/lucene/codecs/idversion/VersionBlockTreeTermsWriter.java@@ -247,21 +247,15 @@ public final class VersionBlockTreeTermsWriter extends FieldsConsumer {TermsEnum termsEnum = terms.iterator(null);TermsWriter termsWriter = new TermsWriter(fieldInfos.fieldInfo(field));- BytesRef minTerm = null;- BytesRef maxTerm = new BytesRef();while (true) {BytesRef term = termsEnum.next();if (term == null) {break;}- if (minTerm == null) {- minTerm = BytesRef.deepCopyOf(term);- }- maxTerm.copyBytes(term);termsWriter.write(term, termsEnum);}- termsWriter.finish(minTerm, minTerm == null ? null : maxTerm);+ termsWriter.finish();}}@@ -442,13 +436,16 @@ public final class VersionBlockTreeTermsWriter extends FieldsConsumer {// Pending stack of terms and blocks. As terms arrive (in sorted order)// we append to this stack, and once the top of the stack has enough- // terms starting with a common prefix, write write a new block with+ // terms starting with a common prefix, we write a new block with// those terms and replace those terms in the stack with a new block:private final List<PendingEntry> pending = new ArrayList<>();// Reused in writeBlocks:private final List<PendingBlock> newBlocks = new ArrayList<>();+ private PendingTerm firstPendingTerm;+ private PendingTerm lastPendingTerm;+/** Writes the top count entries in pending, using prevTerm to compute the prefix. */void writeBlocks(int prefixLength, int count) throws IOException {@@ -756,6 +753,10 @@ public final class VersionBlockTreeTermsWriter extends FieldsConsumer {PendingTerm term = new PendingTerm(BytesRef.deepCopyOf(text), state);pending.add(term);numTerms++;+ if (firstPendingTerm == null) {+ firstPendingTerm = term;+ }+ lastPendingTerm = term;}}@@ -797,7 +798,7 @@ public final class VersionBlockTreeTermsWriter extends FieldsConsumer {}// Finishes all terms in this field- public void finish(BytesRef minTerm, BytesRef maxTerm) throws IOException {+ public void finish() throws IOException {if (numTerms > 0) {// TODO: if pending.size() is already 1 with a non-zero prefix length@@ -824,6 +825,12 @@ public final class VersionBlockTreeTermsWriter extends FieldsConsumer {// w.close();// }+ assert firstPendingTerm != null;+ BytesRef minTerm = new BytesRef(firstPendingTerm.termBytes);++ assert lastPendingTerm != null;+ BytesRef maxTerm = new BytesRef(lastPendingTerm.termBytes);+fields.add(new FieldMetaData(fieldInfo,((PendingBlock) pending.get(0)).index.getEmptyOutput(),numTerms,