Skip to content

Commit 39e2aaa

Browse files
committed
Size the ElementsParser based on expected number of elements
Previously, the ElementsParser would be created using its default capacity of 6 even when parsing a String that is expected to produce a single element. This commit updates ConfigurationPropertyName to create an ElementsParser with a capacity of 1 when parsing a String that should contain only a single element. See gh-15760
1 parent 8ec6c37 commit 39e2aaa

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/ConfigurationPropertyName.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ public ConfigurationPropertyName append(String elementValue) {
195195
if (elementValue == null) {
196196
return this;
197197
}
198-
Elements additionalElements = elementsOf(elementValue);
198+
Elements additionalElements = probablySingleElementOf(elementValue);
199199
return new ConfigurationPropertyName(this.elements.append(additionalElements));
200200
}
201201

@@ -424,11 +424,16 @@ static ConfigurationPropertyName of(CharSequence name, boolean returnNullIfInval
424424
return (elements != null) ? new ConfigurationPropertyName(elements) : null;
425425
}
426426

427-
private static Elements elementsOf(CharSequence name) {
428-
return elementsOf(name, false);
427+
private static Elements probablySingleElementOf(CharSequence name) {
428+
return elementsOf(name, false, 1);
429429
}
430430

431431
private static Elements elementsOf(CharSequence name, boolean returnNullIfInvalid) {
432+
return elementsOf(name, returnNullIfInvalid, ElementsParser.DEFAULT_CAPACITY);
433+
}
434+
435+
private static Elements elementsOf(CharSequence name, boolean returnNullIfInvalid,
436+
int parserCapacity) {
432437
if (name == null) {
433438
Assert.isTrue(returnNullIfInvalid, "Name must not be null");
434439
return null;
@@ -443,7 +448,7 @@ private static Elements elementsOf(CharSequence name, boolean returnNullIfInvali
443448
throw new InvalidConfigurationPropertyNameException(name,
444449
Collections.singletonList('.'));
445450
}
446-
Elements elements = new ElementsParser(name, '.').parse();
451+
Elements elements = new ElementsParser(name, '.', parserCapacity).parse();
447452
for (int i = 0; i < elements.getSize(); i++) {
448453
if (elements.getType(i) == ElementType.NON_UNIFORM) {
449454
if (returnNullIfInvalid) {

0 commit comments

Comments
 (0)