Performance improvement based on case study by @me4502

This commit is contained in:
MattBDev
2020-03-19 13:08:25 -04:00
parent 2e1c0c83f5
commit 9f07894f28
14 changed files with 106 additions and 65 deletions

View File

@ -108,12 +108,13 @@ public class DefaultProgressTracker implements BiConsumer<DefaultProgressTracker
}
public void sendTask() {
String queue = StringMan.padRight("" + totalQueue, 3);
String dispatch = StringMan.padLeft("" + amountDispatch, 3);
String queue = StringMan.padRight(String.valueOf(totalQueue), 3);
String dispatch = StringMan.padLeft(String.valueOf(amountDispatch), 3);
int total = amountDispatch != 0 ? amountDispatch : amountQueue;
int speed = total != 0 ? (int) (total / Math.max((System.currentTimeMillis() - start) / 1000d, 1)) : 0;
String speedStr = StringMan.padRight("" + speed, 3);
String percent = StringMan.padRight("" + (amountDispatch != 0 ? (amountDispatch * 100) / totalQueue : 0), 3);
String speedStr = StringMan.padRight(String.valueOf(speed), 3);
String percent = StringMan.padRight(
String.valueOf(amountDispatch != 0 ? (amountDispatch * 100) / totalQueue : 0), 3);
int remaining = speed != 0 ? amountQueue / speed : -1;
sendTile(TextComponent.empty(), Caption.of("fawe.progress.progress.message", queue, dispatch, percent, StringMan.padLeft("" + speed, 3), StringMan.padLeft("" + remaining, 3)));
}

View File

