Apply a few micro-optimisations (#2391)

* Apply a few micro-optimisations

* Remove this bit as it alters behaviour

* Review changes
This commit is contained in:
Maddy Miller 2023-09-03 19:01:26 +10:00 committed by GitHub
parent 3f165638b6
commit 854fa63851
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 60 additions and 70 deletions

View File

@ -458,9 +458,9 @@ public <B extends BlockStateHolder<B>> boolean setBlock(BlockVector3 position, B
try { try {
return worldNativeAccess.setBlock(position, block, sideEffects); return worldNativeAccess.setBlock(position, block, sideEffects);
} catch (Exception e) { } catch (Exception e) {
if (block instanceof BaseBlock && ((BaseBlock) block).getNbt() != null) { if (block instanceof BaseBlock baseBlock && baseBlock.getNbt() != null) {
LOGGER.warn("Tried to set a corrupt tile entity at " + position.toString() LOGGER.warn("Tried to set a corrupt tile entity at " + position.toString()
+ ": " + ((BaseBlock) block).getNbt(), e); + ": " + baseBlock.getNbt(), e);
} else { } else {
LOGGER.warn("Failed to set block via adapter, falling back to generic", e); LOGGER.warn("Failed to set block via adapter, falling back to generic", e);
} }

View File

@ -89,7 +89,7 @@ public void build(EditSession editSession, BlockVector3 position, Pattern patter
} }
for (LocatedBlock block : column) { for (LocatedBlock block : column) {
editSession.setBlock(block.getLocation(), block.getBlock()); editSession.setBlock(block.location(), block.block());
} }
for (BlockVector3 removedBlock : removedBlocks) { for (BlockVector3 removedBlock : removedBlocks) {

View File

@ -88,6 +88,7 @@ public boolean isEnabled() {
*/ */
public void setEnabled(boolean enabled) { public void setEnabled(boolean enabled) {
this.enabled = enabled; this.enabled = enabled;
this.changeSet.setRecordChanges(enabled);
} }
@Override @Override

View File

@ -50,6 +50,8 @@ public BlockState getBlock(BlockVector3 position) {
CachedBlock<BlockState> lastBlock = this.lastBlock; CachedBlock<BlockState> lastBlock = this.lastBlock;
if (lastBlock != null && lastBlock.position.equals(position)) { if (lastBlock != null && lastBlock.position.equals(position)) {
return lastBlock.block; return lastBlock.block;
} else if (lastFullBlock != null && lastFullBlock.position.equals(position)) {
return lastFullBlock.block().toImmutableState();
} else { } else {
BlockState block = super.getBlock(position); BlockState block = super.getBlock(position);
this.lastBlock = new CachedBlock<>(position, block); this.lastBlock = new CachedBlock<>(position, block);
@ -84,14 +86,7 @@ public <T extends BlockStateHolder<T>> boolean setBlock(BlockVector3 location, T
return false; return false;
} }
private static class CachedBlock<B extends BlockStateHolder<B>> { private record CachedBlock<B extends BlockStateHolder<B>>(BlockVector3 position, B block) {
private final BlockVector3 position;
private final B block;
private CachedBlock(BlockVector3 position, B block) {
this.position = position;
this.block = block;
}
} }
} }

View File

@ -77,6 +77,10 @@ public <B extends BlockStateHolder<B>> boolean setBlock(BlockVector3 location, B
@Override @Override
protected BaseBlock getBufferedFullBlock(BlockVector3 position) { protected BaseBlock getBufferedFullBlock(BlockVector3 position) {
if (!enabled) {
// Early exit if we're not enabled.
return null;
}
return blockMap.get(position); return blockMap.get(position);
} }

View File

@ -259,6 +259,10 @@ public <B extends BlockStateHolder<B>> boolean setBlock(BlockVector3 location, B
@Override @Override
protected BaseBlock getBufferedFullBlock(BlockVector3 position) { protected BaseBlock getBufferedFullBlock(BlockVector3 position) {
if (!enabled) {
// Early-exit if we know we're not enabled.
return null;
}
for (BlockMap<BaseBlock> blocks : stages.values()) { for (BlockMap<BaseBlock> blocks : stages.values()) {
BaseBlock baseBlock = blocks.get(position); BaseBlock baseBlock = blocks.get(position);
if (baseBlock != null) { if (baseBlock != null) {

View File

@ -117,12 +117,15 @@ public static <B extends BlockStateHolder<B>> B transform(B block, Transform tra
checkNotNull(block); checkNotNull(block);
checkNotNull(transform); checkNotNull(transform);
if (transform.isIdentity()) {
return block;
}
B result = block; B result = block;
List<? extends Property<?>> properties = block.getBlockType().getProperties(); List<? extends Property<?>> properties = block.getBlockType().getProperties();
for (Property<?> property : properties) { for (Property<?> property : properties) {
if (property instanceof DirectionalProperty) { if (property instanceof DirectionalProperty dirProp) {
DirectionalProperty dirProp = (DirectionalProperty) property;
Direction value = (Direction) block.getState(property); Direction value = (Direction) block.getState(property);
if (value != null) { if (value != null) {
Vector3 newValue = getNewStateValue(dirProp.getValues(), transform, value.toVector()); Vector3 newValue = getNewStateValue(dirProp.getValues(), transform, value.toVector());
@ -130,24 +133,15 @@ public static <B extends BlockStateHolder<B>> B transform(B block, Transform tra
result = result.with(dirProp, Direction.findClosest(newValue, Direction.Flag.ALL)); result = result.with(dirProp, Direction.findClosest(newValue, Direction.Flag.ALL));
} }
} }
} else if (property instanceof EnumProperty) { } else if (property instanceof EnumProperty enumProp) {
EnumProperty enumProp = (EnumProperty) property;
if (property.getName().equals("axis")) { if (property.getName().equals("axis")) {
// We have an axis - this is something we can do the rotations to :sunglasses: // We have an axis - this is something we can do the rotations to :sunglasses:
Direction value = null; Direction value = switch ((String) block.getState(property)) {
switch ((String) block.getState(property)) { case "x" -> Direction.EAST;
case "x": case "y" -> Direction.UP;
value = Direction.EAST; case "z" -> Direction.NORTH;
break; default -> null;
case "y": };
value = Direction.UP;
break;
case "z":
value = Direction.NORTH;
break;
default:
break;
}
if (value != null) { if (value != null) {
Vector3 newValue = getNewStateValue(Direction.valuesOf(Direction.Flag.UPRIGHT | Direction.Flag.CARDINAL), transform, value.toVector()); Vector3 newValue = getNewStateValue(Direction.valuesOf(Direction.Flag.UPRIGHT | Direction.Flag.CARDINAL), transform, value.toVector());
if (newValue != null) { if (newValue != null) {
@ -225,8 +219,7 @@ public static <B extends BlockStateHolder<B>> B transform(B block, Transform tra
} }
} }
} }
} else if (property instanceof IntegerProperty) { } else if (property instanceof IntegerProperty intProp) {
IntegerProperty intProp = (IntegerProperty) property;
if (property.getName().equals("rotation")) { if (property.getName().equals("rotation")) {
if (intProp.getValues().size() == 16) { if (intProp.getValues().size() == 16) {
Optional<Direction> direction = Direction.fromRotationIndex(block.getState(intProp)); Optional<Direction> direction = Direction.fromRotationIndex(block.getState(intProp));

View File

@ -38,7 +38,7 @@ public SetLocatedBlocks(Extent extent, Iterable<LocatedBlock> blocks) {
@Override @Override
public Operation resume(RunContext run) throws WorldEditException { public Operation resume(RunContext run) throws WorldEditException {
for (LocatedBlock block : blocks) { for (LocatedBlock block : blocks) {
extent.setBlock(block.getLocation(), block.getBlock()); extent.setBlock(block.location(), block.block());
} }
return null; return null;
} }

View File

@ -42,7 +42,7 @@
public class BlockOptimizedHistory extends ArrayListHistory { public class BlockOptimizedHistory extends ArrayListHistory {
private static Change createChange(LocatedBlock block) { private static Change createChange(LocatedBlock block) {
return new BlockChange(block.getLocation(), block.getBlock(), block.getBlock()); return new BlockChange(block.location(), block.block(), block.block());
} }
private final LocatedBlockList previous = new LocatedBlockList(); private final LocatedBlockList previous = new LocatedBlockList();
@ -52,14 +52,16 @@ private static Change createChange(LocatedBlock block) {
public void add(Change change) { public void add(Change change) {
checkNotNull(change); checkNotNull(change);
if (change instanceof BlockChange blockChange) { if (isRecordingChanges()) {
BlockVector3 position = blockChange.getPosition(); if (change instanceof BlockChange blockChange) {
if (!previous.containsLocation(position)) { BlockVector3 position = blockChange.getPosition();
previous.add(position, blockChange.getPrevious()); if (!previous.containsLocation(position)) {
previous.add(position, blockChange.getPrevious());
}
current.add(position, blockChange.getCurrent());
} else {
super.add(change);
} }
current.add(position, blockChange.getCurrent());
} else {
super.add(change);
} }
} }

View File

@ -22,46 +22,37 @@
import com.sk89q.worldedit.math.BlockVector3; import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.world.block.BaseBlock; import com.sk89q.worldedit.world.block.BaseBlock;
import java.util.Objects;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
/** /**
* Represents a block located at some position. * Represents a block located at some position.
*/ */
public final class LocatedBlock { public record LocatedBlock(BlockVector3 location, BaseBlock block) {
private final BlockVector3 location; public LocatedBlock {
private final BaseBlock block; checkNotNull(location);
checkNotNull(block);
public LocatedBlock(BlockVector3 location, BaseBlock block) {
this.location = checkNotNull(location);
this.block = checkNotNull(block);
} }
/**
* Gets the location.
*
* @return The location
* @deprecated This class is now a record. Use {@link #location()} instead.
*/
@Deprecated
public BlockVector3 getLocation() { public BlockVector3 getLocation() {
return location; return this.location;
} }
/**
* Gets the block.
*
* @return The block
* @deprecated This class is now a record. Use {@link #block()} instead.
*/
public BaseBlock getBlock() { public BaseBlock getBlock() {
return block; return this.block;
}
@Override
public int hashCode() {
return Objects.hash(location, block);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (this.getClass() != obj.getClass()) {
return false;
}
LocatedBlock lb = (LocatedBlock) obj;
return Objects.equals(location, lb.location) && Objects.equals(block, lb.block);
} }
} }

View File

@ -47,13 +47,13 @@ public LocatedBlockList() {
public LocatedBlockList(Collection<? extends LocatedBlock> collection) { public LocatedBlockList(Collection<? extends LocatedBlock> collection) {
for (LocatedBlock locatedBlock : collection) { for (LocatedBlock locatedBlock : collection) {
add(locatedBlock.getLocation(), locatedBlock.getBlock()); add(locatedBlock.location(), locatedBlock.block());
} }
} }
public void add(LocatedBlock setBlockCall) { public void add(LocatedBlock setBlockCall) {
checkNotNull(setBlockCall); checkNotNull(setBlockCall);
add(setBlockCall.getLocation(), setBlockCall.getBlock()); add(setBlockCall.location(), setBlockCall.block());
} }
public <B extends BlockStateHolder<B>> void add(BlockVector3 location, B block) { public <B extends BlockStateHolder<B>> void add(BlockVector3 location, B block) {