mirror of
https://github.com/plexusorg/Plex-FAWE.git
synced 2025-06-11 20:13:55 +00:00
async chunk loading
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
package com.boydti.fawe;
|
||||
|
||||
import com.boydti.fawe.beta.Trimable;
|
||||
import com.boydti.fawe.config.Settings;
|
||||
import com.boydti.fawe.jnbt.anvil.BitArray4096;
|
||||
import com.boydti.fawe.object.collection.IterableThreadLocal;
|
||||
import com.boydti.fawe.util.MathMan;
|
||||
@ -11,6 +12,12 @@ import com.sk89q.worldedit.world.block.BlockTypes;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ArrayBlockingQueue;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class FaweCache implements Trimable {
|
||||
public static final char[] EMPTY_CHAR_4096 = new char[4096];
|
||||
@ -326,4 +333,16 @@ public class FaweCache implements Trimable {
|
||||
if (clazz == null) clazz = EndTag.class;
|
||||
return new ListTag(clazz, list);
|
||||
}
|
||||
|
||||
/*
|
||||
Thread stuff
|
||||
*/
|
||||
public static ThreadPoolExecutor newBlockingExecutor() {
|
||||
int nThreads = Settings.IMP.QUEUE.PARALLEL_THREADS;
|
||||
ArrayBlockingQueue<Runnable> queue = new ArrayBlockingQueue<>(nThreads);
|
||||
return new ThreadPoolExecutor(nThreads, nThreads,
|
||||
0L, TimeUnit.MILLISECONDS, queue
|
||||
, Executors.defaultThreadFactory(),
|
||||
new ThreadPoolExecutor.CallerRunsPolicy());
|
||||
}
|
||||
}
|
||||
|
@ -13,12 +13,12 @@ public class CharFilterBlock implements FilterBlock {
|
||||
private char[] section;
|
||||
|
||||
@Override
|
||||
public void init(IQueueExtent queue) {
|
||||
public final void init(IQueueExtent queue) {
|
||||
this.queue = queue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init(int X, int Z, IGetBlocks chunk) {
|
||||
public final void init(int X, int Z, IGetBlocks chunk) {
|
||||
this.chunk = (CharGetBlocks) chunk;
|
||||
this.X = X;
|
||||
this.Z = Z;
|
||||
@ -26,68 +26,85 @@ public class CharFilterBlock implements FilterBlock {
|
||||
this.zz = Z << 4;
|
||||
}
|
||||
|
||||
public void init(char[] section, int layer) {
|
||||
this.section = section;
|
||||
this.layer = layer;
|
||||
this.yy = layer << 4;
|
||||
// local
|
||||
private int layer, index, x, y, z, xx, yy, zz, X, Z;
|
||||
|
||||
public final void filter(CharGetBlocks blocks, Filter filter) {
|
||||
for (int layer = 0; layer < 16; layer++) {
|
||||
if (!blocks.hasSection(layer)) continue;
|
||||
char[] arr = blocks.sections[layer].get(blocks, layer);
|
||||
|
||||
this.section = arr;
|
||||
this.layer = layer;
|
||||
this.yy = layer << 4;
|
||||
|
||||
for (y = 0, index = 0; y < 16; y++) {
|
||||
for (z = 0; z < 16; z++) {
|
||||
for (x = 0; x < 16; x++, index++) {
|
||||
filter.applyBlock(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// local
|
||||
public int layer, index, x, y, z, xx, yy, zz, X, Z;
|
||||
|
||||
@Override
|
||||
public int getX() {
|
||||
public final int getX() {
|
||||
return xx + x;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getY() {
|
||||
public final int getY() {
|
||||
return yy + y;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getZ() {
|
||||
public final int getZ() {
|
||||
return zz + z;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getLocalX() {
|
||||
public final int getLocalX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getLocalY() {
|
||||
public final int getLocalY() {
|
||||
return y;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getLocalZ() {
|
||||
public final int getLocalZ() {
|
||||
return z;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getChunkX() {
|
||||
public final int getChunkX() {
|
||||
return X;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getChunkZ() {
|
||||
public final int getChunkZ() {
|
||||
return Z;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getOrdinal() {
|
||||
public final char getOrdinalChar() {
|
||||
return section[index];
|
||||
}
|
||||
|
||||
@Override
|
||||
public BlockState getState() {
|
||||
public final int getOrdinal() {
|
||||
return section[index];
|
||||
}
|
||||
|
||||
@Override
|
||||
public final BlockState getState() {
|
||||
int ordinal = section[index];
|
||||
return BlockTypes.states[ordinal];
|
||||
}
|
||||
|
||||
@Override
|
||||
public BaseBlock getBaseBlock() {
|
||||
public final BaseBlock getBaseBlock() {
|
||||
BlockState state = getState();
|
||||
BlockMaterial material = state.getMaterial();
|
||||
if (material.hasContainer()) {
|
||||
@ -98,11 +115,11 @@ public class CharFilterBlock implements FilterBlock {
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompoundTag getTag() {
|
||||
public final CompoundTag getTag() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public BlockState getOrdinalBelow() {
|
||||
public final BlockState getOrdinalBelow() {
|
||||
if (y > 0) {
|
||||
return states[section[index - 256]];
|
||||
}
|
||||
@ -114,7 +131,7 @@ public class CharFilterBlock implements FilterBlock {
|
||||
return BlockTypes.__RESERVED__.getDefaultState();
|
||||
}
|
||||
|
||||
public BlockState getStateAbove() {
|
||||
public final BlockState getStateAbove() {
|
||||
if (y < 16) {
|
||||
return states[section[index + 256]];
|
||||
}
|
||||
@ -126,7 +143,7 @@ public class CharFilterBlock implements FilterBlock {
|
||||
return BlockTypes.__RESERVED__.getDefaultState();
|
||||
}
|
||||
|
||||
public BlockState getStateRelativeY(int y) {
|
||||
public final BlockState getStateRelativeY(int y) {
|
||||
int newY = this.y + y;
|
||||
int layerAdd = newY >> 4;
|
||||
switch (layerAdd) {
|
||||
@ -180,7 +197,7 @@ public class CharFilterBlock implements FilterBlock {
|
||||
return BlockTypes.__RESERVED__.getDefaultState();
|
||||
}
|
||||
|
||||
public BlockState getStateRelative(final int x, final int y, final int z) {
|
||||
public final BlockState getStateRelative(final int x, final int y, final int z) {
|
||||
int newX = this.x + x;
|
||||
if (newX >> 4 == 0) {
|
||||
int newZ = this.z + z;
|
||||
@ -189,7 +206,7 @@ public class CharFilterBlock implements FilterBlock {
|
||||
int layerAdd = newY >> 4;
|
||||
switch (layerAdd) {
|
||||
case 0:
|
||||
return states[section[this.index + ((y << 8) | (z << 4) | x)]];
|
||||
return states[section[this.index + ((y << 8) + (z << 4) + x)]];
|
||||
case 1:
|
||||
case 2:
|
||||
case 3:
|
||||
@ -207,7 +224,7 @@ public class CharFilterBlock implements FilterBlock {
|
||||
case 15: {
|
||||
int newLayer = layer + layerAdd;
|
||||
if (newLayer < 16) {
|
||||
int index = this.index + (((y & 15) << 8) | (z << 4) | x);
|
||||
int index = ((newY & 15) << 8) + (newZ << 4) + newX;
|
||||
return states[chunk.sections[newLayer].get(chunk, newLayer, index)];
|
||||
}
|
||||
break;
|
||||
@ -229,7 +246,7 @@ public class CharFilterBlock implements FilterBlock {
|
||||
case -15: {
|
||||
int newLayer = layer + layerAdd;
|
||||
if (newLayer >= 0) {
|
||||
int index = this.index + (((y & 15) << 8) | (z << 4) | x);
|
||||
int index = ((newY & 15) << 8) + (newZ << 4) + newX;
|
||||
return states[chunk.sections[newLayer].get(chunk, newLayer, index)];
|
||||
}
|
||||
break;
|
||||
|
@ -5,11 +5,16 @@ import com.sk89q.worldedit.world.block.BaseBlock;
|
||||
import com.sk89q.worldedit.world.block.BlockState;
|
||||
import com.sk89q.worldedit.world.block.BlockStateHolder;
|
||||
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* Represents a chunk in the queue {@link IQueueExtent}
|
||||
* Used for getting and setting blocks / biomes / entities
|
||||
*/
|
||||
public interface IChunk extends Trimable {
|
||||
public interface IChunk<T extends Future<T>> extends Trimable, Callable<T> {
|
||||
/**
|
||||
* Initialize at the location
|
||||
* @param extent
|
||||
@ -22,6 +27,8 @@ public interface IChunk extends Trimable {
|
||||
|
||||
int getZ();
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* If the chunk is a delegate, returns it's paren'ts root
|
||||
* @return root IChunk
|
||||
@ -36,16 +43,33 @@ public interface IChunk extends Trimable {
|
||||
boolean isEmpty();
|
||||
|
||||
/**
|
||||
* Apply the queued async changes to the world
|
||||
* @return false if applySync needs to run
|
||||
* Spend time optimizing for apply<br>
|
||||
* default behavior: do nothing
|
||||
*/
|
||||
boolean applyAsync();
|
||||
default void optimize() {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply the queued sync changes to the world
|
||||
* @return true
|
||||
* Apply the queued changes to the world<br>
|
||||
* The future returned may return another future<br>
|
||||
* To ensure completion keep calling {@link Future#get()} on each result
|
||||
* @return Futures
|
||||
*/
|
||||
boolean applySync();
|
||||
T call();
|
||||
|
||||
/**
|
||||
* Call and join
|
||||
* @throws ExecutionException
|
||||
* @throws InterruptedException
|
||||
*/
|
||||
default void join() throws ExecutionException, InterruptedException {
|
||||
T future = call();
|
||||
while (future != null) {
|
||||
future = future.get();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
/* set - queues a change */
|
||||
boolean setBiome(int x, int y, int z, BiomeType biome);
|
||||
|
@ -5,6 +5,9 @@ import com.sk89q.worldedit.world.block.BaseBlock;
|
||||
import com.sk89q.worldedit.world.block.BlockState;
|
||||
import com.sk89q.worldedit.world.block.BlockStateHolder;
|
||||
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
/**
|
||||
* Delegate for IChunk
|
||||
* @param <U> parent class
|
||||
@ -67,13 +70,13 @@ public interface IDelegateChunk<U extends IChunk> extends IChunk {
|
||||
}
|
||||
|
||||
@Override
|
||||
default boolean applySync() {
|
||||
return getParent().applySync();
|
||||
default Future call() {
|
||||
return getParent().call();
|
||||
}
|
||||
|
||||
@Override
|
||||
default boolean applyAsync() {
|
||||
return getParent().applyAsync();
|
||||
default void join() throws ExecutionException, InterruptedException {
|
||||
getParent().join();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -21,4 +21,8 @@ public interface IGetBlocks extends IBlocks, Trimable {
|
||||
boolean trim(boolean aggressive);
|
||||
|
||||
void filter(Filter filter, FilterBlock block);
|
||||
|
||||
default void optimize() {
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -34,7 +34,7 @@ public interface IQueueExtent extends Flushable, Trimable {
|
||||
* @param chunk
|
||||
* @return result
|
||||
*/
|
||||
Future<?> submit(IChunk chunk);
|
||||
<T extends Future<T>> T submit(IChunk<T> chunk);
|
||||
|
||||
default boolean setBlock(final int x, final int y, final int z, final BlockStateHolder state) {
|
||||
final IChunk chunk = getCachedChunk(x >> 4, z >> 4);
|
||||
|
@ -12,4 +12,8 @@ public interface ISetBlocks extends IBlocks {
|
||||
boolean setBlock(int x, int y, int z, BlockStateHolder holder);
|
||||
|
||||
boolean isEmpty();
|
||||
|
||||
default void optimize() {
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
package com.boydti.fawe.beta.implementation;
|
||||
|
||||
import com.boydti.fawe.Fawe;
|
||||
import com.boydti.fawe.FaweCache;
|
||||
import com.boydti.fawe.beta.Filter;
|
||||
import com.boydti.fawe.beta.FilterBlock;
|
||||
import com.boydti.fawe.beta.IChunk;
|
||||
@ -8,9 +8,9 @@ import com.boydti.fawe.beta.IQueueExtent;
|
||||
import com.boydti.fawe.beta.Trimable;
|
||||
import com.boydti.fawe.config.Settings;
|
||||
import com.boydti.fawe.object.collection.IterableThreadLocal;
|
||||
import com.boydti.fawe.util.MathMan;
|
||||
import com.boydti.fawe.util.TaskManager;
|
||||
import com.boydti.fawe.util.MemUtil;
|
||||
import com.boydti.fawe.wrappers.WorldWrapper;
|
||||
import com.google.common.util.concurrent.Futures;
|
||||
import com.sk89q.worldedit.math.BlockVector2;
|
||||
import com.sk89q.worldedit.regions.Region;
|
||||
import com.sk89q.worldedit.world.World;
|
||||
@ -20,32 +20,36 @@ import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.ForkJoinPool;
|
||||
import java.util.concurrent.ForkJoinTask;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
|
||||
/**
|
||||
* Class which handles all the queues {@link IQueueExtent}
|
||||
*/
|
||||
public abstract class QueueHandler implements Trimable {
|
||||
private Map<World, WeakReference<WorldChunkCache>> chunkCache = new HashMap<>();
|
||||
private ForkJoinPool forkJoinPoolPrimary = new ForkJoinPool();
|
||||
private ForkJoinPool forkJoinPoolSecondary = new ForkJoinPool();
|
||||
private ThreadPoolExecutor blockingExecutor = FaweCache.newBlockingExecutor();
|
||||
private ConcurrentLinkedQueue<Runnable> syncTasks = new ConcurrentLinkedQueue();
|
||||
|
||||
private IterableThreadLocal<IQueueExtent> pool = new IterableThreadLocal<IQueueExtent>() {
|
||||
private Map<World, WeakReference<WorldChunkCache>> chunkCache = new HashMap<>();
|
||||
private IterableThreadLocal<IQueueExtent> queuePool = new IterableThreadLocal<IQueueExtent>() {
|
||||
@Override
|
||||
public IQueueExtent init() {
|
||||
return create();
|
||||
}
|
||||
};
|
||||
|
||||
public Future<?> submit(IChunk chunk) {
|
||||
if (Fawe.isMainThread()) {
|
||||
if (!chunk.applyAsync()) {
|
||||
chunk.applySync();
|
||||
}
|
||||
return null;
|
||||
public <T extends Future<T>> T submit(IChunk<T> chunk) {
|
||||
if (MemUtil.isMemoryFree()) {
|
||||
// return (T) forkJoinPoolSecondary.submit(chunk);
|
||||
}
|
||||
// TODO return future
|
||||
return null;
|
||||
return (T) blockingExecutor.submit(chunk);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@ -73,7 +77,7 @@ public abstract class QueueHandler implements Trimable {
|
||||
public abstract IQueueExtent create();
|
||||
|
||||
public IQueueExtent getQueue(World world) {
|
||||
IQueueExtent queue = pool.get();
|
||||
IQueueExtent queue = queuePool.get();
|
||||
queue.init(getOrCreate(world));
|
||||
return queue;
|
||||
}
|
||||
@ -103,62 +107,63 @@ public abstract class QueueHandler implements Trimable {
|
||||
final Iterator<BlockVector2> chunksIter = chunks.iterator();
|
||||
|
||||
// Get a pool, to operate on the chunks in parallel
|
||||
final ForkJoinPool pool = TaskManager.IMP.getPublicForkJoinPool();
|
||||
final int size = Math.min(chunks.size(), Settings.IMP.QUEUE.PARALLEL_THREADS);
|
||||
final ForkJoinTask[] tasks = new ForkJoinTask[size];
|
||||
|
||||
ForkJoinTask[] tasks = new ForkJoinTask[size];
|
||||
for (int i = 0; i < size; i++) {
|
||||
tasks[i] = pool.submit(new Runnable() {
|
||||
tasks[i] = forkJoinPoolPrimary.submit(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
Filter newFilter = filter.fork();
|
||||
// Create a chunk that we will reuse/reset for each operation
|
||||
IQueueExtent queue = getQueue(world);
|
||||
FilterBlock block = null;
|
||||
synchronized (queue) {
|
||||
FilterBlock block = null;
|
||||
|
||||
while (true) {
|
||||
// Get the next chunk pos
|
||||
final BlockVector2 pos;
|
||||
synchronized (chunksIter) {
|
||||
if (!chunksIter.hasNext()) return;
|
||||
pos = chunksIter.next();
|
||||
}
|
||||
final int X = pos.getX();
|
||||
final int Z = pos.getZ();
|
||||
// TODO create full
|
||||
IChunk chunk = queue.getCachedChunk(X, Z);
|
||||
// Initialize
|
||||
chunk.init(queue, X, Z);
|
||||
try {
|
||||
if (!newFilter.appliesChunk(X, Z)) {
|
||||
continue;
|
||||
while (true) {
|
||||
// Get the next chunk pos
|
||||
final BlockVector2 pos;
|
||||
synchronized (chunksIter) {
|
||||
if (!chunksIter.hasNext()) break;
|
||||
pos = chunksIter.next();
|
||||
}
|
||||
chunk = newFilter.applyChunk(chunk);
|
||||
final int X = pos.getX();
|
||||
final int Z = pos.getZ();
|
||||
IChunk chunk = queue.getCachedChunk(X, Z);
|
||||
// Initialize
|
||||
chunk.init(queue, X, Z);
|
||||
try {
|
||||
if (!newFilter.appliesChunk(X, Z)) {
|
||||
continue;
|
||||
}
|
||||
chunk = newFilter.applyChunk(chunk);
|
||||
|
||||
if (chunk == null) continue;
|
||||
if (chunk == null) continue;
|
||||
|
||||
if (block == null) block = queue.initFilterBlock();
|
||||
chunk.filter(newFilter, block);
|
||||
if (block == null) block = queue.initFilterBlock();
|
||||
chunk.filter(newFilter, block);
|
||||
|
||||
newFilter.finishChunk(chunk);
|
||||
newFilter.finishChunk(chunk);
|
||||
|
||||
queue.submit(chunk);
|
||||
} finally
|
||||
{
|
||||
if (filter != newFilter) {
|
||||
synchronized (filter) {
|
||||
newFilter.join(filter);
|
||||
queue.submit(chunk);
|
||||
} finally {
|
||||
if (filter != newFilter) {
|
||||
synchronized (filter) {
|
||||
newFilter.join(filter);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
queue.flush();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Join the tasks
|
||||
for (final ForkJoinTask task : tasks) {
|
||||
task.join();
|
||||
// Join filters
|
||||
for (int i = 0; i < tasks.length; i++) {
|
||||
ForkJoinTask task = tasks[i];
|
||||
if (task != null) {
|
||||
task.quietlyJoin();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -5,17 +5,12 @@ import com.boydti.fawe.beta.IChunk;
|
||||
import com.boydti.fawe.beta.IQueueExtent;
|
||||
import com.boydti.fawe.beta.implementation.holder.ReferenceChunk;
|
||||
import com.boydti.fawe.config.Settings;
|
||||
import com.boydti.fawe.util.MathMan;
|
||||
import com.boydti.fawe.util.MemUtil;
|
||||
import com.boydti.fawe.util.SetQueue;
|
||||
import com.boydti.fawe.util.TaskManager;
|
||||
import com.google.common.util.concurrent.Futures;
|
||||
import it.unimi.dsi.fastutil.longs.Long2ObjectLinkedOpenHashMap;
|
||||
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.ForkJoinPool;
|
||||
import java.util.concurrent.ForkJoinTask;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
@ -29,6 +24,7 @@ import static com.google.common.base.Preconditions.checkNotNull;
|
||||
public abstract class SingleThreadQueueExtent implements IQueueExtent {
|
||||
private WorldChunkCache cache;
|
||||
private Thread currentThread;
|
||||
private ConcurrentLinkedQueue<Future> submissions = new ConcurrentLinkedQueue<>();
|
||||
|
||||
/**
|
||||
* Safety check to ensure that the thread being used matches the one being initialized on
|
||||
@ -66,7 +62,7 @@ public abstract class SingleThreadQueueExtent implements IQueueExtent {
|
||||
*/
|
||||
@Override
|
||||
public synchronized void init(final WorldChunkCache cache) {
|
||||
if (cache != null) {
|
||||
if (this.cache != null) {
|
||||
reset();
|
||||
}
|
||||
currentThread = Thread.currentThread();
|
||||
@ -83,19 +79,17 @@ public abstract class SingleThreadQueueExtent implements IQueueExtent {
|
||||
private static final ConcurrentLinkedQueue<IChunk> CHUNK_POOL = new ConcurrentLinkedQueue<>();
|
||||
|
||||
@Override
|
||||
public Future<?> submit(final IChunk chunk) {
|
||||
public <T extends Future<T>> T submit(final IChunk<T> chunk) {
|
||||
if (chunk.isEmpty()) {
|
||||
CHUNK_POOL.add(chunk);
|
||||
return null;
|
||||
return (T) (Future) Futures.immediateFuture(null);
|
||||
}
|
||||
|
||||
if (Fawe.isMainThread()) {
|
||||
if (!chunk.applyAsync()) {
|
||||
chunk.applySync();
|
||||
}
|
||||
return null;
|
||||
return chunk.call();
|
||||
}
|
||||
QueueHandler handler = Fawe.get().getQueueHandler();
|
||||
return handler.submit(chunk);
|
||||
|
||||
return Fawe.get().getQueueHandler().submit(chunk);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -107,6 +101,13 @@ public abstract class SingleThreadQueueExtent implements IQueueExtent {
|
||||
lastPair = Long.MAX_VALUE;
|
||||
return chunks.isEmpty();
|
||||
}
|
||||
if (!submissions.isEmpty()) {
|
||||
if (aggressive) {
|
||||
pollSubmissions(0, aggressive);
|
||||
} else {
|
||||
pollSubmissions(Settings.IMP.QUEUE.PARALLEL_THREADS, aggressive);
|
||||
}
|
||||
}
|
||||
synchronized (this) {
|
||||
return currentThread == null;
|
||||
}
|
||||
@ -121,7 +122,10 @@ public abstract class SingleThreadQueueExtent implements IQueueExtent {
|
||||
*/
|
||||
private IChunk poolOrCreate(final int X, final int Z) {
|
||||
IChunk next = CHUNK_POOL.poll();
|
||||
if (next == null) next = create(false);
|
||||
if (next == null) {
|
||||
System.out.println("Create");
|
||||
next = create(false);
|
||||
}
|
||||
next.init(this, X, Z);
|
||||
return next;
|
||||
}
|
||||
@ -145,10 +149,19 @@ public abstract class SingleThreadQueueExtent implements IQueueExtent {
|
||||
|
||||
checkThread();
|
||||
final int size = chunks.size();
|
||||
if (size > Settings.IMP.QUEUE.TARGET_SIZE || MemUtil.isMemoryLimited()) {
|
||||
if (size > Settings.IMP.QUEUE.PARALLEL_THREADS * 2 + 16) {
|
||||
chunk = chunks.removeFirst();
|
||||
submit(chunk);
|
||||
boolean lowMem = MemUtil.isMemoryLimited();
|
||||
if (lowMem || size > Settings.IMP.QUEUE.TARGET_SIZE) {
|
||||
chunk = chunks.removeFirst();
|
||||
Future future = submit(chunk);
|
||||
if (future != null && !future.isDone()) {
|
||||
int targetSize;
|
||||
if (lowMem) {
|
||||
targetSize = Settings.IMP.QUEUE.PARALLEL_THREADS;
|
||||
} else {
|
||||
targetSize = Settings.IMP.QUEUE.TARGET_SIZE;
|
||||
}
|
||||
pollSubmissions(targetSize, true);
|
||||
submissions.add(future);
|
||||
}
|
||||
}
|
||||
chunk = poolOrCreate(X, Z);
|
||||
@ -161,27 +174,59 @@ public abstract class SingleThreadQueueExtent implements IQueueExtent {
|
||||
return chunk;
|
||||
}
|
||||
|
||||
private void pollSubmissions(int targetSize, boolean aggressive) {
|
||||
int overflow = submissions.size() - targetSize;
|
||||
if (aggressive) {
|
||||
for (int i = 0; i < overflow; i++) {
|
||||
Future first = submissions.poll();
|
||||
try {
|
||||
while ((first = (Future) first.get()) != null) ;
|
||||
} catch (InterruptedException | ExecutionException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (int i = 0; i < overflow; i++) {
|
||||
Future next = submissions.peek();
|
||||
while (next != null) {
|
||||
if (next.isDone()) {
|
||||
try {
|
||||
next = (Future) next.get();
|
||||
} catch (InterruptedException | ExecutionException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
submissions.poll();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void flush() {
|
||||
checkThread();
|
||||
if (!chunks.isEmpty()) {
|
||||
final Future[] tasks = new ForkJoinTask[chunks.size()];
|
||||
int i = 0;
|
||||
for (final IChunk chunk : chunks.values()) {
|
||||
tasks[i++] = submit(chunk);
|
||||
}
|
||||
chunks.clear();
|
||||
for (final Future task : tasks) {
|
||||
if (task != null) {
|
||||
try {
|
||||
task.get();
|
||||
} catch (InterruptedException | ExecutionException e) {
|
||||
e.printStackTrace();
|
||||
throw new RuntimeException(e);
|
||||
if (MemUtil.isMemoryLimited()) {
|
||||
for (IChunk chunk : chunks.values()) {
|
||||
Future future = submit(chunk);
|
||||
if (future != null && !future.isDone()) {
|
||||
pollSubmissions(Settings.IMP.QUEUE.PARALLEL_THREADS, true);
|
||||
submissions.add(future);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (final IChunk chunk : chunks.values()) {
|
||||
Future future = submit(chunk);
|
||||
if (future != null && !future.isDone()) {
|
||||
submissions.add(future);
|
||||
}
|
||||
}
|
||||
}
|
||||
chunks.clear();
|
||||
}
|
||||
pollSubmissions(0, true);
|
||||
reset();
|
||||
}
|
||||
}
|
@ -22,18 +22,7 @@ public abstract class CharGetBlocks extends CharBlocks implements IGetBlocks {
|
||||
@Override
|
||||
public void filter(Filter filter, FilterBlock block) {
|
||||
CharFilterBlock b = (CharFilterBlock) block;
|
||||
for (int layer = 0; layer < 16; layer++) {
|
||||
if (!hasSection(layer)) continue;
|
||||
char[] arr = sections[layer].get(this, layer);
|
||||
b.init(arr, layer);
|
||||
for (b.y = 0, b.index = 0; b.y < 16; b.y++) {
|
||||
for (b.z = 0; b.z < 16; b.z++) {
|
||||
for (b.x = 0; b.x < 16; b.x++, b.index++) {
|
||||
filter.applyBlock(b);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
b.filter(this, filter);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -1,10 +1,8 @@
|
||||
package com.boydti.fawe.beta.implementation.holder;
|
||||
|
||||
import com.boydti.fawe.beta.CharFilterBlock;
|
||||
import com.boydti.fawe.beta.Filter;
|
||||
import com.boydti.fawe.beta.FilterBlock;
|
||||
import com.boydti.fawe.beta.IQueueExtent;
|
||||
import com.boydti.fawe.beta.implementation.blocks.CharGetBlocks;
|
||||
import com.boydti.fawe.beta.implementation.blocks.CharSetBlocks;
|
||||
import com.boydti.fawe.beta.IChunk;
|
||||
import com.boydti.fawe.beta.IGetBlocks;
|
||||
@ -22,7 +20,7 @@ import java.util.function.Supplier;
|
||||
/**
|
||||
* Abstract IChunk class that implements basic get/set blocks
|
||||
*/
|
||||
public abstract class ChunkHolder<T> implements IChunk, Supplier<IGetBlocks> {
|
||||
public abstract class ChunkHolder implements IChunk, Supplier<IGetBlocks> {
|
||||
private IGetBlocks get;
|
||||
private ISetBlocks set;
|
||||
private IBlockDelegate delegate;
|
||||
@ -40,7 +38,7 @@ public abstract class ChunkHolder<T> implements IChunk, Supplier<IGetBlocks> {
|
||||
@Override
|
||||
public void filter(Filter filter, FilterBlock block) {
|
||||
block.init(X, Z, get);
|
||||
IGetBlocks get = cachedGet();
|
||||
IGetBlocks get = getOrCreateGet();
|
||||
get.filter(filter, block);
|
||||
}
|
||||
|
||||
@ -73,12 +71,12 @@ public abstract class ChunkHolder<T> implements IChunk, Supplier<IGetBlocks> {
|
||||
return set == null || set.isEmpty();
|
||||
}
|
||||
|
||||
public final IGetBlocks cachedGet() {
|
||||
public final IGetBlocks getOrCreateGet() {
|
||||
if (get == null) get = newGet();
|
||||
return get;
|
||||
}
|
||||
|
||||
public final ISetBlocks cachedSet() {
|
||||
public final ISetBlocks getOrCreateSet() {
|
||||
if (set == null) set = set();
|
||||
return set;
|
||||
}
|
||||
@ -95,6 +93,13 @@ public abstract class ChunkHolder<T> implements IChunk, Supplier<IGetBlocks> {
|
||||
return get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void optimize() {
|
||||
if (set != null) {
|
||||
set.optimize();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init(IQueueExtent extent, final int X, final int Z) {
|
||||
this.extent = extent;
|
||||
@ -163,35 +168,35 @@ public abstract class ChunkHolder<T> implements IChunk, Supplier<IGetBlocks> {
|
||||
public static final IBlockDelegate NULL = new IBlockDelegate() {
|
||||
@Override
|
||||
public boolean setBiome(final ChunkHolder chunk, final int x, final int y, final int z, final BiomeType biome) {
|
||||
chunk.cachedSet();
|
||||
chunk.getOrCreateSet();
|
||||
chunk.delegate = SET;
|
||||
return chunk.setBiome(x, y, z, biome);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean setBlock(final ChunkHolder chunk, final int x, final int y, final int z, final BlockStateHolder block) {
|
||||
chunk.cachedSet();
|
||||
chunk.getOrCreateSet();
|
||||
chunk.delegate = SET;
|
||||
return chunk.setBlock(x, y, z, block);
|
||||
}
|
||||
|
||||
@Override
|
||||
public BiomeType getBiome(final ChunkHolder chunk, final int x, final int z) {
|
||||
chunk.cachedGet();
|
||||
chunk.getOrCreateGet();
|
||||
chunk.delegate = GET;
|
||||
return chunk.getBiome(x, z);
|
||||
}
|
||||
|
||||
@Override
|
||||
public BlockState getBlock(final ChunkHolder chunk, final int x, final int y, final int z) {
|
||||
chunk.cachedGet();
|
||||
chunk.getOrCreateGet();
|
||||
chunk.delegate = GET;
|
||||
return chunk.getBlock(x, y, z);
|
||||
}
|
||||
|
||||
@Override
|
||||
public BaseBlock getFullBlock(final ChunkHolder chunk, final int x, final int y, final int z) {
|
||||
chunk.cachedGet();
|
||||
chunk.getOrCreateGet();
|
||||
chunk.delegate = GET;
|
||||
return chunk.getFullBlock(x, y, z);
|
||||
}
|
||||
@ -200,14 +205,14 @@ public abstract class ChunkHolder<T> implements IChunk, Supplier<IGetBlocks> {
|
||||
public static final IBlockDelegate GET = new IBlockDelegate() {
|
||||
@Override
|
||||
public boolean setBiome(final ChunkHolder chunk, final int x, final int y, final int z, final BiomeType biome) {
|
||||
chunk.cachedSet();
|
||||
chunk.getOrCreateSet();
|
||||
chunk.delegate = BOTH;
|
||||
return chunk.setBiome(x, y, z, biome);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean setBlock(final ChunkHolder chunk, final int x, final int y, final int z, final BlockStateHolder block) {
|
||||
chunk.cachedSet();
|
||||
chunk.getOrCreateSet();
|
||||
chunk.delegate = BOTH;
|
||||
return chunk.setBlock(x, y, z, block);
|
||||
}
|
||||
@ -241,21 +246,21 @@ public abstract class ChunkHolder<T> implements IChunk, Supplier<IGetBlocks> {
|
||||
|
||||
@Override
|
||||
public BiomeType getBiome(final ChunkHolder chunk, final int x, final int z) {
|
||||
chunk.cachedGet();
|
||||
chunk.getOrCreateGet();
|
||||
chunk.delegate = BOTH;
|
||||
return chunk.getBiome(x, z);
|
||||
}
|
||||
|
||||
@Override
|
||||
public BlockState getBlock(final ChunkHolder chunk, final int x, final int y, final int z) {
|
||||
chunk.cachedGet();
|
||||
chunk.getOrCreateGet();
|
||||
chunk.delegate = BOTH;
|
||||
return chunk.getBlock(x, y, z);
|
||||
}
|
||||
|
||||
@Override
|
||||
public BaseBlock getFullBlock(final ChunkHolder chunk, final int x, final int y, final int z) {
|
||||
chunk.cachedGet();
|
||||
chunk.getOrCreateGet();
|
||||
chunk.delegate = BOTH;
|
||||
return chunk.getFullBlock(x, y, z);
|
||||
}
|
||||
|
@ -281,7 +281,10 @@ public class RegionCommands extends MethodCommands {
|
||||
QueueHandler queueHandler = Fawe.get().getQueueHandler();
|
||||
World world = player.getWorld();
|
||||
CountFilter filter = new CountFilter();
|
||||
long start = System.currentTimeMillis();
|
||||
queueHandler.apply(world, region, filter);
|
||||
long diff = System.currentTimeMillis() - start;
|
||||
System.out.println(diff);
|
||||
}
|
||||
|
||||
@Command(
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -62,6 +62,10 @@ import java.util.stream.Stream;
|
||||
public final class BlockTypes {
|
||||
// Doesn't really matter what the hardcoded values are, as FAWE will update it on load
|
||||
@Nullable public static final BlockType __RESERVED__ = null;
|
||||
@Nullable public static final BlockType AIR = null;
|
||||
@Nullable public static final BlockType CAVE_AIR = null;
|
||||
@Nullable public static final BlockType VOID_AIR = null;
|
||||
|
||||
@Nullable public static final BlockType ACACIA_BUTTON = null;
|
||||
@Nullable public static final BlockType ACACIA_DOOR = null;
|
||||
@Nullable public static final BlockType ACACIA_FENCE = null;
|
||||
@ -76,7 +80,6 @@ public final class BlockTypes {
|
||||
@Nullable public static final BlockType ACACIA_TRAPDOOR = null;
|
||||
@Nullable public static final BlockType ACACIA_WOOD = null;
|
||||
@Nullable public static final BlockType ACTIVATOR_RAIL = null;
|
||||
@Nullable public static final BlockType AIR = null;
|
||||
@Nullable public static final BlockType ALLIUM = null;
|
||||
@Nullable public static final BlockType ANDESITE = null;
|
||||
@Nullable public static final BlockType ANVIL = null;
|
||||
@ -160,7 +163,6 @@ public final class BlockTypes {
|
||||
@Nullable public static final BlockType CARROTS = null;
|
||||
@Nullable public static final BlockType CARVED_PUMPKIN = null;
|
||||
@Nullable public static final BlockType CAULDRON = null;
|
||||
@Nullable public static final BlockType CAVE_AIR = null;
|
||||
@Nullable public static final BlockType CHAIN_COMMAND_BLOCK = null;
|
||||
@Nullable public static final BlockType CHEST = null;
|
||||
@Nullable public static final BlockType CHIPPED_ANVIL = null;
|
||||
@ -625,7 +627,6 @@ public final class BlockTypes {
|
||||
@Nullable public static final BlockType TUBE_CORAL_WALL_FAN = null;
|
||||
@Nullable public static final BlockType TURTLE_EGG = null;
|
||||
@Nullable public static final BlockType VINE = null;
|
||||
@Nullable public static final BlockType VOID_AIR = null;
|
||||
@Nullable public static final BlockType WALL_SIGN = null;
|
||||
@Nullable public static final BlockType WALL_TORCH = null;
|
||||
@Nullable public static final BlockType WATER = null;
|
||||
|
Reference in New Issue
Block a user