Browse Source
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-31617pull/31632/head
12 changed files with 288 additions and 33 deletions
@ -0,0 +1,73 @@
@@ -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; |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,122 @@
@@ -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)); |
||||
} |
||||
}; |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue