diff options
author | Jason Zaugg <jzaugg@gmail.com> | 2013-03-29 13:16:27 +0100 |
---|---|---|
committer | Jason Zaugg <jzaugg@gmail.com> | 2013-03-29 13:51:47 +0100 |
commit | 7072acb786663d554e28a4c355ee4d94478aef54 (patch) | |
tree | 81873c3eea5319886e28d6ac34fbac0679b6eb82 | |
parent | 246eceb830ec1efb43a9b02424637c1e6b6a978b (diff) | |
download | scala-7072acb786663d554e28a4c355ee4d94478aef54.tar.gz scala-7072acb786663d554e28a4c355ee4d94478aef54.tar.bz2 scala-7072acb786663d554e28a4c355ee4d94478aef54.zip |
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
-rw-r--r-- | src/reflect/scala/reflect/io/PlainFile.scala | 8 |
1 files changed, 7 insertions, 1 deletions
diff --git a/src/reflect/scala/reflect/io/PlainFile.scala b/src/reflect/scala/reflect/io/PlainFile.scala index 31df78f995..b892fe7cef 100644 --- a/src/reflect/scala/reflect/io/PlainFile.scala +++ b/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(_)) } /** |