Revert "Don't use the region chunk has table until a threshold."

This reverts commit f57afb2944.
This commit is contained in:
sk89q 2014-08-21 22:49:53 -07:00
parent 2d147bb7e1
commit e649973318

View File

@ -51,14 +51,8 @@
*/
public class ChunkHashTable implements ConcurrentRegionIndex {
/**
* The number of regions required before this hash table kicks in.
*/
private static final int CACHE_THRESHOLD = 5;
private ListeningExecutorService executor = createExecutor();
@Nullable
private LongHashTable<ChunkState> states;
private LongHashTable<ChunkState> states = new LongHashTable<ChunkState>();
private final RegionIndex index;
private final Object lock = new Object();
@Nullable
@ -85,23 +79,35 @@ private ListeningExecutorService createExecutor() {
}
/**
* Get a state object at the given position, returning an entry only
* if one exists.
* Get a state object at the given position.
*
* @param position the position
* @return a chunk state object, or {@code null}
* @param create true to create an entry if one does not exist
* @return a chunk state object, or {@code null} (only if {@code create} is false)
*/
@Nullable
private ChunkState get(Vector2D position) {
LongHashTable<ChunkState> states = this.states;
if (states != null) {
synchronized (lock) {
return states.get(position.getBlockX(), position.getBlockZ());
private ChunkState get(Vector2D position, boolean create) {
ChunkState state;
synchronized (lock) {
state = states.get(position.getBlockX(), position.getBlockZ());
if (state == null && create) {
state = new ChunkState(position);
states.put(position.getBlockX(), position.getBlockZ(), state);
executor.submit(new EnumerateRegions(position));
}
} else {
return null;
}
return state;
}
/**
* Get a state at the given position or create a new entry if one does
* not exist.
*
* @param position the position
* @return a state
*/
private ChunkState getOrCreate(Vector2D position) {
return get(position, true);
}
/**
@ -112,30 +118,22 @@ private void rebuild() {
ListeningExecutorService previousExecutor = executor;
LongHashTable<ChunkState> previousStates = states;
if (index.size() > CACHE_THRESHOLD) {
previousExecutor.shutdownNow();
previousExecutor.shutdownNow();
states = new LongHashTable<ChunkState>();
executor = createExecutor();
states = new LongHashTable<ChunkState>();
executor = createExecutor();
if (previousStates != null) {
List<Vector2D> positions = new ArrayList<Vector2D>();
for (ChunkState state : previousStates.values()) {
Vector2D position = state.getPosition();
positions.add(position);
states.put(position.getBlockX(), position.getBlockZ(), new ChunkState(position));
}
if (!positions.isEmpty()) {
executor.submit(new EnumerateRegions(positions));
}
}
lastState = null;
} else {
states = null;
List<Vector2D> positions = new ArrayList<Vector2D>();
for (ChunkState state : previousStates.values()) {
Vector2D position = state.getPosition();
positions.add(position);
states.put(position.getBlockX(), position.getBlockZ(), new ChunkState(position));
}
if (!positions.isEmpty()) {
executor.submit(new EnumerateRegions(positions));
}
lastState = null;
}
}
@ -161,19 +159,7 @@ public boolean awaitCompletion(long timeout, TimeUnit unit) throws InterruptedEx
@Override
public void bias(Vector2D chunkPosition) {
checkNotNull(chunkPosition);
if (states != null) {
synchronized (lock) {
if (states != null) {
ChunkState state = states.get(chunkPosition.getBlockX(), chunkPosition.getBlockZ());
if (state == null) {
state = new ChunkState(chunkPosition);
states.put(chunkPosition.getBlockX(), chunkPosition.getBlockZ(), state);
executor.submit(new EnumerateRegions(chunkPosition));
}
}
}
}
getOrCreate(chunkPosition);
}
@Override
@ -189,12 +175,10 @@ public void biasAll(Collection<Vector2D> chunkPositions) {
public void forget(Vector2D chunkPosition) {
checkNotNull(chunkPosition);
synchronized (lock) {
if (states != null) {
states.remove(chunkPosition.getBlockX(), chunkPosition.getBlockZ());
ChunkState state = lastState;
if (state != null && state.getPosition().getBlockX() == chunkPosition.getBlockX() && state.getPosition().getBlockZ() == chunkPosition.getBlockZ()) {
lastState = null;
}
states.remove(chunkPosition.getBlockX(), chunkPosition.getBlockZ());
ChunkState state = lastState;
if (state != null && state.getPosition().getBlockX() == chunkPosition.getBlockX() && state.getPosition().getBlockZ() == chunkPosition.getBlockZ()) {
lastState = null;
}
}
}
@ -254,7 +238,7 @@ public void applyContaining(Vector position, Predicate<ProtectedRegion> consumer
int chunkZ = position.getBlockZ() >> 4;
if (state == null || state.getPosition().getBlockX() != chunkX || state.getPosition().getBlockZ() != chunkZ) {
state = get(new Vector2D(chunkX, chunkZ));
state = get(new Vector2D(chunkX, chunkZ), false);
}
if (state != null && state.isLoaded()) {
@ -317,7 +301,7 @@ private EnumerateRegions(List<Vector2D> positions) {
@Override
public void run() {
for (Vector2D position : positions) {
ChunkState state = get(position);
ChunkState state = get(position, false);
if (state != null) {
List<ProtectedRegion> regions = new ArrayList<ProtectedRegion>();