Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8321591

(fs) Improve String -> Path conversion performance (win)

XMLWordPrintable

    • 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();
      }

      }
      ```

            bpb Brian Burkhalter
            mkarg Markus Karg
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: