Skip to content

Commit

Permalink
General code benchmarks and actually run benchmarks on Java 17 (not 8…
Browse files Browse the repository at this point in the history
… lol)
  • Loading branch information
P3ridot committed Jul 30, 2023
1 parent 81836d1 commit 663f35e
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@

/* JDK17 (I5-8600K OC 4.5 Ghz, 32GB RAM 3200Mhz, Windows 10)
Benchmark Mode Cnt Score Error Units
InstanceConstructionBenchmark.direct thrpt 10 1786513.8222208.503 ops/ms
InstanceConstructionBenchmark.injected thrpt 10 1793.49910.822 ops/ms
InstanceConstructionBenchmark.injectedFast thrpt 10 2871.33425.445 ops/ms
InstanceConstructionBenchmark.injectedStatic thrpt 10 2700.881 � 3.946 ops/ms
InstanceConstructionBenchmark.injectedStaticFast thrpt 10 2852.00521.373 ops/ms
InstanceConstructionBenchmark.reflection thrpt 10 104001.17081.311 ops/ms
InstanceConstructionBenchmark.direct thrpt 10 1789904.3781122.822 ops/ms
InstanceConstructionBenchmark.injected thrpt 10 2629.14966.627 ops/ms
InstanceConstructionBenchmark.injectedFast thrpt 10 2890.65035.665 ops/ms
InstanceConstructionBenchmark.injectedStatic thrpt 10 2681.387 � 3.025 ops/ms
InstanceConstructionBenchmark.injectedStaticFast thrpt 10 2890.723 8.385 ops/ms
InstanceConstructionBenchmark.reflection thrpt 10 104444.64088.579 ops/ms
*/
@Fork(value = 1)
@Warmup(iterations = 10, time = 2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@
import panda.utilities.ReflectionUtils;

/* JDK17 (I5-8600K OC 4.5 Ghz, 32GB RAM 3200Mhz, Windows 10)
Benchmark Mode Cnt Score Error Units
InvokeMethodBenchmark.direct thrpt 10 275095.4918486.067 ops/ms
InvokeMethodBenchmark.generatedInjected thrpt 10 259745.279219.899 ops/ms
InvokeMethodBenchmark.injected thrpt 10 40816.82627.053 ops/ms
InvokeMethodBenchmark.reflection thrpt 10 162744.758 101.912 ops/ms
Benchmark Mode Cnt Score Error Units
InvokeMethodBenchmark.direct thrpt 10 492235.407 988.297 ops/ms
InvokeMethodBenchmark.generatedInjected thrpt 10 432266.343 737.527 ops/ms
InvokeMethodBenchmark.injected thrpt 10 142187.855502.770 ops/ms
InvokeMethodBenchmark.reflection thrpt 10 189730.28222464.101 ops/ms
*/
@Fork(value = 1)
@Warmup(iterations = 10, time = 2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.lang.reflect.Method;

public class CodegenMethodInjectorFactory implements MethodInjectorFactory {
public final class CodegenMethodInjectorFactory implements MethodInjectorFactory {

@Override
public MethodInjector createMethodInjector(InjectorProcessor processor, Method method) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import net.bytebuddy.matcher.ElementMatchers;
import panda.utilities.ObjectUtils;

public final class GeneratedMethodInjector implements MethodInjector {
final class GeneratedMethodInjector implements MethodInjector {

private static final Object[] EMPTY = new Object[0];

Expand All @@ -50,7 +50,7 @@ public final class GeneratedMethodInjector implements MethodInjector {
@SuppressWarnings("unchecked")
@Override
public <T> T invoke(Object instance, Object... injectorArgs) throws Exception {
return (T) function.apply(instance, empty ? EMPTY : processor.fetchValues(cache, injectorArgs));
return (T) this.function.apply(instance, this.empty ? EMPTY : this.processor.fetchValues(this.cache, injectorArgs));
}

private static BiFunction<Object, Object[], Object> generate(Method method) throws Exception {
Expand All @@ -66,8 +66,6 @@ private static BiFunction<Object, Object[], Object> generate(Method method) thro
ByteBuddy byteBuddy = new ByteBuddy();
DynamicType.Unloaded<?> classPackage = byteBuddy
.makePackage(declaringClass.getPackage().getName())
.innerTypeOf(declaringClass)
.asMemberType()
.make();

Class<?> loaded = byteBuddy.subclass(Object.class)
Expand Down
24 changes: 22 additions & 2 deletions di/src/main/java/org/panda_lang/utilities/inject/ClassCache.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,48 @@
package org.panda_lang.utilities.inject;

import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.security.InvalidParameterException;
import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.panda_lang.utilities.inject.annotations.AutoConstruct;
import org.panda_lang.utilities.inject.annotations.Inject;
import panda.std.Pair;
import panda.utilities.ObjectUtils;

/**
* Utility class for caching class data (fields, methods, etc.) to improve performance.
*/
final class ClassCache {

private static final Map<Class<?>, Constructor<?>> CACHED_CONSTRUCTORS = new ConcurrentHashMap<>();

private static final Map<Class<?>, Field[]> CACHED_FIELDS = new ConcurrentHashMap<>();
private static final Map<Class<?>, Field[]> INJECTOR_CACHED_FIELDS = new ConcurrentHashMap<>();

private static final Map<Pair<Class<?>, Class<? extends Annotation>>, Method[]> CACHED_ANNOTATED_METHODS = new ConcurrentHashMap<>();

private ClassCache() { }
private ClassCache() {}

public static <T> Constructor<T> getConstructor(Class<T> clazz) {
return ObjectUtils.cast(CACHED_CONSTRUCTORS.computeIfAbsent(clazz, key -> {
Constructor<?>[] constructors = clazz.getDeclaredConstructors();
if (constructors.length != 1) {
throw new InvalidParameterException("Class has to contain one and only constructor");
}
Constructor<T> constructor = ObjectUtils.cast(constructors[0]);
constructor.setAccessible(true);
return constructor;
}));
}

/**
* Get all fields of the class.
* The result is cached.
*
* @param clazz class to get fields from
* @return array of fields
*/
Expand All @@ -35,6 +53,7 @@ public static Field[] getFields(Class<?> clazz) {
/**
* Get all fields of the class that are annotated with {@link Inject} or {@link AutoConstruct} and make them accessible.
* The result is cached.
*
* @param clazz class to get fields from
* @return array of fields
*/
Expand All @@ -60,7 +79,8 @@ private static Field[] getAllFields(Class<?> type) {
/**
* Get all methods of the class that are annotated with the specified annotation.
* The result is cached.
* @param clazz class to get methods from
*
* @param clazz class to get methods from
* @param annotation annotation to filter methods by
* @return array of methods
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,8 @@ public DefaultInjector(Resources resources) {
}

@Override
@SuppressWarnings("unchecked")
public <T> ConstructorInjector<T> forConstructor(Class<T> type) {
Constructor<?>[] constructors = type.getDeclaredConstructors();
if (constructors.length != 1) {
throw new InvalidParameterException("Class has to contain one and only constructor");
}
return new ConstructorInjector<>(this.processor, (Constructor<T>) constructors[0]);
return new ConstructorInjector<>(this.processor, ClassCache.getConstructor(type));
}

@Override
Expand Down

0 comments on commit 663f35e

Please sign in to comment.