Mitigate excessive resource usage from Night Config
This commit is contained in:
parent
e1ff64785e
commit
bf74ab5a80
|
|
@ -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");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -31,6 +31,7 @@ import org.embeddedt.modernfix.api.constants.IntegrationConstants;
|
||||||
import org.embeddedt.modernfix.forge.classloading.FastAccessTransformerList;
|
import org.embeddedt.modernfix.forge.classloading.FastAccessTransformerList;
|
||||||
import org.embeddedt.modernfix.forge.classloading.ModernFixResourceFinder;
|
import org.embeddedt.modernfix.forge.classloading.ModernFixResourceFinder;
|
||||||
import org.embeddedt.modernfix.forge.config.NightConfigFixer;
|
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.init.ModernFixForge;
|
||||||
import org.embeddedt.modernfix.forge.packet.PacketHandler;
|
import org.embeddedt.modernfix.forge.packet.PacketHandler;
|
||||||
import org.embeddedt.modernfix.forge.spark.SparkLaunchProfiler;
|
import org.embeddedt.modernfix.forge.spark.SparkLaunchProfiler;
|
||||||
|
|
@ -192,6 +193,7 @@ public class ModernFixPlatformHooksImpl implements ModernFixPlatformHooks {
|
||||||
}
|
}
|
||||||
|
|
||||||
NightConfigFixer.monitorFileWatcher();
|
NightConfigFixer.monitorFileWatcher();
|
||||||
|
NightConfigWatchThrottler.throttle();
|
||||||
MixinExtrasBootstrap.init();
|
MixinExtrasBootstrap.init();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user