Skip to content

Commit ff0ed64

Browse files
committed
Do not use Files.{exists, isRegularFile, isDirectory}
Apparently these methods are known to be slow on Java 8 (it got better with Java 9): https://rules.sonarsource.com/java/tag/performance/RSPEC-3725, this is significant enough to show up in our benchmarks.
1 parent d35caba commit ff0ed64

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

compiler/src/dotty/tools/io/AbstractFile.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ abstract class AbstractFile extends Iterable[AbstractFile] {
121121

122122
/** Does this abstract file denote an existing file? */
123123
def exists: Boolean = {
124-
(jpath eq null) || Files.exists(jpath)
124+
(jpath eq null) || jpath.toFile.exists
125125
}
126126

127127
/** Does this abstract file represent something which can contain classfiles? */

compiler/src/dotty/tools/io/Path.scala

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,9 @@ object Path {
5454

5555
def apply(path: String): Path = apply(Paths.get(path))
5656
def apply(jpath: JPath): Path = try {
57-
if (Files.isRegularFile(jpath)) new File(jpath)
58-
else if (Files.isDirectory(jpath)) new Directory(jpath)
57+
val jfile = jpath.toFile
58+
if (jfile.isFile) new File(jpath)
59+
else if (jfile.isDirectory) new Directory(jpath)
5960
else new Path(jpath)
6061
} catch { case ex: SecurityException => new Path(jpath) }
6162

@@ -160,10 +161,10 @@ class Path private[io] (val jpath: JPath) {
160161
// Boolean tests
161162
def canRead: Boolean = Files.isReadable(jpath)
162163
def canWrite: Boolean = Files.isWritable(jpath)
163-
def exists: Boolean = try Files.exists(jpath) catch { case ex: SecurityException => false }
164-
def isFile: Boolean = try Files.isRegularFile(jpath) catch { case ex: SecurityException => false }
164+
def exists: Boolean = try jpath.toFile.exists catch { case ex: SecurityException => false }
165+
def isFile: Boolean = try jpath.toFile.isFile catch { case ex: SecurityException => false }
165166
def isDirectory: Boolean =
166-
try Files.isDirectory(jpath)
167+
try jpath.toFile.isDirectory
167168
catch { case ex: SecurityException => jpath.toString == "." }
168169
def isAbsolute: Boolean = jpath.isAbsolute()
169170
def isEmpty: Boolean = path.length == 0

0 commit comments

Comments
 (0)