mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-12-03 03:31:32 +00:00
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:
1 parent
c1f23830a2
commit
63bd3e3de8
4 files changed
+40
-79
No files matched your search
+9
-11
@@ -1,6 +1,7 @@
|
|||||||
package art.arcane.iris.engine.framework.structure;
|
package art.arcane.iris.engine.framework.structure;
|
||||||
|
|
||||||
import art.arcane.iris.core.loader.IrisData;
|
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.framework.StructureAssembler;
|
||||||
import art.arcane.iris.engine.object.value.IrisPosition;
|
import art.arcane.iris.engine.object.value.IrisPosition;
|
||||||
import art.arcane.iris.engine.object.structure.IrisStructure;
|
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.ConcurrentHashMap;
|
||||||
import java.util.concurrent.ConcurrentMap;
|
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 {
|
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();
|
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();
|
Caffeine.newBuilder().weakKeys().build();
|
||||||
private static final List<Long> RUNTIME_SAMPLE_SEEDS = List.of(
|
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);
|
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);
|
return compileAndReport(activeData, activeStructure);
|
||||||
}
|
}
|
||||||
ConcurrentMap<String, StructureGraphCompilation> catalog = CACHE.get(
|
ConcurrentMap<String, StructureGraphCompilation> catalog = CACHE.get(
|
||||||
activeData, ignored -> new ConcurrentHashMap<>());
|
activeData.getPackResources(), ignored -> new ConcurrentHashMap<>());
|
||||||
return catalog.computeIfAbsent(structureKey, ignored -> compileAndReport(activeData, activeStructure));
|
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) {
|
public static boolean guaranteesRuntimeOutput(IrisData data, IrisStructure structure) {
|
||||||
IrisData activeData = Objects.requireNonNull(data);
|
IrisData activeData = Objects.requireNonNull(data);
|
||||||
IrisStructure activeStructure = Objects.requireNonNull(structure);
|
IrisStructure activeStructure = Objects.requireNonNull(structure);
|
||||||
@@ -55,7 +53,7 @@ public final class StructureGraphCatalog {
|
|||||||
return sampleRuntimeAssembly(activeData, activeStructure);
|
return sampleRuntimeAssembly(activeData, activeStructure);
|
||||||
}
|
}
|
||||||
ConcurrentMap<String, Boolean> catalog = RUNTIME_OUTPUT.get(
|
ConcurrentMap<String, Boolean> catalog = RUNTIME_OUTPUT.get(
|
||||||
activeData, ignored -> new ConcurrentHashMap<>());
|
activeData.getPackResources(), ignored -> new ConcurrentHashMap<>());
|
||||||
return catalog.computeIfAbsent(
|
return catalog.computeIfAbsent(
|
||||||
structureKey, ignored -> sampleRuntimeAssembly(activeData, activeStructure));
|
structureKey, ignored -> sampleRuntimeAssembly(activeData, activeStructure));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -184,12 +184,12 @@ public class FloatingObjectPlacing {
|
|||||||
if (raw == null) {
|
if (raw == null) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
IrisObject obj0 = placement.getScale().get(rng, raw);
|
IrisObject obj0 = placement.getScale().get(rng, raw, engine);
|
||||||
if (obj0 == null) {
|
if (obj0 == null) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (entry != null && entry.hasObjectShrink()) {
|
if (entry != null && entry.hasObjectShrink()) {
|
||||||
obj0 = entry.getShrinkScale().get(rng, obj0);
|
obj0 = entry.getShrinkScale().get(rng, obj0, engine);
|
||||||
if (obj0 == null) {
|
if (obj0 == null) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -232,12 +232,12 @@ public class FloatingObjectPlacing {
|
|||||||
if (raw == null) {
|
if (raw == null) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
IrisObject obj0 = placement.getScale().get(rng, raw);
|
IrisObject obj0 = placement.getScale().get(rng, raw, engine);
|
||||||
if (obj0 == null) {
|
if (obj0 == null) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (entry != null && entry.hasObjectShrink()) {
|
if (entry != null && entry.hasObjectShrink()) {
|
||||||
obj0 = entry.getShrinkScale().get(rng, obj0);
|
obj0 = entry.getShrinkScale().get(rng, obj0, engine);
|
||||||
if (obj0 == null) {
|
if (obj0 == null) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -317,12 +317,12 @@ public class FloatingObjectPlacing {
|
|||||||
if (raw == null) {
|
if (raw == null) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
IrisObject obj0 = placement.getScale().get(rng, raw);
|
IrisObject obj0 = placement.getScale().get(rng, raw, engine);
|
||||||
if (obj0 == null) {
|
if (obj0 == null) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (entry != null && entry.hasObjectShrink()) {
|
if (entry != null && entry.hasObjectShrink()) {
|
||||||
obj0 = entry.getShrinkScale().get(rng, obj0);
|
obj0 = entry.getShrinkScale().get(rng, obj0, engine);
|
||||||
if (obj0 == null) {
|
if (obj0 == null) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -663,7 +663,7 @@ public class ObjectPlacing {
|
|||||||
|
|
||||||
for (int i = 0; i < density; i++) {
|
for (int i = 0; i < density; i++) {
|
||||||
attempts++;
|
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) {
|
if (v == null) {
|
||||||
nullObjects++;
|
nullObjects++;
|
||||||
if (traceRegen) {
|
if (traceRegen) {
|
||||||
@@ -801,7 +801,7 @@ public class ObjectPlacing {
|
|||||||
|
|
||||||
for (int i = 0; i < density; i++) {
|
for (int i = 0; i < density; i++) {
|
||||||
attempts++;
|
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) {
|
if (object == null) {
|
||||||
nullObjects++;
|
nullObjects++;
|
||||||
if (traceRegen) {
|
if (traceRegen) {
|
||||||
@@ -1034,7 +1034,7 @@ public class ObjectPlacing {
|
|||||||
int density = objectPlacement.getDensity(rng, minX, minZ, engine.getData());
|
int density = objectPlacement.getDensity(rng, minX, minZ, engine.getData());
|
||||||
|
|
||||||
for (int i = 0; i < density; i++) {
|
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) {
|
if (v == null) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -1419,7 +1419,7 @@ public class ObjectPlacing {
|
|||||||
private Set<String> guessPlacedKeys(RNG rng, int x, int z, IrisObjectPlacement objectPlacement) {
|
private Set<String> guessPlacedKeys(RNG rng, int x, int z, IrisObjectPlacement objectPlacement) {
|
||||||
Set<String> f = new KSet<>();
|
Set<String> f = new KSet<>();
|
||||||
for (int i = 0; i < objectPlacement.getDensity(rng, x, z, engine.getData()); i++) {
|
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) {
|
if (v == null) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,7 +18,7 @@
|
|||||||
|
|
||||||
package art.arcane.iris.engine.object.prefab;
|
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.Desc;
|
||||||
import art.arcane.iris.engine.object.annotations.MaxNumber;
|
import art.arcane.iris.engine.object.annotations.MaxNumber;
|
||||||
import art.arcane.iris.engine.object.annotations.MinNumber;
|
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_VARIANT_ESTIMATED_BYTES = 256L;
|
||||||
private static final long CACHE_ESTIMATED_BYTES_PER_VOXEL = 128L;
|
private static final long CACHE_ESTIMATED_BYTES_PER_VOXEL = 128L;
|
||||||
private static final int CACHE_LOAD_LOCK_COUNT = 256;
|
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)
|
@MinNumber(0.01)
|
||||||
@MaxNumber(50)
|
@MaxNumber(50)
|
||||||
@@ -99,7 +94,8 @@ public class IrisObjectScale {
|
|||||||
return Math.max(minimumScale, maximumScale);
|
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) {
|
if (origin == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -110,40 +106,33 @@ public class IrisObjectScale {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int variantIndex = request.selectVariant(rng);
|
int variantIndex = request.selectVariant(rng);
|
||||||
CacheKey key = CACHE.key(origin, request, variantIndex);
|
ScaleCache cache = cacheOf(engine);
|
||||||
CacheLookup lookup = CACHE.lookup(key);
|
CacheKey key = cache.key(origin, request, variantIndex);
|
||||||
if (lookup.variant() != null) {
|
IrisObject cached = cache.lookup(key);
|
||||||
return lookup.variant();
|
if (cached != null) {
|
||||||
|
return cached;
|
||||||
}
|
}
|
||||||
|
|
||||||
synchronized (CACHE.loadLock(key)) {
|
synchronized (cache.loadLock(key)) {
|
||||||
CacheLookup afterWait = CACHE.lookup(key);
|
IrisObject afterWait = cache.lookup(key);
|
||||||
if (afterWait.variant() != null) {
|
if (afterWait != null) {
|
||||||
return afterWait.variant();
|
return afterWait;
|
||||||
}
|
}
|
||||||
|
|
||||||
IrisObject variant = origin.scaled(request.scaleAt(variantIndex), request.interpolation());
|
IrisObject variant = origin.scaled(request.scaleAt(variantIndex), request.interpolation());
|
||||||
long estimatedBytes = estimateVariant(variant);
|
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() {
|
public boolean canScaleBeyond() {
|
||||||
return shouldScale() && getMaxScale() > 1;
|
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() {
|
private ScaleRequest snapshotRequest() {
|
||||||
IrisObjectPlacementScaleInterpolator configuredInterpolation = interpolation == null
|
IrisObjectPlacementScaleInterpolator configuredInterpolation = interpolation == null
|
||||||
? IrisObjectPlacementScaleInterpolator.NONE
|
? IrisObjectPlacementScaleInterpolator.NONE
|
||||||
@@ -217,7 +206,6 @@ public class IrisObjectScale {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static final class CacheKey extends WeakReference<IrisObject> {
|
static final class CacheKey extends WeakReference<IrisObject> {
|
||||||
private final IrisData owner;
|
|
||||||
private final ScaleRequest request;
|
private final ScaleRequest request;
|
||||||
private final int variantIndex;
|
private final int variantIndex;
|
||||||
private final int originRevision;
|
private final int originRevision;
|
||||||
@@ -231,22 +219,16 @@ public class IrisObjectScale {
|
|||||||
CacheKey(IrisObject origin, ScaleRequest request, int variantIndex,
|
CacheKey(IrisObject origin, ScaleRequest request, int variantIndex,
|
||||||
ReferenceQueue<IrisObject> collectedOrigins) {
|
ReferenceQueue<IrisObject> collectedOrigins) {
|
||||||
super(origin, collectedOrigins);
|
super(origin, collectedOrigins);
|
||||||
this.owner = origin.getLoader();
|
|
||||||
this.request = request;
|
this.request = request;
|
||||||
this.variantIndex = variantIndex;
|
this.variantIndex = variantIndex;
|
||||||
this.originRevision = originRevision(origin);
|
this.originRevision = originRevision(origin);
|
||||||
int result = System.identityHashCode(origin);
|
int result = System.identityHashCode(origin);
|
||||||
result = (31 * result) + System.identityHashCode(owner);
|
|
||||||
result = (31 * result) + originRevision;
|
result = (31 * result) + originRevision;
|
||||||
result = (31 * result) + request.hashCode();
|
result = (31 * result) + request.hashCode();
|
||||||
this.loadHashCode = result;
|
this.loadHashCode = result;
|
||||||
this.hashCode = (31 * result) + variantIndex;
|
this.hashCode = (31 * result) + variantIndex;
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean belongsTo(IrisData candidate) {
|
|
||||||
return owner == candidate;
|
|
||||||
}
|
|
||||||
|
|
||||||
boolean hasOrigin(IrisObject candidate) {
|
boolean hasOrigin(IrisObject candidate) {
|
||||||
return get() == candidate;
|
return get() == candidate;
|
||||||
}
|
}
|
||||||
@@ -262,7 +244,6 @@ public class IrisObjectScale {
|
|||||||
IrisObject origin = get();
|
IrisObject origin = get();
|
||||||
return origin != null
|
return origin != null
|
||||||
&& origin == other.get()
|
&& origin == other.get()
|
||||||
&& owner == other.owner
|
|
||||||
&& originRevision == other.originRevision
|
&& originRevision == other.originRevision
|
||||||
&& request.equals(other.request)
|
&& request.equals(other.request)
|
||||||
&& variantIndex == other.variantIndex;
|
&& variantIndex == other.variantIndex;
|
||||||
@@ -300,16 +281,12 @@ public class IrisObjectScale {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
record CacheLookup(IrisObject variant, long generation) {
|
|
||||||
}
|
|
||||||
|
|
||||||
static final class ScaleCache {
|
static final class ScaleCache {
|
||||||
private final long maximumEstimatedBytes;
|
private final long maximumEstimatedBytes;
|
||||||
private final LinkedHashMap<CacheKey, CacheValue> entries;
|
private final LinkedHashMap<CacheKey, CacheValue> entries;
|
||||||
private final Object[] loadLocks;
|
private final Object[] loadLocks;
|
||||||
private final ReferenceQueue<IrisObject> collectedOrigins;
|
private final ReferenceQueue<IrisObject> collectedOrigins;
|
||||||
private long estimatedBytes;
|
private long estimatedBytes;
|
||||||
private long generation;
|
|
||||||
|
|
||||||
ScaleCache(long maximumEstimatedBytes) {
|
ScaleCache(long maximumEstimatedBytes) {
|
||||||
if (maximumEstimatedBytes <= 0L) {
|
if (maximumEstimatedBytes <= 0L) {
|
||||||
@@ -332,20 +309,19 @@ public class IrisObjectScale {
|
|||||||
return loadLocks[key.loadHashCode() & (loadLocks.length - 1)];
|
return loadLocks[key.loadHashCode() & (loadLocks.length - 1)];
|
||||||
}
|
}
|
||||||
|
|
||||||
synchronized CacheLookup lookup(CacheKey key) {
|
synchronized IrisObject lookup(CacheKey key) {
|
||||||
drainCollectedOrigins();
|
drainCollectedOrigins();
|
||||||
CacheValue value = entries.get(key);
|
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,
|
synchronized IrisObject putIfAbsent(CacheKey key, IrisObject variant, long entryEstimatedBytes) {
|
||||||
long entryEstimatedBytes, long expectedGeneration) {
|
|
||||||
drainCollectedOrigins();
|
drainCollectedOrigins();
|
||||||
CacheValue present = entries.get(key);
|
CacheValue present = entries.get(key);
|
||||||
if (present != null) {
|
if (present != null) {
|
||||||
return present.variant();
|
return present.variant();
|
||||||
}
|
}
|
||||||
if (generation != expectedGeneration || entryEstimatedBytes > maximumEstimatedBytes) {
|
if (entryEstimatedBytes > maximumEstimatedBytes) {
|
||||||
return variant;
|
return variant;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -355,19 +331,6 @@ public class IrisObjectScale {
|
|||||||
return variant;
|
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() {
|
synchronized int size() {
|
||||||
drainCollectedOrigins();
|
drainCollectedOrigins();
|
||||||
return entries.size();
|
return entries.size();
|
||||||
|
|||||||
Reference in new issue
Block a user