Add TagReadPathBenchmark

Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
TheMode 2022-03-26 15:26:46 +01:00
parent ac4d78acf1
commit 835b8ca89d
3 changed files with 56 additions and 9 deletions

View File

@ -0,0 +1,40 @@
package net.minestom.jmh.tag;
import net.minestom.server.tag.Tag;
import net.minestom.server.tag.TagHandler;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.infra.Blackhole;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
@Warmup(iterations = 5, time = 1000, timeUnit = TimeUnit.MILLISECONDS)
@Measurement(iterations = 10, time = 1000, timeUnit = TimeUnit.MILLISECONDS)
@Fork(3)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@State(Scope.Benchmark)
public class TagReadPathBenchmark {
@Param({"0", "1", "2", "3"})
public int scope;
TagHandler tagHandler;
Tag<String> tag;
@Setup
public void setup() {
this.tagHandler = TagHandler.newHandler();
List<String> path = new ArrayList<>(scope);
for (int i = 0; i < scope; i++) path.add("key" + i);
this.tag = Tag.String("key").path(path.toArray(String[]::new));
tagHandler.setTag(tag, "value");
}
@Benchmark
public void read(Blackhole blackhole) {
blackhole.consume(tagHandler.getTag(tag));
}
}

View File

@ -9,7 +9,6 @@ import org.jetbrains.annotations.Nullable;
import org.jglrxavpok.hephaistos.nbt.*;
import org.jglrxavpok.hephaistos.nbt.mutable.MutableNBTCompound;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.function.Supplier;
@ -37,14 +36,14 @@ public class Tag<T> {
final Function<?, ?> originalRead;
// Optional properties
final List<PathEntry> path;
final PathEntry[] path;
final UnaryOperator<T> copy;
final int listScope;
Tag(int index, String key,
Function<?, ?> originalRead,
Function<NBT, T> readFunction, Function<T, NBT> writeFunction,
Supplier<T> defaultValue, List<PathEntry> path, UnaryOperator<T> copy, int listScope) {
Supplier<T> defaultValue, PathEntry[] path, UnaryOperator<T> copy, int listScope) {
assert index == INDEX_MAP.get(key);
this.index = index;
this.key = key;
@ -150,7 +149,15 @@ public class Tag<T> {
@ApiStatus.Experimental
@Contract(value = "_ -> new", pure = true)
public Tag<T> path(@NotNull String @Nullable ... path) {
final List<PathEntry> entries = path != null ? Arrays.stream(path).map(s -> new PathEntry(s, INDEX_MAP.get(s))).toList() : null;
if (path == null || path.length == 0) {
return new Tag<>(index, key, originalRead,
readFunction, writeFunction, defaultValue, null, copy, listScope);
}
PathEntry[] entries = new PathEntry[path.length];
for (int i = 0; i < path.length; i++) {
var name = path[i];
entries[i] = new PathEntry(name, INDEX_MAP.get(name));
}
return new Tag<>(index, key, originalRead,
readFunction, writeFunction, defaultValue, entries, copy, listScope);
}

View File

@ -38,7 +38,7 @@ final class TagHandlerImpl implements TagHandler {
final var paths = tag.path;
TagHandlerImpl[] pathHandlers = null;
if (paths != null) {
pathHandlers = new TagHandlerImpl[paths.size()];
pathHandlers = new TagHandlerImpl[paths.length];
int in = 0;
for (var path : paths) {
final int pathIndex = path.index();
@ -70,14 +70,14 @@ final class TagHandlerImpl implements TagHandler {
empty = tagIndex >= entr.length || ArrayUtils.isEmpty(entr);
if (empty && i > 0) {
TagHandlerImpl parent = pathHandlers[i - 1];
parent.entries[paths.get(i).index()] = null;
parent.entries[paths[i].index()] = null;
}
}
if (empty) {
// Remove the root handler
local = this;
entries = localEntries;
tagIndex = paths.get(0).index();
tagIndex = paths[0].index();
}
}
}
@ -182,9 +182,9 @@ final class TagHandlerImpl implements TagHandler {
private static <T> T read(Entry<?>[] entries, Tag<T> tag) {
final int index = tag.index;
Entry<?> entry;
if (tag.path != null) {
final var paths = tag.path;
if (paths != null) {
// Must be a path-able entry
var paths = tag.path;
for (var path : paths) {
final int pathIndex = path.index();
if (pathIndex >= entries.length || (entry = entries[pathIndex]) == null) {