Merge remote-tracking branch 'origin/1.16' into 1.18

This commit is contained in:
embeddedt 2024-03-20 14:53:06 -04:00
commit 94d37544b2
No known key found for this signature in database
GPG Key ID: A69433EC199B5613
2 changed files with 58 additions and 0 deletions

View File

@ -0,0 +1,56 @@
package org.embeddedt.modernfix.forge.config;
import com.electronwill.nightconfig.core.file.FileWatcher;
import com.google.common.collect.ForwardingCollection;
import com.google.common.collect.ForwardingMap;
import net.minecraftforge.fml.common.ObfuscationReflectionHelper;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;
/**
* Throttle NightConfig's file watching. There are reports of this consuming excessive CPU time
* (<a href="https://github.com/TheElectronWill/night-config/pull/144">example</a>) and the spammed iterator calls
* end up being 10% of allocations when testing in a dev environment.
*/
public class NightConfigWatchThrottler {
private static final long DELAY = TimeUnit.MILLISECONDS.toNanos(1000);
@SuppressWarnings("rawtypes")
public static void throttle() {
Map watchedDirs = ObfuscationReflectionHelper.getPrivateValue(FileWatcher.class, FileWatcher.defaultInstance(), "watchedDirs");
ObfuscationReflectionHelper.setPrivateValue(FileWatcher.class, FileWatcher.defaultInstance(), new ForwardingMap() {
@Override
protected Map delegate() {
return watchedDirs;
}
private Collection cachedValues;
@Override
public Collection values() {
if(cachedValues == null) {
Collection values = super.values();
cachedValues = new ForwardingCollection() {
@Override
protected Collection delegate() {
return values;
}
@Override
public Iterator iterator() {
// iterator() is called at the beginning of each iteration of the watch loop,
// so it is a good spot to inject the delay.
LockSupport.parkNanos(DELAY);
return super.iterator();
}
};
}
return cachedValues;
}
}, "watchedDirs");
}
}

View File

@ -27,6 +27,7 @@ import org.embeddedt.modernfix.api.constants.IntegrationConstants;
import org.embeddedt.modernfix.forge.classloading.ATInjector;
import org.embeddedt.modernfix.forge.classloading.FastAccessTransformerList;
import org.embeddedt.modernfix.forge.config.NightConfigFixer;
import org.embeddedt.modernfix.forge.config.NightConfigWatchThrottler;
import org.embeddedt.modernfix.forge.init.ModernFixForge;
import org.embeddedt.modernfix.forge.packet.PacketHandler;
import org.embeddedt.modernfix.platform.ModernFixPlatformHooks;
@ -158,6 +159,7 @@ public class ModernFixPlatformHooksImpl implements ModernFixPlatformHooks {
}
NightConfigFixer.monitorFileWatcher();
NightConfigWatchThrottler.throttle();
}
public void applyASMTransformers(String mixinClassName, ClassNode targetClass) {