You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by jp...@apache.org on 2022/12/27 10:17:54 UTC

[lucene] 01/04: Avoid sorting values of multi-valued writers if there is a single value. (#12039)

This is an automated email from the ASF dual-hosted git repository.

jpountz pushed a commit to branch branch_9x
in repository https://gitbox.apache.org/repos/asf/lucene.git

commit 97b108a32f53efbe27c6618880e6ed0ef8c7bf47
Author: Adrien Grand <jp...@gmail.com>
AuthorDate: Tue Dec 27 11:03:06 2022 +0100

    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.
---
 .../java/org/apache/lucene/index/SortedNumericDocValuesWriter.java    | 4 +++-
 .../src/java/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 66a28374c3d..07dd60b5ec5 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<SortedNumericDocValue
     if (currentDoc == -1) {
       return;
     }
-    Arrays.sort(currentValues, 0, currentUpto);
+    if (currentUpto > 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 7b4ab53dfa2..b1676f84dfd 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<SortedSetDocValues> {
     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++) {