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

File.listFiles() can be improved by caching the length, last modified times, and attributes [perf]

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Won't Fix
    • Icon: P4 P4
    • None
    • 5.0
    • core-libs
    • x86
    • windows_2000

      Name: rmT116609 Date: 09/09/2004


      A DESCRIPTION OF THE REQUEST :
      On Windows the native directory listing routines provide all informations about a file at once.

      FindFirstFile with it W32FindData

      The java.io.File.listFiles(...) should be rewitten so that they return a subclass of java.io.File that contains the file length, last modified and the attributes of the file.


      JUSTIFICATION :
      This would give a great speedup >20% for code that needs to know the size and last modified time of all files in a folder.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Access to the file metadata should be fast.
      ACTUAL -
      A recursive directoroy listing is many times slower in java as in C/C++ when more data then just the filename is needed.

      ---------- BEGIN SOURCE ----------

      import java.io.*;

      /**
       *
       * @author mam
       */
      public class A {
          
          /**
           * @param args the command line arguments
           */
          public static void main(String[] args) {
              long start = System.nanoTime();
              for(int i=0 ; i<10 ; ++i) {
                  File[] f = new File("c:\\winnt").listFiles();
                  long totalFileSize = 0;
                  long jungestFile = Long.MIN_VALUE;
                  String fileName = null;
                  for(int j=0 ; j<f.length ; ++j) {
                      fileName = f[j].getName();
                      totalFileSize = Math.max(totalFileSize, f[j].length());
                      jungestFile = Math.max(jungestFile, f[j].lastModified());
                  }
              }
              long diff = System.nanoTime() - start;
              System.out.println("Time " + diff/1000000.0);
          }
          
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Write a JNI class / dll that collects all the data and provides a File subclass.
      (Incident Review ID: 305494)
      ======================================================================

            iris Iris Clark
            rmandalasunw Ranjith Mandala (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: