-
Bug
-
Resolution: Fixed
-
P4
-
21
-
b18
-
x86_64
-
windows
Some applications (including Jersey, the core of GlassFish) do hold a file://-URL and want to access that file path using the NIO files API. For that reason, such source code has to turn a URL instance into a Path instance in some way.
The correct way to do so is `Path.of(url.toURI())`, but a simple JMH benchmark proofs that this is approx. 28% slower (JDK 21 / Windows 11) than the "wrong" alternative `new File(url.getPath()).toPath()`. Due to that remarkable Performance drop, people refuse to use the "correct" way and keep using the "wrong" way, even if this implies potential bugs at runtime (at least on Windows).
In an attempt to support people to use the modern NIO files API, we need to remedy this performance penalty.
Benchmark result (JDK 21 / Windows 11):
```
Benchmark Mode Cnt Score Error Units
FileUrlToPath.usingNewFileGetPathToPath thrpt 25 1063561,813 ± 76839,140 ops/s
FileUrlToPath.usingPathOfToURI thrpt 25 767906,156 ± 38055,188 ops/s
```
Benchmark source code:
```
public class FileUrlToPath {
public static void main(final String[] args) throws Throwable {
Main.main(args);
}
private static final URL FILE_URL = FileUrlToPath.class.getResource("");
@Benchmark
public Path usingPathOfToURI() throws Throwable {
return Path.of(FILE_URL.toURI());
}
@Benchmark
public Path usingNewFileGetPathToPath() throws Throwable {
return new File(FILE_URL.getPath()).toPath();
}
}
```
The correct way to do so is `Path.of(url.toURI())`, but a simple JMH benchmark proofs that this is approx. 28% slower (JDK 21 / Windows 11) than the "wrong" alternative `new File(url.getPath()).toPath()`. Due to that remarkable Performance drop, people refuse to use the "correct" way and keep using the "wrong" way, even if this implies potential bugs at runtime (at least on Windows).
In an attempt to support people to use the modern NIO files API, we need to remedy this performance penalty.
Benchmark result (JDK 21 / Windows 11):
```
Benchmark Mode Cnt Score Error Units
FileUrlToPath.usingNewFileGetPathToPath thrpt 25 1063561,813 ± 76839,140 ops/s
FileUrlToPath.usingPathOfToURI thrpt 25 767906,156 ± 38055,188 ops/s
```
Benchmark source code:
```
public class FileUrlToPath {
public static void main(final String[] args) throws Throwable {
Main.main(args);
}
private static final URL FILE_URL = FileUrlToPath.class.getResource("");
@Benchmark
public Path usingPathOfToURI() throws Throwable {
return Path.of(FILE_URL.toURI());
}
@Benchmark
public Path usingNewFileGetPathToPath() throws Throwable {
return new File(FILE_URL.getPath()).toPath();
}
}
```
- relates to
-
JDK-8353920 (fs) Refactor sun.nio.fs.WindowsPathParser (win)
-
- Open
-
-
JDK-8353923 (fs) Further improve performance of URI -> Path conversion (win)
-
- In Progress
-
-
JDK-8314511 WindowsPathParser throws InvalidPathException: Illegal char <:> at index 2
-
- Closed
-
- links to
-
Commit(master) openjdk/jdk/5481021e
-
Review(master) openjdk/jdk/24400