Various minor

thread local cleans on close
remove unnecessary loop on set
remove unnecessary get on set
clean CFI cache on generate
This commit is contained in:
Jesse Boyd 2019-11-03 16:59:11 +00:00
parent 3212f32b01
commit 930dfb7f7c
No known key found for this signature in database
GPG Key ID: 59F1DE6293AF6E1F
5 changed files with 208 additions and 133 deletions

View File

@ -36,9 +36,10 @@ import java.util.Arrays;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.locks.Lock;
import java.util.function.Function;
import java.util.function.Supplier;
public class BukkitAdapter_1_14 {
public final class BukkitAdapter_1_14 {
/*
NMS fields
@ -87,13 +88,13 @@ public class BukkitAdapter_1_14 {
} catch (NoSuchFieldException paper) {
tmp = DataPaletteBlock.class.getDeclaredField("j");
}
fieldLock = tmp;
fieldLock.setAccessible(true);
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
int modifiers = modifiersField.getInt(fieldLock);
int modifiers = modifiersField.getInt(tmp);
int newModifiers = modifiers & (~Modifier.FINAL);
if (newModifiers != modifiers) modifiersField.setInt(fieldLock, newModifiers);
if (newModifiers != modifiers) modifiersField.setInt(tmp, newModifiers);
fieldLock = tmp;
fieldLock.setAccessible(true);
}
Unsafe unsafe = UnsafeUtils.getUNSAFE();
@ -213,23 +214,20 @@ public class BukkitAdapter_1_14 {
/*
NMS conversion
*/
public static ChunkSection newChunkSection(final int layer, final char[] blocks) {
ChunkSection section = new ChunkSection(layer << 4);
if (blocks == null) {
return section;
return newChunkSection(layer, null, blocks);
}
final int[] blockToPalette = FaweCache.IMP.BLOCK_TO_PALETTE.get();
final int[] paletteToBlock = FaweCache.IMP.PALETTE_TO_BLOCK.get();
final long[] blockStates = FaweCache.IMP.BLOCK_STATES.get();
final int[] blocksCopy = FaweCache.IMP.SECTION_BLOCKS.get();
try {
int num_palette = 0;
private static int createPalette(int layer, int[] blockToPalette, int[] paletteToBlock, int[] blocksCopy, int[] num_palette_buffer, Function<Integer, char[]> get, char[] set) {
int air = 0;
for (int i = 0; i < 4096; i++) {
char ordinal = blocks[i];
int num_palette = 0;
int i = 0;
outer:
for (; i < 4096; i++) {
char ordinal = set[i];
switch (ordinal) {
case 0:
break outer;
case BlockID.AIR:
case BlockID.CAVE_AIR:
case BlockID.VOID_AIR:
@ -243,7 +241,75 @@ public class BukkitAdapter_1_14 {
}
blocksCopy[i] = palette;
}
if (i != 4096) {
char[] getArr = get.apply(layer);
for (; i < 4096; i++) {
char ordinal = set[i];
switch (ordinal) {
case 0:
ordinal = getArr[i];
set[i] = ordinal;
case BlockID.AIR:
case BlockID.CAVE_AIR:
case BlockID.VOID_AIR:
air++;
}
int palette = blockToPalette[ordinal];
if (palette == Integer.MAX_VALUE) {
blockToPalette[ordinal] = palette = num_palette;
paletteToBlock[num_palette] = ordinal;
num_palette++;
}
blocksCopy[i] = palette;
}
}
num_palette_buffer[0] = num_palette;
return air;
}
private static int createPalette(int[] blockToPalette, int[] paletteToBlock, int[] blocksCopy, int[] num_palette_buffer, char[] set) {
int air = 0;
int num_palette = 0;
char airOrdinal = BlockTypes.AIR.getDefaultState().getOrdinalChar();
for (int i = 0; i < 4096; i++) {
char ordinal = set[i];
switch (ordinal) {
case 0:
ordinal = airOrdinal;
case BlockID.AIR:
case BlockID.CAVE_AIR:
case BlockID.VOID_AIR:
air++;
}
int palette = blockToPalette[ordinal];
if (palette == Integer.MAX_VALUE) {
blockToPalette[ordinal] = palette = num_palette;
paletteToBlock[num_palette] = ordinal;
num_palette++;
}
blocksCopy[i] = palette;
}
num_palette_buffer[0] = num_palette;
return air;
}
public static ChunkSection newChunkSection(final int layer, final Function<Integer, char[]> get, char[] set) {
if (set == null) {
return newChunkSection(layer);
}
final int[] blockToPalette = FaweCache.IMP.BLOCK_TO_PALETTE.get();
final int[] paletteToBlock = FaweCache.IMP.PALETTE_TO_BLOCK.get();
final long[] blockStates = FaweCache.IMP.BLOCK_STATES.get();
final int[] blocksCopy = FaweCache.IMP.SECTION_BLOCKS.get();
try {
int[] num_palette_buffer = new int[1];
int air;
if (get == null) {
air = createPalette(blockToPalette, paletteToBlock, blocksCopy, num_palette_buffer, set);
} else {
air = createPalette(layer, blockToPalette, paletteToBlock, blocksCopy, num_palette_buffer, get, set);
}
int num_palette = num_palette_buffer[0];
// BlockStates
int bitsPerEntry = MathMan.log2nlz(num_palette - 1);
if (Settings.IMP.PROTOCOL_SUPPORT_FIX || num_palette != 1) {
@ -260,6 +326,7 @@ public class BukkitAdapter_1_14 {
bitArray.fromRaw(blocksCopy);
}
ChunkSection section = newChunkSection(layer);
// set palette & data bits
final DataPaletteBlock<IBlockData> dataPaletteBlocks = section.getBlocks();
// private DataPalette<T> h;
@ -294,6 +361,10 @@ public class BukkitAdapter_1_14 {
}
}
private static ChunkSection newChunkSection(int layer) {
return new ChunkSection(layer << 4);
}
public static void setCount(final int tickingBlockCount, final int nonEmptyBlockCount, final ChunkSection section) throws NoSuchFieldException, IllegalAccessException {
fieldFluidCount.setShort(section, (short) 0); // TODO FIXME
fieldTickingBlockCount.setShort(section, (short) tickingBlockCount);

View File

@ -300,19 +300,12 @@ public class BukkitGetBlocks_1_14 extends CharGetBlocks {
this.reset(layer);
}
}
char[] getArr = this.load(layer);
for (int i = 0; i < 4096; i++) {
char value = setArr[i];
if (value != 0) {
getArr[i] = value;
}
}
newSection = BukkitAdapter_1_14.newChunkSection(layer, getArr);
newSection = BukkitAdapter_1_14.newChunkSection(layer, this::load, setArr);
if (!BukkitAdapter_1_14.setSectionAtomic(sections, existingSection, newSection, layer)) {
System.out.println("Failed to set chunk section:" + X + "," + Z + " layer: " + layer);
continue;
} else {
updateGet(this, nmsChunk, sections, newSection, getArr, layer);
updateGet(this, nmsChunk, sections, newSection, setArr, layer);
}
}
}

View File

@ -12,6 +12,7 @@ import java.io.IOException;
import java.util.Arrays;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import java.util.zip.Deflater;
public abstract class MCAWriter implements Extent {
@ -74,6 +75,15 @@ public abstract class MCAWriter implements Extent {
public abstract MCAChunk write(MCAChunk input, int startX, int endX, int startZ, int endZ);
private static CleanableThreadLocal<MCAChunk> createCache() {
return new CleanableThreadLocal<>(() -> {
MCAChunk chunk = new MCAChunk();
Arrays.fill(chunk.blocks, (char) BlockID.AIR);
// Arrays.fill(chunk.skyLight, (byte) 255);
return chunk;
});
}
public void generate() throws IOException {
if (!folder.exists()) {
folder.mkdirs();
@ -81,14 +91,7 @@ public abstract class MCAWriter implements Extent {
final ForkJoinPool pool = new ForkJoinPool();
int tcx = (width - 1) >> 4;
int tcz = (length - 1) >> 4;
final ThreadLocal<MCAChunk> chunkStore = new ThreadLocal<MCAChunk>() {
@Override
protected MCAChunk initialValue() {
MCAChunk chunk = new MCAChunk();
Arrays.fill(chunk.blocks, (char) BlockID.AIR);
// Arrays.fill(chunk.skyLight, (byte) 255);
return chunk;
});
try (CleanableThreadLocal<MCAChunk> chunkStore = createCache()){
final ThreadLocal<byte[]> byteStore1 = ThreadLocal.withInitial(() -> new byte[500000]);
final ThreadLocal<byte[]> byteStore2 = ThreadLocal.withInitial(() -> new byte[500000]);
final ThreadLocal<Deflater> deflateStore = ThreadLocal
@ -132,7 +135,7 @@ public abstract class MCAWriter implements Extent {
chunk = write(chunk, csx, cex, csz, cez);
if (chunk != null) {
// Generation offset
chunk.setPosition( fcx + (getOffsetX() >> 4), fcz + (getOffsetZ() >> 4));
chunk.setPosition(fcx + (getOffsetX() >> 4), fcz + (getOffsetZ() >> 4));
// Compress
byte[] bytes = chunk.toBytes(byteStore1.get());
@ -199,4 +202,5 @@ public abstract class MCAWriter implements Extent {
CleanableThreadLocal.clean(byteStore2);
CleanableThreadLocal.clean(deflateStore);
}
}
}

View File

@ -2,6 +2,8 @@ package com.boydti.fawe.object.collection;
import com.boydti.fawe.util.MainUtil;
import java.io.Closeable;
import java.io.IOException;
import java.lang.ref.Reference;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
@ -14,7 +16,7 @@ import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
public class CleanableThreadLocal<T> extends ThreadLocal<T> {
public class CleanableThreadLocal<T> extends ThreadLocal<T> implements Closeable {
private final Supplier<T> supplier;
private final Function<T, T> modifier;
private LongAdder count = new LongAdder();
@ -167,4 +169,9 @@ public class CleanableThreadLocal<T> extends ThreadLocal<T> {
clean(this);
super.finalize();
}
@Override
public void close() throws IOException {
clean();
}
}

View File

@ -164,7 +164,7 @@ public class BlockType implements FawePattern, Keyed {
*
* @return The default state
*/
public BlockState getDefaultState() {
public final BlockState getDefaultState() {
return this.settings.defaultState;
}