-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove hot allocations in ForgeRegistry#getDelegateOrThrow
- Loading branch information
Showing
1 changed file
with
65 additions
and
0 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
...ava/org/embeddedt/modernfix/forge/mixin/perf/forge_registry_alloc/ForgeRegistryMixin.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,65 @@ | ||
package org.embeddedt.modernfix.forge.mixin.perf.forge_registry_alloc; | ||
|
||
import net.minecraft.core.Holder; | ||
import net.minecraft.resources.ResourceKey; | ||
import net.minecraft.resources.ResourceLocation; | ||
import net.minecraftforge.registries.ForgeRegistry; | ||
import org.spongepowered.asm.mixin.Final; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Overwrite; | ||
import org.spongepowered.asm.mixin.Shadow; | ||
|
||
import java.util.Locale; | ||
import java.util.Map; | ||
|
||
@Mixin(value = ForgeRegistry.class, remap = false) | ||
public abstract class ForgeRegistryMixin<V> { | ||
@Shadow @Final private Map<ResourceLocation, Holder.Reference<V>> delegatesByName; | ||
|
||
@Shadow @Final private Map<V, Holder.Reference<V>> delegatesByValue; | ||
|
||
/** | ||
* @author embeddedt | ||
* @reason stop allocating so many unneeded objects. stop. | ||
*/ | ||
@Overwrite | ||
public Holder.Reference<V> getDelegateOrThrow(ResourceLocation location) { | ||
Holder.Reference<V> holder = delegatesByName.get(location); | ||
|
||
if (holder == null) { | ||
throw new IllegalArgumentException(String.format(Locale.ENGLISH, "No delegate exists for location %s", location)); | ||
} | ||
|
||
return holder; | ||
} | ||
|
||
/** | ||
* @author embeddedt | ||
* @reason see above | ||
*/ | ||
@Overwrite | ||
public Holder.Reference<V> getDelegateOrThrow(ResourceKey<V> rkey) { | ||
Holder.Reference<V> holder = delegatesByName.get(rkey.location()); | ||
|
||
if (holder == null) { | ||
throw new IllegalArgumentException(String.format(Locale.ENGLISH, "No delegate exists for key %s", rkey)); | ||
} | ||
|
||
return holder; | ||
} | ||
|
||
/** | ||
* @author embeddedt | ||
* @reason see above | ||
*/ | ||
@Overwrite | ||
public Holder.Reference<V> getDelegateOrThrow(V value) { | ||
Holder.Reference<V> holder = delegatesByValue.get(value); | ||
|
||
if (holder == null) { | ||
throw new IllegalArgumentException(String.format(Locale.ENGLISH, "No delegate exists for value %s", value)); | ||
} | ||
|
||
return holder; | ||
} | ||
} |