Skip to content

Commit

Permalink
Reduce allocations of iterateNames (#1213)
Browse files Browse the repository at this point in the history
* Reduce allocations of the Profile, Relocate and Fallback interceptors

* Reduce allocations of iterateNames
  • Loading branch information
radcortez authored Sep 19, 2024
1 parent 18ea916 commit 646d233
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 30 deletions.
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

0 comments on commit 646d233

Please sign in to comment.