mirror of https://github.com/Minestom/Minestom.git
Add TagReadPathBenchmark
Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
parent
ac4d78acf1
commit
835b8ca89d
|
@ -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));
|
||||||
|
}
|
||||||
|
}
|
|
@ -9,7 +9,6 @@ import org.jetbrains.annotations.Nullable;
|
||||||
import org.jglrxavpok.hephaistos.nbt.*;
|
import org.jglrxavpok.hephaistos.nbt.*;
|
||||||
import org.jglrxavpok.hephaistos.nbt.mutable.MutableNBTCompound;
|
import org.jglrxavpok.hephaistos.nbt.mutable.MutableNBTCompound;
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
|
@ -37,14 +36,14 @@ public class Tag<T> {
|
||||||
|
|
||||||
final Function<?, ?> originalRead;
|
final Function<?, ?> originalRead;
|
||||||
// Optional properties
|
// Optional properties
|
||||||
final List<PathEntry> path;
|
final PathEntry[] path;
|
||||||
final UnaryOperator<T> copy;
|
final UnaryOperator<T> copy;
|
||||||
final int listScope;
|
final int listScope;
|
||||||
|
|
||||||
Tag(int index, String key,
|
Tag(int index, String key,
|
||||||
Function<?, ?> originalRead,
|
Function<?, ?> originalRead,
|
||||||
Function<NBT, T> readFunction, Function<T, NBT> writeFunction,
|
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);
|
assert index == INDEX_MAP.get(key);
|
||||||
this.index = index;
|
this.index = index;
|
||||||
this.key = key;
|
this.key = key;
|
||||||
|
@ -150,7 +149,15 @@ public class Tag<T> {
|
||||||
@ApiStatus.Experimental
|
@ApiStatus.Experimental
|
||||||
@Contract(value = "_ -> new", pure = true)
|
@Contract(value = "_ -> new", pure = true)
|
||||||
public Tag<T> path(@NotNull String @Nullable ... path) {
|
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,
|
return new Tag<>(index, key, originalRead,
|
||||||
readFunction, writeFunction, defaultValue, entries, copy, listScope);
|
readFunction, writeFunction, defaultValue, entries, copy, listScope);
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,7 +38,7 @@ final class TagHandlerImpl implements TagHandler {
|
||||||
final var paths = tag.path;
|
final var paths = tag.path;
|
||||||
TagHandlerImpl[] pathHandlers = null;
|
TagHandlerImpl[] pathHandlers = null;
|
||||||
if (paths != null) {
|
if (paths != null) {
|
||||||
pathHandlers = new TagHandlerImpl[paths.size()];
|
pathHandlers = new TagHandlerImpl[paths.length];
|
||||||
int in = 0;
|
int in = 0;
|
||||||
for (var path : paths) {
|
for (var path : paths) {
|
||||||
final int pathIndex = path.index();
|
final int pathIndex = path.index();
|
||||||
|
@ -70,14 +70,14 @@ final class TagHandlerImpl implements TagHandler {
|
||||||
empty = tagIndex >= entr.length || ArrayUtils.isEmpty(entr);
|
empty = tagIndex >= entr.length || ArrayUtils.isEmpty(entr);
|
||||||
if (empty && i > 0) {
|
if (empty && i > 0) {
|
||||||
TagHandlerImpl parent = pathHandlers[i - 1];
|
TagHandlerImpl parent = pathHandlers[i - 1];
|
||||||
parent.entries[paths.get(i).index()] = null;
|
parent.entries[paths[i].index()] = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (empty) {
|
if (empty) {
|
||||||
// Remove the root handler
|
// Remove the root handler
|
||||||
local = this;
|
local = this;
|
||||||
entries = localEntries;
|
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) {
|
private static <T> T read(Entry<?>[] entries, Tag<T> tag) {
|
||||||
final int index = tag.index;
|
final int index = tag.index;
|
||||||
Entry<?> entry;
|
Entry<?> entry;
|
||||||
if (tag.path != null) {
|
final var paths = tag.path;
|
||||||
|
if (paths != null) {
|
||||||
// Must be a path-able entry
|
// Must be a path-able entry
|
||||||
var paths = tag.path;
|
|
||||||
for (var path : paths) {
|
for (var path : paths) {
|
||||||
final int pathIndex = path.index();
|
final int pathIndex = path.index();
|
||||||
if (pathIndex >= entries.length || (entry = entries[pathIndex]) == null) {
|
if (pathIndex >= entries.length || (entry = entries[pathIndex]) == null) {
|
||||||
|
|
Loading…
Reference in New Issue