Skip to content

Commit

Permalink
Reduce allocations of iterateNames
Browse files Browse the repository at this point in the history
  • Loading branch information
radcortez committed Sep 19, 2024
1 parent 4f3cb4d commit 1eb7bf1
Showing 1 changed file with 31 additions and 10 deletions.
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

0 comments on commit 1eb7bf1

Please sign in to comment.