Browse Source

Avoid wasted memory on empty maps and sets

This commit avoids wasted memory on empty hash maps in
MergedAnnotationReadingVisitor and empty sets in InjectionMetadata.

Closes gh-29742
pull/29800/head
Vasiliy Kudriavtsev 2 years ago committed by Sam Brannen
parent
commit
3738a45658
  1. 19
      spring-beans/src/main/java/org/springframework/beans/factory/annotation/InjectionMetadata.java
  2. 5
      spring-core/src/main/java/org/springframework/core/type/classreading/MergedAnnotationReadingVisitor.java

19
spring-beans/src/main/java/org/springframework/beans/factory/annotation/InjectionMetadata.java

@ -107,15 +107,20 @@ public class InjectionMetadata { @@ -107,15 +107,20 @@ public class InjectionMetadata {
}
public void checkConfigMembers(RootBeanDefinition beanDefinition) {
Set<InjectedElement> checkedElements = new LinkedHashSet<>(this.injectedElements.size());
for (InjectedElement element : this.injectedElements) {
Member member = element.getMember();
if (!beanDefinition.isExternallyManagedConfigMember(member)) {
beanDefinition.registerExternallyManagedConfigMember(member);
checkedElements.add(element);
if (this.injectedElements.isEmpty()) {
this.checkedElements = Collections.emptySet();
}
else {
Set<InjectedElement> checkedElements = new LinkedHashSet<>(this.injectedElements.size()*4/3 + 1);
for (InjectedElement element : this.injectedElements) {
Member member = element.getMember();
if (!beanDefinition.isExternallyManagedConfigMember(member)) {
beanDefinition.registerExternallyManagedConfigMember(member);
checkedElements.add(element);
}
}
this.checkedElements = checkedElements;
}
this.checkedElements = checkedElements;
}
public void inject(Object target, @Nullable String beanName, @Nullable PropertyValues pvs) throws Throwable {

5
spring-core/src/main/java/org/springframework/core/type/classreading/MergedAnnotationReadingVisitor.java

@ -19,6 +19,7 @@ package org.springframework.core.type.classreading; @@ -19,6 +19,7 @@ package org.springframework.core.type.classreading;
import java.lang.annotation.Annotation;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@ -92,8 +93,10 @@ class MergedAnnotationReadingVisitor<A extends Annotation> extends AnnotationVis @@ -92,8 +93,10 @@ class MergedAnnotationReadingVisitor<A extends Annotation> extends AnnotationVis
@Override
public void visitEnd() {
Map<String, Object> compactedAttributes
= this.attributes.size() == 0 ? Collections.emptyMap() : this.attributes;
MergedAnnotation<A> annotation = MergedAnnotation.of(
this.classLoader, this.source, this.annotationType, this.attributes);
this.classLoader, this.source, this.annotationType, compactedAttributes);
this.consumer.accept(annotation);
}

Loading…
Cancel
Save