Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generator: improve performance of creating instances #2439

Merged
merged 2 commits into from
Jul 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@
import java.util.EnumSet;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import org.jdbi.v3.core.Jdbi;
import org.jdbi.v3.core.config.ConfigRegistry;
import org.jdbi.v3.core.extension.ExtensionMetadata;
import org.jdbi.v3.core.extension.Extensions;
import org.jdbi.v3.core.extension.HandleSupplier;
import org.jdbi.v3.core.internal.JdbiClassUtils;
import org.jdbi.v3.core.internal.JdbiClassUtils.MethodHandleHolder;
import org.jdbi.v3.core.internal.OnDemandExtensions;

import static java.lang.String.format;
Expand All @@ -33,6 +37,12 @@
*/
final class GeneratorSqlObjectFactory extends AbstractSqlObjectFactory implements OnDemandExtensions.Factory {

private static final Class<?>[] EXTENSION_TYPES = {ExtensionMetadata.class, HandleSupplier.class, ConfigRegistry.class};
private static final Class<?>[] ON_DEMAND_TYPES = {Jdbi.class};

private final ConcurrentMap<Class<?>, MethodHandleHolder<?>> attachedTypeCache = new ConcurrentHashMap<>();
private final ConcurrentMap<Class<?>, MethodHandleHolder<?>> onDemandTypeCache = new ConcurrentHashMap<>();

GeneratorSqlObjectFactory() {}

@Override
Expand All @@ -52,6 +62,7 @@ public Set<FactoryFlag> getFactoryFlags() {
* @param handleSupplier the Handle instance to attach this sql object to.
* @return the new sql object bound to this handle.
*/
@SuppressWarnings("unchecked")
@Override
public <E> E attach(Class<E> extensionType, HandleSupplier handleSupplier) {
if (!isConcrete(extensionType)) {
Expand All @@ -63,13 +74,8 @@ public <E> E attach(Class<E> extensionType, HandleSupplier handleSupplier) {
final ExtensionMetadata extensionMetaData = config.get(Extensions.class).findMetadata(extensionType, this);
final ConfigRegistry instanceConfig = extensionMetaData.createInstanceConfiguration(config);

try {
Class<?> klazz = Class.forName(getGeneratedClassName(extensionType));
return extensionType.cast(klazz.getConstructor(ExtensionMetadata.class, HandleSupplier.class, ConfigRegistry.class)
.newInstance(extensionMetaData, handleSupplier, instanceConfig));
} catch (ReflectiveOperationException | SecurityException e) {
throw new UnableToCreateSqlObjectException(e);
}
return (E) attachedTypeCache.computeIfAbsent(extensionType, GeneratorSqlObjectFactory::getGeneratedClass)
.invoke(handle -> handle.invokeExact(extensionMetaData, handleSupplier, instanceConfig));
}

@Override
Expand All @@ -78,20 +84,31 @@ public Optional<Object> onDemand(Jdbi jdbi, Class<?> extensionType, Class<?>...
return Optional.empty();
}

return Optional.of(onDemandTypeCache.computeIfAbsent(extensionType, GeneratorSqlObjectFactory::getOnDemandClass)
.invoke(handle -> handle.invokeExact(jdbi)));
}

private static MethodHandleHolder<?> getGeneratedClass(Class<?> extensionType) {
try {
return JdbiClassUtils.findConstructor(Class.forName(getGeneratedClassName(extensionType)), EXTENSION_TYPES);
} catch (Throwable t) {
throw new UnableToCreateSqlObjectException(t);
}
}

private static MethodHandleHolder<?> getOnDemandClass(Class<?> extensionType) {
try {
return Optional.of(Class.forName(getOnDemandClassName(extensionType))
.getConstructor(Jdbi.class)
.newInstance(jdbi));
} catch (ReflectiveOperationException | SecurityException | ExceptionInInitializerError e) {
throw new UnableToCreateSqlObjectException(e);
return JdbiClassUtils.findConstructor(Class.forName(getOnDemandClassName(extensionType)), ON_DEMAND_TYPES);
} catch (Throwable t) {
throw new UnableToCreateSqlObjectException(t);
}
}

private String getGeneratedClassName(Class<?> extensionType) {
private static String getGeneratedClassName(Class<?> extensionType) {
return extensionType.getPackage().getName() + "." + extensionType.getSimpleName() + "Impl";
}

private String getOnDemandClassName(Class<?> extensionType) {
private static String getOnDemandClassName(Class<?> extensionType) {
return getGeneratedClassName(extensionType) + "$OnDemand";
}
}