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

java.io.File::exists performance

    XMLWordPrintable

    Details

    • Subcomponent:
    • CPU:
      x86_64
    • OS:
      windows_10

      Description

      ADDITIONAL SYSTEM INFORMATION :
      Windows 10

      A DESCRIPTION OF THE PROBLEM :
      I just recognized that
      java.io.File::exists is factor ~ 1.4 slower then the eclipe.org native equivalent org.eclipse.core.internal.filesystem.local.LocalFileNatives.internalGetFileInfoW(char[], IFileInfo).exists()
      Both provide the same information but the internal systemcalls to windows differ: While JDK opens the file itself eclipse does open the directory, (as reported by sysinternals ProcessMonitor).
      I dont know why that is faster (antivirus detection may be?)
      Maybe that may be an improvement to the JDK.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      benchmark
      org.eclipse.core.internal.filesystem.local.LocalFileNativesManager
      .fetchFileInfo(FILENAME).exists()
      vs
      new File(FILENAME).exists()

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      JDK not slower then eclipse
      ACTUAL -
      JDK slower then eclipse

      ---------- BEGIN SOURCE ----------
      package org.eclipse.core.tests.filesystem;

      import java.io.File;
      import org.eclipse.core.filesystem.provider.FileInfo;


      public class Test extends FileSystemTest {
      private static final String FILENAME = "C:\\Users\\JKubitz\\eclipseDev\\jdt-master2\\git\\eclipse.jdt.core_copy\\org.eclipse.jdt.core\\dom\\org\\eclipse\\jdt\\core\\dom\\Annotation.java";

      public void test() throws InterruptedException {
      time(this::extracted);
      time(this::extracted2);
      time(this::extracted);
      time(this::extracted2);
      }

      private void extracted2() {
      File file = new File(FILENAME);
      file.exists();
      }

      private void extracted() {
      FileInfo info = org.eclipse.core.internal.filesystem.local.LocalFileNativesManager
      .fetchFileInfo(FILENAME);
      info.exists();
      }

      void time(Runnable run) {
      long n0 = System.nanoTime();
      for (long i = 1; i < 10000; i++) {
      run.run();
      }
      long n1 = System.nanoTime();
      long diff = n1 - n0;
      System.out.println("Took: " + (diff / 1000_000) + "ms =" + (diff / 1000_000_000) + "s");
      }
      }

      ---------- END SOURCE ----------

      FREQUENCY : always


        Attachments

          Activity

            People

            Assignee:
            sswsharm swati sharma
            Reporter:
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: