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

ArC: performance optimizations related to client proxy invocations #36626

Merged
merged 3 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -201,8 +201,14 @@ public List<Resource> generateResources(ReflectionRegistration reflectionRegistr
beanGenerator.precomputeGeneratedName(bean);
}

CustomAlterableContextsGenerator alterableContextsGenerator = new CustomAlterableContextsGenerator(generateSources);
List<CustomAlterableContextInfo> alterableContexts = customAlterableContexts.getRegistered();

// Set of normal scopes for which the client proxy delegate can be optimized
Set<DotName> singleContextNormalScopes = findSingleContextNormalScopes();

ClientProxyGenerator clientProxyGenerator = new ClientProxyGenerator(applicationClassPredicate, generateSources,
allowMocking, refReg, existingClasses);
allowMocking, refReg, existingClasses, singleContextNormalScopes);

InterceptorGenerator interceptorGenerator = new InterceptorGenerator(annotationLiterals, applicationClassPredicate,
privateMembers, generateSources, refReg, existingClasses, beanToGeneratedName,
Expand Down Expand Up @@ -233,15 +239,12 @@ public List<Resource> generateResources(ReflectionRegistration reflectionRegistr
}

ContextInstancesGenerator contextInstancesGenerator = new ContextInstancesGenerator(generateSources,
reflectionRegistration, beanDeployment, scopeToGeneratedName);
refReg, beanDeployment, scopeToGeneratedName);
if (optimizeContexts) {
contextInstancesGenerator.precomputeGeneratedName(BuiltinScope.APPLICATION.getName());
contextInstancesGenerator.precomputeGeneratedName(BuiltinScope.REQUEST.getName());
}

CustomAlterableContextsGenerator alterableContextsGenerator = new CustomAlterableContextsGenerator(generateSources);
List<CustomAlterableContextInfo> alterableContexts = customAlterableContexts.getRegistered();

List<Resource> resources = new ArrayList<>();

