From 827c8865d8aab72cca3951fc1f5fbb3e23ba1c14 Mon Sep 17 00:00:00 2001 From: Claes Redestad Date: Wed, 3 Jun 2020 22:29:34 +0200 Subject: [PATCH] 8246451: Reduce overhead of normalizing file paths with trailing slash Reviewed-by: lancea --- src/java.base/unix/classes/java/io/UnixFileSystem.java | 3 ++- test/micro/org/openjdk/bench/java/io/FileOpen.java | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/java.base/unix/classes/java/io/UnixFileSystem.java b/src/java.base/unix/classes/java/io/UnixFileSystem.java index 0bfdb0e4b66..8a59b02e883 100644 --- a/src/java.base/unix/classes/java/io/UnixFileSystem.java +++ b/src/java.base/unix/classes/java/io/UnixFileSystem.java @@ -71,8 +71,9 @@ class UnixFileSystem extends FileSystem { */ private String normalize(String pathname, int off) { int n = pathname.length(); - while ((n > 0) && (pathname.charAt(n - 1) == '/')) n--; + while ((n > off) && (pathname.charAt(n - 1) == '/')) n--; if (n == 0) return "/"; + if (n == off) return pathname.substring(0, off); StringBuilder sb = new StringBuilder(n); if (off > 0) sb.append(pathname, 0, off); diff --git a/test/micro/org/openjdk/bench/java/io/FileOpen.java b/test/micro/org/openjdk/bench/java/io/FileOpen.java index b411b8f351b..8a50f597bb3 100644 --- a/test/micro/org/openjdk/bench/java/io/FileOpen.java +++ b/test/micro/org/openjdk/bench/java/io/FileOpen.java @@ -41,7 +41,7 @@ public class FileOpen { public String normalFile = "/test/dir/file/name.txt"; public String root = "/"; - public String trailingSlash = "/test/dir/file//name.txt"; + public String trailingSlash = "/test/dir/file/name.txt/"; public String notNormalizedFile = "/test/dir/file//name.txt"; @Benchmark