9
0
mirror of https://github.com/Winds-Studio/Leaf.git synced 2025-12-25 09:59:15 +00:00

perf: apply fast bit radix sort [ci skip]

This commit is contained in:
hayanesuru
2025-08-28 17:11:05 +09:00
parent e5e7b690e8
commit 5cc4388005
2 changed files with 42 additions and 45 deletions

View File

@@ -0,0 +1,100 @@
package org.dreeam.leaf.util;
import net.minecraft.world.entity.Entity;
public class FastBitRadixSort {
private static final int SMALL_ARRAY_THRESHOLD = 6;
private static final long[] LONGS = new long[0];
private long[] bitsBuffer = LONGS;
public <T extends Entity> void sort(T[] entities, int size, net.minecraft.core.Position target) {
if (size <= 1) {
return;
}
if (this.bitsBuffer.length < size) {
this.bitsBuffer = new long[size];
}
double tx = target.x();
double ty = target.y();
double tz = target.z();
for (int i = 0; i < size; i++) {
T e = entities[i];
this.bitsBuffer[i] = Double.doubleToRawLongBits(e.distanceToSqr(tx, ty, tz));
}
fastRadixSort(entities, this.bitsBuffer, 0, size - 1, 62);
}
private static void fastRadixSort(
Entity[] ents,
long[] bits,
int low,
int high,
int bit
) {
if (bit < 0 || low >= high) {
return;
}
if (high - low <= SMALL_ARRAY_THRESHOLD) {
insertionSort(ents, bits, low, high);
return;
}
int i = low;
int j = high;
final long mask = 1L << bit;
while (i <= j) {
while (i <= j && (bits[i] & mask) == 0) {
i++;
}
while (i <= j && (bits[j] & mask) != 0) {
j--;
}
if (i < j) {
swap(ents, bits, i++, j--);
}
}
if (low < j) {
fastRadixSort(ents, bits, low, j, bit - 1);
}
if (i < high) {
fastRadixSort(ents, bits, i, high, bit - 1);
}
}
private static void insertionSort(
Entity[] ents,
long[] bits,
int low,
int high
) {
for (int i = low + 1; i <= high; i++) {
int j = i;
Entity currentEntity = ents[j];
long currentBits = bits[j];
while (j > low && bits[j - 1] > currentBits) {
ents[j] = ents[j - 1];
bits[j] = bits[j - 1];
j--;
}
ents[j] = currentEntity;
bits[j] = currentBits;
}
}
private static void swap(Entity[] ents, long[] bits, int a, int b) {
Entity tempEntity = ents[a];
ents[a] = ents[b];
ents[b] = tempEntity;
long tempBits = bits[a];
bits[a] = bits[b];
bits[b] = tempBits;
}
}