mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-04 23:47:59 +01:00
Do not allocate arrays when refreshing a player chunks, optimize array lookup
This commit is contained in:
parent
94a96d7df1
commit
bebdcf59d5
@ -1445,16 +1445,11 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
|
|||||||
// New chunks indexes
|
// New chunks indexes
|
||||||
final long[] updatedVisibleChunks = ChunkUtils.getChunksInRange(newChunk.toPosition(), getChunkRange());
|
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
|
// Update client render distance
|
||||||
updateViewPosition(newChunk.getChunkX(), newChunk.getChunkZ());
|
updateViewPosition(newChunk.getChunkX(), newChunk.getChunkZ());
|
||||||
|
|
||||||
// Unload old chunks
|
// Unload old chunks
|
||||||
for (int index : oldChunks) {
|
ArrayUtils.forDifferencesBetweenArray(lastVisibleChunks, updatedVisibleChunks, chunkIndex -> {
|
||||||
final long chunkIndex = lastVisibleChunks[index];
|
|
||||||
final int chunkX = ChunkUtils.getChunkCoordX(chunkIndex);
|
final int chunkX = ChunkUtils.getChunkCoordX(chunkIndex);
|
||||||
final int chunkZ = ChunkUtils.getChunkCoordZ(chunkIndex);
|
final int chunkZ = ChunkUtils.getChunkCoordZ(chunkIndex);
|
||||||
|
|
||||||
@ -1467,14 +1462,11 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
|
|||||||
if (chunk != null) {
|
if (chunk != null) {
|
||||||
chunk.removeViewer(this);
|
chunk.removeViewer(this);
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
|
|
||||||
// Load new chunks
|
// Load new chunks
|
||||||
for (int index : newChunks) {
|
ArrayUtils.forDifferencesBetweenArray(updatedVisibleChunks, lastVisibleChunks, chunkIndex -> {
|
||||||
final long chunkIndex = updatedVisibleChunks[index];
|
|
||||||
final int chunkX = ChunkUtils.getChunkCoordX(chunkIndex);
|
final int chunkX = ChunkUtils.getChunkCoordX(chunkIndex);
|
||||||
final int chunkZ = ChunkUtils.getChunkCoordZ(chunkIndex);
|
final int chunkZ = ChunkUtils.getChunkCoordZ(chunkIndex);
|
||||||
|
|
||||||
this.instance.loadOptionalChunk(chunkX, chunkZ).thenAccept(chunk -> {
|
this.instance.loadOptionalChunk(chunkX, chunkZ).thenAccept(chunk -> {
|
||||||
if (chunk == null) {
|
if (chunk == null) {
|
||||||
// Cannot load chunk (auto load is not enabled)
|
// Cannot load chunk (auto load is not enabled)
|
||||||
@ -1482,7 +1474,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
|
|||||||
}
|
}
|
||||||
chunk.addViewer(this);
|
chunk.addViewer(this);
|
||||||
});
|
});
|
||||||
}
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public void refreshVisibleChunks() {
|
public void refreshVisibleChunks() {
|
||||||
|
@ -5,6 +5,7 @@ import org.jetbrains.annotations.ApiStatus;
|
|||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
import java.util.function.LongConsumer;
|
||||||
|
|
||||||
@ApiStatus.Internal
|
@ApiStatus.Internal
|
||||||
public final class ArrayUtils {
|
public final class ArrayUtils {
|
||||||
@ -30,19 +31,9 @@ public final class ArrayUtils {
|
|||||||
System.arraycopy(arr, index + 1, arr, index, arr.length - 1 - index);
|
System.arraycopy(arr, index + 1, arr, index, arr.length - 1 - index);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public static void forDifferencesBetweenArray(long @NotNull [] a, long @NotNull [] b,
|
||||||
* Gets the differences between 2 arrays.
|
@NotNull LongConsumer consumer) {
|
||||||
*
|
for (final long aValue : a) {
|
||||||
* @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];
|
|
||||||
boolean contains = false;
|
boolean contains = false;
|
||||||
for (final long bValue : b) {
|
for (final long bValue : b) {
|
||||||
if (bValue == aValue) {
|
if (bValue == aValue) {
|
||||||
@ -51,14 +42,9 @@ public final class ArrayUtils {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!contains) {
|
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) {
|
public static int @NotNull [] toArray(@NotNull IntList list) {
|
||||||
|
Loading…
Reference in New Issue
Block a user