-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement caching on the blockstate and item to model maps
- Loading branch information
Showing
4 changed files
with
134 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
common/src/main/java/org/embeddedt/modernfix/dynamicresources/DynamicModelCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package org.embeddedt.modernfix.dynamicresources; | ||
|
||
import it.unimi.dsi.fastutil.Function; | ||
import it.unimi.dsi.fastutil.objects.Reference2ReferenceLinkedOpenHashMap; | ||
import net.minecraft.client.resources.model.BakedModel; | ||
|
||
import java.util.concurrent.locks.StampedLock; | ||
|
||
/** | ||
* The Mojang Triple-based baked cache system is too slow to be hitting on every model retrieval, so | ||
* we need a fast, concurrency-safe wrapper on top. | ||
*/ | ||
public class DynamicModelCache<K> { | ||
private final Reference2ReferenceLinkedOpenHashMap<K, BakedModel> cache = new Reference2ReferenceLinkedOpenHashMap<>(); | ||
private final StampedLock lock = new StampedLock(); | ||
private final Function<K, BakedModel> modelRetriever; | ||
private final boolean allowNulls; | ||
|
||
public DynamicModelCache(Function<K, BakedModel> modelRetriever, boolean allowNulls) { | ||
this.modelRetriever = modelRetriever; | ||
this.allowNulls = allowNulls; | ||
} | ||
|
||
public void clear() { | ||
long stamp = lock.writeLock(); | ||
try { | ||
cache.clear(); | ||
} finally { | ||
lock.unlock(stamp); | ||
} | ||
} | ||
|
||
private boolean needToPopulate(K state) { | ||
long stamp = lock.readLock(); | ||
try { | ||
return !cache.containsKey(state); | ||
} finally { | ||
lock.unlock(stamp); | ||
} | ||
} | ||
|
||
private BakedModel getModelFromCache(K state) { | ||
long stamp = lock.readLock(); | ||
try { | ||
return cache.get(state); | ||
} finally { | ||
lock.unlock(stamp); | ||
} | ||
} | ||
|
||
private BakedModel cacheModel(K state) { | ||
BakedModel model = modelRetriever.apply(state); | ||
|
||
// Lock and modify our local, faster cache | ||
long stamp = lock.writeLock(); | ||
|
||
try { | ||
cache.putAndMoveToFirst(state, model); | ||
// TODO: choose less arbitrary number | ||
if(cache.size() >= 1000) { | ||
cache.removeLast(); | ||
} | ||
} finally { | ||
lock.unlock(stamp); | ||
} | ||
|
||
return model; | ||
} | ||
|
||
public BakedModel get(K key) { | ||
BakedModel model = getModelFromCache(key); | ||
|
||
if(model == null && (!allowNulls || needToPopulate(key))) { | ||
model = cacheModel(key); | ||
} | ||
|
||
return model; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
fabric/src/test/java/org/embeddedt/modernfix/dynamicresources/DynamicModelCacheTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package org.embeddedt.modernfix.dynamicresources; | ||
|
||
import net.minecraft.client.resources.model.BakedModel; | ||
import net.minecraft.client.resources.model.BuiltInModel; | ||
import net.minecraft.world.item.Item; | ||
import net.minecraft.world.item.Items; | ||
import org.embeddedt.modernfix.testing.util.BootstrapMinecraft; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
@BootstrapMinecraft | ||
public class DynamicModelCacheTest { | ||
@Test | ||
public void testCacheReturnsNullForNullGetter() { | ||
DynamicModelCache<Item> cache = new DynamicModelCache(k -> null, true); | ||
assertNull(cache.get(Items.STONE)); | ||
} | ||
|
||
@Test | ||
public void testCacheFunctions() { | ||
BakedModel model = new BuiltInModel(null, null, null, false); | ||
DynamicModelCache<Item> cache = new DynamicModelCache(k -> model, true); | ||
assertEquals(model, cache.get(Items.STONE)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters