Share internal StandardExecutionContext delegates

This commit makes sure that the per-operation execution context for
caching and event listening does not recreate the default internal
delegates, but rather get initialized with a shared state.

This reduces the number of instances created per operation execution,
reducing the GC pressure as a result. This also makes sure that any
cache, such as the one in StandardTypeLocator, is reused.

Closes gh-31617
This commit is contained in:
Stéphane Nicoll
2023-11-17 12:20:28 +01:00
parent 7006d0a80e
commit ec905cb073
12 changed files with 288 additions and 33 deletions

View File

@@ -47,11 +47,13 @@ import org.springframework.beans.factory.annotation.BeanFactoryAnnotationUtils;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.context.expression.AnnotatedElementKey;
import org.springframework.context.expression.BeanFactoryResolver;
import org.springframework.core.BridgeMethodResolver;
import org.springframework.core.KotlinDetector;
import org.springframework.core.ReactiveAdapter;
import org.springframework.core.ReactiveAdapterRegistry;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
@@ -99,7 +101,10 @@ public abstract class CacheAspectSupport extends AbstractCacheInvoker
private final Map<CacheOperationCacheKey, CacheOperationMetadata> metadataCache = new ConcurrentHashMap<>(1024);
private final CacheOperationExpressionEvaluator evaluator = new CacheOperationExpressionEvaluator();
private final StandardEvaluationContext originalEvaluationContext = new StandardEvaluationContext();
private final CacheOperationExpressionEvaluator evaluator = new CacheOperationExpressionEvaluator(
new CacheEvaluationContextFactory(this.originalEvaluationContext));
@Nullable
private final ReactiveCachingHandler reactiveCachingHandler;
@@ -222,6 +227,7 @@ public abstract class CacheAspectSupport extends AbstractCacheInvoker
@Override
public void setBeanFactory(BeanFactory beanFactory) {
this.beanFactory = beanFactory;
this.originalEvaluationContext.setBeanResolver(new BeanFactoryResolver(beanFactory));
}
@@ -852,7 +858,7 @@ public abstract class CacheAspectSupport extends AbstractCacheInvoker
private EvaluationContext createEvaluationContext(@Nullable Object result) {
return evaluator.createEvaluationContext(this.caches, this.metadata.method, this.args,
this.target, this.metadata.targetClass, this.metadata.targetMethod, result, beanFactory);
this.target, this.metadata.targetClass, this.metadata.targetMethod, result);
}
protected Collection<? extends Cache> getCaches() {

View File

@@ -0,0 +1,73 @@
/*
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cache.interceptor;
import java.lang.reflect.Method;
import java.util.function.Supplier;
import org.springframework.core.DefaultParameterNameDiscoverer;
import org.springframework.core.ParameterNameDiscoverer;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.lang.Nullable;
import org.springframework.util.function.SingletonSupplier;
/**
* A factory for {@link CacheEvaluationContext} that makes sure that internal
* delegates are reused.
*
* @author Stephane Nicoll
* @since 6.1.1
*/
class CacheEvaluationContextFactory {
private final StandardEvaluationContext originalContext;
@Nullable
private Supplier<ParameterNameDiscoverer> parameterNameDiscoverer;
CacheEvaluationContextFactory(StandardEvaluationContext originalContext) {
this.originalContext = originalContext;
}
public void setParameterNameDiscoverer(Supplier<ParameterNameDiscoverer> parameterNameDiscoverer) {
this.parameterNameDiscoverer = parameterNameDiscoverer;
}
public ParameterNameDiscoverer getParameterNameDiscoverer() {
if (this.parameterNameDiscoverer == null) {
this.parameterNameDiscoverer = SingletonSupplier.of(new DefaultParameterNameDiscoverer());
}
return this.parameterNameDiscoverer.get();
}
/**
* Creates a {@link CacheEvaluationContext} for the specified operation.
* @param rootObject the {@code root} object to use for the context
* @param targetMethod the target cache {@link Method}
* @param args the arguments of the method invocation
* @return a context suitable for this cache operation
*/
public CacheEvaluationContext forOperation(CacheExpressionRootObject rootObject,
Method targetMethod, Object[] args) {
CacheEvaluationContext evaluationContext = new CacheEvaluationContext(
rootObject, targetMethod, args, getParameterNameDiscoverer());
this.originalContext.applyDelegatesTo(evaluationContext);
return evaluationContext;
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -21,10 +21,8 @@ import java.util.Collection;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.cache.Cache;
import org.springframework.context.expression.AnnotatedElementKey;
import org.springframework.context.expression.BeanFactoryResolver;
import org.springframework.context.expression.CachedExpressionEvaluator;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
@@ -67,6 +65,13 @@ class CacheOperationExpressionEvaluator extends CachedExpressionEvaluator {
private final Map<ExpressionKey, Expression> unlessCache = new ConcurrentHashMap<>(64);
private final CacheEvaluationContextFactory evaluationContextFactory;
public CacheOperationExpressionEvaluator(CacheEvaluationContextFactory evaluationContextFactory) {
super();
this.evaluationContextFactory = evaluationContextFactory;
this.evaluationContextFactory.setParameterNameDiscoverer(this::getParameterNameDiscoverer);
}
/**
* Create an {@link EvaluationContext}.
@@ -81,21 +86,18 @@ class CacheOperationExpressionEvaluator extends CachedExpressionEvaluator {
*/
public EvaluationContext createEvaluationContext(Collection<? extends Cache> caches,
Method method, Object[] args, Object target, Class<?> targetClass, Method targetMethod,
@Nullable Object result, @Nullable BeanFactory beanFactory) {
@Nullable Object result) {
CacheExpressionRootObject rootObject = new CacheExpressionRootObject(
caches, method, args, target, targetClass);
CacheEvaluationContext evaluationContext = new CacheEvaluationContext(
rootObject, targetMethod, args, getParameterNameDiscoverer());
CacheEvaluationContext evaluationContext = this.evaluationContextFactory
.forOperation(rootObject, targetMethod, args);
if (result == RESULT_UNAVAILABLE) {
evaluationContext.addUnavailableVariable(RESULT_VARIABLE);
}
else if (result != NO_RESULT) {
evaluationContext.setVariable(RESULT_VARIABLE, result);
}
if (beanFactory != null) {
evaluationContext.setBeanResolver(new BeanFactoryResolver(beanFactory));
}
return evaluationContext;
}

View File

@@ -262,7 +262,7 @@ public class ApplicationListenerMethodAdapter implements GenericApplicationListe
if (StringUtils.hasText(condition)) {
Assert.notNull(this.evaluator, "EventExpressionEvaluator must not be null");
return this.evaluator.condition(
condition, event, this.targetMethod, this.methodKey, args, this.applicationContext);
condition, event, this.targetMethod, this.methodKey, args);
}
return true;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,14 +20,13 @@ import java.lang.reflect.Method;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.expression.AnnotatedElementKey;
import org.springframework.context.expression.BeanFactoryResolver;
import org.springframework.context.expression.CachedExpressionEvaluator;
import org.springframework.context.expression.MethodBasedEvaluationContext;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.lang.Nullable;
import org.springframework.expression.spel.support.StandardEvaluationContext;
/**
* Utility class for handling SpEL expression parsing for application events.
@@ -41,23 +40,32 @@ class EventExpressionEvaluator extends CachedExpressionEvaluator {
private final Map<ExpressionKey, Expression> conditionCache = new ConcurrentHashMap<>(64);
private final StandardEvaluationContext originalEvaluationContext;
EventExpressionEvaluator(StandardEvaluationContext originalEvaluationContext) {
this.originalEvaluationContext = originalEvaluationContext;
}
/**
* Determine if the condition defined by the specified expression evaluates
* to {@code true}.
*/
public boolean condition(String conditionExpression, ApplicationEvent event, Method targetMethod,
AnnotatedElementKey methodKey, Object[] args, @Nullable BeanFactory beanFactory) {
EventExpressionRootObject root = new EventExpressionRootObject(event, args);
MethodBasedEvaluationContext evaluationContext = new MethodBasedEvaluationContext(
root, targetMethod, args, getParameterNameDiscoverer());
if (beanFactory != null) {
evaluationContext.setBeanResolver(new BeanFactoryResolver(beanFactory));
}
AnnotatedElementKey methodKey, Object[] args) {
EventExpressionRootObject rootObject = new EventExpressionRootObject(event, args);
EvaluationContext evaluationContext = createEvaluationContext(rootObject, targetMethod, args);
return (Boolean.TRUE.equals(getExpression(this.conditionCache, methodKey, conditionExpression).getValue(
evaluationContext, Boolean.class)));
}
private EvaluationContext createEvaluationContext(EventExpressionRootObject rootObject,
Method method, Object[] args) {
MethodBasedEvaluationContext evaluationContext = new MethodBasedEvaluationContext(rootObject,
method, args, getParameterNameDiscoverer());
this.originalEvaluationContext.applyDelegatesTo(evaluationContext);
return evaluationContext;
}
}

View File

@@ -39,10 +39,12 @@ import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.expression.BeanFactoryResolver;
import org.springframework.core.MethodIntrospector;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
@@ -75,6 +77,8 @@ public class EventListenerMethodProcessor
@Nullable
private List<EventListenerFactory> eventListenerFactories;
private final StandardEvaluationContext originalEvaluationContext;
@Nullable
private final EventExpressionEvaluator evaluator;
@@ -82,7 +86,8 @@ public class EventListenerMethodProcessor
public EventListenerMethodProcessor() {
this.evaluator = new EventExpressionEvaluator();
this.originalEvaluationContext = new StandardEvaluationContext();
this.evaluator = new EventExpressionEvaluator(this.originalEvaluationContext);
}
@Override
@@ -95,6 +100,7 @@ public class EventListenerMethodProcessor
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
this.beanFactory = beanFactory;
this.originalEvaluationContext.setBeanResolver(new BeanFactoryResolver(this.beanFactory));
Map<String, EventListenerFactory> beans = beanFactory.getBeansOfType(EventListenerFactory.class, false, false);
List<EventListenerFactory> factories = new ArrayList<>(beans.values());