Add KubeJS optimizations

This commit is contained in:
embeddedt 2023-02-19 22:19:20 -05:00
parent 36217fe479
commit 9b9b13d24b
No known key found for this signature in database
GPG Key ID: A69433EC199B5613
7 changed files with 153 additions and 2 deletions

View File

@ -43,6 +43,21 @@ repositories {
name = 'ParchmentMC'
url = 'https://maven.parchmentmc.org'
}
maven {
// Shedaniel's maven (Architectury API)
url = "https://maven.architectury.dev"
content {
includeGroup "me.shedaniel"
}
}
maven {
// saps.dev Maven (KubeJS and Rhino)
url = "https://maven.saps.dev/minecraft"
content {
includeGroup "dev.latvian.mods"
}
}
}
dependencies {
@ -70,6 +85,8 @@ dependencies {
modRuntimeOnly("mezz.jei:jei-${minecraft_version}:${jei_version}")
modCompileOnly("curse.maven:refinedstorage-243076:${refined_storage_version}")
modImplementation("dev.latvian.mods:kubejs-forge:${kubejs_version}")
}
tasks.withType(JavaCompile) {

View File

@ -14,4 +14,5 @@ lazydfu_version=3249059
mekanism_version=1.16.5-10.1.2.457
parchment_version=2022.03.06
jei_version=7.7.1.153
refined_storage_version=3807951
refined_storage_version=3807951
kubejs_version=1605.3.19-build.299

View File

@ -44,6 +44,7 @@ public class ModernFixEarlyConfig {
this.addMixinRule("perf.cache_model_materials", true);
this.addMixinRule("perf.datapack_reload_exceptions", true);
this.addMixinRule("perf.async_locator", true);
this.addMixinRule("perf.kubejs", true);
/* Keep this off if JEI isn't installed to prevent breaking vanilla gameplay */
this.addMixinRule("perf.blast_search_trees", FMLLoader.getLoadingModList().getModFileById("jei") != null);
this.addMixinRule("safety", true);

View File

@ -0,0 +1,28 @@
package org.embeddedt.modernfix.mixin.perf.kubejs;
import dev.latvian.kubejs.item.ItemStackJS;
import dev.latvian.kubejs.item.ingredient.IngredientJS;
import dev.latvian.kubejs.item.ingredient.TagIngredientJS;
import net.minecraft.tags.Tag;
import net.minecraft.world.item.Item;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
@Mixin(TagIngredientJS.class)
public abstract class TagIngredientJSMixin {
@Shadow public abstract Tag<Item> getActualTag();
/**
* @author embeddedt
* @reason avoid pointless construction of many ItemStack objects
*/
@Inject(method = "anyStackMatches", at = @At("HEAD"), cancellable = true, remap = false)
private void checkItemOnly(IngredientJS ingredient, CallbackInfoReturnable<Boolean> cir) {
if(ingredient instanceof ItemStackJS) {
cir.setReturnValue(((ItemStackJS)ingredient).getItem().is(this.getActualTag()));
}
}
}

View File

@ -0,0 +1,82 @@
package org.embeddedt.modernfix.mixin.perf.kubejs;
import dev.latvian.kubejs.server.TagEventJS;
import dev.latvian.kubejs.util.ConsoleJS;
import dev.latvian.kubejs.util.ListJS;
import dev.latvian.kubejs.util.UtilsJS;
import it.unimi.dsi.fastutil.chars.CharOpenHashSet;
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
import me.shedaniel.architectury.registry.Registry;
import net.minecraft.resources.ResourceLocation;
import org.embeddedt.modernfix.util.KubeUtil;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
@Mixin(TagEventJS.TagWrapper.class)
public class TagWrapperMixin<T> {
private static final CharOpenHashSet REGEX_SPECIAL_CHARS = new CharOpenHashSet(new char[] {
'.', '+', '*','?','^','$','(',')','[',']','{','}','|','\\', '/'
});
/**
* @author embeddedt
* @reason only iterate over the whole registry if a regex is given, otherwise use the given registry name as-is
*/
@Redirect(method = "add", at = @At(value = "INVOKE", target = "Ldev/latvian/kubejs/util/UtilsJS;parseRegex(Ljava/lang/Object;)Ljava/util/regex/Pattern;"), remap = false)
private Pattern skipRegex(Object o) {
String inputStr = (String)o;
boolean regexCharFound = false;
for(int i = 0; i < inputStr.length(); i++) {
if(REGEX_SPECIAL_CHARS.contains(inputStr.charAt(i))) {
System.out.println(inputStr);
regexCharFound = true;
break;
}
}
if(regexCharFound)
return UtilsJS.parseRegex(inputStr);
else
return null;
}
private String currentPatternStr = null;
@Inject(method = "add", at = @At(value = "INVOKE", target = "Lme/shedaniel/architectury/registry/Registry;getIds()Ljava/util/Set;", ordinal = 0), locals = LocalCapture.CAPTURE_FAILHARD, remap = false)
private void saveCurrentPattern(Object ids, CallbackInfoReturnable<TagEventJS.TagWrapper<T>> cir, Iterator<Object> iterator, Object o, String patternStr) {
currentPatternStr = patternStr;
}
@Redirect(method = "add", at = @At(value = "INVOKE", target = "Lme/shedaniel/architectury/registry/Registry;getIds()Ljava/util/Set;", ordinal = 0), remap = false)
private Set<ResourceLocation> getCachedIds(Registry<T> registryIn) {
if(currentPatternStr == null)
throw new AssertionError();
Set<ResourceLocation> cachedSet = KubeUtil.matchedIdsForRegex.get(currentPatternStr);
if(cachedSet == null) {
Pattern thePattern = UtilsJS.parseRegex(currentPatternStr);
ArrayList<ResourceLocation> locations = new ArrayList<>(registryIn.getIds());
cachedSet = locations.parallelStream()
.filter(rLoc -> thePattern.matcher(rLoc.toString()).find())
.collect(Collectors.toSet());
KubeUtil.matchedIdsForRegex.put(currentPatternStr, cachedSet);
}
return cachedSet;
}
/**
* @author embeddedt
* @reason we handle pattern-matching ourselves to build the cache
*/
@Redirect(method = "add", at = @At(value = "INVOKE", target = "Ljava/util/regex/Matcher;find()Z"), remap = false)
private boolean isMatchedStr(Matcher matcher) {
return true;
}
}

View File

@ -0,0 +1,20 @@
package org.embeddedt.modernfix.util;
import net.minecraft.resources.ResourceLocation;
import net.minecraftforge.event.AddReloadListenerEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import org.embeddedt.modernfix.ModernFix;
import java.util.HashMap;
import java.util.Set;
@Mod.EventBusSubscriber(modid = ModernFix.MODID)
public class KubeUtil {
public static final HashMap<String, Set<ResourceLocation>> matchedIdsForRegex = new HashMap<>();
@SubscribeEvent
public static void clearRegexCache(AddReloadListenerEvent event) {
matchedIdsForRegex.clear();
}
}

View File

@ -41,7 +41,9 @@
"perf.async_locator.TreasureMapForEmeraldsMixin",
"feature.measure_time.BootstrapMixin",
"feature.measure_time.SimpleReloadableResourceManagerMixin",
"feature.measure_time.ProfiledReloadInstanceMixin"
"feature.measure_time.ProfiledReloadInstanceMixin",
"perf.kubejs.TagIngredientJSMixin",
"perf.kubejs.TagWrapperMixin"
],
"client": [
"feature.measure_time.MinecraftMixin",