Use MergeSorter in StableStringSorter (#12652)

This commit is contained in:
gf2121 2023-10-17 17:52:16 +08:00 committed by GitHub
parent c6e76d3e01
commit da032535d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 2 deletions

View File

@ -39,6 +39,16 @@ public abstract class StableMSBRadixSorter extends MSBRadixSorter {
@Override
protected Sorter getFallbackSorter(int k) {
return new MergeSorter() {
@Override
protected void save(int i, int j) {
StableMSBRadixSorter.this.save(i, j);
}
@Override
protected void restore(int i, int j) {
StableMSBRadixSorter.this.restore(i, j);
}
@Override
protected void swap(int i, int j) {
StableMSBRadixSorter.this.swap(i, j);
@ -80,7 +90,7 @@ public abstract class StableMSBRadixSorter extends MSBRadixSorter {
}
/** A MergeSorter taking advantage of temporary storage. */
protected abstract class MergeSorter extends Sorter {
protected abstract static class MergeSorter extends Sorter {
@Override
public void sort(int from, int to) {
checkRange(from, to);
@ -98,6 +108,14 @@ public abstract class StableMSBRadixSorter extends MSBRadixSorter {
}
}
/** Save the i-th value into the j-th position in temporary storage. */
protected abstract void save(int i, int j);
/**
* Restore values between i-th and j-th(excluding) in temporary storage into original storage.
*/
protected abstract void restore(int i, int j);
/**
* We tried to expose this to implementations to get a bulk copy optimization. But it did not
* bring a noticeable improvement in benchmark as {@code len} is usually small.

View File

@ -65,7 +65,17 @@ abstract class StableStringSorter extends StringSorter {
@Override
protected Sorter fallbackSorter(Comparator<BytesRef> cmp) {
// TODO: Maybe tim sort is better?
return new InPlaceMergeSorter() {
return new StableMSBRadixSorter.MergeSorter() {
@Override
protected void save(int i, int j) {
StableStringSorter.this.save(i, j);
}
@Override
protected void restore(int i, int j) {
StableStringSorter.this.restore(i, j);
}
@Override
protected int compare(int i, int j) {
return StableStringSorter.this.compare(i, j);