reduce overhead of profiler when not profiling

This commit is contained in:
dfsek
2022-05-26 12:51:18 -07:00
parent 5275c40c6a
commit 3ec15960cf
6 changed files with 121 additions and 148 deletions
@@ -18,7 +18,6 @@ import com.dfsek.terra.addons.chunkgenerator.generation.math.samplers.Sampler3D;
import com.dfsek.terra.addons.chunkgenerator.generation.math.samplers.SamplerProvider; import com.dfsek.terra.addons.chunkgenerator.generation.math.samplers.SamplerProvider;
import com.dfsek.terra.api.Platform; import com.dfsek.terra.api.Platform;
import com.dfsek.terra.api.block.state.BlockState; import com.dfsek.terra.api.block.state.BlockState;
import com.dfsek.terra.api.profiler.ProfileFrame;
import com.dfsek.terra.api.world.biome.Biome; import com.dfsek.terra.api.world.biome.Biome;
import com.dfsek.terra.api.world.biome.generation.BiomeProvider; import com.dfsek.terra.api.world.biome.generation.BiomeProvider;
import com.dfsek.terra.api.world.chunk.generation.ChunkGenerator; import com.dfsek.terra.api.world.chunk.generation.ChunkGenerator;
@@ -51,7 +50,7 @@ public class NoiseChunkGenerator3D implements ChunkGenerator {
public void generateChunkData(@NotNull ProtoChunk chunk, @NotNull WorldProperties world, public void generateChunkData(@NotNull ProtoChunk chunk, @NotNull WorldProperties world,
@NotNull BiomeProvider biomeProvider, @NotNull BiomeProvider biomeProvider,
int chunkX, int chunkZ) { int chunkX, int chunkZ) {
try(ProfileFrame ignore = platform.getProfiler().profile("chunk_base_3d")) { platform.getProfiler().push("chunk_base_3d");
int xOrig = (chunkX << 4); int xOrig = (chunkX << 4);
int zOrig = (chunkZ << 4); int zOrig = (chunkZ << 4);
@@ -100,7 +99,7 @@ public class NoiseChunkGenerator3D implements ChunkGenerator {
} }
} }
} }
} platform.getProfiler().pop("chunk_base_3d");
} }
@Override @Override
@@ -12,7 +12,6 @@ import java.util.Random;
import com.dfsek.terra.addons.generation.feature.config.BiomeFeatures; import com.dfsek.terra.addons.generation.feature.config.BiomeFeatures;
import com.dfsek.terra.api.Platform; import com.dfsek.terra.api.Platform;
import com.dfsek.terra.api.profiler.ProfileFrame;
import com.dfsek.terra.api.registry.key.StringIdentifiable; import com.dfsek.terra.api.registry.key.StringIdentifiable;
import com.dfsek.terra.api.util.Rotation; import com.dfsek.terra.api.util.Rotation;
import com.dfsek.terra.api.util.vector.Vector3Int; import com.dfsek.terra.api.util.vector.Vector3Int;
@@ -27,15 +26,18 @@ public class FeatureGenerationStage implements GenerationStage, StringIdentifiab
private final String id; private final String id;
private final String profile;
public FeatureGenerationStage(Platform platform, String id) { public FeatureGenerationStage(Platform platform, String id) {
this.platform = platform; this.platform = platform;
this.id = id; this.id = id;
this.profile = "feature_stage:" + id;
} }
@Override @Override
@SuppressWarnings("try") @SuppressWarnings("try")
public void populate(ProtoWorld world) { public void populate(ProtoWorld world) {
try(ProfileFrame ignore = platform.getProfiler().profile("feature_stage:" + id)) { platform.getProfiler().push(profile);
int cx = world.centerChunkX() << 4; int cx = world.centerChunkX() << 4;
int cz = world.centerChunkZ() << 4; int cz = world.centerChunkZ() << 4;
long seed = world.getSeed(); long seed = world.getSeed();
@@ -53,7 +55,7 @@ public class FeatureGenerationStage implements GenerationStage, StringIdentifiab
.getFeatures() .getFeatures()
.getOrDefault(this, Collections.emptyList()) .getOrDefault(this, Collections.emptyList())
.forEach(feature -> { .forEach(feature -> {
try(ProfileFrame ignored = platform.getProfiler().profile(feature.getID())) { platform.getProfiler().push(feature.getID());
if(feature.getDistributor().matches(tx, tz, seed)) { if(feature.getDistributor().matches(tx, tz, seed)) {
feature.getLocator() feature.getLocator()
.getSuitableCoordinates(column) .getSuitableCoordinates(column)
@@ -65,11 +67,11 @@ public class FeatureGenerationStage implements GenerationStage, StringIdentifiab
Rotation.NONE) Rotation.NONE)
); );
} }
} platform.getProfiler().pop(feature.getID());
}); });
} }
} }
} platform.getProfiler().pop(profile);
} }
@Override @Override
@@ -39,7 +39,6 @@ import com.dfsek.terra.addons.terrascript.script.builders.UnaryNumberFunctionBui
import com.dfsek.terra.addons.terrascript.script.builders.UnaryStringFunctionBuilder; import com.dfsek.terra.addons.terrascript.script.builders.UnaryStringFunctionBuilder;
import com.dfsek.terra.addons.terrascript.script.builders.ZeroArgFunctionBuilder; import com.dfsek.terra.addons.terrascript.script.builders.ZeroArgFunctionBuilder;
import com.dfsek.terra.api.Platform; import com.dfsek.terra.api.Platform;
import com.dfsek.terra.api.profiler.ProfileFrame;
import com.dfsek.terra.api.registry.Registry; import com.dfsek.terra.api.registry.Registry;
import com.dfsek.terra.api.registry.key.Keyed; import com.dfsek.terra.api.registry.key.Keyed;
import com.dfsek.terra.api.registry.key.RegistryKey; import com.dfsek.terra.api.registry.key.RegistryKey;
@@ -54,6 +53,8 @@ public class StructureScript implements Structure, Keyed<StructureScript> {
private static final Logger LOGGER = LoggerFactory.getLogger(StructureScript.class); private static final Logger LOGGER = LoggerFactory.getLogger(StructureScript.class);
private final Block block; private final Block block;
private final RegistryKey id; private final RegistryKey id;
private final String profile;
private final Platform platform; private final Platform platform;
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
@@ -67,6 +68,7 @@ public class StructureScript implements Structure, Keyed<StructureScript> {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
this.id = id; this.id = id;
this.profile = "terrascript_direct:" + id;
//noinspection unchecked //noinspection unchecked
functionRegistry.forEach((key, function) -> parser.registerFunction(key.getID(), function)); // Register registry functions. functionRegistry.forEach((key, function) -> parser.registerFunction(key.getID(), function)); // Register registry functions.
@@ -129,15 +131,17 @@ public class StructureScript implements Structure, Keyed<StructureScript> {
@Override @Override
@SuppressWarnings("try") @SuppressWarnings("try")
public boolean generate(Vector3Int location, WritableWorld world, Random random, Rotation rotation) { public boolean generate(Vector3Int location, WritableWorld world, Random random, Rotation rotation) {
try(ProfileFrame ignore = platform.getProfiler().profile("terrascript_direct:" + id)) { platform.getProfiler().push(profile);
return applyBlock(new TerraImplementationArguments(location, rotation, random, world, 0)); boolean result = applyBlock(new TerraImplementationArguments(location, rotation, random, world, 0));
} platform.getProfiler().pop(profile);
return result;
} }
public boolean generate(Vector3Int location, WritableWorld world, Random random, Rotation rotation, int recursions) { public boolean generate(Vector3Int location, WritableWorld world, Random random, Rotation rotation, int recursions) {
try(ProfileFrame ignore = platform.getProfiler().profile("terrascript_direct:" + id)) { platform.getProfiler().push(profile);
return applyBlock(new TerraImplementationArguments(location, rotation, random, world, recursions)); boolean result = applyBlock(new TerraImplementationArguments(location, rotation, random, world, recursions));
} platform.getProfiler().pop(profile);
return result;
} }
private boolean applyBlock(TerraImplementationArguments arguments) { private boolean applyBlock(TerraImplementationArguments arguments) {
@@ -1,21 +0,0 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the common/api directory.
*/
package com.dfsek.terra.api.profiler;
public class ProfileFrame implements AutoCloseable {
private final Runnable action;
public ProfileFrame(Runnable action) {
this.action = action;
}
@Override
public void close() {
action.run();
}
}
@@ -36,21 +36,6 @@ public interface Profiler {
*/ */
void stop(); void stop();
/**
* Return a {@link AutoCloseable} implementation that
* may be used in a try-with-resources statement for
* more intuitive profiling, with auto-push/pop.
*
* @param frame ID of frame.
*
* @return {@link AutoCloseable} implementation for use
* in try-with-resources.
*/
default ProfileFrame profile(String frame) {
push(frame);
return new ProfileFrame(() -> pop(frame));
}
/** /**
* Clear the profiler data. * Clear the profiler data.
*/ */
@@ -51,18 +51,21 @@ public class ProfilerImpl implements Profiler {
@Override @Override
public void push(String frame) { public void push(String frame) {
if(running) {
STACK_SIZE.get().increment(); STACK_SIZE.get().increment();
if(running && SAFE.get()) { if(SAFE.get()) {
Stack<Frame> stack = THREAD_STACK.get(); Stack<Frame> stack = THREAD_STACK.get();
stack.push(new Frame(stack.isEmpty() ? frame : stack.peek().getId() + "." + frame)); stack.push(new Frame(stack.isEmpty() ? frame : stack.peek().getId() + "." + frame));
} else SAFE.set(false); } else SAFE.set(false);
} else SAFE.set(false);
} }
@Override @Override
public void pop(String frame) { public void pop(String frame) {
if(running) {
MutableInteger size = STACK_SIZE.get(); MutableInteger size = STACK_SIZE.get();
size.decrement(); size.decrement();
if(running && SAFE.get()) { if(SAFE.get()) {
long time = System.nanoTime(); long time = System.nanoTime();
Stack<Frame> stack = THREAD_STACK.get(); Stack<Frame> stack = THREAD_STACK.get();
@@ -84,6 +87,7 @@ public class ProfilerImpl implements Profiler {
} }
if(size.get() == 0) SAFE.set(true); if(size.get() == 0) SAFE.set(true);
} }
}
@Override @Override
public void start() { public void start() {