if (executor != null) {
Expand Down Expand Up @@ -509,6 +512,34 @@ public Predicate<DotName> getInjectionPointAnnotationsPredicate() {
return injectionPointAnnotationsPredicate;
}

private Set<DotName> findSingleContextNormalScopes() {
Set<DotName> ret = new HashSet<>();
Set<DotName> builtinScopes = Set.of(BuiltinScope.REQUEST.getName());
Set<DotName> customScopes = beanDeployment.getCustomContexts().keySet().stream().filter(ScopeInfo::isNormal)
.map(ScopeInfo::getDotName)
.collect(Collectors.toSet());
Set<DotName> alterableScopes = customAlterableContexts.getRegistered().stream()
.filter(cac -> Boolean.TRUE.equals(cac.isNormal)).map(cac -> cac.scopeAnnotation)
.map(DotName::createSimple)
.collect(Collectors.toSet());
for (DotName s : builtinScopes) {
if (!customScopes.contains(s) && !alterableScopes.contains(s)) {
ret.add(s);
}
}
for (DotName s : customScopes) {
if (!builtinScopes.contains(s) && !alterableScopes.contains(s)) {
ret.add(s);
}
}
for (DotName s : alterableScopes) {
if (!builtinScopes.contains(s) && !customScopes.contains(s)) {
ret.add(s);
}
}
return ret;
}

public static class Builder {

String name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,17 @@ public class ClientProxyGenerator extends AbstractGenerator {
private final Predicate<DotName> applicationClassPredicate;
private final boolean mockable;
private final Set<String> existingClasses;
// We optimize the access to the delegate if a single context is registered for a given scope
private final Set<DotName> singleContextNormalScopes;

public ClientProxyGenerator(Predicate<DotName> applicationClassPredicate, boolean generateSources, boolean mockable,
ReflectionRegistration reflectionRegistration, Set<String> existingClasses) {
ReflectionRegistration reflectionRegistration, Set<String> existingClasses,
Set<DotName> singleContextNormalScopes) {
super(generateSources, reflectionRegistration);
this.applicationClassPredicate = applicationClassPredicate;
this.mockable = mockable;
this.existingClasses = existingClasses;
this.singleContextNormalScopes = singleContextNormalScopes;
}

/**
Expand Down Expand Up @@ -136,8 +140,9 @@ Collection<Resource> generate(BeanInfo bean, String beanClassName,
clientProxy.getFieldCreator(MOCK_FIELD, providerType.descriptorName()).setModifiers(ACC_PRIVATE | ACC_VOLATILE);
}
FieldCreator contextField = null;
if (BuiltinScope.APPLICATION.is(bean.getScope())) {
// It is safe to store the application context instance on the proxy
if (BuiltinScope.APPLICATION.is(bean.getScope())
|| singleContextNormalScopes.contains(bean.getScope().getDotName())) {
// It is safe to store the context instance on the proxy
contextField = clientProxy.getFieldCreator(CONTEXT_FIELD, InjectableContext.class)
.setModifiers(ACC_PRIVATE | ACC_FINAL);
}
Expand Down Expand Up @@ -275,11 +280,14 @@ void createConstructor(ClassCreator clientProxy, String superClasName, FieldDesc
beanIdentifierHandle);
creator.writeInstanceField(beanField, creator.getThis(), beanHandle);
if (contextField != null) {
creator.writeInstanceField(contextField, creator.getThis(), creator.invokeInterfaceMethod(
MethodDescriptors.ARC_CONTAINER_GET_ACTIVE_CONTEXT,
// At this point we can be sure there's only one context implementation available
ResultHandle contextList = creator.invokeInterfaceMethod(
MethodDescriptors.ARC_CONTAINER_GET_CONTEXTS,
containerHandle, creator
.invokeInterfaceMethod(MethodDescriptor.ofMethod(InjectableBean.class, "getScope", Class.class),
beanHandle)));
beanHandle));
creator.writeInstanceField(contextField, creator.getThis(),
creator.invokeInterfaceMethod(MethodDescriptors.LIST_GET, contextList, creator.load(0)));
}
creator.returnValue(null);
}
Expand All @@ -305,6 +313,12 @@ void implementDelegate(ClassCreator clientProxy, ProviderType providerType, Fiel
FieldDescriptor.of(clientProxy.getClassName(), CONTEXT_FIELD, InjectableContext.class),
creator.getThis()),
beanHandle));
} else if (singleContextNormalScopes.contains(bean.getScope().getDotName())) {
creator.returnValue(creator.invokeStaticMethod(MethodDescriptors.CLIENT_PROXIES_GET_SINGLE_CONTEXT_DELEGATE,
creator.readInstanceField(
FieldDescriptor.of(clientProxy.getClassName(), CONTEXT_FIELD, InjectableContext.class),
creator.getThis()),
beanHandle));
} else {
creator.returnValue(creator.invokeStaticMethod(MethodDescriptors.CLIENT_PROXIES_GET_DELEGATE,
beanHandle));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.quarkus.arc.processor;

import java.lang.annotation.Annotation;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -19,11 +20,12 @@ public class CustomAlterableContexts {
this.applicationClassPredicate = applicationClassPredicate;
}

public CustomAlterableContextInfo add(Class<? extends AlterableContext> contextClass, Boolean isNormal) {
public CustomAlterableContextInfo add(Class<? extends AlterableContext> contextClass, Boolean isNormal,
Class<? extends Annotation> scopeAnnotation) {
String generatedName = contextClass.getName() + "_InjectableContext";
boolean isApplicationClass = applicationClassPredicate.test(DotName.createSimple(contextClass));
CustomAlterableContextInfo result = new CustomAlterableContextInfo(contextClass, isNormal, generatedName,
isApplicationClass);
isApplicationClass, scopeAnnotation);
registered.add(result);
return result;
}
Expand Down Expand Up @@ -52,13 +54,15 @@ public static class CustomAlterableContextInfo {
public final Boolean isNormal;
public final String generatedName;
public final boolean isApplicationClass;
public final Class<? extends Annotation> scopeAnnotation;

CustomAlterableContextInfo(Class<? extends AlterableContext> contextClass, Boolean isNormal,
String generatedName, boolean isApplicationClass) {
String generatedName, boolean isApplicationClass, Class<? extends Annotation> scopeAnnotation) {
this.contextClass = contextClass;
this.isNormal = isNormal;
this.generatedName = generatedName;
this.isApplicationClass = isApplicationClass;
this.scopeAnnotation = scopeAnnotation;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,9 @@ public final class MethodDescriptors {
public static final MethodDescriptor ARC_CONTAINER_GET_ACTIVE_CONTEXT = MethodDescriptor.ofMethod(ArcContainer.class,
"getActiveContext", InjectableContext.class, Class.class);

public static final MethodDescriptor ARC_CONTAINER_GET_CONTEXTS = MethodDescriptor.ofMethod(ArcContainer.class,
"getContexts", List.class, Class.class);

public static final MethodDescriptor CONTEXT_GET = MethodDescriptor.ofMethod(Context.class, "get", Object.class,
Contextual.class,
CreationalContext.class);
Expand Down Expand Up @@ -273,6 +276,10 @@ public final class MethodDescriptors {
public static final MethodDescriptor CLIENT_PROXIES_GET_APP_SCOPED_DELEGATE = MethodDescriptor.ofMethod(ClientProxies.class,
"getApplicationScopedDelegate", Object.class, InjectableContext.class, InjectableBean.class);

public static final MethodDescriptor CLIENT_PROXIES_GET_SINGLE_CONTEXT_DELEGATE = MethodDescriptor.ofMethod(
ClientProxies.class,
"getSingleContextDelegate", Object.class, InjectableContext.class, InjectableBean.class);

public static final MethodDescriptor CLIENT_PROXIES_GET_DELEGATE = MethodDescriptor.ofMethod(ClientProxies.class,
"getDelegate", Object.class, InjectableBean.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,8 @@ public void register(RegistrationContext registrationContext) {
if (InjectableContext.class.isAssignableFrom(contextClass)) {
config.contextClass((Class<? extends InjectableContext>) contextClass);
} else {
CustomAlterableContextInfo info = customAlterableContexts.add(contextClass, context.isNormal);
CustomAlterableContextInfo info = customAlterableContexts.add(contextClass, context.isNormal,
scopeAnnotation);
config.creator(bytecode -> {
return bytecode.newInstance(MethodDescriptor.ofConstructor(info.generatedName));
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ public static <T> T getApplicationScopedDelegate(InjectableContext applicationCo
return result;
}

// This method is only used if a single context is registered for the given scope
public static <T> T getSingleContextDelegate(InjectableContext context, InjectableBean<T> bean) {
T result = context.getIfActive(bean, ClientProxies::newCreationalContext);
if (result == null) {
throw notActive(bean);
}
return result;
}

public static <T> T getDelegate(InjectableBean<T> bean) {
List<InjectableContext> contexts = Arc.container().getContexts(bean.getScope());
T result = null;
Expand All @@ -46,17 +55,21 @@ public static <T> T getDelegate(InjectableBean<T> bean) {
}
}
if (result == null) {
String msg = String.format(
"%s context was not active when trying to obtain a bean instance for a client proxy of %s",
bean.getScope().getSimpleName(), bean);
if (bean.getScope().equals(RequestScoped.class)) {
msg += "\n\t- you can activate the request context for a specific method using the @ActivateRequestContext interceptor binding";
}
throw new ContextNotActiveException(msg);
throw notActive(bean);
}
return result;
}

private static ContextNotActiveException notActive(InjectableBean<?> bean) {
String msg = String.format(
"%s context was not active when trying to obtain a bean instance for a client proxy of %s",
bean.getScope().getSimpleName(), bean);
if (bean.getScope().equals(RequestScoped.class)) {
msg += "\n\t- you can activate the request context for a specific method using the @ActivateRequestContext interceptor binding";
}
return new ContextNotActiveException(msg);
}

private static <T> CreationalContextImpl<T> newCreationalContext(Contextual<T> contextual) {
return new CreationalContextImpl<>(contextual);
}
Expand Down