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.
This commit is contained in:
Adrien Grand 2022-12-27 11:03:06 +01:00 committed by GitHub
parent 008a0d4206
commit e9dc4f9188
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 2 deletions

View File

@ -72,7 +72,9 @@ class SortedNumericDocValuesWriter extends DocValuesWriter<SortedNumericDocValue
if (currentDoc == -1) { if (currentDoc == -1) {
return; return;
} }
Arrays.sort(currentValues, 0, currentUpto); if (currentUpto > 1) {
Arrays.sort(currentValues, 0, currentUpto);
}
for (int i = 0; i < currentUpto; i++) { for (int i = 0; i < currentUpto; i++) {
pending.add(currentValues[i]); pending.add(currentValues[i]);
} }

View File

@ -102,7 +102,9 @@ class SortedSetDocValuesWriter extends DocValuesWriter<SortedSetDocValues> {
if (currentDoc == -1) { if (currentDoc == -1) {
return; return;
} }
Arrays.sort(currentValues, 0, currentUpto); if (currentUpto > 1) {
Arrays.sort(currentValues, 0, currentUpto);
}
int lastValue = -1; int lastValue = -1;
int count = 0; int count = 0;
for (int i = 0; i < currentUpto; i++) { for (int i = 0; i < currentUpto; i++) {