Only compute if absent/present if compute is not needed (locking opts)

This commit is contained in:
cyberpwn
2021-09-13 09:31:56 -04:00
parent ca961e8498
commit eeab12ed86
18 changed files with 25 additions and 64 deletions

View File

@@ -32,8 +32,8 @@ import java.util.function.Supplier;
public class HyperLock {
private final ConcurrentLinkedHashMap<Long, ReentrantLock> locks;
private final BiFunction<? super Long, ? super ReentrantLock, ? extends ReentrantLock> accessor;
private boolean enabled = true;
private boolean fair = false;
public HyperLock() {
this(1024, false);
@@ -44,6 +44,7 @@ public class HyperLock {
}
public HyperLock(int capacity, boolean fair) {
this.fair = fair;
locks = new ConcurrentLinkedHashMap.Builder<Long, ReentrantLock>()
.initialCapacity(capacity)
.maximumWeightedCapacity(capacity)
@@ -54,7 +55,6 @@ public class HyperLock {
})
.concurrencyLevel(32)
.build();
accessor = (k, v) -> v == null ? new ReentrantLock(fair) : v;
}
public void with(int x, int z, Runnable r) {
@@ -123,7 +123,7 @@ public class HyperLock {
}
private ReentrantLock getLock(int x, int z) {
return locks.compute(Cache.key(x, z), accessor);
return locks.computeIfAbsent(Cache.key(x, z), k -> new ReentrantLock(fair));
}
public void lock(int x, int z) {