Permalink
Browse files

Eliminating reflective calls.

Frobbed knobs and made little traits until all relevant
looking reflective calls were gone.
  • Loading branch information...
paulp committed May 2, 2012
1 parent b6e989f commit 6300c3033e7b852c6cbef332af6085aac6150a70
@@ -5,10 +5,7 @@ import settings.MutableSettings
trait Required { self: SymbolTable =>
- type AbstractFileType >: Null <: {
- def path: String
- def canonicalPath: String
- }
+ type AbstractFileType >: Null <: api.RequiredFile
def picklerPhase: Phase
@@ -280,16 +280,8 @@ abstract class SymbolTable extends api.Universe
object perRunCaches {
import java.lang.ref.WeakReference
import scala.runtime.ScalaRunTime.stringOf
+ import scala.collection.generic.Clearable
- import language.reflectiveCalls
-
- // We can allow ourselves a structural type, these methods
- // amount to a few calls per run at most. This does suggest
- // a "Clearable" trait may be useful.
- private type Clearable = {
- def size: Int
- def clear(): Unit
- }
// Weak references so the garbage collector will take care of
// letting us know when a cache is really out of commission.
private val caches = mutable.HashSet[WeakReference[Clearable]]()
@@ -298,10 +290,14 @@ abstract class SymbolTable extends api.Universe
println(caches.size + " structures are in perRunCaches.")
caches.zipWithIndex foreach { case (ref, index) =>
val cache = ref.get()
- println("(" + index + ")" + (
- if (cache == null) " has been collected."
- else " has " + cache.size + " entries:\n" + stringOf(cache)
- ))
+ cache match {
+ case xs: Traversable[_] =>
+ println("(" + index + ")" + (
+ if (cache == null) " has been collected."
+ else " has " + xs.size + " entries:\n" + stringOf(xs)
+ ))
+ case _ =>
+ }
}
}
// if (settings.debug.value) {
@@ -315,8 +311,9 @@ abstract class SymbolTable extends api.Universe
def clearAll() = {
if (settings.debug.value) {
- val size = caches flatMap (ref => Option(ref.get)) map (_.size) sum;
- log("Clearing " + caches.size + " caches totalling " + size + " entries.")
+ // val size = caches flatMap (ref => Option(ref.get)) map (_.size) sum;
+ log("Clearing " + caches.size + " caches.")
+ // totalling " + size + " entries.")
}
caches foreach { ref =>
val cache = ref.get()
@@ -6,7 +6,8 @@
package scala.reflect
package internal
-import scala.collection.{ mutable, immutable }
+import scala.collection.{ mutable, immutable, generic }
+import generic.Clearable
import scala.ref.WeakReference
import mutable.ListBuffer
import Flags._
@@ -115,7 +116,7 @@ trait Types extends api.Types { self: SymbolTable =>
protected def newUndoLog = new UndoLog
- class UndoLog {
+ class UndoLog extends Clearable {
private type UndoPairs = List[(TypeVar, TypeConstraint)]
private var log: UndoPairs = List()
@@ -139,7 +140,7 @@ trait Types extends api.Types { self: SymbolTable =>
log ::= ((tv, tv.constr.cloneInternal))
}
- private[scala] def clear() {
+ def clear() {
if (settings.debug.value)
self.log("Clearing " + log.size + " entries from the undoLog.")
@@ -1,6 +1,7 @@
-package scala.reflect.runtime
+package scala.reflect
+package runtime
-class AbstractFile(val jfile: java.io.File) {
- def path: String = jfile.getPath()
- def canonicalPath: String = jfile.getCanonicalPath()
-}
+class AbstractFile(val jfile: java.io.File) extends api.RequiredFile {
+ def path: String = jfile.getPath()
+ def canonicalPath: String = jfile.getCanonicalPath()
+}
@@ -10,6 +10,7 @@ package io
import java.io.{ FileOutputStream, IOException, InputStream, OutputStream, BufferedOutputStream }
import java.net.URL
import scala.collection.mutable.ArrayBuffer
+import scala.reflect.api.RequiredFile
/**
* @author Philippe Altherr
@@ -81,7 +82,7 @@ object AbstractFile {
* <code>global.settings.encoding.value</code>.
* </p>
*/
-abstract class AbstractFile extends AnyRef with Iterable[AbstractFile] {
+abstract class AbstractFile extends AnyRef with RequiredFile with Iterable[AbstractFile] {
/** Returns the name of this abstract file. */
def name: String
@@ -4,6 +4,7 @@ import scala.collection.mutable
import scala.collection.mutable.ArrayBuffer
import scala.collection.mutable.Builder
import scala.collection.mutable.SetBuilder
+import scala.collection.generic.Clearable
import scala.runtime.AbstractFunction1
/** A bare-bones implementation of a mutable `Set` that uses weak references
@@ -12,7 +13,7 @@ import scala.runtime.AbstractFunction1
* This implementation offers only add/remove/test operations,
* therefore it does not fulfill the contract of Scala collection sets.
*/
-class WeakHashSet[T <: AnyRef] extends AbstractFunction1[T, Boolean] {
+class WeakHashSet[T <: AnyRef] extends AbstractFunction1[T, Boolean] with Clearable {
private val underlying = mutable.HashSet[WeakReferenceWithEquals[T]]()
/** Add the given element to this set. */
@@ -0,0 +1,26 @@
+/* __ *\
+** ________ ___ / / ___ Scala API **
+** / __/ __// _ | / / / _ | (c) 2003-2011, LAMP/EPFL **
+** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
+** /____/\___/_/ |_/____/_/ | | **
+** |/ **
+\* */
+
+package scala.collection
+package generic
+
+/** This trait forms part of collections that can be cleared
+ * with a clear() call.
+ *
+ * @author Paul Phillips
+ * @version 2.10
+ * @since 2.10
+ * @define coll clearable collection
+ * @define Coll `Clearable`
+ */
+trait Clearable {
+ /** Clears the $coll's contents. After this operation, the
+ * $coll is empty.
+ */
+ def clear(): Unit
+}
@@ -22,7 +22,7 @@ package generic
* @define add add
* @define Add add
*/
-trait Growable[-A] {
+trait Growable[-A] extends Clearable {
/** ${Add}s a single element to this $coll.
*
@@ -50,5 +50,5 @@ trait Growable[-A] {
/** Clears the $coll's contents. After this operation, the
* $coll is empty.
*/
- def clear()
+ def clear(): Unit
}
@@ -0,0 +1,7 @@
+package scala.reflect
+package api
+
+trait RequiredFile {
+ def path: String
+ def canonicalPath: String
+}

0 comments on commit 6300c30

Please sign in to comment.