Skip to content

Commit 9674ce4

Browse files
committed
Avoid HashSet/StringBuilder allocation for non-placeholder values
Closes gh-22870
1 parent ea4a174 commit 9674ce4

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

spring-core/src/main/java/org/springframework/util/PropertyPlaceholderHelper.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -121,20 +121,26 @@ public String replacePlaceholders(String value, final Properties properties) {
121121
*/
122122
public String replacePlaceholders(String value, PlaceholderResolver placeholderResolver) {
123123
Assert.notNull(value, "'value' must not be null");
124-
return parseStringValue(value, placeholderResolver, new HashSet<>());
124+
return parseStringValue(value, placeholderResolver, null);
125125
}
126126

127127
protected String parseStringValue(
128-
String value, PlaceholderResolver placeholderResolver, Set<String> visitedPlaceholders) {
129-
130-
StringBuilder result = new StringBuilder(value);
128+
String value, PlaceholderResolver placeholderResolver, @Nullable Set<String> visitedPlaceholders) {
131129

132130
int startIndex = value.indexOf(this.placeholderPrefix);
131+
if (startIndex == -1) {
132+
return value;
133+
}
134+
135+
StringBuilder result = new StringBuilder(value);
133136
while (startIndex != -1) {
134137
int endIndex = findPlaceholderEndIndex(result, startIndex);
135138
if (endIndex != -1) {
136139
String placeholder = result.substring(startIndex + this.placeholderPrefix.length(), endIndex);
137140
String originalPlaceholder = placeholder;
141+
if (visitedPlaceholders == null) {
142+
visitedPlaceholders = new HashSet<>(4);
143+
}
138144
if (!visitedPlaceholders.add(originalPlaceholder)) {
139145
throw new IllegalArgumentException(
140146
"Circular placeholder reference '" + originalPlaceholder + "' in property definitions");
@@ -178,7 +184,6 @@ else if (this.ignoreUnresolvablePlaceholders) {
178184
startIndex = -1;
179185
}
180186
}
181-
182187
return result.toString();
183188
}
184189

0 commit comments

Comments
 (0)