Skip to content

Commit b30b319

Browse files
committed
Deduplicate ResourcefulLib Highlight objects
1 parent e411f11 commit b30b319

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

forge/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ dependencies {
6161
modCompileOnly("curse.maven:supermartijncore-454372:4455391")
6262
modCompileOnly("vazkii.patchouli:Patchouli:1.19.2-77")
6363
modCompileOnly("curse.maven:cofhcore-69162:5374122")
64+
modCompileOnly("curse.maven:resourcefullib-570073:5659871")
6465

6566
// runtime remapping at home
6667
for (extraModJar in fileTree(dir: extraModsDir, include: '*.jar')) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package org.embeddedt.modernfix.forge.mixin.perf.resourcefullib_highlight_deduplication;
2+
3+
import com.teamresourceful.resourcefullib.client.highlights.HighlightHandler;
4+
import com.teamresourceful.resourcefullib.client.highlights.base.Highlight;
5+
import com.teamresourceful.resourcefullib.client.highlights.base.HighlightLine;
6+
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
7+
import net.minecraft.world.level.block.state.BlockState;
8+
import org.embeddedt.modernfix.ModernFix;
9+
import org.embeddedt.modernfix.annotation.ClientOnlyMixin;
10+
import org.embeddedt.modernfix.annotation.RequiresMod;
11+
import org.joml.Vector3f;
12+
import org.spongepowered.asm.mixin.Mixin;
13+
import org.spongepowered.asm.mixin.injection.At;
14+
import org.spongepowered.asm.mixin.injection.Inject;
15+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
16+
17+
import java.util.HashMap;
18+
import java.util.List;
19+
import java.util.Map;
20+
import java.util.function.Function;
21+
22+
@Mixin(HighlightHandler.class)
23+
@RequiresMod("resourcefullib")
24+
@ClientOnlyMixin
25+
public class HighlightHandlerMixin {
26+
@Inject(method = "apply(Ljava/util/Map;Lnet/minecraft/server/packs/resources/ResourceManager;Lnet/minecraft/util/profiling/ProfilerFiller;)V", at = @At("RETURN"), remap = false)
27+
private void deduplicateHighlights(CallbackInfo ci) {
28+
Map<BlockState, Highlight> stateCache;
29+
30+
try {
31+
var stateCacheMap = this.getClass().getDeclaredField("STATE_CACHE");
32+
if (stateCacheMap.get(null) instanceof HashMap<?,?> hashMap) {
33+
stateCache = (Map<BlockState, Highlight>)hashMap;
34+
} else {
35+
throw new ReflectiveOperationException("Unexpected map type");
36+
}
37+
} catch (ReflectiveOperationException e) {
38+
ModernFix.LOGGER.error("Not applying Resourceful Lib patch due to reflection error", e);
39+
return;
40+
}
41+
42+
ObjectOpenHashSet<Vector3f> pointCache = new ObjectOpenHashSet<>();
43+
ObjectOpenHashSet<HighlightLine> lineCache = new ObjectOpenHashSet<>();
44+
ObjectOpenHashSet<List<HighlightLine>> listCache = new ObjectOpenHashSet<>();
45+
Function<HighlightLine, HighlightLine> deduplicator = l -> {
46+
if (!lineCache.contains(l)) {
47+
l = new HighlightLine(
48+
pointCache.addOrGet(l.start()),
49+
pointCache.addOrGet(l.end()),
50+
pointCache.addOrGet(l.normal())
51+
);
52+
}
53+
return lineCache.addOrGet(l);
54+
};
55+
56+
stateCache.replaceAll((rl, highlight) -> {
57+
if (highlight == null) {
58+
return null;
59+
}
60+
List<HighlightLine> newList = highlight.lines();
61+
if (!listCache.contains(newList)) {
62+
newList = newList.stream().map(deduplicator).toList();
63+
}
64+
return new Highlight(highlight.id(), listCache.addOrGet(newList));
65+
});
66+
67+
ModernFix.LOGGER.info("Deduplicated ResourcefulLib highlights ({} points, {} lines)", pointCache.size(), lineCache.size());
68+
}
69+
}

0 commit comments

Comments
 (0)