JDK-5099917 : File.listFiles() can be improved by caching the length, last modified times, and attributes [perf]
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.io
  • Affected Version: 5.0
  • Priority: P4
  • Status: Closed
  • Resolution: Won't Fix
  • OS: windows_2000
  • CPU: x86
  • Submitted: 2004-09-09
  • Updated: 2006-07-31
  • Resolved: 2006-07-31
Related Reports
Relates :  
Description
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) 
======================================================================

Comments
EVALUATION Recall that a java.io.File does not represent a phyical entity such as a file on a file system. It is only an abstract representation of a name of a a file. If we were to modify the implementation of listFiles() to return a cached version of the file's length, last modified times, etc. that would introduce a slight incompatibility as these methods should return the present values. 4755618 requests a more tractable solution to this use-case. A solution for that problem is expected in Dolphin under JSR-203. Closing this issues as "will not fix".
31-07-2006