Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Optimization: avoid call to exists in PlainFile#iterator

If we just established that the given path is a File or a Directory,
we can assume it continues to exist.

Before
----------------------------------
File.isFile calls           : 7620
File.isDirectory calls      : 2319
File.exists calls           : 5770

After
----------------------------------
File.isFile calls           : 7620
File.isDirectory calls      : 2319
File.exists calls           : 345
  • Loading branch information...
commit 7072acb786663d554e28a4c355ee4d94478aef54 1 parent 246eceb
@retronym retronym authored
Showing with 7 additions and 1 deletion.
  1. +7 −1 src/reflect/scala/reflect/io/PlainFile.scala
View
8 src/reflect/scala/reflect/io/PlainFile.scala
@@ -56,8 +56,14 @@ class PlainFile(val givenPath: Path) extends AbstractFile {
/** Returns all abstract subfiles of this abstract directory. */
def iterator: Iterator[AbstractFile] = {
+ // Optimization: Assume that the file was not deleted and did not have permissions changed
+ // between the call to `list` and the iteration. This saves a call to `exists`.
+ def existsFast(path: Path) = path match {
+ case (_: Directory | _: io.File) => true
+ case _ => path.exists
+ }
if (!isDirectory) Iterator.empty
- else givenPath.toDirectory.list filter (_.exists) map (new PlainFile(_))
+ else givenPath.toDirectory.list filter existsFast map (new PlainFile(_))
}
/**
Please sign in to comment.
Something went wrong with that request. Please try again.