mirror of
https://github.com/plexusorg/Plex-FAWE.git
synced 2025-07-12 08:18:35 +00:00
Memory optimizations (#505)
* Remove LocatedBlock overhead in LBL map * Add new space-efficient block map, with thourough testing * Drop ordering property, add full insertion test * Add licenses * Fix mocked platform conflicts * Disable full block map testing for faster builds * Re-implement BlockMap with fastutil maps * Re-write chunk batching to be memory efficient * Make MultiStageReorder use BlockMap * Increase LBL load factor, fix long-pack limit detection * Fix infinite loop in chunk batching * Save memory in history by cleaning up MSR * Re-implement LocatedBlockList in BlockMap * Fix data race with BlockType lazy fields * Make IDs ALWAYS present, only runtime-consistent. Use for memory efficiency in BlockMap * Remap inner structure of BlockMap for smaller maps * Remove containedBlocks fields, not very efficient * Fix minor de-optimizing bug in stage reorder * Make long packed y signed * Add extended Y limit configuration option * Add licenses * Store 3 ints for unoptimized BV list * Add final to BitMath * Correct int-cast for long-packing
This commit is contained in:
committed by
Matthew Miller
parent
ec5bc5a3b7
commit
f472c20bfb
@ -0,0 +1,52 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldedit.math;
|
||||
|
||||
public final class BitMath {
|
||||
|
||||
public static int mask(int bits) {
|
||||
return ~(~0 << bits);
|
||||
}
|
||||
|
||||
public static int unpackX(long packed) {
|
||||
return extractSigned(packed, 0, 26);
|
||||
}
|
||||
|
||||
public static int unpackZ(long packed) {
|
||||
return extractSigned(packed, 26, 26);
|
||||
}
|
||||
|
||||
public static int unpackY(long packed) {
|
||||
return extractSigned(packed, 26 + 26, 12);
|
||||
}
|
||||
|
||||
public static int extractSigned(long i, int shift, int bits) {
|
||||
return fixSign((int) (i >> shift) & mask(bits), bits);
|
||||
}
|
||||
|
||||
public static int fixSign(int i, int bits) {
|
||||
// Using https://stackoverflow.com/a/29266331/436524
|
||||
return i << (32 - bits) >> (32 - bits);
|
||||
}
|
||||
|
||||
private BitMath() {
|
||||
}
|
||||
|
||||
}
|
@ -24,6 +24,10 @@ import com.sk89q.worldedit.math.transform.AffineTransform;
|
||||
import java.util.Comparator;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkArgument;
|
||||
import static com.sk89q.worldedit.math.BitMath.mask;
|
||||
import static com.sk89q.worldedit.math.BitMath.unpackX;
|
||||
import static com.sk89q.worldedit.math.BitMath.unpackY;
|
||||
import static com.sk89q.worldedit.math.BitMath.unpackZ;
|
||||
|
||||
/**
|
||||
* An immutable 3-dimensional vector.
|
||||
@ -61,6 +65,31 @@ public final class BlockVector3 {
|
||||
return new BlockVector3(x, y, z);
|
||||
}
|
||||
|
||||
private static final int WORLD_XZ_MINMAX = 30_000_000;
|
||||
private static final int WORLD_Y_MAX = 4095;
|
||||
|
||||
private static boolean isHorizontallyInBounds(int h) {
|
||||
return -WORLD_XZ_MINMAX <= h && h <= WORLD_XZ_MINMAX;
|
||||
}
|
||||
|
||||
public static boolean isLongPackable(BlockVector3 location) {
|
||||
return isHorizontallyInBounds(location.getX()) &&
|
||||
isHorizontallyInBounds(location.getZ()) &&
|
||||
0 <= location.getY() && location.getY() <= WORLD_Y_MAX;
|
||||
}
|
||||
|
||||
public static void checkLongPackable(BlockVector3 location) {
|
||||
checkArgument(isLongPackable(location),
|
||||
"Location exceeds long packing limits: %s", location);
|
||||
}
|
||||
|
||||
private static final long BITS_26 = mask(26);
|
||||
private static final long BITS_12 = mask(12);
|
||||
|
||||
public static BlockVector3 fromLongPackedForm(long packed) {
|
||||
return at(unpackX(packed), unpackY(packed), unpackZ(packed));
|
||||
}
|
||||
|
||||
// thread-safe initialization idiom
|
||||
private static final class YzxOrderComparator {
|
||||
private static final Comparator<BlockVector3> YZX_ORDER =
|
||||
@ -94,6 +123,11 @@ public final class BlockVector3 {
|
||||
this.z = z;
|
||||
}
|
||||
|
||||
public long toLongPackedForm() {
|
||||
checkLongPackable(this);
|
||||
return (x & BITS_26) | ((z & BITS_26) << 26) | (((y & (long) BITS_12) << (26 + 26)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the X coordinate.
|
||||
*
|
||||
|
@ -0,0 +1,41 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldedit.math;
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
import static com.sk89q.worldedit.math.BlockVector2.COMPARING_GRID_ARRANGEMENT;
|
||||
|
||||
/**
|
||||
* Sort block positions by region, then chunk.
|
||||
*/
|
||||
public class RegionOptimizedChunkComparator {
|
||||
|
||||
private static final Comparator<BlockVector2> CHUNK_COMPARATOR =
|
||||
Comparator.comparing((BlockVector2 chunkPos) -> chunkPos.shr(5), COMPARING_GRID_ARRANGEMENT)
|
||||
.thenComparing(COMPARING_GRID_ARRANGEMENT);
|
||||
|
||||
public static final Comparator<BlockVector3> INSTANCE
|
||||
= Comparator.comparing(blockPos -> blockPos.toBlockVector2().shr(4), CHUNK_COMPARATOR);
|
||||
|
||||
private RegionOptimizedChunkComparator() {
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldedit.math;
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
/**
|
||||
* Sort block positions by region, chunk, and finally Y-Z-X.
|
||||
*/
|
||||
public class RegionOptimizedComparator {
|
||||
|
||||
public static final Comparator<BlockVector3> INSTANCE
|
||||
= RegionOptimizedChunkComparator.INSTANCE
|
||||
.thenComparing(BlockVector3.sortByCoordsYzx());
|
||||
|
||||
private RegionOptimizedComparator() {
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user