mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2025-07-18 10:32:30 +00:00
Slightly improve performance of NoiseFunction2
This commit is contained in:
parent
e43d814169
commit
c96127fde7
@ -18,7 +18,6 @@ import java.io.IOException;
|
||||
import java.net.URISyntaxException;
|
||||
import java.time.Duration;
|
||||
import java.util.EnumSet;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
@ -29,7 +28,6 @@ public final class ConfigUtil {
|
||||
public static boolean debug;
|
||||
public static long dataSave; // Period of population data saving, in ticks.
|
||||
public static boolean masterDisableCaves;
|
||||
public static int cacheSize;
|
||||
|
||||
public static void loadConfig(JavaPlugin main) {
|
||||
main.saveDefaultConfig();
|
||||
@ -38,7 +36,6 @@ public final class ConfigUtil {
|
||||
LangUtil.load(config.getString("language", "en_us"), main);
|
||||
|
||||
debug = config.getBoolean("debug", false);
|
||||
cacheSize = config.getInt("cache-size", 3);
|
||||
dataSave = Duration.parse(Objects.requireNonNull(config.getString("data-save", "PT6M"))).toMillis() / 20L;
|
||||
masterDisableCaves = config.getBoolean("master-disable.caves", false);
|
||||
|
||||
|
@ -1,6 +1,5 @@
|
||||
package com.dfsek.terra.math;
|
||||
|
||||
import com.dfsek.terra.config.base.ConfigUtil;
|
||||
import com.dfsek.terra.generation.config.NoiseBuilder;
|
||||
import org.polydev.gaea.math.FastNoiseLite;
|
||||
import parsii.eval.Expression;
|
||||
@ -8,7 +7,6 @@ import parsii.eval.Expression;
|
||||
import java.util.List;
|
||||
|
||||
public class NoiseFunction2 implements NoiseFunction {
|
||||
private final Cache cache = new Cache();
|
||||
private final FastNoiseLite gen;
|
||||
|
||||
public NoiseFunction2(long seed, NoiseBuilder builder) {
|
||||
@ -22,32 +20,11 @@ public class NoiseFunction2 implements NoiseFunction {
|
||||
|
||||
@Override
|
||||
public double eval(List<Expression> list) {
|
||||
return cache.get(list.get(0).evaluate(), list.get(1).evaluate());
|
||||
return gen.getNoise(list.get(0).evaluate(), list.get(1).evaluate());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isNaturalFunction() {
|
||||
return true;
|
||||
}
|
||||
|
||||
private final class Cache {
|
||||
private final double[] cacheX = new double[ConfigUtil.cacheSize];
|
||||
private final double[] cacheZ = new double[ConfigUtil.cacheSize];
|
||||
private final double[] cacheValues = new double[ConfigUtil.cacheSize];
|
||||
|
||||
public double get(double x, double z) {
|
||||
for(int i = 0; i < cacheX.length; i++) {
|
||||
if(cacheX[i] == x && cacheZ[i] == z) return cacheValues[i];
|
||||
}
|
||||
cacheX[0] = x;
|
||||
cacheZ[0] = z;
|
||||
cacheValues[0] = gen.getNoise(x, z);
|
||||
for(int i = 0; i < cacheX.length - 1; i++) {
|
||||
cacheX[i + 1] = cacheX[i];
|
||||
cacheZ[i + 1] = cacheZ[i];
|
||||
cacheValues[i + 1] = cacheValues[i];
|
||||
}
|
||||
return cacheValues[0];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -3,6 +3,5 @@ data-save: PT6M
|
||||
language: "en_us"
|
||||
fail-type: SHUTDOWN
|
||||
dump-default: true
|
||||
cache-size: 8
|
||||
master-disable:
|
||||
caves: false
|
28
src/test/java/NoiseTest.java
Normal file
28
src/test/java/NoiseTest.java
Normal file
@ -0,0 +1,28 @@
|
||||
import com.dfsek.terra.generation.config.NoiseBuilder;
|
||||
import com.dfsek.terra.math.NoiseFunction2;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import parsii.eval.Expression;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class NoiseTest {
|
||||
@Test
|
||||
public void noise() {
|
||||
NoiseFunction2 noiseFunction = new NoiseFunction2(12345, new NoiseBuilder());
|
||||
|
||||
for(int i = 0; i < 10; i++) {
|
||||
long l = System.nanoTime();
|
||||
for(int j = 0; j < 1000000; j++) {
|
||||
noiseFunction.eval(Arrays.asList(get(j), get(i)));
|
||||
noiseFunction.eval(Arrays.asList(get(j), get(i)));
|
||||
noiseFunction.eval(Arrays.asList(get(j), get(i)));
|
||||
}
|
||||
double n = System.nanoTime() - l;
|
||||
System.out.println(n / 1000000 + "ms");
|
||||
}
|
||||
}
|
||||
|
||||
private Expression get(double val) {
|
||||
return () -> val;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user