Avoid recreating tag ID strings

This commit is contained in:
embeddedt 2023-04-26 20:56:03 -04:00
parent 0ae2225d9e
commit d3bf2271fc
No known key found for this signature in database
GPG Key ID: A69433EC199B5613
4 changed files with 62 additions and 0 deletions

View File

@ -65,6 +65,7 @@ public class ModernFixEarlyConfig {
this.addMixinRule("perf.scan_cache", true);
this.addMixinRule("perf.kubejs", modPresent("kubejs"));
this.addMixinRule("perf.flatten_model_predicates", true);
this.addMixinRule("perf.tag_id_caching", true);
this.addMixinRule("perf.deduplicate_location", false);
this.addMixinRule("perf.cache_blockstate_cache_arrays", true);
this.addMixinRule("perf.cache_model_materials", true);

View File

@ -0,0 +1,30 @@
package org.embeddedt.modernfix.mixin.perf.tag_id_caching;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.TagEntry;
import net.minecraft.util.ExtraCodecs;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.Shadow;
@Mixin(TagEntry.class)
public class TagEntryMixin {
@Shadow @Final private boolean tag;
@Shadow @Final private ResourceLocation id;
private ExtraCodecs.TagOrElementLocation cachedLoc;
/**
* @author embeddedt
* @reason use cached location, overwrite rather than inject to avoid allocs
*/
@Overwrite
private ExtraCodecs.TagOrElementLocation elementOrTag() {
ExtraCodecs.TagOrElementLocation loc = cachedLoc;
if(loc == null) {
loc = new ExtraCodecs.TagOrElementLocation(this.id, this.tag);
cachedLoc = loc;
}
return loc;
}
}

View File

@ -0,0 +1,29 @@
package org.embeddedt.modernfix.mixin.perf.tag_id_caching;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.ExtraCodecs;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.Shadow;
@Mixin(ExtraCodecs.TagOrElementLocation.class)
public class TagOrElementLocationMixin {
@Shadow @Final private boolean tag;
@Shadow @Final private ResourceLocation id;
private String cachedDecoratedId;
/**
* @author embeddedt
* @reason use cached ID, overwrite rather than inject to avoid allocs
*/
@Overwrite
private String decoratedId() {
String id = cachedDecoratedId;
if(id == null) {
id = this.tag ? "#" + this.id : this.id.toString();
cachedDecoratedId = id;
}
return id;
}
}

View File

@ -33,6 +33,8 @@
"perf.nbt_memory_usage.CompoundTagMixin",
"perf.fast_registry_validation.ForgeRegistryMixin",
"perf.kubejs.RecipeEventJSMixin",
"perf.tag_id_caching.TagEntryMixin",
"perf.tag_id_caching.TagOrElementLocationMixin",
"perf.cache_strongholds.ChunkGeneratorMixin",
"perf.cache_upgraded_structures.StructureManagerMixin",
"perf.cache_strongholds.ServerLevelMixin",