Skip to content

Commit

Permalink
Reduce allocations of the Profile, Relocate and Fallback interceptors
Browse files Browse the repository at this point in the history
  • Loading branch information
radcortez committed Sep 19, 2024
1 parent 18ea916 commit 4f3cb4d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 20 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

0 comments on commit 4f3cb4d

Please sign in to comment.