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

Files.isReadable/isWritable/isExecutable expensive when file does not exist

XMLWordPrintable

    • b03
    • generic
    • generic

        ADDITIONAL SYSTEM INFORMATION :
        Tested on OpenJDK 21.0.1 on Ubuntu Linux 64, but behavior goes back to JDK 7.

        A DESCRIPTION OF THE PROBLEM :
        The method Files.isAccessible() are implemented on checkAccess so need to catch the IOException when the file does not exist or access can't be determined.
        When using Files.isReadable() / isWritable() / isExecutable(), we check only for one mode. The native code in UnixNativeDispatcher.c / Java_sun_nio_fs_UnixNativeDispatcher_access0() is able to give us an error code if the single mode we ask for is not valid for the file.
        Since we are asking for a single mode, we could translate the value of error code to a boolean directly, instead of creating an exception with is caught in Files.isAccessible() to return a boolean.

        I've tested a patch available here : https://github.com/gdarmont/jdk/commit/5d0128fde225844cf9155505ea005ad50fc74a8c
        The performance improvement is especially visible on use cases where a call to Files.isXXX() is likely to fail.
        This is for example the case when checking if many files are executable, as it is expected that most of them are not.

        Eg. the well known JGit library (Java Git Implementation) is affected, as it needs to check if a file is executable to compute Git mode attributes.


              bpb Brian Burkhalter
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              6 Start watching this issue

                Created:
                Updated:
                Resolved: