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

Reduce allocations of iterateNames #1213

Merged
merged 2 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package io.smallrye.config;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;

public abstract class AbstractMappingConfigSourceInterceptor implements ConfigSourceInterceptor {
Expand All @@ -26,17 +24,30 @@ public String apply(final String name) {

@Override
public Iterator<String> iterateNames(final ConfigSourceInterceptorContext context) {
final Set<String> names = new HashSet<>();
final Iterator<String> namesIterator = context.iterateNames();
while (namesIterator.hasNext()) {
final String name = namesIterator.next();
names.add(name);
final String mappedName = mapping.apply(name);
if (mappedName != null) {
names.add(mappedName);
return new Iterator<>() {
final Iterator<String> iterator = context.iterateNames();
String mappedName = null;

@Override
public boolean hasNext() {
return mappedName != null || iterator.hasNext();
}

@Override
public String next() {
if (mappedName != null) {
String mappedName = this.mappedName;
this.mappedName = null;
return mappedName;
}
String name = iterator.next();
String mappedName = mapping.apply(name);
if (!name.equals(mappedName)) {
this.mappedName = mappedName;
}
return name;
}
}
return names.iterator();
};
}

protected Function<String, String> getMapping() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import jakarta.annotation.Priority;

Expand Down Expand Up @@ -66,12 +64,19 @@ public ConfigValue getProfileValue(final ConfigSourceInterceptorContext context,

@Override
public Iterator<String> iterateNames(final ConfigSourceInterceptorContext context) {
final Set<String> names = new HashSet<>();
final Iterator<String> namesIterator = context.iterateNames();
while (namesIterator.hasNext()) {
names.add(activeName(namesIterator.next(), profiles));
}
return names.iterator();
return new Iterator<>() {
final Iterator<String> iterator = context.iterateNames();

@Override
public boolean hasNext() {
return iterator.hasNext();
}

@Override
public String next() {
return activeName(iterator.next(), profiles);
}
};
}

public List<String> getProfiles() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package io.smallrye.config;

import static java.util.Collections.emptyIterator;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -43,16 +44,36 @@ public ConfigValue getValue(final ConfigSourceInterceptorContext context, final

@Override
public Iterator<String> iterateNames(final ConfigSourceInterceptorContext context) {
final Set<String> names = new HashSet<>();
for (final ConfigValueConfigSource configSource : configSources) {
final Set<String> propertyNames = configSource.getPropertyNames();
if (propertyNames != null) {
names.addAll(propertyNames);
return new Iterator<>() {
final Iterator<ConfigValueConfigSource> configSourceIterator = configSources.iterator();
Iterator<String> propertiesIterator = context.iterateNames();

@Override
public boolean hasNext() {
if (propertiesIterator.hasNext()) {
return true;
} else {
propertiesIterator = nextConfigSource();
if (propertiesIterator.hasNext()) {
return true;
} else if (configSourceIterator.hasNext()) {
return hasNext();
} else {
return false;
}
}
}
}
Iterator<String> iter = context.iterateNames();
iter.forEachRemaining(names::add);
return names.iterator();

@Override
public String next() {
return propertiesIterator.next();
}

private Iterator<String> nextConfigSource() {
return configSourceIterator.hasNext() ? configSourceIterator.next().getPropertyNames().iterator()
: emptyIterator();
}
};
}

boolean negative() {
Expand Down