Return to static "sections" in CharBlocks.

- Synchronise (lock) outside the static object
 - Reduces object creation
This commit is contained in:
dordsor21 2021-01-05 13:51:26 +00:00
parent aece0229c4
commit 7c174ee6b3
No known key found for this signature in database
GPG Key ID: 1E53E88969FFCF0B

View File

@ -9,13 +9,13 @@ import org.jetbrains.annotations.Range;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.util.Arrays; import java.util.concurrent.locks.ReentrantLock;
public abstract class CharBlocks implements IBlocks { public abstract class CharBlocks implements IBlocks {
public static final Logger logger = LoggerFactory.getLogger(CharBlocks.class); public static final Logger logger = LoggerFactory.getLogger(CharBlocks.class);
protected final Section FULL = new Section() { protected static final Section FULL = new Section() {
@Override @Override
public final char[] get(CharBlocks blocks, int layer) { public final char[] get(CharBlocks blocks, int layer) {
return blocks.blocks[layer]; return blocks.blocks[layer];
@ -26,9 +26,11 @@ public abstract class CharBlocks implements IBlocks {
return true; return true;
} }
}; };
protected final Section EMPTY = new Section() { protected static final Section EMPTY = new Section() {
@Override @Override
public final synchronized char[] get(CharBlocks blocks, int layer) { public final char[] get(CharBlocks blocks, int layer) {
blocks.loadLock.lock();
try {
char[] arr = blocks.blocks[layer]; char[] arr = blocks.blocks[layer];
if (arr == null) { if (arr == null) {
arr = blocks.blocks[layer] = blocks.update(layer, null); arr = blocks.blocks[layer] = blocks.update(layer, null);
@ -45,6 +47,9 @@ public abstract class CharBlocks implements IBlocks {
blocks.sections[layer] = FULL; blocks.sections[layer] = FULL;
} }
return arr; return arr;
} finally {
blocks.loadLock.unlock();
}
} }
@Override @Override
@ -54,7 +59,7 @@ public abstract class CharBlocks implements IBlocks {
}; };
public final char[][] blocks; public final char[][] blocks;
public final Section[] sections; public final Section[] sections;
private final Object loadLock = new Object(); private final ReentrantLock loadLock = new ReentrantLock ();
public CharBlocks() { public CharBlocks() {
blocks = new char[16][]; blocks = new char[16][];
@ -117,14 +122,8 @@ public abstract class CharBlocks implements IBlocks {
@Override @Override
public char[] load(@Range(from = 0, to = 15) int layer) { public char[] load(@Range(from = 0, to = 15) int layer) {
Section section = sections[layer];
if (section.isFull()) {
return section.get(this, layer);
}
synchronized (loadLock) {
return sections[layer].get(this, layer); return sections[layer].get(this, layer);
} }
}
@Override @Override
public BlockState getBlock(int x, int y, int z) { public BlockState getBlock(int x, int y, int z) {