thread local cleans on close
remove unnecessary loop on set
remove unnecessary get on set
clean CFI cache on generate
Dieser Commit ist enthalten in:
Jesse Boyd 2019-11-03 16:59:11 +00:00
Ursprung 3212f32b01
Commit 930dfb7f7c
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: 59F1DE6293AF6E1F
5 geänderte Dateien mit 208 neuen und 133 gelöschten Zeilen

Datei anzeigen

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

Datei anzeigen

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

Datei anzeigen

@ -12,6 +12,7 @@ import java.io.IOException;
import java.util.Arrays; import java.util.Arrays;
import java.util.concurrent.ForkJoinPool; import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import java.util.zip.Deflater; import java.util.zip.Deflater;
public abstract class MCAWriter implements Extent { 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); 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 { public void generate() throws IOException {
if (!folder.exists()) { if (!folder.exists()) {
folder.mkdirs(); folder.mkdirs();
@ -81,14 +91,7 @@ public abstract class MCAWriter implements Extent {
final ForkJoinPool pool = new ForkJoinPool(); final ForkJoinPool pool = new ForkJoinPool();
int tcx = (width - 1) >> 4; int tcx = (width - 1) >> 4;
int tcz = (length - 1) >> 4; int tcz = (length - 1) >> 4;
final ThreadLocal<MCAChunk> chunkStore = new ThreadLocal<MCAChunk>() { try (CleanableThreadLocal<MCAChunk> chunkStore = createCache()){
@Override
protected MCAChunk initialValue() {
MCAChunk chunk = new MCAChunk();
Arrays.fill(chunk.blocks, (char) BlockID.AIR);
// Arrays.fill(chunk.skyLight, (byte) 255);
return chunk;
});
final ThreadLocal<byte[]> byteStore1 = ThreadLocal.withInitial(() -> new byte[500000]); final ThreadLocal<byte[]> byteStore1 = ThreadLocal.withInitial(() -> new byte[500000]);
final ThreadLocal<byte[]> byteStore2 = ThreadLocal.withInitial(() -> new byte[500000]); final ThreadLocal<byte[]> byteStore2 = ThreadLocal.withInitial(() -> new byte[500000]);
final ThreadLocal<Deflater> deflateStore = ThreadLocal final ThreadLocal<Deflater> deflateStore = ThreadLocal
@ -200,3 +203,4 @@ public abstract class MCAWriter implements Extent {
CleanableThreadLocal.clean(deflateStore); CleanableThreadLocal.clean(deflateStore);
} }
} }
}

Datei anzeigen

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

Datei anzeigen

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