Do not allocate arrays when refreshing a player chunks, optimize array lookup

This commit is contained in:
TheMode 2021-07-21 08:42:49 +02:00
parent 94a96d7df1
commit bebdcf59d5
2 changed files with 9 additions and 31 deletions

View File

@ -1445,16 +1445,11 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
// New chunks indexes
final long[] updatedVisibleChunks = ChunkUtils.getChunksInRange(newChunk.toPosition(), getChunkRange());
// Find the difference between the two arrays
final int[] oldChunks = ArrayUtils.getDifferencesBetweenArray(lastVisibleChunks, updatedVisibleChunks);
final int[] newChunks = ArrayUtils.getDifferencesBetweenArray(updatedVisibleChunks, lastVisibleChunks);
// Update client render distance
updateViewPosition(newChunk.getChunkX(), newChunk.getChunkZ());
// Unload old chunks
for (int index : oldChunks) {
final long chunkIndex = lastVisibleChunks[index];
ArrayUtils.forDifferencesBetweenArray(lastVisibleChunks, updatedVisibleChunks, chunkIndex -> {
final int chunkX = ChunkUtils.getChunkCoordX(chunkIndex);
final int chunkZ = ChunkUtils.getChunkCoordZ(chunkIndex);
@ -1467,14 +1462,11 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
if (chunk != null) {
chunk.removeViewer(this);
}
}
});
// Load new chunks
for (int index : newChunks) {
final long chunkIndex = updatedVisibleChunks[index];
ArrayUtils.forDifferencesBetweenArray(updatedVisibleChunks, lastVisibleChunks, chunkIndex -> {
final int chunkX = ChunkUtils.getChunkCoordX(chunkIndex);
final int chunkZ = ChunkUtils.getChunkCoordZ(chunkIndex);
this.instance.loadOptionalChunk(chunkX, chunkZ).thenAccept(chunk -> {
if (chunk == null) {
// Cannot load chunk (auto load is not enabled)
@ -1482,7 +1474,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
}
chunk.addViewer(this);
});
}
});
}
public void refreshVisibleChunks() {

View File

@ -5,6 +5,7 @@ import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import java.util.Objects;
import java.util.function.LongConsumer;
@ApiStatus.Internal
public final class ArrayUtils {
@ -30,19 +31,9 @@ public final class ArrayUtils {
System.arraycopy(arr, index + 1, arr, index, arr.length - 1 - index);
}
/**
* Gets the differences between 2 arrays.
*
* @param a the first array
* @param b the second array
* @return an array containing a's indexes that aren't in b array
*/
public static int @NotNull [] getDifferencesBetweenArray(long @NotNull [] a, long @NotNull [] b) {
int counter = 0;
int[] indexes = new int[Math.max(a.length, b.length)];
for (int i = 0; i < a.length; i++) {
final long aValue = a[i];
public static void forDifferencesBetweenArray(long @NotNull [] a, long @NotNull [] b,
@NotNull LongConsumer consumer) {
for (final long aValue : a) {
boolean contains = false;
for (final long bValue : b) {
if (bValue == aValue) {
@ -51,14 +42,9 @@ public final class ArrayUtils {
}
}
if (!contains) {
indexes[counter++] = i;
consumer.accept(aValue);
}
}
// Resize array
int[] result = new int[counter];
System.arraycopy(indexes, 0, result, 0, counter);
return result;
}
public static int @NotNull [] toArray(@NotNull IntList list) {