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:
Kenzie Togami
2019-08-12 05:06:40 -07:00
committed by Matthew Miller
parent ec5bc5a3b7
commit f472c20bfb
30 changed files with 2014 additions and 139 deletions

View File

@ -26,9 +26,7 @@ import com.google.common.collect.Maps;
import com.google.common.collect.Table;
import com.sk89q.jnbt.CompoundTag;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.extension.platform.Capability;
import com.sk89q.worldedit.registry.state.Property;
import com.sk89q.worldedit.world.registry.BlockRegistry;
import java.util.Collections;
import java.util.Comparator;
@ -60,7 +58,6 @@ public class BlockState implements BlockStateHolder<BlockState> {
}
static Map<Map<Property<?>, Object>, BlockState> generateStateMap(BlockType blockType) {
BlockRegistry registry = WorldEdit.getInstance().getPlatformManager().queryCapability(Capability.WORLD_EDITING).getRegistries().getBlockRegistry();
Map<Map<Property<?>, Object>, BlockState> stateMap = new LinkedHashMap<>();
List<? extends Property<?>> properties = blockType.getProperties();

View File

@ -19,28 +19,26 @@
package com.sk89q.worldedit.world.block;
import static com.google.common.base.Preconditions.checkArgument;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.extension.platform.Capability;
import com.sk89q.worldedit.registry.Keyed;
import com.sk89q.worldedit.registry.NamespacedRegistry;
import com.sk89q.worldedit.registry.state.Property;
import com.sk89q.worldedit.util.concurrency.LazyReference;
import com.sk89q.worldedit.world.item.ItemType;
import com.sk89q.worldedit.world.item.ItemTypes;
import com.sk89q.worldedit.world.registry.BlockMaterial;
import com.sk89q.worldedit.world.registry.LegacyMapper;
import java.util.ArrayList;
import javax.annotation.Nullable;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;
import java.util.function.Supplier;
import javax.annotation.Nullable;
import static com.google.common.base.Preconditions.checkArgument;
public class BlockType implements Keyed {
@ -48,11 +46,18 @@ public class BlockType implements Keyed {
private final String id;
private final Function<BlockState, BlockState> values;
private final AtomicReference<BlockState> defaultState = new AtomicReference<>();
private final AtomicReference<FuzzyBlockState> emptyFuzzy = new AtomicReference<>();
private final AtomicReference<Map<String, ? extends Property<?>>> properties = new AtomicReference<>();
private final AtomicReference<BlockMaterial> blockMaterial = new AtomicReference<>();
private final AtomicReference<Map<Map<Property<?>, Object>, BlockState>> blockStatesMap = new AtomicReference<>();
private final LazyReference<BlockState> defaultState
= LazyReference.from(this::computeDefaultState);
private final LazyReference<FuzzyBlockState> emptyFuzzy
= LazyReference.from(() -> new FuzzyBlockState(this));
private final LazyReference<Map<String, ? extends Property<?>>> properties
= LazyReference.from(() -> ImmutableMap.copyOf(WorldEdit.getInstance().getPlatformManager()
.queryCapability(Capability.GAME_HOOKS).getRegistries().getBlockRegistry().getProperties(this)));
private final LazyReference<BlockMaterial> blockMaterial
= LazyReference.from(() -> WorldEdit.getInstance().getPlatformManager()
.queryCapability(Capability.GAME_HOOKS).getRegistries().getBlockRegistry().getMaterial(this));
private final LazyReference<Map<Map<Property<?>, Object>, BlockState>> blockStatesMap
= LazyReference.from(() -> BlockState.generateStateMap(this));
public BlockType(String id) {
this(id, null);
@ -67,24 +72,16 @@ public class BlockType implements Keyed {
this.values = values;
}
private <T> T updateField(AtomicReference<T> field, Supplier<T> value) {
T result = field.get();
if (result == null) {
// swap in new value, if someone doesn't beat us
T update = value.get();
if (field.compareAndSet(null, update)) {
// use ours
result = update;
} else {
// update to real value
result = field.get();
}
private BlockState computeDefaultState() {
BlockState defaultState = Iterables.getFirst(getBlockStatesMap().values(), null);
if (values != null) {
defaultState = values.apply(defaultState);
}
return result;
return defaultState;
}
private Map<Map<Property<?>, Object>, BlockState> getBlockStatesMap() {
return updateField(blockStatesMap, () -> BlockState.generateStateMap(this));
return blockStatesMap.getValue();
}
/**
@ -117,8 +114,7 @@ public class BlockType implements Keyed {
* @return The properties map
*/
public Map<String, ? extends Property<?>> getPropertyMap() {
return updateField(properties, () -> ImmutableMap.copyOf(WorldEdit.getInstance().getPlatformManager()
.queryCapability(Capability.GAME_HOOKS).getRegistries().getBlockRegistry().getProperties(this)));
return properties.getValue();
}
/**
@ -150,17 +146,11 @@ public class BlockType implements Keyed {
* @return The default state
*/
public BlockState getDefaultState() {
return updateField(defaultState, () -> {
BlockState defaultState = new ArrayList<>(getBlockStatesMap().values()).get(0);
if (values != null) {
defaultState = values.apply(defaultState);
}
return defaultState;
});
return defaultState.getValue();
}
public FuzzyBlockState getFuzzyMatcher() {
return updateField(emptyFuzzy, () -> new FuzzyBlockState(this));
return emptyFuzzy.getValue();
}
/**
@ -208,8 +198,7 @@ public class BlockType implements Keyed {
* @return The material
*/
public BlockMaterial getMaterial() {
return updateField(blockMaterial, () -> WorldEdit.getInstance().getPlatformManager()
.queryCapability(Capability.GAME_HOOKS).getRegistries().getBlockRegistry().getMaterial(this));
return blockMaterial.getValue();
}
/**