mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-19 19:59:42 +00:00
Add support for caching of configuration values. (#7608)
This commit is contained in:
1 parent
ba15737fe3
commit
65bc41281f
3 files changed
+176
-1
No files matched your search
@@ -42,6 +42,7 @@ import java.util.stream.Collectors;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
|
||||
@@ -90,6 +91,7 @@ public class ForgeConfigSpec extends UnmodifiableConfigWrapper<UnmodifiableConfi
|
||||
((FileConfig) config).save();
|
||||
}
|
||||
}
|
||||
this.afterReload();
|
||||
}
|
||||
|
||||
public boolean isCorrecting() {
|
||||
@@ -108,6 +110,22 @@ public class ForgeConfigSpec extends UnmodifiableConfigWrapper<UnmodifiableConfi
|
||||
return this.values;
|
||||
}
|
||||
|
||||
public void afterReload() {
|
||||
this.resetCaches(getValues().valueMap().values());
|
||||
}
|
||||
|
||||
private void resetCaches(final Iterable<Object> configValues) {
|
||||
configValues.forEach(value -> {
|
||||
if (value instanceof ConfigValue) {
|
||||
final ConfigValue<?> configValue = (ConfigValue<?>) value;
|
||||
configValue.clearCache();
|
||||
} else if (value instanceof Config) {
|
||||
final Config innerConfig = (Config) value;
|
||||
this.resetCaches(innerConfig.valueMap().values());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void save()
|
||||
{
|
||||
Preconditions.checkNotNull(childConfig, "Cannot save config value without assigned Config object present");
|
||||
@@ -690,10 +708,15 @@ public class ForgeConfigSpec extends UnmodifiableConfigWrapper<UnmodifiableConfi
|
||||
|
||||
public static class ConfigValue<T>
|
||||
{
|
||||
@VisibleForTesting
|
||||
static boolean USE_CACHES = true;
|
||||
|
||||
private final Builder parent;
|
||||
private final List<String> path;
|
||||
private final Supplier<T> defaultSupplier;
|
||||
|
||||
private T cachedValue = null;
|
||||
|
||||
private ForgeConfigSpec spec;
|
||||
|
||||
ConfigValue(Builder parent, List<String> path, Supplier<T> defaultSupplier)
|
||||
@@ -714,7 +737,13 @@ public class ForgeConfigSpec extends UnmodifiableConfigWrapper<UnmodifiableConfi
|
||||
Preconditions.checkNotNull(spec, "Cannot get config value before spec is built");
|
||||
if (spec.childConfig == null)
|
||||
return defaultSupplier.get();
|
||||
return getRaw(spec.childConfig, path, defaultSupplier);
|
||||
|
||||
if (USE_CACHES && cachedValue == null)
|
||||
cachedValue = getRaw(spec.childConfig, path, defaultSupplier);
|
||||
else if (!USE_CACHES)
|
||||
return getRaw(spec.childConfig, path, defaultSupplier);
|
||||
|
||||
return cachedValue;
|
||||
}
|
||||
|
||||
protected T getRaw(Config config, List<String> path, Supplier<T> defaultSupplier)
|
||||
@@ -739,6 +768,11 @@ public class ForgeConfigSpec extends UnmodifiableConfigWrapper<UnmodifiableConfi
|
||||
Preconditions.checkNotNull(spec, "Cannot set config value before spec is built");
|
||||
Preconditions.checkNotNull(spec.childConfig, "Cannot set config value without assigned Config object present");
|
||||
spec.childConfig.set(path, value);
|
||||
this.cachedValue = value;
|
||||
}
|
||||
|
||||
public void clearCache() {
|
||||
this.cachedValue = null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -156,6 +156,7 @@ public class ConfigFileTypeHandler {
|
||||
throw new ConfigLoadingException(modConfig, ex);
|
||||
}
|
||||
LOGGER.debug(CONFIG, "Config file {} changed, sending notifies", this.modConfig.getFileName());
|
||||
this.modConfig.getSpec().afterReload();
|
||||
this.modConfig.fireEvent(new ModConfig.Reloading(this.modConfig));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,140 @@
|
||||
package net.minecraftforge.common;
|
||||
|
||||
import com.electronwill.nightconfig.core.file.CommentedFileConfig;
|
||||
import com.electronwill.nightconfig.core.io.WritingMode;
|
||||
import com.google.common.base.Stopwatch;
|
||||
import com.google.common.collect.Lists;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TestRule;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.function.ToLongFunction;
|
||||
|
||||
public class ForgeConfigSpecTest
|
||||
{
|
||||
private final String TEST_CONFIG_PATH_TEMPLATE = "./tests/config/%s.toml";
|
||||
private final int TEST_SIZE = 10000;
|
||||
|
||||
@Test
|
||||
public void simpleSpeedTest() throws IOException
|
||||
{
|
||||
executeSpeedTest("test", "someDefaultValue", "simpleSpeedTest");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void objectSpeedTest() throws IOException
|
||||
{
|
||||
executeSpeedTest("test", Lists.newArrayList(Lists.newArrayList("someValue")), "objectSpeedTest");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deepKeySpeedTest() throws IOException
|
||||
{
|
||||
executeSpeedTest("test.test.test.test.test.test.test.test.test.test", "deepKeyValue", "deepKeySpeedTest");
|
||||
}
|
||||
|
||||
private <T> void executeSpeedTest(final String configKey, final T defaultKeyValue, final String testName) throws IOException
|
||||
{
|
||||
final ForgeConfigSpec.Builder builder = new ForgeConfigSpec.Builder();
|
||||
final ForgeConfigSpec.ConfigValue<T> simpleValue = builder.define(configKey, defaultKeyValue);
|
||||
final ForgeConfigSpec spec = builder.build();
|
||||
|
||||
final String configPath = String.format(TEST_CONFIG_PATH_TEMPLATE, testName);
|
||||
final File configFile = new File(configPath);
|
||||
configFile.getParentFile().mkdirs();
|
||||
configFile.createNewFile();
|
||||
final CommentedFileConfig configData = CommentedFileConfig.builder(configPath)
|
||||
.sync()
|
||||
.preserveInsertionOrder()
|
||||
.onFileNotFound((newfile, configFormat) -> {
|
||||
Files.createFile(newfile);
|
||||
return true;
|
||||
})
|
||||
.writingMode(WritingMode.REPLACE)
|
||||
.build();
|
||||
spec.setConfig(configData);
|
||||
|
||||
final List<TestResult> results = runTestHarness(defaultKeyValue, testName, simpleValue, spec, 3, 20);
|
||||
final double averageEnabled = results.stream().mapToLong(value -> value.enabledWatch.elapsed(TimeUnit.NANOSECONDS)).average().getAsDouble();
|
||||
final double averageDisabled = results.stream().mapToLong(value -> value.disabledWatch.elapsed(TimeUnit.NANOSECONDS)).average().getAsDouble();
|
||||
final double maxEnabledDeviation = results.stream().mapToLong(value -> value.enabledWatch.elapsed(TimeUnit.NANOSECONDS))
|
||||
.mapToDouble(e -> e - averageEnabled)
|
||||
.map(Math::abs)
|
||||
.max()
|
||||
.getAsDouble();
|
||||
final double maxDisabledDeviation = results.stream().mapToLong(value -> value.disabledWatch.elapsed(TimeUnit.NANOSECONDS))
|
||||
.mapToDouble(e -> e - averageDisabled)
|
||||
.map(Math::abs)
|
||||
.max()
|
||||
.getAsDouble();
|
||||
|
||||
System.out.printf("Computed test results for: %s:%n", testName);
|
||||
System.out.printf(" > Enabled: ~%10.2f ns. +- %10.2f ns.%n", averageEnabled, maxEnabledDeviation);
|
||||
System.out.printf(" > Disabled: ~%10.2f ns. +- %10.2f ns.%n", averageDisabled, maxDisabledDeviation);
|
||||
|
||||
configFile.delete();
|
||||
}
|
||||
|
||||
private <T> List<TestResult> runTestHarness(final T defaultKeyValue, final String testName, final ForgeConfigSpec.ConfigValue<T> simpleValue, final ForgeConfigSpec spec, final int warmupRounds, final int testRounds)
|
||||
{
|
||||
final List<TestResult> results = new ArrayList<>();
|
||||
|
||||
for (int i = 0; i < warmupRounds; i++)
|
||||
{
|
||||
//Run a bunch of warm up rounds and ignore the results.
|
||||
runTestOnce(defaultKeyValue, testName, simpleValue, spec);
|
||||
}
|
||||
|
||||
for (int i = 0; i < testRounds; i++)
|
||||
{
|
||||
results.add(runTestOnce(defaultKeyValue, testName, simpleValue, spec));
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
private <T> TestResult runTestOnce(final T defaultKeyValue, final String testName, final ForgeConfigSpec.ConfigValue<T> simpleValue, final ForgeConfigSpec spec)
|
||||
{
|
||||
spec.afterReload();
|
||||
ForgeConfigSpec.ConfigValue.USE_CACHES = false;
|
||||
final Stopwatch watchDisabled = Stopwatch.createStarted();
|
||||
for (int i = 0; i < TEST_SIZE; i++)
|
||||
{
|
||||
Assert.assertEquals(defaultKeyValue, simpleValue.get());
|
||||
}
|
||||
watchDisabled.stop();
|
||||
|
||||
spec.afterReload();
|
||||
ForgeConfigSpec.ConfigValue.USE_CACHES = true;
|
||||
final Stopwatch watchEnabled = Stopwatch.createStarted();
|
||||
for (int i = 0; i < TEST_SIZE; i++)
|
||||
{
|
||||
Assert.assertEquals(defaultKeyValue, simpleValue.get());
|
||||
}
|
||||
watchEnabled.stop();
|
||||
|
||||
final TestResult result = new TestResult(watchEnabled, watchDisabled);
|
||||
|
||||
System.out.printf("Test result for: %s (Disabled: %s vs. Enabled: %s)%n", testName, watchDisabled.toString(), watchEnabled.toString());
|
||||
return result;
|
||||
}
|
||||
|
||||
private final static class TestResult {
|
||||
|
||||
private final Stopwatch enabledWatch;
|
||||
private final Stopwatch disabledWatch;
|
||||
|
||||
public TestResult(final Stopwatch enabledWatch, final Stopwatch disabledWatch)
|
||||
{
|
||||
this.enabledWatch = enabledWatch;
|
||||
this.disabledWatch = disabledWatch;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user