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

@@ -55,6 +55,7 @@ import org.springframework.util.Assert;
* @author Andy Clement
* @author Juergen Hoeller
* @author Sam Brannen
* @author Stephane Nicoll
* @since 3.0
* @see SimpleEvaluationContext
* @see ReflectivePropertyAccessor
@@ -90,9 +91,9 @@ public class StandardEvaluationContext implements EvaluationContext {
@Nullable
private TypeConverter typeConverter;
private TypeComparator typeComparator = new StandardTypeComparator();
private TypeComparator typeComparator = StandardTypeComparator.INSTANCE;
private OperatorOverloader operatorOverloader = new StandardOperatorOverloader();
private OperatorOverloader operatorOverloader = StandardOperatorOverloader.INSTANCE;
private final Map<String, Object> variables = new ConcurrentHashMap<>();
@@ -329,6 +330,29 @@ public class StandardEvaluationContext implements EvaluationContext {
resolver.registerMethodFilter(type, filter);
}
/**
* Apply the internal delegates of this instance to the specified
* {@code evaluationContext}. Typically invoked right after the new context
* instance has been created to reuse the delegates. Do not modify the
* {@linkplain #setRootObject(Object) root object} or any registered
* {@linkplain #setVariables(Map) variables}.
* @param evaluationContext the evaluation context to update
* @since 6.1.1
*/
public void applyDelegatesTo(StandardEvaluationContext evaluationContext) {
// Triggers initialization for default delegates
evaluationContext.setConstructorResolvers(new ArrayList<>(this.getConstructorResolvers()));
evaluationContext.setMethodResolvers(new ArrayList<>(this.getMethodResolvers()));
evaluationContext.setPropertyAccessors(new ArrayList<>(this.getPropertyAccessors()));
evaluationContext.setTypeLocator(this.getTypeLocator());
evaluationContext.setTypeConverter(this.getTypeConverter());
evaluationContext.beanResolver = this.beanResolver;
evaluationContext.operatorOverloader = this.operatorOverloader;
evaluationContext.reflectiveMethodResolver = this.reflectiveMethodResolver;
evaluationContext.typeComparator = this.typeComparator;
}
private List<PropertyAccessor> initPropertyAccessors() {
List<PropertyAccessor> accessors = this.propertyAccessors;

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.
@@ -29,6 +29,8 @@ import org.springframework.lang.Nullable;
*/
public class StandardOperatorOverloader implements OperatorOverloader {
static final StandardOperatorOverloader INSTANCE = new StandardOperatorOverloader();
@Override
public boolean overridesOperation(Operation operation, @Nullable Object leftOperand, @Nullable Object rightOperand)
throws EvaluationException {

View File

@@ -37,6 +37,8 @@ import org.springframework.util.NumberUtils;
*/
public class StandardTypeComparator implements TypeComparator {
static final StandardTypeComparator INSTANCE = new StandardTypeComparator();
@Override
public boolean canCompare(@Nullable Object left, @Nullable Object right) {
if (left == null || right == null) {

View File

@@ -0,0 +1,122 @@
/*
* 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.expression.spel.support;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import org.junit.jupiter.api.Test;
import org.springframework.expression.BeanResolver;
import org.springframework.expression.ConstructorResolver;
import org.springframework.expression.MethodResolver;
import org.springframework.expression.OperatorOverloader;
import org.springframework.expression.PropertyAccessor;
import org.springframework.expression.TypeConverter;
import org.springframework.expression.TypeLocator;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
/**
* Tests for {@link StandardEvaluationContext}.
*
* @author Stephane Nicoll
*/
class StandardEvaluationContextTests {
private final StandardEvaluationContext evaluationContext = new StandardEvaluationContext();
@Test
void applyDelegatesToSetDelegatesToTarget() {
StandardEvaluationContext target = new StandardEvaluationContext();
this.evaluationContext.applyDelegatesTo(target);
assertThat(target).hasFieldOrProperty("reflectiveMethodResolver").isNotNull();
assertThat(target.getBeanResolver()).isSameAs(this.evaluationContext.getBeanResolver());
assertThat(target.getTypeLocator()).isSameAs(this.evaluationContext.getTypeLocator());
assertThat(target.getTypeConverter()).isSameAs(this.evaluationContext.getTypeConverter());
assertThat(target.getOperatorOverloader()).isSameAs(this.evaluationContext.getOperatorOverloader());
assertThat(target.getPropertyAccessors()).satisfies(hasSameElements(
this.evaluationContext.getPropertyAccessors()));
assertThat(target.getConstructorResolvers()).satisfies(hasSameElements(
this.evaluationContext.getConstructorResolvers()));
assertThat(target.getMethodResolvers()).satisfies(hasSameElements(
this.evaluationContext.getMethodResolvers()));
}
@Test
void applyDelegatesToSetOverrideDelegatesInTarget() {
StandardEvaluationContext target = new StandardEvaluationContext();
target.setBeanResolver(mock(BeanResolver.class));
target.setTypeLocator(mock(TypeLocator.class));
target.setTypeConverter(mock(TypeConverter.class));
target.setOperatorOverloader(mock(OperatorOverloader.class));
target.setPropertyAccessors(new ArrayList<>());
target.setConstructorResolvers(new ArrayList<>());
target.setMethodResolvers(new ArrayList<>());
this.evaluationContext.applyDelegatesTo(target);
assertThat(target).hasFieldOrProperty("reflectiveMethodResolver").isNotNull();
assertThat(target.getBeanResolver()).isSameAs(this.evaluationContext.getBeanResolver());
assertThat(target.getTypeLocator()).isSameAs(this.evaluationContext.getTypeLocator());
assertThat(target.getTypeConverter()).isSameAs(this.evaluationContext.getTypeConverter());
assertThat(target.getOperatorOverloader()).isSameAs(this.evaluationContext.getOperatorOverloader());
assertThat(target.getPropertyAccessors()).satisfies(hasSameElements(
this.evaluationContext.getPropertyAccessors()));
assertThat(target.getConstructorResolvers()).satisfies(hasSameElements(
this.evaluationContext.getConstructorResolvers()));
assertThat(target.getMethodResolvers()).satisfies(hasSameElements(
this.evaluationContext.getMethodResolvers()));
}
@Test
void applyDelegatesToMakesACopyOfPropertyAccessors() {
StandardEvaluationContext target = new StandardEvaluationContext();
this.evaluationContext.applyDelegatesTo(target);
PropertyAccessor propertyAccessor = mock(PropertyAccessor.class);
this.evaluationContext.getPropertyAccessors().add(propertyAccessor);
assertThat(target.getPropertyAccessors()).doesNotContain(propertyAccessor);
}
@Test
void applyDelegatesToMakesACopyOfConstructorResolvers() {
StandardEvaluationContext target = new StandardEvaluationContext();
this.evaluationContext.applyDelegatesTo(target);
ConstructorResolver methodResolver = mock(ConstructorResolver.class);
this.evaluationContext.getConstructorResolvers().add(methodResolver);
assertThat(target.getConstructorResolvers()).doesNotContain(methodResolver);
}
@Test
void applyDelegatesToMakesACopyOfMethodResolvers() {
StandardEvaluationContext target = new StandardEvaluationContext();
this.evaluationContext.applyDelegatesTo(target);
MethodResolver methodResolver = mock(MethodResolver.class);
this.evaluationContext.getMethodResolvers().add(methodResolver);
assertThat(target.getMethodResolvers()).doesNotContain(methodResolver);
}
private Consumer<List<?>> hasSameElements(List<?> candidates) {
return actual -> {
assertThat(actual.size()).isEqualTo(candidates.size());
for (int i = 0; i < candidates.size(); i++) {
assertThat(candidates.get(i)).isSameAs(actual.get(i));
}
};
}
}