From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: 2No2Name <2No2Name@web.de> Date: Wed, 15 Dec 2021 11:20:48 -0500 Subject: [PATCH] lithium: fast retrieval Original code by CaffeineMC, licensed under GNU Lesser General Public License v3.0 You can find the original code on https://github.com/CaffeineMC/lithium-fabric (Yarn mappings) diff --git a/src/main/java/net/minecraft/world/level/entity/EntitySectionStorage.java b/src/main/java/net/minecraft/world/level/entity/EntitySectionStorage.java index 74210dc2eef63da7360b1f833bb59b278419fb2b..aae1f6d5d0e47363042657d755f6bfeea2420347 100644 --- a/src/main/java/net/minecraft/world/level/entity/EntitySectionStorage.java +++ b/src/main/java/net/minecraft/world/level/entity/EntitySectionStorage.java @@ -32,33 +32,44 @@ public class EntitySectionStorage { this.intialSectionVisibility = chunkStatusDiscriminator; } + // JettPack start - lithium: fast retrieval public void forEachAccessibleNonEmptySection(AABB box, Consumer> action) { - int i = SectionPos.posToSectionCoord(box.minX - 2.0D); - int j = SectionPos.posToSectionCoord(box.minY - 2.0D); - int k = SectionPos.posToSectionCoord(box.minZ - 2.0D); - int l = SectionPos.posToSectionCoord(box.maxX + 2.0D); - int m = SectionPos.posToSectionCoord(box.maxY + 2.0D); - int n = SectionPos.posToSectionCoord(box.maxZ + 2.0D); - - for(int o = i; o <= l; ++o) { - long p = SectionPos.asLong(o, 0, 0); - long q = SectionPos.asLong(o, -1, -1); - LongIterator longIterator = this.sectionIds.subSet(p, q + 1L).iterator(); - - while(longIterator.hasNext()) { - long r = longIterator.nextLong(); - int s = SectionPos.y(r); - int t = SectionPos.z(r); - if (s >= j && s <= m && t >= k && t <= n) { - EntitySection entitySection = this.sections.get(r); - if (entitySection != null && !entitySection.isEmpty() && entitySection.getStatus().isAccessible()) { - action.accept(entitySection); - } - } + int minX = SectionPos.posToSectionCoord(box.minX - 2.0D); + int minY = SectionPos.posToSectionCoord(box.minY - 2.0D); + int minZ = SectionPos.posToSectionCoord(box.minZ - 2.0D); + int maxX = SectionPos.posToSectionCoord(box.maxX + 2.0D); + int maxY = SectionPos.posToSectionCoord(box.maxY + 2.0D); + int maxZ = SectionPos.posToSectionCoord(box.maxZ + 2.0D); + + for (int x = minX; x <= maxX; x++) { + for (int z = Math.max(minZ, 0); z <= maxZ; z++) { + this.forEachInColumn(x, minY, maxY, z, action); } + + int bound = Math.min(-1, maxZ); + for (int z = minZ; z <= bound; z++) { + this.forEachInColumn(x, minY, maxY, z, action); + } + } + } + + private void forEachInColumn(int x, int minY, int maxY, int z, Consumer> action) { + for (int y = Math.max(minY, 0); y <= maxY; y++) { + this.consumeSection(x, y, z, action); + } + int bound = Math.min(-1, maxY); + for (int y = minY; y <= bound; y++) { + this.consumeSection(x, y, z, action); } + } + private void consumeSection(int x, int y, int z, Consumer> action) { + EntitySection section = this.getSection(SectionPos.asLong(x, y, z)); + if (section != null && section.getStatus().isAccessible()) { + action.accept(section); + } } + // JettPack end public LongStream getExistingSectionPositionsInChunk(long chunkPos) { int i = ChunkPos.getX(chunkPos);