From a57d6ba5f3cf8afd6f333435d47035d5fe61fcf3 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Fri, 8 Mar 2019 17:20:50 -0800 Subject: [PATCH] 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 --- .../src/main/java/org/springframework/util/StringUtils.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/spring-core/src/main/java/org/springframework/util/StringUtils.java b/spring-core/src/main/java/org/springframework/util/StringUtils.java index b8d58e47d4..b616602b14 100644 --- a/spring-core/src/main/java/org/springframework/util/StringUtils.java +++ b/spring-core/src/main/java/org/springframework/util/StringUtils.java @@ -639,6 +639,11 @@ public abstract class StringUtils { } String pathToUse = replace(path, WINDOWS_FOLDER_SEPARATOR, FOLDER_SEPARATOR); + // Shortcut if there is no work to do + if (pathToUse.indexOf('.') == -1) { + return pathToUse; + } + // Strip prefix from path to analyze, to not treat it as part of the // first path element. This is necessary to correctly parse paths like // "file:core/../core/io/Resource.class", where the ".." should just