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
@@ -880,24 +880,8 @@ public class CommandStudio implements DecreeExecutor {
} }
String n3 = nn3; String n3 = nn3;
objects.computeIfAbsent(n1, (k1) -> new KMap<>())
objects.compute(n1, (k1, v1) -> .computeIfAbsent(n2, (k) -> new KList<>()).addIfMissing(n3);
{
//noinspection ReplaceNullCheck
if (v1 == null) {
return new KMap<>();
}
return v1;
}).compute(n2, (k, v) ->
{
if (v == null) {
return new KList<String>().qaddIfMissing(n3);
}
v.addIfMissing(n3);
return v;
});
} }
} }
} }
@@ -104,7 +104,7 @@ public class IrisData implements ExclusionStrategy, TypeAdapterFactory {
} }
public static IrisData get(File dataFolder) { public static IrisData get(File dataFolder) {
return dataLoaders.compute(dataFolder, (k, v) -> v == null ? new IrisData(dataFolder) : v); return dataLoaders.computeIfAbsent(dataFolder, IrisData::new);
} }
public static void dereference() { public static void dereference() {
@@ -84,7 +84,7 @@ public class ResourceLoader<T extends IrisRegistrant> {
o.put("fileMatch", new JSONArray(fm.toArray())); o.put("fileMatch", new JSONArray(fm.toArray()));
o.put("url", "./.iris/schema/" + getFolderName() + "-schema.json"); o.put("url", "./.iris/schema/" + getFolderName() + "-schema.json");
File a = new File(getManager().getDataFolder(), ".iris/schema/" + getFolderName() + "-schema.json"); File a = new File(getManager().getDataFolder(), ".iris/schema/" + getFolderName() + "-schema.json");
J.attemptAsync(() -> IO.writeAll(a, new SchemaBuilder(objectClass, manager).compute().toString(4))); J.attemptAsync(() -> IO.writeAll(a, new SchemaBuilder(objectClass, manager).construct().toString(4)));
return o; return o;
} }
@@ -356,7 +356,7 @@ public class IrisProject {
File a = new File(dm.getDataFolder(), ".iris/schema/snippet/" + snipType + "-schema.json"); File a = new File(dm.getDataFolder(), ".iris/schema/snippet/" + snipType + "-schema.json");
J.attemptAsync(() -> { J.attemptAsync(() -> {
try { try {
IO.writeAll(a, new SchemaBuilder(i, dm).compute().toString(4)); IO.writeAll(a, new SchemaBuilder(i, dm).construct().toString(4));
} catch (Throwable e) { } catch (Throwable e) {
e.printStackTrace(); e.printStackTrace();
} }
@@ -87,7 +87,7 @@ public class SchemaBuilder {
return a; return a;
} }
public JSONObject compute() { public JSONObject construct() {
JSONObject schema = new JSONObject(); JSONObject schema = new JSONObject();
schema.put("$schema", "http://json-schema.org/draft-07/schema#"); schema.put("$schema", "http://json-schema.org/draft-07/schema#");
schema.put("$id", "http://volmit.com/iris-schema/" + root.getSimpleName().toLowerCase() + ".json"); schema.put("$id", "http://volmit.com/iris-schema/" + root.getSimpleName().toLowerCase() + ".json");
@@ -167,11 +167,7 @@ public class IrisEngineMantle implements EngineMantle {
for (String i : objects) { for (String i : objects) {
e.queue(() -> { e.queue(() -> {
try { try {
BlockVector bv = sizeCache.compute(i, (k, v) -> { BlockVector bv = sizeCache.computeIfAbsent(i, (k) -> {
if (v != null) {
return v;
}
try { try {
return IrisObject.sampleSize(getData().getObjectLoader().findFile(i)); return IrisObject.sampleSize(getData().getObjectLoader().findFile(i));
} catch (IOException ex) { } catch (IOException ex) {
@@ -207,11 +203,7 @@ public class IrisEngineMantle implements EngineMantle {
for (String j : entry.getValue()) { for (String j : entry.getValue()) {
e.queue(() -> { e.queue(() -> {
try { try {
BlockVector bv = sizeCache.compute(j, (k, v) -> { BlockVector bv = sizeCache.computeIfAbsent(j, (k) -> {
if (v != null) {
return v;
}
try { try {
return IrisObject.sampleSize(getData().getObjectLoader().findFile(j)); return IrisObject.sampleSize(getData().getObjectLoader().findFile(j));
} catch (IOException ioException) { } catch (IOException ioException) {
@@ -329,12 +329,6 @@ public class PlannedStructure {
public IrisObject rotated(IrisJigsawPiece piece, IrisObjectRotation rotation) { public IrisObject rotated(IrisJigsawPiece piece, IrisObjectRotation rotation) {
String key = piece.getObject() + "-" + rotation.hashCode(); String key = piece.getObject() + "-" + rotation.hashCode();
return objectRotationCache.compute(key, (k, v) -> { return objectRotationCache.computeIfAbsent(key, (k) -> rotation.rotateCopy(data.getObjectLoader().load(piece.getObject())));
if (v == null) {
return rotation.rotateCopy(data.getObjectLoader().load(piece.getObject()));
}
return v;
});
} }
} }
@@ -84,7 +84,7 @@ public class IrisCarveModifier extends EngineAssignedModifier<BlockData> {
return; return;
} }
positions.compute(Cache.key(rx, rz), (k, v) -> Objects.requireNonNullElseGet(v, (Supplier<KList<Integer>>) KList::new).qadd(yy)); positions.computeIfAbsent(Cache.key(rx, rz), (k) -> new KList<>()).qadd(yy);
if (rz < 15 && mantle.get(xx, yy, zz + 1, MatterCavern.class) == null) { if (rz < 15 && mantle.get(xx, yy, zz + 1, MatterCavern.class) == null) {
walls.put(new IrisPosition(rx, yy, rz + 1), c); walls.put(new IrisPosition(rx, yy, rz + 1), c);
@@ -144,7 +144,7 @@ public class IrisBiomeCustom {
KMap<IrisBiomeCustomSpawnType, JSONArray> groups = new KMap<>(); KMap<IrisBiomeCustomSpawnType, JSONArray> groups = new KMap<>();
for (IrisBiomeCustomSpawn i : getSpawns()) { for (IrisBiomeCustomSpawn i : getSpawns()) {
JSONArray g = groups.compute(i.getGroup(), (k, v) -> v != null ? v : new JSONArray()); JSONArray g = groups.computeIfAbsent(i.getGroup(), (k) -> new JSONArray());
JSONObject o = new JSONObject(); JSONObject o = new JSONObject();
o.put("type", "minecraft:" + i.getType().name().toLowerCase()); o.put("type", "minecraft:" + i.getType().name().toLowerCase());
o.put("weight", i.getWeight()); o.put("weight", i.getWeight());
@@ -81,11 +81,7 @@ public class IrisObjectScale {
return origin; return origin;
} }
return cache.compute(origin, (k, v) -> { return cache.computeIfAbsent(origin, (k) -> {
if (v != null) {
return v;
}
KList<IrisObject> c = new KList<>(); KList<IrisObject> c = new KList<>();
for (double i = minimumScale; i < maximumScale; i += (maximumScale - minimumScale) / (double) (Math.min(variations, 32))) { for (double i = minimumScale; i < maximumScale; i += (maximumScale - minimumScale) / (double) (Math.min(variations, 32))) {
c.add(origin.scaled(i, getInterpolation())); c.add(origin.scaled(i, getInterpolation()));
@@ -892,7 +892,7 @@ public class IrisInterpolation {
int fj = j; int fj = j;
for (k = 0; k < d; k++) { for (k = 0; k < d; k++) {
int fk = k; int fk = k;
hunk.set(i, j, k, cache.compute((k * w * h) + (j * w) + i, (p, v) hunk.set(i, j, k, cache.computeIfAbsent((k * w * h) + (j * w) + i, (p)
-> getNoise3D(method, fi + xo, fj + yo, fk + zo, -> getNoise3D(method, fi + xo, fj + yo, fk + zo,
radX, radY, radZ, n))); radX, radY, radZ, n)));
} }
@@ -103,8 +103,8 @@ public class EntityMatter extends RawMatter<MatterEntityGroup> {
entityCache = new KMap<>(); entityCache = new KMap<>();
for (Entity i : ((World) w).getNearbyEntities(new BoundingBox(x, y, z, x + getWidth(), y + getHeight(), z + getHeight()))) { for (Entity i : ((World) w).getNearbyEntities(new BoundingBox(x, y, z, x + getWidth(), y + getHeight(), z + getHeight()))) {
entityCache.compute(new IrisPosition(i.getLocation()), entityCache.computeIfAbsent(new IrisPosition(i.getLocation()),
(k, v) -> v == null ? new KList<>() : v).add(i); k -> new KList<>()).add(i);
} }
for (IrisPosition i : entityCache.keySet()) { for (IrisPosition i : entityCache.keySet()) {
@@ -32,8 +32,8 @@ import java.util.function.Supplier;
public class HyperLock { public class HyperLock {
private final ConcurrentLinkedHashMap<Long, ReentrantLock> locks; private final ConcurrentLinkedHashMap<Long, ReentrantLock> locks;
private final BiFunction<? super Long, ? super ReentrantLock, ? extends ReentrantLock> accessor;
private boolean enabled = true; private boolean enabled = true;
private boolean fair = false;
public HyperLock() { public HyperLock() {
this(1024, false); this(1024, false);
@@ -44,6 +44,7 @@ public class HyperLock {
} }
public HyperLock(int capacity, boolean fair) { public HyperLock(int capacity, boolean fair) {
this.fair = fair;
locks = new ConcurrentLinkedHashMap.Builder<Long, ReentrantLock>() locks = new ConcurrentLinkedHashMap.Builder<Long, ReentrantLock>()
.initialCapacity(capacity) .initialCapacity(capacity)
.maximumWeightedCapacity(capacity) .maximumWeightedCapacity(capacity)
@@ -54,7 +55,6 @@ public class HyperLock {
}) })
.concurrencyLevel(32) .concurrencyLevel(32)
.build(); .build();
accessor = (k, v) -> v == null ? new ReentrantLock(fair) : v;
} }
public void with(int x, int z, Runnable r) { public void with(int x, int z, Runnable r) {
@@ -123,7 +123,7 @@ public class HyperLock {
} }
private ReentrantLock getLock(int x, int z) { 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) { public void lock(int x, int z) {
@@ -499,11 +499,7 @@ public class VolmitSender implements CommandSender {
public void sendDecreeHelpNode(VirtualDecreeCommand i) { public void sendDecreeHelpNode(VirtualDecreeCommand i) {
if (isPlayer() || s instanceof CommandDummy) { if (isPlayer() || s instanceof CommandDummy) {
sendMessageRaw(helpCache.compute(i.getPath(), (k, v) -> { sendMessageRaw(helpCache.computeIfAbsent(i.getPath(), (k) -> {
if (v != null) {
return v;
}
String newline = "<reset>\n"; String newline = "<reset>\n";
/// Command /// Command
@@ -85,7 +85,6 @@ public class GroupedExecutor {
public void queue(String q, NastyRunnable r) { public void queue(String q, NastyRunnable r) {
mirror.compute(q, (k, v) -> k == null || v == null ? 1 : v + 1); mirror.compute(q, (k, v) -> k == null || v == null ? 1 : v + 1);
service.execute(() -> service.execute(() ->
{ {
try { try {
@@ -95,7 +94,7 @@ public class GroupedExecutor {
e.printStackTrace(); e.printStackTrace();
} }
mirror.compute(q, (k, v) -> v - 1); mirror.computeIfPresent(q, (k, v) -> v - 1);
}); });
} }
@@ -58,11 +58,11 @@ public class CachedConversionStream<T, V> extends BasicLayer implements Procedur
@Override @Override
public V get(double x, double z) { public V get(double x, double z) {
return cache.compute(stream.get(x, z), (k, v) -> v != null ? v : converter.apply(k)); return cache.computeIfAbsent(stream.get(x, z), converter);
} }
@Override @Override
public V get(double x, double y, double z) { public V get(double x, double y, double z) {
return cache.compute(stream.get(x, y, z), (k, v) -> v != null ? v : converter.apply(k)); return cache.computeIfAbsent(stream.get(x, y, z), converter);
} }
} }
@@ -49,11 +49,11 @@ public class CachedStream3D<T> extends BasicStream<T> implements ProceduralStrea
@Override @Override
public T get(double x, double z) { public T get(double x, double z) {
return cache.compute(new BlockPosition((int) x, -1, (int) z), (k, v) -> v != null ? v : stream.get((int) x, (int) z)); return cache.computeIfAbsent(new BlockPosition((int) x, -1, (int) z), (k) -> stream.get((int) x, (int) z));
} }
@Override @Override
public T get(double x, double y, double z) { public T get(double x, double y, double z) {
return cache.compute(new BlockPosition((int) x, (int) y, (int) z), (k, v) -> v != null ? v : stream.get((int) x, (int) y, (int) z)); return cache.computeIfAbsent(new BlockPosition((int) x, (int) y, (int) z), (k) -> stream.get((int) x, (int) y, (int) z));
} }
} }