Skip to content

Commit

Permalink
Use Java collections
Browse files Browse the repository at this point in the history
  • Loading branch information
retronym committed Feb 22, 2018
1 parent 3e4b3b4 commit 52959c9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 19 deletions.
41 changes: 27 additions & 14 deletions internal/compiler-bridge/src/main/scala/xsbt/ExtractAPI.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ package xsbt

import java.io.File
import java.util.{ Arrays, Comparator }

import scala.tools.nsc.symtab.Flags
import xsbti.api._

import scala.annotation.tailrec
import scala.collection.JavaConverters.asScalaIteratorConverter
import scala.tools.nsc.Global

/**
Expand Down Expand Up @@ -62,17 +64,18 @@ class ExtractAPI[GlobalType <: Global](
// this cache reduces duplicate work both here and when persisting
// caches on other structures had minimal effect on time and cache size
// (tried: Definition, Modifier, Path, Id, String)
private[this] val typeCache = perRunCaches.newAnyRefMap[(Symbol, Type), xsbti.api.Type]()

private[this] val typeCache = new java.util.HashMap[(Symbol, Type), xsbti.api.Type]()
// these caches are necessary for correctness
private[this] val structureCache = perRunCaches.newAnyRefMap[Symbol, xsbti.api.Structure]()
private[this] val structureCache = new java.util.HashMap[Symbol, xsbti.api.Structure]()
private[this] val classLikeCache =
perRunCaches.newAnyRefMap[(Symbol, Symbol), xsbti.api.ClassLikeDef]()
private[this] val pending = perRunCaches.newSet[xsbti.api.Lazy[_]]()
new java.util.HashMap[(Symbol, Symbol), xsbti.api.ClassLikeDef]()
private[this] val pending = new java.util.HashSet[xsbti.api.Lazy[_]]()

private[this] val emptyStringArray = Array.empty[String]

private[this] val allNonLocalClassesInSrc = perRunCaches.newSet[xsbti.api.ClassLike]()
private[this] val _mainClasses = perRunCaches.newSet[String]()
private[this] val allNonLocalClassesInSrc = new collection.mutable.HashSet[xsbti.api.ClassLike]()
private[this] val _mainClasses = new collection.mutable.HashSet[String]()

/**
* Implements a work-around for https://github.com/sbt/sbt/issues/823
Expand Down Expand Up @@ -153,7 +156,7 @@ class ExtractAPI[GlobalType <: Global](
*/
private def lzy[S <: AnyRef](s: => S): xsbti.api.Lazy[S] = {
val lazyImpl = xsbti.api.SafeLazy.apply(Message(s))
pending += lazyImpl
pending.add(lazyImpl)
lazyImpl
}

Expand All @@ -165,7 +168,7 @@ class ExtractAPI[GlobalType <: Global](
if (pending.isEmpty)
structureCache.clear()
else {
val toProcess = pending.toList
val toProcess = pending.iterator().asScala.toList
pending.clear()
toProcess foreach { _.get() }
forceStructures()
Expand Down Expand Up @@ -358,9 +361,13 @@ class ExtractAPI[GlobalType <: Global](
}

private def structure(info: Type, s: Symbol): xsbti.api.Structure =
structureCache.getOrElseUpdate(s, mkStructure(info, s))
structureCache.computeIfAbsent(s, new java.util.function.Function[Symbol, xsbti.api.Structure] {
def apply(key: Symbol) = mkStructure(info, s)
})
private def structureWithInherited(info: Type, s: Symbol): xsbti.api.Structure =
structureCache.getOrElseUpdate(s, mkStructureWithInherited(info, s))
structureCache.computeIfAbsent(s, new java.util.function.Function[Symbol, xsbti.api.Structure] {
def apply(key: Symbol) = mkStructureWithInherited(info, s)
})

private def removeConstructors(ds: List[Symbol]): List[Symbol] = ds filter { !_.isConstructor }

Expand Down Expand Up @@ -492,10 +499,13 @@ class ExtractAPI[GlobalType <: Global](
else mapOver(tp)
}

private def processType(in: Symbol, t: Type): xsbti.api.Type =
typeCache.getOrElseUpdate((in, t), makeType(in, t))
private def processType(in: Symbol, t: Type): xsbti.api.Type = {
typeCache.computeIfAbsent((in, t),
new java.util.function.Function[(Symbol, Type), xsbti.api.Type] {
def apply(key: (Symbol, Type)) = makeType(in, t)
})
}
private def makeType(in: Symbol, t: Type): xsbti.api.Type = {

val dealiased = t match {
case TypeRef(_, sym, _) if sym.isAliasType => t.dealias
case _ => t
Expand Down Expand Up @@ -646,7 +656,10 @@ class ExtractAPI[GlobalType <: Global](
}

private def classLike(in: Symbol, c: Symbol): ClassLikeDef =
classLikeCache.getOrElseUpdate((in, c), mkClassLike(in, c))
classLikeCache.computeIfAbsent((in, c), mkClassLike0)
private val mkClassLike0 = new java.util.function.Function[(Symbol, Symbol), ClassLikeDef] {
def apply(k: ((Symbol, Symbol))) = mkClassLike(k._1, k._2)
}
private def mkClassLike(in: Symbol, c: Symbol): ClassLikeDef = {
// Normalize to a class symbol, and initialize it.
// (An object -- aka module -- also has a term symbol,
Expand Down
15 changes: 10 additions & 5 deletions internal/zinc-apiinfo/src/main/scala/xsbt/api/HashAPI.scala
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,19 @@ final class HashAPI private (

private[this] val visitedStructures = visitedMap[Structure]
private[this] val visitedClassLike = visitedMap[ClassLike]
private[this] def visitedMap[T <: AnyRef] = new mutable.AnyRefMap[T, List[Hash]]
private[this] def visit[T](map: mutable.Map[T, List[Hash]], t: T)(hashF: T => Unit): Unit = {
map.put(t, hash :: map.getOrElse(t, Nil)) match {
case Some(x :: _) => extend(x)
private[this] def visitedMap[T <: AnyRef] = new java.util.HashMap[T, List[Hash]]
private[this] def visit[T](map: java.util.HashMap[T, List[Hash]], t: T)(
hashF: T => Unit): Unit = {
val newVal = hash :: map.computeIfAbsent(t, new java.util.function.Function[T, List[Hash]] {
def apply(key: T) = Nil
})
map.put(t, newVal) match {
case x :: _ => extend(x)
case _ =>
hashF(t)
for (hs <- map(t))
map.get(t).foreach { hs =>
extend(hs)
}
map.put(t, hash :: Nil)
()
}
Expand Down

0 comments on commit 52959c9

Please sign in to comment.