Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Speeding up file system access?

My app scans part of a file system, and my users reported it was very slow when they were scanning a network drive. Testing my code, I identified the bottleneck: the methods File.isFile(), File.isDirectory(), and File.isHidden(), which are all calling fs.getBooleanAttributes(File f). This method appears to be very slow on Windows network drives. How can I improve performance? Can I avoid calling this method in some way?

like image 679
Amanda S Avatar asked Dec 18 '09 18:12

Amanda S


1 Answers

Defensive code oftencalls those isXYZ() methods, and it's generally good practise. However, sometimes the performance is poor, as you've discovered.

An alternative approach is to assume that the file is a file, it exists, it's visible, readable, etc, and just try and read it. If it isn't those things, you'll get an exception, which you can catch, and then do the checks to find out exactly what went wrong. That way, you're optimising for the common case (i.e. everything's fine), and only perform the slow operations when things go wrong.

like image 109
skaffman Avatar answered Oct 14 '22 16:10

skaffman