diff --git a/lucene/src/java/org/apache/lucene/util/ArrayUtil.java b/lucene/src/java/org/apache/lucene/util/ArrayUtil.java index 765e153a899..50e04ac93d1 100644 --- a/lucene/src/java/org/apache/lucene/util/ArrayUtil.java +++ b/lucene/src/java/org/apache/lucene/util/ArrayUtil.java @@ -19,7 +19,6 @@ package org.apache.lucene.util; import java.util.Collection; import java.util.Comparator; -import java.lang.reflect.Array; /** * Methods for manipulating arrays. @@ -430,6 +429,7 @@ public final class ArrayUtil { return false; } + /* DISABLE THIS FOR NOW: This has performance problems until Java creates intrinsics for Class#getComponentType() and Array.newInstance() public static T[] grow(T[] array, int minSize) { if (array.length < minSize) { @SuppressWarnings("unchecked") final T[] newArray = @@ -454,6 +454,7 @@ public final class ArrayUtil { } else return array; } + */ // Since Arrays.equals doesn't implement offsets for equals /** diff --git a/lucene/src/java/org/apache/lucene/util/automaton/fst/Builder.java b/lucene/src/java/org/apache/lucene/util/automaton/fst/Builder.java index 2445e40f06d..f39fb2889ae 100644 --- a/lucene/src/java/org/apache/lucene/util/automaton/fst/Builder.java +++ b/lucene/src/java/org/apache/lucene/util/automaton/fst/Builder.java @@ -18,6 +18,7 @@ package org.apache.lucene.util.automaton.fst; */ import org.apache.lucene.util.ArrayUtil; +import org.apache.lucene.util.RamUsageEstimator; import org.apache.lucene.util.BytesRef; import org.apache.lucene.util.IntsRef; @@ -287,7 +288,9 @@ public class Builder { final int prefixLenPlus1 = pos1+1; if (frontier.length < input.length+1) { - final UnCompiledNode[] next = ArrayUtil.grow(frontier, input.length+1); + @SuppressWarnings("unchecked") final UnCompiledNode[] next = + new UnCompiledNode[ArrayUtil.oversize(input.length+1, RamUsageEstimator.NUM_BYTES_OBJECT_REF)]; + System.arraycopy(frontier, 0, next, 0, frontier.length); for(int idx=frontier.length;idx(this); } @@ -450,7 +453,9 @@ public class Builder { assert label >= 0; assert numArcs == 0 || label > arcs[numArcs-1].label: "arc[-1].label=" + arcs[numArcs-1].label + " new label=" + label + " numArcs=" + numArcs; if (numArcs == arcs.length) { - final Arc[] newArcs = ArrayUtil.grow(arcs); + @SuppressWarnings("unchecked") final Arc[] newArcs = + new Arc[ArrayUtil.oversize(numArcs+1, RamUsageEstimator.NUM_BYTES_OBJECT_REF)]; + System.arraycopy(arcs, 0, newArcs, 0, arcs.length); for(int arcIdx=numArcs;arcIdx(); } diff --git a/lucene/src/java/org/apache/lucene/util/automaton/fst/BytesRefFSTEnum.java b/lucene/src/java/org/apache/lucene/util/automaton/fst/BytesRefFSTEnum.java index 150a0e7dcf6..4fb6e29e5fd 100644 --- a/lucene/src/java/org/apache/lucene/util/automaton/fst/BytesRefFSTEnum.java +++ b/lucene/src/java/org/apache/lucene/util/automaton/fst/BytesRefFSTEnum.java @@ -19,6 +19,7 @@ package org.apache.lucene.util.automaton.fst; import org.apache.lucene.util.ArrayUtil; import org.apache.lucene.util.BytesRef; +import org.apache.lucene.util.RamUsageEstimator; import java.io.IOException; @@ -30,7 +31,7 @@ public class BytesRefFSTEnum { private final FST fst; private BytesRef current = new BytesRef(10); - @SuppressWarnings("unchecked") private FST.Arc[] arcs = (FST.Arc[]) new FST.Arc[10]; + @SuppressWarnings("unchecked") private FST.Arc[] arcs = new FST.Arc[10]; // outputs are cumulative @SuppressWarnings("unchecked") private T[] output = (T[]) new Object[10]; @@ -235,8 +236,18 @@ public class BytesRefFSTEnum { private void grow() { final int l = current.length + 1; current.grow(l); - arcs = ArrayUtil.grow(arcs, l); - output = ArrayUtil.grow(output, l); + if (arcs.length < l) { + @SuppressWarnings("unchecked") final FST.Arc[] newArcs = + new FST.Arc[ArrayUtil.oversize(l, RamUsageEstimator.NUM_BYTES_OBJECT_REF)]; + System.arraycopy(arcs, 0, newArcs, 0, arcs.length); + arcs = newArcs; + } + if (output.length < l) { + @SuppressWarnings("unchecked") final T[] newOutput = + (T[]) new Object[ArrayUtil.oversize(l, RamUsageEstimator.NUM_BYTES_OBJECT_REF)]; + System.arraycopy(output, 0, newOutput, 0, output.length); + output = newOutput; + } } private void appendOutput(T addedOutput) { diff --git a/lucene/src/java/org/apache/lucene/util/automaton/fst/IntsRefFSTEnum.java b/lucene/src/java/org/apache/lucene/util/automaton/fst/IntsRefFSTEnum.java index 743f17c7072..6be025ab721 100644 --- a/lucene/src/java/org/apache/lucene/util/automaton/fst/IntsRefFSTEnum.java +++ b/lucene/src/java/org/apache/lucene/util/automaton/fst/IntsRefFSTEnum.java @@ -19,6 +19,7 @@ package org.apache.lucene.util.automaton.fst; import org.apache.lucene.util.ArrayUtil; import org.apache.lucene.util.IntsRef; +import org.apache.lucene.util.RamUsageEstimator; import java.io.IOException; @@ -30,7 +31,7 @@ public class IntsRefFSTEnum { private final FST fst; private IntsRef current = new IntsRef(10); - @SuppressWarnings("unchecked") private FST.Arc[] arcs = (FST.Arc[]) new FST.Arc[10]; + @SuppressWarnings("unchecked") private FST.Arc[] arcs = new FST.Arc[10]; // outputs are cumulative @SuppressWarnings("unchecked") private T[] output = (T[]) new Object[10]; @@ -235,8 +236,18 @@ public class IntsRefFSTEnum { private void grow() { final int l = current.length + 1; current.grow(l); - arcs = ArrayUtil.grow(arcs, l); - output = ArrayUtil.grow(output, l); + if (arcs.length < l) { + @SuppressWarnings("unchecked") final FST.Arc[] newArcs = + new FST.Arc[ArrayUtil.oversize(l, RamUsageEstimator.NUM_BYTES_OBJECT_REF)]; + System.arraycopy(arcs, 0, newArcs, 0, arcs.length); + arcs = newArcs; + } + if (output.length < l) { + @SuppressWarnings("unchecked") final T[] newOutput = + (T[]) new Object[ArrayUtil.oversize(l, RamUsageEstimator.NUM_BYTES_OBJECT_REF)]; + System.arraycopy(output, 0, newOutput, 0, output.length); + output = newOutput; + } } private void appendOutput(T addedOutput) {