• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Performance issue with Java 'File' class

 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I tracked down the performance bottle necks in my application they all pointed to the File class. I'm recursively traversing a directory tree and for each node I call: exists(), isFile() and isDirectory(). In Sun's source code these three are just different bits of the same flag but I don't see any way to get access to the flag directly so the three separate calls gets the security manager and then gets the flag from the file system three times. On a local hard disk the performance is bearable (sort of) but on network storage these calls are taking a hundred times longer than the actual reading of a file on the network storage. (I'm on XP but I'm not sure what flavor system hosts the network storage.)


Is there any way around this?

 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could use JNI but then you're stuck with one platform.
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sun's File class make use of a FileSystem class that is native BUT it is also package-private so that I can't use it directly.
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wasn't talking about FileSystem - I was talking about JNI. You will basically write your own version of FileSystem.getBooleanAttributes:
Note that GetFileAttributes returns 0xFFFFFFFF if the attributes could not be retrieved. See GetFileAttributes
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
'FileSystem' is JNI. It is also platform independent. It already ships with Java. Is there any way around the 'package-private' issue?
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not one that always works.

You can try it with reflection:
However, the calls to method.setAccessible(true) may be guarded by a security manager, so they may fail.
 
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I'm recursively traversing a directory tree and for each node I call: exists(), isFile() and isDirectory().


Aside from using JNI, you should be able to get some minor improvements just by skipping some of these calls as unnecessary. For example:

The listFiles() method shouldn't be giving you any file objects for things that don't exist, after all.

Some OS's may have other objects that aren't considered files or directories. If so, you can go ahead and put in the directory check explicitly:

But you could also tell if something was really a directory by looking at the return value of listFiles(), as it will be null for any non-directory. So I might do something like this:
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your responses. I'll look to see if I can fit those suggestions into my application.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic