Apache Lucene open-source search software
LUCENE-2792: Revert generic ArrayUtil.grow(T[]) for now and add unchecked code back into FST package
Uwe Schindlerfbab97a0f19e
diff --git a/lucene/src/java/org/apache/lucene/util/ArrayUtil.java b/lucene/src/java/org/apache/lucene/util/ArrayUtil.javaindex 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> T[] grow(T[] array, int minSize) {if (array.length < minSize) {@SuppressWarnings("unchecked") final T[] newArray =@@ -454,6 +454,7 @@ public final class ArrayUtil {} elsereturn 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.javaindex 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<T> {final int prefixLenPlus1 = pos1+1;if (frontier.length < input.length+1) {- final UnCompiledNode<T>[] next = ArrayUtil.grow(frontier, input.length+1);+ @SuppressWarnings("unchecked") final UnCompiledNode<T>[] 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<next.length;idx++) {next[idx] = new UnCompiledNode<T>(this);}@@ -450,7 +453,9 @@ public class Builder<T> {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<T>[] newArcs = ArrayUtil.grow(arcs);+ @SuppressWarnings("unchecked") final Arc<T>[] 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<newArcs.length;arcIdx++) {newArcs[arcIdx] = new Arc<T>();}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.javaindex 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<T> {private final FST<T> fst;private BytesRef current = new BytesRef(10);- @SuppressWarnings("unchecked") private FST.Arc<T>[] arcs = (FST.Arc<T>[]) new FST.Arc[10];+ @SuppressWarnings("unchecked") private FST.Arc<T>[] arcs = new FST.Arc[10];// outputs are cumulative@SuppressWarnings("unchecked") private T[] output = (T[]) new Object[10];@@ -235,8 +236,18 @@ public class BytesRefFSTEnum<T> {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<T>[] 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.javaindex 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<T> {private final FST<T> fst;private IntsRef current = new IntsRef(10);- @SuppressWarnings("unchecked") private FST.Arc<T>[] arcs = (FST.Arc<T>[]) new FST.Arc[10];+ @SuppressWarnings("unchecked") private FST.Arc<T>[] arcs = new FST.Arc[10];// outputs are cumulative@SuppressWarnings("unchecked") private T[] output = (T[]) new Object[10];@@ -235,8 +236,18 @@ public class IntsRefFSTEnum<T> {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<T>[] 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) {