From e9dc4f91888e61de234c51a871bb78b4e098980b Mon Sep 17 00:00:00 2001 From: Adrien Grand Date: Tue, 27 Dec 2022 11:03:06 +0100 Subject: [PATCH] Avoid sorting values of multi-valued writers if there is a single value. (#12039) They currently call `Arrays#sort`, which incurs a tiny bit of overhead due to range checks and some logic to determine the optimal sorting algorithm to use depending on the number of values. We can skip this overhead in the case when there is a single value. --- .../org/apache/lucene/index/SortedNumericDocValuesWriter.java | 4 +++- .../org/apache/lucene/index/SortedSetDocValuesWriter.java | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/lucene/core/src/java/org/apache/lucene/index/SortedNumericDocValuesWriter.java b/lucene/core/src/java/org/apache/lucene/index/SortedNumericDocValuesWriter.java index 5d303118028..4f4d3f4128e 100644 --- a/lucene/core/src/java/org/apache/lucene/index/SortedNumericDocValuesWriter.java +++ b/lucene/core/src/java/org/apache/lucene/index/SortedNumericDocValuesWriter.java @@ -72,7 +72,9 @@ class SortedNumericDocValuesWriter extends DocValuesWriter 1) { + Arrays.sort(currentValues, 0, currentUpto); + } for (int i = 0; i < currentUpto; i++) { pending.add(currentValues[i]); } diff --git a/lucene/core/src/java/org/apache/lucene/index/SortedSetDocValuesWriter.java b/lucene/core/src/java/org/apache/lucene/index/SortedSetDocValuesWriter.java index ab993b89960..119b7efc8c8 100644 --- a/lucene/core/src/java/org/apache/lucene/index/SortedSetDocValuesWriter.java +++ b/lucene/core/src/java/org/apache/lucene/index/SortedSetDocValuesWriter.java @@ -102,7 +102,9 @@ class SortedSetDocValuesWriter extends DocValuesWriter { if (currentDoc == -1) { return; } - Arrays.sort(currentValues, 0, currentUpto); + if (currentUpto > 1) { + Arrays.sort(currentValues, 0, currentUpto); + } int lastValue = -1; int count = 0; for (int i = 0; i < currentUpto; i++) {