Significantly improve performance in ChunkBatchingExtent and other buffering extents (#550)

* Re-apply non-adapter performance boosts in separate PR

* Improved deprecation handling

* Move TODO
This commit is contained in:
Matthew Miller 2020-02-07 20:28:14 +10:00 committed by GitHub
parent 92eab931ea
commit f111f6bfce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 45 additions and 25 deletions

View File

@ -26,7 +26,6 @@
import com.sk89q.worldedit.blocks.BaseItemStack;
import com.sk89q.worldedit.bukkit.adapter.BukkitImplAdapter;
import com.sk89q.worldedit.entity.BaseEntity;
import com.sk89q.worldedit.history.change.BlockChange;
import com.sk89q.worldedit.math.BlockVector2;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.math.Vector3;

View File

@ -27,6 +27,8 @@
import java.util.Optional;
import javax.annotation.Nullable;
/**
* Base extent class for buffering changes between {@link #setBlock(BlockVector3, BlockStateHolder)}
* and the delegate extent. This class ensures that {@link #getBlock(BlockVector3)} is properly
@ -51,17 +53,39 @@ protected final <T extends BlockStateHolder<T>> boolean setDelegateBlock(BlockVe
@Override
public BlockState getBlock(BlockVector3 position) {
return getBufferedBlock(position)
.map(BaseBlock::toImmutableState)
.orElseGet(() -> super.getBlock(position));
BaseBlock block = getBufferedFullBlock(position);
if (block == null) {
return super.getBlock(position);
}
return block.toImmutableState();
}
@Override
public BaseBlock getFullBlock(BlockVector3 position) {
return getBufferedBlock(position)
.orElseGet(() -> super.getFullBlock(position));
BaseBlock block = getBufferedFullBlock(position);
if (block == null) {
return super.getFullBlock(position);
}
return block;
}
protected abstract Optional<BaseBlock> getBufferedBlock(BlockVector3 position);
@Deprecated
protected Optional<BaseBlock> getBufferedBlock(BlockVector3 position) {
throw new IllegalStateException("Invalid BufferingExtent provided. Must override `getBufferedFullBlock(BlockVector3)`.");
}
//TODO make below abstract
/**
* Gets a block from the buffer, or null if not buffered.
*
* This **must** be overridden, and will be abstract in WorldEdit 8.
*
* @param position The position
* @return The buffered block, or null
*/
@Nullable
protected BaseBlock getBufferedFullBlock(BlockVector3 position) {
return getBufferedBlock(position).orElse(null);
}
}

View File

@ -30,7 +30,6 @@
import com.sk89q.worldedit.world.block.BlockStateHolder;
import java.util.Map;
import java.util.Optional;
import static com.google.common.base.Preconditions.checkNotNull;
@ -67,11 +66,11 @@ public ExtentBuffer(Extent delegate, Mask mask) {
}
@Override
protected Optional<BaseBlock> getBufferedBlock(BlockVector3 position) {
protected BaseBlock getBufferedFullBlock(BlockVector3 position) {
if (mask.test(position)) {
return Optional.of(buffer.computeIfAbsent(position, (pos -> getExtent().getFullBlock(pos))));
return buffer.computeIfAbsent(position, (pos -> getExtent().getFullBlock(pos)));
}
return Optional.empty();
return null;
}
@Override

View File

@ -32,8 +32,6 @@
import com.sk89q.worldedit.world.block.BlockStateHolder;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
/**
* A special extent that batches changes into Minecraft chunks. This helps
@ -77,8 +75,8 @@ public <B extends BlockStateHolder<B>> boolean setBlock(BlockVector3 location, B
}
@Override
protected Optional<BaseBlock> getBufferedBlock(BlockVector3 position) {
return Optional.ofNullable(blockMap.get(position));
protected BaseBlock getBufferedFullBlock(BlockVector3 position) {
return blockMap.get(position);
}
@Override
@ -94,8 +92,7 @@ protected Operation commitBefore() {
@Override
public Operation resume(RunContext run) throws WorldEditException {
if (iterator == null) {
iterator = ImmutableSortedSet.copyOf(RegionOptimizedComparator.INSTANCE,
blockMap.keySet()).iterator();
iterator = blockMap.keySet().parallelStream().sorted(RegionOptimizedComparator.INSTANCE).iterator();
}
while (iterator.hasNext()) {
BlockVector3 position = iterator.next();

View File

@ -39,8 +39,6 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
/**
* Re-orders blocks into several stages.
@ -247,11 +245,14 @@ public <B extends BlockStateHolder<B>> boolean setBlock(BlockVector3 location, B
}
@Override
protected Optional<BaseBlock> getBufferedBlock(BlockVector3 position) {
return stages.values().stream()
.map(blocks -> blocks.get(position))
.filter(Objects::nonNull)
.findAny();
protected BaseBlock getBufferedFullBlock(BlockVector3 position) {
for (BlockMap<BaseBlock> blocks : stages.values()) {
BaseBlock baseBlock = blocks.get(position);
if (baseBlock != null) {
return baseBlock;
}
}
return null;
}
@Override

View File

@ -74,7 +74,7 @@
*/
@Plugin(id = SpongeWorldEdit.MOD_ID, name = "WorldEdit",
description = "WorldEdit is an easy-to-use in-game world editor for Minecraft",
url = "http://www.enginehub.org/worldedit")
url = "https://enginehub.org/worldedit/")
public class SpongeWorldEdit {
@Inject