Rework block-batching, create draft of chunk batching

Dieser Commit ist enthalten in:
Kenzie Togami 2018-10-04 00:12:06 -07:00
Ursprung d1cb6e2156
Commit e059490cd1
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: 5D200B325E157A81
9 geänderte Dateien mit 306 neuen und 269 gelöschten Zeilen

Datei anzeigen

@ -19,12 +19,37 @@
package com.sk89q.worldedit;
import com.google.common.collect.ComparisonChain;
import com.sk89q.worldedit.math.transform.AffineTransform;
import java.util.Comparator;
/**
* An immutable 2-dimensional vector.
*/
public class Vector2D {
/**
* A comparator for Vector2Ds that orders the vectors by rows, with x as the
* column and z as the row.
*
* For example, if x is the horizontal axis and z is the vertical axis, it
* sorts like so:
*
* <pre>
* 0123
* 4567
* 90ab
* cdef
* </pre>
*/
public static final Comparator<Vector2D> COMPARING_GRID_ARRANGEMENT = (a, b) -> {
return ComparisonChain.start()
.compare(a.getBlockZ(), b.getBlockZ())
.compare(a.getBlockX(), b.getBlockX())
.result();
};
public static final Vector2D ZERO = new Vector2D(0, 0);
public static final Vector2D UNIT_X = new Vector2D(1, 0);
public static final Vector2D UNIT_Z = new Vector2D(0, 1);

Datei anzeigen

@ -0,0 +1,93 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.extent.reorder;
import com.sk89q.worldedit.BlockVector2D;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.Vector2D;
import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.extent.AbstractDelegateExtent;
import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.function.operation.Operation;
import com.sk89q.worldedit.function.operation.RunContext;
import com.sk89q.worldedit.function.operation.SetLocatedBlocks;
import com.sk89q.worldedit.util.collection.LocatedBlockList;
import com.sk89q.worldedit.world.block.BlockStateHolder;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.SortedMap;
import java.util.TreeMap;
/**
* A special extent that batches changes into Minecraft chunks. This helps
* improve the speed of setting the blocks, since chunks do not need to be
* loaded repeatedly, however it does take more memory due to caching the
* blocks.
*/
public class ChunkBatchingExtent extends AbstractDelegateExtent {
/**
* Comparator optimized for sorting chunks by the region file they reside
* in. This allows for file caches to be used while loading the chunk.
*/
private static final Comparator<Vector2D> REGION_OPTIMIZED_SORT =
Comparator.<Vector2D, Vector2D>comparing(vec -> vec.divide(32).floor(), Vector2D.COMPARING_GRID_ARRANGEMENT)
.thenComparing(Vector2D.COMPARING_GRID_ARRANGEMENT);
private final SortedMap<BlockVector2D, LocatedBlockList> batches = new TreeMap<>(REGION_OPTIMIZED_SORT);
protected ChunkBatchingExtent(Extent extent) {
super(extent);
}
@Override
public boolean setBlock(Vector location, BlockStateHolder block) throws WorldEditException {
BlockVector2D chunkPos = new BlockVector2D(location.getBlockX() >> 4, location.getBlockZ() >> 4);
batches.computeIfAbsent(chunkPos, k -> new LocatedBlockList()).add(location, block);
return true;
}
@Override
protected Operation commitBefore() {
return new Operation() {
private final Iterator<LocatedBlockList> batchIterator = batches.values().iterator();
@Override
public Operation resume(RunContext run) throws WorldEditException {
if (!batchIterator.hasNext()) {
return null;
}
new SetLocatedBlocks(getExtent(), batchIterator.next()).resume(run);
return this;
}
@Override
public void cancel() {
}
@Override
public void addStatusMessages(List<String> messages) {
}
};
}
}

Datei anzeigen

@ -19,19 +19,20 @@
package com.sk89q.worldedit.extent.reorder;
import com.google.common.collect.Iterators;
import com.google.common.collect.Iterables;
import com.sk89q.worldedit.BlockVector;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.blocks.Blocks;
import com.sk89q.worldedit.extent.AbstractDelegateExtent;
import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.function.operation.BlockMapEntryPlacer;
import com.sk89q.worldedit.function.operation.Operation;
import com.sk89q.worldedit.function.operation.OperationQueue;
import com.sk89q.worldedit.function.operation.RunContext;
import com.sk89q.worldedit.function.operation.SetLocatedBlocks;
import com.sk89q.worldedit.registry.state.Property;
import com.sk89q.worldedit.util.collection.TupleArrayList;
import com.sk89q.worldedit.util.LocatedBlock;
import com.sk89q.worldedit.util.collection.LocatedBlockList;
import com.sk89q.worldedit.world.block.BlockCategories;
import com.sk89q.worldedit.world.block.BlockState;
import com.sk89q.worldedit.world.block.BlockStateHolder;
@ -50,9 +51,9 @@ import java.util.Set;
*/
public class MultiStageReorder extends AbstractDelegateExtent implements ReorderingExtent {
private TupleArrayList<BlockVector, BlockStateHolder> stage1 = new TupleArrayList<>();
private TupleArrayList<BlockVector, BlockStateHolder> stage2 = new TupleArrayList<>();
private TupleArrayList<BlockVector, BlockStateHolder> stage3 = new TupleArrayList<>();
private LocatedBlockList stage1 = new LocatedBlockList();
private LocatedBlockList stage2 = new LocatedBlockList();
private LocatedBlockList stage3 = new LocatedBlockList();
private boolean enabled;
/**
@ -103,18 +104,18 @@ public class MultiStageReorder extends AbstractDelegateExtent implements Reorder
if (Blocks.shouldPlaceLast(block.getBlockType())) {
// Place torches, etc. last
stage2.put(location.toBlockVector(), block);
stage2.add(location.toBlockVector(), block);
return !existing.equalsFuzzy(block);
} else if (Blocks.shouldPlaceFinal(block.getBlockType())) {
// Place signs, reed, etc even later
stage3.put(location.toBlockVector(), block);
stage3.add(location.toBlockVector(), block);
return !existing.equalsFuzzy(block);
} else if (Blocks.shouldPlaceLast(existing.getBlockType())) {
// Destroy torches, etc. first
super.setBlock(location, BlockTypes.AIR.getDefaultState());
return super.setBlock(location, block);
} else {
stage1.put(location.toBlockVector(), block);
stage1.add(location.toBlockVector(), block);
return !existing.equalsFuzzy(block);
}
}
@ -122,9 +123,9 @@ public class MultiStageReorder extends AbstractDelegateExtent implements Reorder
@Override
public Operation commitBefore() {
return new OperationQueue(
new BlockMapEntryPlacer(
new SetLocatedBlocks(
getExtent(),
Iterators.concat(stage1.iterator(), stage2.iterator())),
Iterables.concat(stage1, stage2)),
new Stage3Committer());
}
@ -136,10 +137,10 @@ public class MultiStageReorder extends AbstractDelegateExtent implements Reorder
final Set<BlockVector> blocks = new HashSet<>();
final Map<BlockVector, BlockStateHolder> blockTypes = new HashMap<>();
for (Map.Entry<BlockVector, BlockStateHolder> entry : stage3) {
final BlockVector pt = entry.getKey();
for (LocatedBlock entry : stage3) {
final BlockVector pt = entry.getLocation().toBlockVector();
blocks.add(pt);
blockTypes.put(pt, entry.getValue());
blockTypes.put(pt, entry.getBlock());
}
while (!blocks.isEmpty()) {

Datei anzeigen

@ -16,50 +16,31 @@
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.function.operation;
import static com.google.common.base.Preconditions.checkNotNull;
import com.sk89q.worldedit.BlockVector;
import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.world.block.BaseBlock;
import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.world.block.BlockStateHolder;
import com.sk89q.worldedit.util.LocatedBlock;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
* Sets block from an iterator of {@link Map.Entry} containing a
* {@link BlockVector} as the key and a {@link BaseBlock} as the value.
*/
public class BlockMapEntryPlacer implements Operation {
public class SetLocatedBlocks implements Operation {
private final Extent extent;
private final Iterator<Map.Entry<BlockVector, BlockStateHolder>> iterator;
private final Iterable<LocatedBlock> blocks;
/**
* Create a new instance.
*
* @param extent the extent to set the blocks on
* @param iterator the iterator
*/
public BlockMapEntryPlacer(Extent extent, Iterator<Map.Entry<BlockVector, BlockStateHolder>> iterator) {
checkNotNull(extent);
checkNotNull(iterator);
this.extent = extent;
this.iterator = iterator;
public SetLocatedBlocks(Extent extent, Iterable<LocatedBlock> blocks) {
this.extent = checkNotNull(extent);
this.blocks = checkNotNull(blocks);
}
@Override
public Operation resume(RunContext run) throws WorldEditException {
while (iterator.hasNext()) {
Map.Entry<BlockVector, BlockStateHolder> entry = iterator.next();
extent.setBlock(entry.getKey(), entry.getValue());
for (LocatedBlock block : blocks) {
extent.setBlock(block.getLocation(), block.getBlock());
}
return null;
}

Datei anzeigen

@ -20,15 +20,13 @@
package com.sk89q.worldedit.history.changeset;
import static com.google.common.base.Preconditions.checkNotNull;
import static java.util.Map.Entry;
import com.google.common.base.Function;
import com.google.common.collect.Iterators;
import com.sk89q.worldedit.BlockVector;
import com.sk89q.worldedit.history.change.BlockChange;
import com.sk89q.worldedit.history.change.Change;
import com.sk89q.worldedit.util.collection.TupleArrayList;
import com.sk89q.worldedit.world.block.BlockStateHolder;
import com.sk89q.worldedit.util.LocatedBlock;
import com.sk89q.worldedit.util.collection.LocatedBlockList;
import java.util.ArrayList;
import java.util.Iterator;
@ -43,8 +41,12 @@ import java.util.Iterator;
*/
public class BlockOptimizedHistory extends ArrayListHistory {
private final TupleArrayList<BlockVector, BlockStateHolder> previous = new TupleArrayList<>();
private final TupleArrayList<BlockVector, BlockStateHolder> current = new TupleArrayList<>();
private static Change createChange(LocatedBlock block) {
return new BlockChange(block.getLocation().toBlockPoint(), block.getBlock(), block.getBlock());
}
private final LocatedBlockList previous = new LocatedBlockList();
private final LocatedBlockList current = new LocatedBlockList();
@Override
public void add(Change change) {
@ -53,8 +55,8 @@ public class BlockOptimizedHistory extends ArrayListHistory {
if (change instanceof BlockChange) {
BlockChange blockChange = (BlockChange) change;
BlockVector position = blockChange.getPosition();
previous.put(position, blockChange.getPrevious());
current.put(position, blockChange.getCurrent());
previous.add(position, blockChange.getPrevious());
current.add(position, blockChange.getCurrent());
} else {
super.add(change);
}
@ -64,14 +66,14 @@ public class BlockOptimizedHistory extends ArrayListHistory {
public Iterator<Change> forwardIterator() {
return Iterators.concat(
super.forwardIterator(),
Iterators.transform(current.iterator(), createTransform()));
Iterators.transform(current.iterator(), BlockOptimizedHistory::createChange));
}
@Override
public Iterator<Change> backwardIterator() {
return Iterators.concat(
super.backwardIterator(),
Iterators.transform(previous.iterator(true), createTransform()));
Iterators.transform(previous.reverseIterator(), BlockOptimizedHistory::createChange));
}
@Override
@ -79,14 +81,4 @@ public class BlockOptimizedHistory extends ArrayListHistory {
return super.size() + previous.size();
}
/**
* Create a function that transforms each entry from the double array lists' iterator
* into an {@link Change}.
*
* @return a function
*/
private Function<Entry<BlockVector, BlockStateHolder>, Change> createTransform() {
return entry -> new BlockChange(entry.getKey(), entry.getValue(), entry.getValue());
}
}

Datei anzeigen

@ -0,0 +1,66 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.util;
import static com.google.common.base.Preconditions.checkNotNull;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.world.block.BlockStateHolder;
import java.util.Objects;
/**
* Represents a block located at some position.
*/
public final class LocatedBlock {
private final Vector location;
private final BlockStateHolder block;
public LocatedBlock(Vector location, BlockStateHolder block) {
this.location = checkNotNull(location);
this.block = checkNotNull(block);
}
public Vector getLocation() {
return location;
}
public BlockStateHolder getBlock() {
return 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);
}
}

Datei anzeigen

@ -1,114 +0,0 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.util.collection;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
/**
* A fast iterator for lists that uses an internal index integer
* and caches the size of the list. The size of the list cannot change
* during iteration and {@link Iterator#remove()} is not supported.
*
* <p>The iterator in Java, at least in older Java versions, is very slow,
* causing a significant amount of time in operations in WorldEdit
* being spent on {@link Iterator#hasNext()}. In contrast, the iterator
* implemented by this class is very quick, as long as
* {@link List#get(int)} is fast.</p>
*
* @param <E> the element
*/
public class FastListIterator<E> implements Iterator<E> {
private final List<E> list;
private int index;
private final int size;
private final int increment;
/**
* Create a new fast iterator.
*
* @param list the list
* @param index the index to start from
* @param size the size of the list
* @param increment the increment amount (i.e. 1 or -1)
*/
private FastListIterator(List<E> list, int index, int size, int increment) {
checkNotNull(list);
checkArgument(size >= 0, "size >= 0 required");
checkArgument(index >= 0, "index >= 0 required");
this.list = list;
this.index = index;
this.size = size;
this.increment = increment;
}
@Override
public boolean hasNext() {
return index >= 0 && index < size;
}
@Override
public E next() {
if (hasNext()) {
E entry = list.get(index);
index += increment;
return entry;
} else {
throw new NoSuchElementException();
}
}
@Override
public void remove() {
throw new UnsupportedOperationException("Not supported");
}
/**
* Create a new forward iterator for the given list.
*
* @param list the list
* @param <E> the element
* @return an iterator
*/
public static <E> Iterator<E> forwardIterator(List<E> list) {
return new FastListIterator<>(list, 0, list.size(), 1);
}
/**
* Create a new reverse iterator for the given list.
*
* @param list the list
* @param <E> the element
* @return an iterator
*/
public static <E> Iterator<E> reverseIterator(List<E> list) {
if (!list.isEmpty()) {
return new FastListIterator<>(list, list.size() - 1, list.size(), -1);
} else {
return new FastListIterator<>(list, 0, 0, -1);
}
}
}

Datei anzeigen

@ -0,0 +1,87 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.util.collection;
import static com.google.common.base.Preconditions.checkNotNull;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.util.LocatedBlock;
import com.sk89q.worldedit.world.block.BlockStateHolder;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
/**
* Wrapper around a list of blocks located in the world.
*/
public class LocatedBlockList implements Iterable<LocatedBlock> {
private final List<LocatedBlock> list;
public LocatedBlockList() {
list = new ArrayList<>();
}
public LocatedBlockList(Collection<? extends LocatedBlock> collection) {
list = new ArrayList<>(collection);
}
public void add(LocatedBlock setBlockCall) {
checkNotNull(setBlockCall);
list.add(setBlockCall);
}
public void add(Vector location, BlockStateHolder block) {
add(new LocatedBlock(location, block));
}
public int size() {
return list.size();
}
public void clear() {
list.clear();
}
@Override
public Iterator<LocatedBlock> iterator() {
return list.iterator();
}
public Iterator<LocatedBlock> reverseIterator() {
return new Iterator<LocatedBlock>() {
private final ListIterator<LocatedBlock> backingIterator = list.listIterator(list.size());
@Override
public boolean hasNext() {
return backingIterator.hasPrevious();
}
@Override
public LocatedBlock next() {
return backingIterator.previous();
}
};
}
}

Datei anzeigen

@ -1,94 +0,0 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.util.collection;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
/**
* An {@link ArrayList} that takes {@link Map.Entry}-like tuples. This class
* exists for legacy reasons.
*
* @param <A> the first type in the tuple
* @param <B> the second type in the tuple
*/
public class TupleArrayList<A, B> extends ArrayList<Map.Entry<A, B>> {
/**
* Add an item to the list.
*
* @param a the 'key'
* @param b the 'value'
*/
public void put(A a, B b) {
add(new Tuple<>(a, b));
}
/**
* Return an entry iterator that traverses in the reverse direction.
*
* @param reverse true to return the reverse iterator
* @return an entry iterator
*/
public Iterator<Map.Entry<A, B>> iterator(boolean reverse) {
return reverse ? reverseIterator() : iterator();
}
@Override
public Iterator<Map.Entry<A, B>> iterator() {
return FastListIterator.forwardIterator(this);
}
/**
* Return an entry iterator that traverses in the reverse direction.
*
* @return an entry iterator
*/
public Iterator<Map.Entry<A, B>> reverseIterator() {
return FastListIterator.reverseIterator(this);
}
private static class Tuple<A, B> implements Map.Entry<A, B> {
private A key;
private B value;
private Tuple(A key, B value) {
this.key = key;
this.value = value;
}
@Override
public A getKey() {
return key;
}
@Override
public B getValue() {
return value;
}
@Override
public B setValue(B value) {
throw new UnsupportedOperationException();
}
}
}