Skip to content

Commit afec252

Browse files
authored
Optimize layer iteration to prevent performance overhead. (#3022)
* Optimize layer iteration to prevent performance overhead. Adjusted the layer iteration logic in the Region class to use the minimum and maximum Y-values from the chunk. This prevents unnecessary iterations when getMinimumY or getMaximumY returns extreme values, improving performance efficiency. * Optimize layer iteration logic in Region class. Refactored the loop to reduce repeated evaluations by storing pre-calculated min and max values for Y coordinates. This change enhances readability and may improve performance by minimizing unnecessary calculations during the iteration process. * Update variable naming * Shift bitwise operations to variable initialization
1 parent b548219 commit afec252

File tree

1 file changed

+6
-2
lines changed
  • worldedit-core/src/main/java/com/sk89q/worldedit/regions

1 file changed

+6
-2
lines changed

worldedit-core/src/main/java/com/sk89q/worldedit/regions/Region.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,9 @@ default IChunkSet processSet(IChunk chunk, IChunkGet get, IChunkSet set) {
403403
if (tx >= min.x() && bx <= max.x() && tz >= min.z() && bz <= max.z()) {
404404
// contains some
405405
boolean processExtra = false;
406-
for (int layer = getMinimumY() >> 4; layer <= getMaximumY() >> 4; layer++) {
406+
final int minLayer = Math.max(getMinimumY(), chunk.getMinY()) >> 4;
407+
final int maxLayer = Math.min(getMaximumY(), chunk.getMaxY()) >> 4;
408+
for (int layer = minLayer; layer <= maxLayer; layer++) {
407409
if (!set.hasSection(layer)) {
408410
continue;
409411
}
@@ -457,7 +459,9 @@ default IChunkSet processSet(IChunk chunk, IChunkGet get, IChunkSet set, boolean
457459
if (tx >= min.x() && bx <= max.x() && tz >= min.z() && bz <= max.z()) {
458460
// contains some
459461
boolean processExtra = false;
460-
for (int layer = getMinimumY() >> 4; layer <= getMaximumY() >> 4; layer++) {
462+
final int minLayer = Math.max(getMinimumY(), chunk.getMinY()) >> 4;
463+
final int maxLayer = Math.min(getMaximumY(), chunk.getMaxY()) >> 4;
464+
for (int layer = minLayer; layer <= maxLayer; layer++) {
461465
int by = layer << 4;
462466
int ty = by + 15;
463467
if (containsEntireCuboid(bx, tx, by, ty, bz, tz)) {

0 commit comments

Comments
 (0)