Skip to content
Permalink
Browse files

Significantly improve performance in ChunkBatchingExtent and other bu…

…ffering extents (#550)

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

* Improved deprecation handling

* Move TODO
  • Loading branch information
me4502 committed Feb 7, 2020
1 parent 92eab93 commit f111f6bfce27ce4ee4c7be2ebcb903cdfc0f5c16
@@ -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;
@@ -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 AbstractBufferingExtent(Extent extent) {

@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;
}

@Deprecated
protected Optional<BaseBlock> getBufferedBlock(BlockVector3 position) {
throw new IllegalStateException("Invalid BufferingExtent provided. Must override `getBufferedFullBlock(BlockVector3)`.");
}

protected abstract Optional<BaseBlock> getBufferedBlock(BlockVector3 position);
//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);
}

}
@@ -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
@@ -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 boolean commitRequired() {
}

@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();
@@ -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 boolean commitRequired() {
}

@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
@@ -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

0 comments on commit f111f6b

Please sign in to comment.
You can’t perform that action at this time.