Skip to content

Commit

Permalink
Remove hot allocations in ForgeRegistry#getDelegateOrThrow
Browse files Browse the repository at this point in the history
  • Loading branch information
embeddedt committed Dec 8, 2023
1 parent bf43ba7 commit ccfc282
Showing 1 changed file with 65 additions and 0 deletions.
@@ -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;
}
}

0 comments on commit ccfc282

Please sign in to comment.