Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speed up BytesRefHash#sort #12775

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lucene/CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,8 @@ Optimizations

* GITHUB#11903: Faster sort on high-cardinality string fields. (Adrien Grand)

* GITHUB#12775: Speed up BytesRefHash#sort with stable radix sorter. (Guo Feng)

Changes in runtime behavior
---------------------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,11 +263,10 @@ <E extends Exception> void forEachOrdered(DeletedTermConsumer<E> consumer) throw
scratch.field = deleteFieldEntry.getKey();
BufferedUpdates.BytesRefIntMap terms = deleteFieldEntry.getValue();
int[] indices = terms.bytesRefHash.sort();
for (int index : indices) {
if (index != -1) {
terms.bytesRefHash.get(index, scratch.bytes);
consumer.accept(scratch, terms.values[index]);
}
for (int i = 0; i < terms.bytesRefHash.size(); i++) {
int index = indices[i];
terms.bytesRefHash.get(index, scratch.bytes);
consumer.accept(scratch, terms.values[index]);
}
}
}
Expand Down
18 changes: 17 additions & 1 deletion lucene/core/src/java/org/apache/lucene/util/BytesRefHash.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,23 @@ public int[] compact() {
*/
public int[] sort() {
final int[] compact = compact();
new StringSorter(BytesRefComparator.NATURAL) {

// We do not really need "stable" here. But since we allocated the extra O(n) memory anyway we
// can use this stable sorter to speed it up ~35%.
assert count * 2 <= compact.length
: "Change the following sorter to a StringSorter if you are increasing the load factor.";
final int tmpOffset = count;
new StableStringSorter(BytesRefComparator.NATURAL) {

@Override
protected void save(int i, int j) {
compact[tmpOffset + j] = compact[i];
}

@Override
protected void restore(int i, int j) {
System.arraycopy(compact, tmpOffset + i, compact, i, j - i);
}

@Override
protected void swap(int i, int j) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,16 +131,12 @@ public void sort(int from, int to) {

protected void sort(int from, int to, int k, int l) {
if (to - from <= LENGTH_THRESHOLD || l >= LEVEL_THRESHOLD) {
introSort(from, to, k);
getFallbackSorter(k).sort(from, to);
} else {
radixSort(from, to, k, l);
}
}

private void introSort(int from, int to, int k) {
getFallbackSorter(k).sort(from, to);
}

/**
* @param k the character number to compare
* @param l the level of recursion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,23 +135,23 @@ private void merge(int from, int to, int mid) {
int left = from;
int right = mid;
int index = from;
outer:
while (true) {
int cmp = compare(left, right);
if (cmp <= 0) {
setPivot(right);
while (comparePivot(left) >= 0) {
save(left++, index++);
if (left == mid) {
assert index == right;
bulkSave(right, index, to - right);
break;
}
} else {
save(right++, index++);
if (right == to) {
assert to - index == mid - left;
bulkSave(left, index, mid - left);
break;
break outer;
}
}
save(right++, index++);
if (right == to) {
assert to - index == mid - left;
bulkSave(left, index, mid - left);
break;
}
}
restore(from, to);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,22 @@ protected void restore(int i, int j) {
StableStringSorter.this.restore(i, j);
}

@Override
protected void setPivot(int i) {
get(pivotBuilder, pivot, i);
}

@Override
protected int comparePivot(int j) {
get(scratch1, scratchBytes1, j);
return cmp.compare(pivot, scratchBytes1);
}

@Override
protected int compare(int i, int j) {
return StableStringSorter.this.compare(i, j);
get(scratch1, scratchBytes1, i);
get(scratch2, scratchBytes2, j);
return cmp.compare(scratchBytes1, scratchBytes2);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ protected void swap(int i, int j) {

@Override
protected int compare(int i, int j) {
return StringSorter.this.compare(i, j);
get(scratch1, scratchBytes1, i);
get(scratch2, scratchBytes2, j);
return cmp.compare(scratchBytes1, scratchBytes2);
}

@Override
Expand Down