From 8178ffda00c183533128847b678b36618e72fdf9 Mon Sep 17 00:00:00 2001 From: Greg Miller Date: Tue, 8 Feb 2022 09:43:55 -0800 Subject: [PATCH] LUCENE-10403: Add ArrayUtil#grow(T[]) (#644) --- lucene/CHANGES.txt | 2 ++ .../org/apache/lucene/analysis/synonym/SynonymFilter.java | 2 +- lucene/core/src/java/org/apache/lucene/util/ArrayUtil.java | 5 +++++ .../src/java/org/apache/lucene/util/fst/FSTCompiler.java | 2 +- 4 files changed, 9 insertions(+), 2 deletions(-) diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt index e9057f826da..6f380a4965b 100644 --- a/lucene/CHANGES.txt +++ b/lucene/CHANGES.txt @@ -142,6 +142,8 @@ New Features * LUCENE-10385: Implement Weight#count on IndexSortSortedNumericDocValuesRangeQuery to speed up computing the number of hits when possible. (Luca Cavanna, Adrien Grand) +* LUCENE-10403: Add ArrayUtil#grow(T[]). (Greg Miller) + Improvements --------------------- diff --git a/lucene/analysis/common/src/java/org/apache/lucene/analysis/synonym/SynonymFilter.java b/lucene/analysis/common/src/java/org/apache/lucene/analysis/synonym/SynonymFilter.java index aa7d15b315f..d3eb6165f74 100644 --- a/lucene/analysis/common/src/java/org/apache/lucene/analysis/synonym/SynonymFilter.java +++ b/lucene/analysis/common/src/java/org/apache/lucene/analysis/synonym/SynonymFilter.java @@ -192,7 +192,7 @@ public final class SynonymFilter extends TokenFilter { public void add(char[] output, int offset, int len, int endOffset, int posLength) { if (count == outputs.length) { - outputs = ArrayUtil.grow(outputs, count + 1); + outputs = ArrayUtil.grow(outputs); } if (count == endOffsets.length) { final int[] next = new int[ArrayUtil.oversize(1 + count, Integer.BYTES)]; diff --git a/lucene/core/src/java/org/apache/lucene/util/ArrayUtil.java b/lucene/core/src/java/org/apache/lucene/util/ArrayUtil.java index 851ff768edb..b0e780d08db 100644 --- a/lucene/core/src/java/org/apache/lucene/util/ArrayUtil.java +++ b/lucene/core/src/java/org/apache/lucene/util/ArrayUtil.java @@ -223,6 +223,11 @@ public final class ArrayUtil { return copy; } + /** Returns a larger array, generally over-allocating exponentially */ + public static T[] grow(T[] array) { + return grow(array, 1 + array.length); + } + /** * Returns an array whose size is at least {@code minSize}, generally over-allocating * exponentially diff --git a/lucene/core/src/java/org/apache/lucene/util/fst/FSTCompiler.java b/lucene/core/src/java/org/apache/lucene/util/fst/FSTCompiler.java index bf6fc6a2e15..550a57b3ec1 100644 --- a/lucene/core/src/java/org/apache/lucene/util/fst/FSTCompiler.java +++ b/lucene/core/src/java/org/apache/lucene/util/fst/FSTCompiler.java @@ -713,7 +713,7 @@ public class FSTCompiler { + " numArcs=" + numArcs; if (numArcs == arcs.length) { - final Arc[] newArcs = ArrayUtil.grow(arcs, numArcs + 1); + final Arc[] newArcs = ArrayUtil.grow(arcs); for (int arcIdx = numArcs; arcIdx < newArcs.length; arcIdx++) { newArcs[arcIdx] = new Arc<>(); }