@ -15,6 +15,7 @@ import com.sk89q.worldedit.regions.AbstractRegion;
import com.sk89q.worldedit.regions.RegionOperationException;
import com.sk89q.worldedit.world.World;
import java.util.Iterator;
import org.jetbrains.annotations.NotNull;
public class FuzzyRegion extends AbstractRegion {
@ -56,6 +57,7 @@ public class FuzzyRegion extends AbstractRegion {
Operations.completeBlindly(search);
}
@NotNull
@Override
public Iterator<BlockVector3> iterator() {
return set.iterator();

View File

@ -249,9 +249,9 @@ public class PolyhedralRegion extends AbstractRegion {
for (int i = 0; i < triangles.size(); ++i) {
final Triangle triangle = triangles.get(i);
final BlockVector3 v0 = change.add(triangle.getVertex(0).toBlockPoint());
final BlockVector3 v1 = change.add(triangle.getVertex(1).toBlockPoint());
final BlockVector3 v2 = change.add(triangle.getVertex(2).toBlockPoint());
final BlockVector3 v0 = change.add(triangle.getVertex(0));
final BlockVector3 v1 = change.add(triangle.getVertex(1));
final BlockVector3 v2 = change.add(triangle.getVertex(2));
triangles.set(i, new Triangle(v0, v1, v2));
}

View File

@ -62,8 +62,8 @@ public class Triangle {
return StringMan.getString(verts);
}
public Vector3 getVertex(int index) {
return Vector3.at(verts[index][0], verts[index][1], verts[index][2]);
public BlockVector3 getVertex(int index) {
return BlockVector3.at(verts[index][0], verts[index][1], verts[index][2]);
}
public boolean contains(BlockVector3 pos) {

View File

@ -40,7 +40,6 @@ import com.sk89q.worldedit.regions.Region;
import com.sk89q.worldedit.util.eventbus.EventBus;
import com.sk89q.worldedit.world.World;
import java.util.UUID;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.jetbrains.annotations.NotNull;
@ -76,7 +75,7 @@ public class EditSessionBuilder {
*
* @param world A world must be provided for all EditSession(s)
*/
public EditSessionBuilder(@Nonnull World world) {
public EditSessionBuilder(@NotNull World world) {
checkNotNull(world);
this.world = world;
this.worldName = world.getName();
@ -88,7 +87,7 @@ public class EditSessionBuilder {
this.worldName = worldName;
}
public EditSessionBuilder(@Nonnull String worldName) {
public EditSessionBuilder(@NotNull String worldName) {
checkNotNull(worldName);
this.worldName = worldName;
this.world = FaweAPI.getWorld(worldName);
@ -108,7 +107,7 @@ public class EditSessionBuilder {
return limit(FaweLimit.MAX.copy());
}
public EditSessionBuilder limitUnprocessed(@Nonnull Player player) {
public EditSessionBuilder limitUnprocessed(@NotNull Player player) {
checkNotNull(player);
limitUnlimited();
FaweLimit tmp = player.getLimit();

View File

@ -26,6 +26,7 @@ import com.sk89q.worldedit.world.block.BlockState;
import com.sk89q.worldedit.world.block.BlockStateHolder;
import java.util.Optional;
import javax.annotation.Nullable;
/**
* Base extent class for buffering changes between {@link #setBlock(BlockVector3, BlockStateHolder)}
@ -51,17 +52,38 @@ public abstract class AbstractBufferingExtent extends AbstractDelegateExtent {
@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

@ -66,11 +66,11 @@ public class ExtentBuffer extends AbstractBufferingExtent {
}
@Override
protected Optional<BaseBlock> getBufferedBlock(BlockVector3 position) {
if (mask.test(getExtent(), position)) {
return Optional.of(buffer.computeIfAbsent(position, (pos -> getExtent().getFullBlock(pos))));
protected BaseBlock getBufferedFullBlock(BlockVector3 position) {
if (mask.test(position)) {
return buffer.computeIfAbsent(position, (pos -> getExtent().getFullBlock(pos)));
}
return Optional.empty();
return null;
}
@Override

View File

@ -77,8 +77,8 @@ public class ChunkBatchingExtent extends AbstractBufferingExtent {
}
@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 +94,7 @@ public class ChunkBatchingExtent extends AbstractBufferingExtent {
@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

@ -247,11 +247,14 @@ public class MultiStageReorder extends AbstractBufferingExtent implements Reorde
}
@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

@ -99,6 +99,9 @@ public final class BlockStateIdAccess {
*/
private BlockStateIdAccess() {
}
public static @Nullable BlockState getBlockStateById(int id) {
return BlockState.getFromOrdinal(id);
}

View File

@ -32,11 +32,13 @@ import com.sk89q.worldedit.registry.NamespacedRegistry;
import com.sk89q.worldedit.registry.state.AbstractProperty;
import com.sk89q.worldedit.registry.state.Property;
import com.sk89q.worldedit.registry.state.PropertyKey;
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.function.Function;
import javax.annotation.Nullable;
import java.util.Collections;
import java.util.List;
@ -53,10 +55,12 @@ public class BlockType implements Keyed, Pattern {
private final String id;
private final BlockTypesCache.Settings settings;
/*
private final LazyReference<Integer> legacyId = LazyReference.from(() -> computeLgacy(0));
private final LazyReference<FuzzyBlockState> emptyFuzzy
= LazyReference.from(() -> new FuzzyBlockState(this));
private final LazyReference<Integer> legacyId = LazyReference.from(() -> computeLegacy(0));
private final LazyReference<Integer> legacyData = LazyReference.from(() -> computeLegacy(1));
*/
private Integer legacyCombinedId;
private boolean initItemType;
private ItemType itemType;
@ -67,6 +71,16 @@ public class BlockType implements Keyed, Pattern {
this.settings = new BlockTypesCache.Settings(this, id, internalId, states);
}
public BlockType(String id, Function<BlockState, BlockState> values) {
// If it has no namespace, assume minecraft.
if (!id.contains(":")) {
id = "minecraft:" + id;
}
this.id = id;
//TODO fix the line below
this.settings = new BlockTypesCache.Settings(this, id, 0, null);
}
@Deprecated
public int getMaxStateId() {
return settings.permutations;
@ -179,13 +193,12 @@ public class BlockType implements Keyed, Pattern {
*
* @return The default state
*/
public final BlockState getDefaultState() {
public BlockState getDefaultState() {
return this.settings.defaultState;
}
@Deprecated
public FuzzyBlockState getFuzzyMatcher() {
return new FuzzyBlockState(this);
return emptyFuzzy.getValue();
}
/**