Skip to content

Commit ccfc282

Browse files
committed
Remove hot allocations in ForgeRegistry#getDelegateOrThrow
1 parent bf43ba7 commit ccfc282

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package org.embeddedt.modernfix.forge.mixin.perf.forge_registry_alloc;
2+
3+
import net.minecraft.core.Holder;
4+
import net.minecraft.resources.ResourceKey;
5+
import net.minecraft.resources.ResourceLocation;
6+
import net.minecraftforge.registries.ForgeRegistry;
7+
import org.spongepowered.asm.mixin.Final;
8+
import org.spongepowered.asm.mixin.Mixin;
9+
import org.spongepowered.asm.mixin.Overwrite;
10+
import org.spongepowered.asm.mixin.Shadow;
11+
12+
import java.util.Locale;
13+
import java.util.Map;
14+
15+
@Mixin(value = ForgeRegistry.class, remap = false)
16+
public abstract class ForgeRegistryMixin<V> {
17+
@Shadow @Final private Map<ResourceLocation, Holder.Reference<V>> delegatesByName;
18+
19+
@Shadow @Final private Map<V, Holder.Reference<V>> delegatesByValue;
20+
21+
/**
22+
* @author embeddedt
23+
* @reason stop allocating so many unneeded objects. stop.
24+
*/
25+
@Overwrite
26+
public Holder.Reference<V> getDelegateOrThrow(ResourceLocation location) {
27+
Holder.Reference<V> holder = delegatesByName.get(location);
28+
29+
if (holder == null) {
30+
throw new IllegalArgumentException(String.format(Locale.ENGLISH, "No delegate exists for location %s", location));
31+
}
32+
33+
return holder;
34+
}
35+
36+
/**
37+
* @author embeddedt
38+
* @reason see above
39+
*/
40+
@Overwrite
41+
public Holder.Reference<V> getDelegateOrThrow(ResourceKey<V> rkey) {
42+
Holder.Reference<V> holder = delegatesByName.get(rkey.location());
43+
44+
if (holder == null) {
45+
throw new IllegalArgumentException(String.format(Locale.ENGLISH, "No delegate exists for key %s", rkey));
46+
}
47+
48+
return holder;
49+
}
50+
51+
/**
52+
* @author embeddedt
53+
* @reason see above
54+
*/
55+
@Overwrite
56+
public Holder.Reference<V> getDelegateOrThrow(V value) {
57+
Holder.Reference<V> holder = delegatesByValue.get(value);
58+
59+
if (holder == null) {
60+
throw new IllegalArgumentException(String.format(Locale.ENGLISH, "No delegate exists for value %s", value));
61+
}
62+
63+
return holder;
64+
}
65+
}

0 commit comments

Comments
 (0)