Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid wasted memory on empty maps and sets #29742

Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,20 @@ protected boolean needsRefresh(Class<?> clazz) {
}

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);
}
sbrannen marked this conversation as resolved.
Show resolved Hide resolved
}
this.checkedElements = checkedElements;
}
this.checkedElements = checkedElements;
}

public void inject(Object target, @Nullable String beanName, @Nullable PropertyValues pvs) throws Throwable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
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;
Expand Down Expand Up @@ -92,8 +93,10 @@ public AnnotationVisitor visitArray(String name) {

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

Expand Down