Keep the object scale cache in world state and the structure graph catalog per resource snapshot

IrisObjectScale takes the engine and keeps its scaled variants in the world's state store; the structure graph
catalog is keyed by the pack's resource snapshot. Both are rebuilt when a resource reload starts a new world or
snapshot, so IrisData no longer invalidates them by hand.
This commit is contained in:
piexel committed 2026-10-09 20:45:00 +02:00
1 parent c1f23830a2
commit 63bd3e3de8
4 files changed
+40 -79

No files matched your search

@@ -1,6 +1,7 @@
package art.arcane.iris.engine.framework.structure;
import art.arcane.iris.core.loader.IrisData;
import art.arcane.iris.core.modules.IrisPackResources;
import art.arcane.iris.engine.framework.StructureAssembler;
import art.arcane.iris.engine.object.value.IrisPosition;
import art.arcane.iris.engine.object.structure.IrisStructure;
@@ -14,10 +15,14 @@ import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
/**
* Compiled structure graphs, kept per resource snapshot of the pack: every reload of the pack's resources starts a new
* snapshot ({@link IrisData#getPackResources()}), so nothing stale is ever read and nothing is invalidated by hand.
*/
public final class StructureGraphCatalog {
private static final Cache<IrisData, ConcurrentMap<String, StructureGraphCompilation>> CACHE =
private static final Cache<IrisPackResources, ConcurrentMap<String, StructureGraphCompilation>> CACHE =
Caffeine.newBuilder().weakKeys().build();
private static final Cache<IrisData, ConcurrentMap<String, Boolean>> RUNTIME_OUTPUT =
private static final Cache<IrisPackResources, ConcurrentMap<String, Boolean>> RUNTIME_OUTPUT =
Caffeine.newBuilder().weakKeys().build();
private static final List<Long> RUNTIME_SAMPLE_SEEDS = List.of(
0L, 1L, 2L, 3L, 5L, 8L, 13L, 21L, 34L, 55L, 89L, 144L, 233L, 377L, 610L, 987L);
@@ -33,17 +38,10 @@ public final class StructureGraphCatalog {
return compileAndReport(activeData, activeStructure);
}
ConcurrentMap<String, StructureGraphCompilation> catalog = CACHE.get(
activeData, ignored -> new ConcurrentHashMap<>());
activeData.getPackResources(), ignored -> new ConcurrentHashMap<>());
return catalog.computeIfAbsent(structureKey, ignored -> compileAndReport(activeData, activeStructure));
}
public static void invalidate(IrisData data) {
if (data != null) {
CACHE.invalidate(data);
RUNTIME_OUTPUT.invalidate(data);
}
}
public static boolean guaranteesRuntimeOutput(IrisData data, IrisStructure structure) {
IrisData activeData = Objects.requireNonNull(data);
IrisStructure activeStructure = Objects.requireNonNull(structure);
@@ -55,7 +53,7 @@ public final class StructureGraphCatalog {
return sampleRuntimeAssembly(activeData, activeStructure);
}
ConcurrentMap<String, Boolean> catalog = RUNTIME_OUTPUT.get(
activeData, ignored -> new ConcurrentHashMap<>());
activeData.getPackResources(), ignored -> new ConcurrentHashMap<>());
return catalog.computeIfAbsent(
structureKey, ignored -> sampleRuntimeAssembly(activeData, activeStructure));
}
@@ -184,12 +184,12 @@ public class FloatingObjectPlacing {
if (raw == null) {
continue;
}
IrisObject obj0 = placement.getScale().get(rng, raw);
IrisObject obj0 = placement.getScale().get(rng, raw, engine);
if (obj0 == null) {
continue;
}
if (entry != null && entry.hasObjectShrink()) {
obj0 = entry.getShrinkScale().get(rng, obj0);
obj0 = entry.getShrinkScale().get(rng, obj0, engine);
if (obj0 == null) {
continue;
}
@@ -232,12 +232,12 @@ public class FloatingObjectPlacing {
if (raw == null) {
continue;
}
IrisObject obj0 = placement.getScale().get(rng, raw);
IrisObject obj0 = placement.getScale().get(rng, raw, engine);
if (obj0 == null) {
continue;
}
if (entry != null && entry.hasObjectShrink()) {
obj0 = entry.getShrinkScale().get(rng, obj0);
obj0 = entry.getShrinkScale().get(rng, obj0, engine);
if (obj0 == null) {
continue;
}
@@ -317,12 +317,12 @@ public class FloatingObjectPlacing {
if (raw == null) {
continue;
}
IrisObject obj0 = placement.getScale().get(rng, raw);
IrisObject obj0 = placement.getScale().get(rng, raw, engine);
if (obj0 == null) {
continue;
}
if (entry != null && entry.hasObjectShrink()) {
obj0 = entry.getShrinkScale().get(rng, obj0);
obj0 = entry.getShrinkScale().get(rng, obj0, engine);
if (obj0 == null) {
continue;
}
@@ -663,7 +663,7 @@ public class ObjectPlacing {
for (int i = 0; i < density; i++) {
attempts++;
IrisObject v = objectPlacement.getScale().get(rng, objectPlacement.getObject(complex, rng));
IrisObject v = objectPlacement.getScale().get(rng, objectPlacement.getObject(complex, rng), engine);
if (v == null) {
nullObjects++;
if (traceRegen) {
@@ -801,7 +801,7 @@ public class ObjectPlacing {
for (int i = 0; i < density; i++) {
attempts++;
IrisObject object = objectPlacement.getScale().get(rng, objectPlacement.getObject(complex, rng));
IrisObject object = objectPlacement.getScale().get(rng, objectPlacement.getObject(complex, rng), engine);
if (object == null) {
nullObjects++;
if (traceRegen) {
@@ -1034,7 +1034,7 @@ public class ObjectPlacing {
int density = objectPlacement.getDensity(rng, minX, minZ, engine.getData());
for (int i = 0; i < density; i++) {
IrisObject v = objectPlacement.getScale().get(rng, objectPlacement.getObject(complex, rng));
IrisObject v = objectPlacement.getScale().get(rng, objectPlacement.getObject(complex, rng), engine);
if (v == null) {
continue;
}
@@ -1419,7 +1419,7 @@ public class ObjectPlacing {
private Set<String> guessPlacedKeys(RNG rng, int x, int z, IrisObjectPlacement objectPlacement) {
Set<String> f = new KSet<>();
for (int i = 0; i < objectPlacement.getDensity(rng, x, z, engine.getData()); i++) {
IrisObject v = objectPlacement.getScale().get(rng, objectPlacement.getObject(engine.getComplex(), rng));
IrisObject v = objectPlacement.getScale().get(rng, objectPlacement.getObject(engine.getComplex(), rng), engine);
if (v == null) {
continue;
}
@@ -18,7 +18,7 @@
package art.arcane.iris.engine.object.prefab;
import art.arcane.iris.core.loader.IrisData;
import art.arcane.iris.engine.framework.Engine;
import art.arcane.iris.engine.object.annotations.Desc;
import art.arcane.iris.engine.object.annotations.MaxNumber;
import art.arcane.iris.engine.object.annotations.MinNumber;
@@ -49,11 +49,6 @@ public class IrisObjectScale {
private static final long CACHE_VARIANT_ESTIMATED_BYTES = 256L;
private static final long CACHE_ESTIMATED_BYTES_PER_VOXEL = 128L;
private static final int CACHE_LOAD_LOCK_COUNT = 256;
private static final ScaleCache CACHE = new ScaleCache(resolveCacheMaximumEstimatedBytes());
public static void invalidate(IrisData owner) {
CACHE.invalidate(owner);
}
@MinNumber(0.01)
@MaxNumber(50)
@@ -99,7 +94,8 @@ public class IrisObjectScale {
return Math.max(minimumScale, maximumScale);
}
public IrisObject get(RNG rng, IrisObject origin) {
/** The scaled variant of {@code origin} this scale picks, built once per world and kept in the world's cache. */
public IrisObject get(RNG rng, IrisObject origin, Engine engine) {
if (origin == null) {
return null;
}
@@ -110,40 +106,33 @@ public class IrisObjectScale {
}
int variantIndex = request.selectVariant(rng);
CacheKey key = CACHE.key(origin, request, variantIndex);
CacheLookup lookup = CACHE.lookup(key);
if (lookup.variant() != null) {
return lookup.variant();
ScaleCache cache = cacheOf(engine);
CacheKey key = cache.key(origin, request, variantIndex);
IrisObject cached = cache.lookup(key);
if (cached != null) {
return cached;
}
synchronized (CACHE.loadLock(key)) {
CacheLookup afterWait = CACHE.lookup(key);
if (afterWait.variant() != null) {
return afterWait.variant();
synchronized (cache.loadLock(key)) {
IrisObject afterWait = cache.lookup(key);
if (afterWait != null) {
return afterWait;
}
IrisObject variant = origin.scaled(request.scaleAt(variantIndex), request.interpolation());
long estimatedBytes = estimateVariant(variant);
return CACHE.putIfCurrent(key, variant, estimatedBytes, lookup.generation());
return cache.putIfAbsent(key, variant, estimatedBytes);
}
}
static ScaleCache cacheOf(Engine engine) {
return engine.getGeneration().state(ScaleCache.class, world -> new ScaleCache(resolveCacheMaximumEstimatedBytes()));
}
public boolean canScaleBeyond() {
return shouldScale() && getMaxScale() > 1;
}
static int cacheEntryCount() {
return CACHE.size();
}
static long cacheEstimatedBytes() {
return CACHE.estimatedBytes();
}
static boolean isOriginCached(IrisObject origin) {
return CACHE.containsOrigin(origin);
}
private ScaleRequest snapshotRequest() {
IrisObjectPlacementScaleInterpolator configuredInterpolation = interpolation == null
? IrisObjectPlacementScaleInterpolator.NONE
@@ -217,7 +206,6 @@ public class IrisObjectScale {
}
static final class CacheKey extends WeakReference<IrisObject> {
private final IrisData owner;
private final ScaleRequest request;
private final int variantIndex;
private final int originRevision;
@@ -231,22 +219,16 @@ public class IrisObjectScale {
CacheKey(IrisObject origin, ScaleRequest request, int variantIndex,
ReferenceQueue<IrisObject> collectedOrigins) {
super(origin, collectedOrigins);
this.owner = origin.getLoader();
this.request = request;
this.variantIndex = variantIndex;
this.originRevision = originRevision(origin);
int result = System.identityHashCode(origin);
result = (31 * result) + System.identityHashCode(owner);
result = (31 * result) + originRevision;
result = (31 * result) + request.hashCode();
this.loadHashCode = result;
this.hashCode = (31 * result) + variantIndex;
}
boolean belongsTo(IrisData candidate) {
return owner == candidate;
}
boolean hasOrigin(IrisObject candidate) {
return get() == candidate;
}
@@ -262,7 +244,6 @@ public class IrisObjectScale {
IrisObject origin = get();
return origin != null
&& origin == other.get()
&& owner == other.owner
&& originRevision == other.originRevision
&& request.equals(other.request)
&& variantIndex == other.variantIndex;
@@ -300,16 +281,12 @@ public class IrisObjectScale {
}
}
record CacheLookup(IrisObject variant, long generation) {
}
static final class ScaleCache {
private final long maximumEstimatedBytes;
private final LinkedHashMap<CacheKey, CacheValue> entries;
private final Object[] loadLocks;
private final ReferenceQueue<IrisObject> collectedOrigins;
private long estimatedBytes;
private long generation;
ScaleCache(long maximumEstimatedBytes) {
if (maximumEstimatedBytes <= 0L) {
@@ -332,20 +309,19 @@ public class IrisObjectScale {
return loadLocks[key.loadHashCode() & (loadLocks.length - 1)];
}
synchronized CacheLookup lookup(CacheKey key) {
synchronized IrisObject lookup(CacheKey key) {
drainCollectedOrigins();
CacheValue value = entries.get(key);
return new CacheLookup(value == null ? null : value.variant(), generation);
return value == null ? null : value.variant();
}
synchronized IrisObject putIfCurrent(CacheKey key, IrisObject variant,
long entryEstimatedBytes, long expectedGeneration) {
synchronized IrisObject putIfAbsent(CacheKey key, IrisObject variant, long entryEstimatedBytes) {
drainCollectedOrigins();
CacheValue present = entries.get(key);
if (present != null) {
return present.variant();
}
if (generation != expectedGeneration || entryEstimatedBytes > maximumEstimatedBytes) {
if (entryEstimatedBytes > maximumEstimatedBytes) {
return variant;
}
@@ -355,19 +331,6 @@ public class IrisObjectScale {
return variant;
}
synchronized void invalidate(IrisData owner) {
drainCollectedOrigins();
generation++;
Iterator<Map.Entry<CacheKey, CacheValue>> iterator = entries.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<CacheKey, CacheValue> entry = iterator.next();
if (entry.getKey().belongsTo(owner)) {
estimatedBytes -= entry.getValue().estimatedBytes();
iterator.remove();
}
}
}
synchronized int size() {
drainCollectedOrigins();
return entries.size();