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 - optimize CreationalContext for synthetic dependent beans #25129

Merged
merged 1 commit into from
Apr 25, 2022
Merged
Show file tree
Hide file tree
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 @@ -1593,17 +1593,8 @@ protected void implementGet(BeanInfo bean, ClassCreator beanCreator, ProviderTyp
// We can optimize if:
// 1) class bean - has no @PreDestroy interceptor and there is no @PreDestroy callback
// 2) producer - there is no disposal method
boolean canBeOptimized = false;
if (bean.isClassBean()) {
canBeOptimized = bean.getLifecycleInterceptors(InterceptionType.PRE_DESTROY).isEmpty()
&& Beans.getCallbacks(bean.getTarget().get().asClass(),
DotNames.PRE_DESTROY,
bean.getDeployment().getBeanArchiveIndex()).isEmpty();
} else if (bean.isProducerMethod() || bean.isProducerField()) {
canBeOptimized = bean.getDisposer() == null;
}

if (canBeOptimized) {
// 3) synthetic bean - has no destruction logic
if (!bean.hasDestroyLogic()) {
// If there is no dependency in the creational context we don't have to store the instance in the CreationalContext
ResultHandle creationalContext = get.checkCast(get.getMethodParam(0), CreationalContextImpl.class);
get.ifNonZero(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@
import static org.junit.jupiter.api.Assertions.assertTrue;

import io.quarkus.arc.Arc;
import io.quarkus.arc.BeanCreator;
import io.quarkus.arc.BeanDestroyer;
import io.quarkus.arc.impl.InstanceImpl;
import io.quarkus.arc.processor.BeanRegistrar;
import io.quarkus.arc.test.ArcTestContainer;
import java.util.Map;
import javax.annotation.PreDestroy;
import javax.enterprise.context.Dependent;
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.Disposes;
import javax.enterprise.inject.Produces;
import javax.inject.Inject;
Expand All @@ -18,8 +23,19 @@
public class DependentCreationalContextTest {

@RegisterExtension
ArcTestContainer container = new ArcTestContainer(NoPreDestroy.class, HasDestroy.class, HasDependency.class,
ProducerNoDisposer.class, ProducerWithDisposer.class, String.class, Boolean.class);
ArcTestContainer container = ArcTestContainer.builder()
.beanClasses(NoPreDestroy.class, HasDestroy.class, HasDependency.class,
ProducerNoDisposer.class, ProducerWithDisposer.class, String.class, Boolean.class)
.beanRegistrars(new BeanRegistrar() {

@Override
public void register(RegistrationContext context) {
context.configure(SyntheticOne.class).addType(SyntheticOne.class).creator(SyntheticOne.class).done();
context.configure(SyntheticTwo.class).addType(SyntheticTwo.class).creator(SyntheticTwo.class)
.destroyer(SyntheticTwo.class).done();
}
})
.build();

@Test
public void testCreationalContextOptimization() {
Expand All @@ -31,6 +47,10 @@ public void testCreationalContextOptimization() {
assertBeanType(instance, boolean.class, false);
// ProducerWithDisposer
assertBeanType(instance, String.class, true);
// Synthetic bean
assertBeanType(instance, SyntheticOne.class, false);
// Synthetic bean with destruction logic
assertBeanType(instance, SyntheticTwo.class, true);
}

<T> void assertBeanType(InstanceImpl<Object> instance, Class<T> beanType, boolean shouldBeStored) {
Expand Down Expand Up @@ -88,4 +108,27 @@ void dispose(@Disposes String ping) {
}

}

public static class SyntheticOne implements BeanCreator<SyntheticOne> {

@Override
public SyntheticOne create(CreationalContext<SyntheticOne> creationalContext, Map<String, Object> params) {
return new SyntheticOne();
}

}

public static class SyntheticTwo implements BeanCreator<SyntheticTwo>, BeanDestroyer<SyntheticTwo> {

@Override
public SyntheticTwo create(CreationalContext<SyntheticTwo> creationalContext, Map<String, Object> params) {
return new SyntheticTwo();
}

@Override
public void destroy(SyntheticTwo instance, CreationalContext<SyntheticTwo> creationalContext,
Map<String, Object> params) {
}

}
}