Skip to content

Commit

Permalink
Reduce allocations when generating default names (#1250)
Browse files Browse the repository at this point in the history
  • Loading branch information
radcortez authored Nov 11, 2024
1 parent ee0dbf9 commit 9e13393
Showing 1 changed file with 17 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package io.smallrye.config;

import static io.smallrye.config.ConfigMappingLoader.getConfigMappingClass;
import static io.smallrye.config.ConfigMappings.prefix;
import static io.smallrye.config.ConfigSourceInterceptorFactory.DEFAULT_PRIORITY;
import static io.smallrye.config.Converters.STRING_CONVERTER;
import static io.smallrye.config.Converters.newCollectionConverter;
Expand Down Expand Up @@ -775,6 +774,8 @@ public final class MappingBuilder {
private final Map<Class<?>, Set<String>> mappings = new HashMap<>();
private final List<String> ignoredPaths = new ArrayList<>();

private final StringBuilder sb = new StringBuilder();

public void mapping(ConfigClass configClass) {
mapping(configClass.getKlass(), configClass.getPrefix());
}
Expand All @@ -790,9 +791,23 @@ public void mapping(Class<?> type, String prefix) {
mappings.computeIfAbsent(mappingClass, k -> new HashSet<>(4)).add(prefix);
// Load the mapping defaults, to make the defaults available to all config sources

sb.setLength(0);
sb.append(prefix);
for (Map.Entry<String, String> defaultEntry : ConfigMappingLoader.configMappingDefaults(mappingClass).entrySet()) {
// Do not override builder defaults with mapping defaults
defaultValues.putIfAbsent(prefix(prefix, defaultEntry.getKey()), defaultEntry.getValue());
String path = defaultEntry.getKey();
String name;
if (prefix.isEmpty()) {
name = path;
} else if (path.isEmpty()) {
name = prefix;
} else if (path.charAt(0) == '[') {
name = sb.append(path).toString();
} else {
name = sb.append(".").append(path).toString();
}
sb.setLength(prefix.length());
defaultValues.putIfAbsent(name, defaultEntry.getValue());
}
}

Expand Down

0 comments on commit 9e13393

Please sign in to comment.