From 0cb632440e07ce370a3b97f81a81ee920eadb282 Mon Sep 17 00:00:00 2001 From: Jason Zaugg Date: Fri, 29 Mar 2013 13:07:54 +0100 Subject: Add counters to File#{exists, isFile, isDirectory}. Due to limitations in the Statistics machinery, these are only reported if this patch is applied. --- a/src/reflect/scala/reflect/internal/util/Statistics.scala +++ b/src/reflect/scala/reflect/internal/util/Statistics.scala @@ -109,7 +109,7 @@ quant) * Quantities with non-empty prefix are printed in the statistics info. */ trait Quantity { - if (enabled && prefix.nonEmpty) { + if (prefix.nonEmpty) { val key = s"${if (underlying != this) underlying.prefix else ""}/$prefix" qs(key) = this } @@ -243,7 +243,7 @@ quant) * * to remove all Statistics code from build */ - final val canEnable = _enabled + final val canEnable = true // _enabled --- src/reflect/scala/reflect/io/AbstractFile.scala | 6 ++++- src/reflect/scala/reflect/io/IOStats.scala | 31 +++++++++++++++++++++++++ src/reflect/scala/reflect/io/Path.scala | 30 ++++++++++++++++++++---- 3 files changed, 61 insertions(+), 6 deletions(-) create mode 100644 src/reflect/scala/reflect/io/IOStats.scala (limited to 'src/reflect/scala/reflect/io') diff --git a/src/reflect/scala/reflect/io/AbstractFile.scala b/src/reflect/scala/reflect/io/AbstractFile.scala index 8b69efc749..4ac56da628 100644 --- a/src/reflect/scala/reflect/io/AbstractFile.scala +++ b/src/reflect/scala/reflect/io/AbstractFile.scala @@ -11,6 +11,7 @@ import java.io.{ FileOutputStream, IOException, InputStream, OutputStream, Buffe import java.io.{ File => JFile } import java.net.URL import scala.collection.mutable.ArrayBuffer +import scala.reflect.internal.util.Statistics /** * An abstraction over files for use in the reflection/compiler libraries. @@ -112,7 +113,10 @@ abstract class AbstractFile extends Iterable[AbstractFile] { def underlyingSource: Option[AbstractFile] = None /** Does this abstract file denote an existing file? */ - def exists: Boolean = (file eq null) || file.exists + def exists: Boolean = { + if (Statistics.canEnable) Statistics.incCounter(IOStats.fileExistsCount) + (file eq null) || file.exists + } /** Does this abstract file represent something which can contain classfiles? */ def isClassContainer = isDirectory || (file != null && (extension == "jar" || extension == "zip")) diff --git a/src/reflect/scala/reflect/io/IOStats.scala b/src/reflect/scala/reflect/io/IOStats.scala new file mode 100644 index 0000000000..64e1e952cd --- /dev/null +++ b/src/reflect/scala/reflect/io/IOStats.scala @@ -0,0 +1,31 @@ +package scala.reflect.io + +import scala.reflect.internal.util.Statistics + +// Due to limitations in the Statistics machinery, these are only +// reported if this patch is applied. +// +// --- a/src/reflect/scala/reflect/internal/util/Statistics.scala +// +++ b/src/reflect/scala/reflect/internal/util/Statistics.scala +// @@ -109,7 +109,7 @@ quant) +// * Quantities with non-empty prefix are printed in the statistics info. +// */ +// trait Quantity { +// - if (enabled && prefix.nonEmpty) { +// + if (prefix.nonEmpty) { +// val key = s"${if (underlying != this) underlying.prefix else ""}/$prefix" +// qs(key) = this +// } +// @@ -243,7 +243,7 @@ quant) +// * +// * to remove all Statistics code from build +// */ +// - final val canEnable = _enabled +// + final val canEnable = true // _enabled +// +// We can commit this change as the first diff reverts a fix for an IDE memory leak. +private[io] object IOStats { + val fileExistsCount = Statistics.newCounter("# File.exists calls") + val fileIsDirectoryCount = Statistics.newCounter("# File.isDirectory calls") + val fileIsFileCount = Statistics.newCounter("# File.isFile calls") +} diff --git a/src/reflect/scala/reflect/io/Path.scala b/src/reflect/scala/reflect/io/Path.scala index 44fb41a1cd..56d4faed99 100644 --- a/src/reflect/scala/reflect/io/Path.scala +++ b/src/reflect/scala/reflect/io/Path.scala @@ -13,6 +13,7 @@ import java.io.{ File => JFile } import java.net.{ URI, URL } import scala.util.Random.alphanumeric import scala.language.implicitConversions +import scala.reflect.internal.util.Statistics /** An abstraction for filesystem paths. The differences between * Path, File, and Directory are primarily to communicate intent. @@ -57,8 +58,18 @@ object Path { def apply(path: String): Path = apply(new JFile(path)) def apply(jfile: JFile): Path = try { - if (jfile.isFile) new File(jfile) - else if (jfile.isDirectory) new Directory(jfile) + def isFile = { + if (Statistics.canEnable) Statistics.incCounter(IOStats.fileIsFileCount) + jfile.isFile + } + + def isDirectory = { + if (Statistics.canEnable) Statistics.incCounter(IOStats.fileIsDirectoryCount) + jfile.isDirectory + } + + if (isFile) new File(jfile) + else if (isDirectory) new Directory(jfile) else new Path(jfile) } catch { case ex: SecurityException => new Path(jfile) } @@ -187,10 +198,19 @@ class Path private[io] (val jfile: JFile) { // Boolean tests def canRead = jfile.canRead() def canWrite = jfile.canWrite() - def exists = try jfile.exists() catch { case ex: SecurityException => false } + def exists = { + if (Statistics.canEnable) Statistics.incCounter(IOStats.fileExistsCount) + try jfile.exists() catch { case ex: SecurityException => false } + } - def isFile = try jfile.isFile() catch { case ex: SecurityException => false } - def isDirectory = try jfile.isDirectory() catch { case ex: SecurityException => false } + def isFile = { + if (Statistics.canEnable) Statistics.incCounter(IOStats.fileIsFileCount) + try jfile.isFile() catch { case ex: SecurityException => false } + } + def isDirectory = { + if (Statistics.canEnable) Statistics.incCounter(IOStats.fileIsDirectoryCount) + try jfile.isDirectory() catch { case ex: SecurityException => false } + } def isAbsolute = jfile.isAbsolute() def isEmpty = path.length == 0 -- cgit v1.2.3