Skip to content

Commit a57d6ba

Browse files
philwebbjhoeller
authored andcommitted
Optimize StringUtils.cleanPath
Add an early exit to `StringUtils.cleanPath` to save array creating and string concatenation. With a typical Spring application, the `cleanPath` method can be called over 600 times, often with a path constructed by a `ClassPathResource` that is likely to already be clean. Closes gh-22568
1 parent 19fb697 commit a57d6ba

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

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

+5
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,11 @@ public static String cleanPath(String path) {
639639
}
640640
String pathToUse = replace(path, WINDOWS_FOLDER_SEPARATOR, FOLDER_SEPARATOR);
641641

642+
// Shortcut if there is no work to do
643+
if (pathToUse.indexOf('.') == -1) {
644+
return pathToUse;
645+
}
646+
642647
// Strip prefix from path to analyze, to not treat it as part of the
643648
// first path element. This is necessary to correctly parse paths like
644649
// "file:core/../core/io/Resource.class", where the ".." should just

0 commit comments

Comments
 (0)