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

@@ -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));
}
};
}
}