Skip to content

Commit

Permalink
Get rid of counterproductive per-run caches, use java collections
Browse files Browse the repository at this point in the history
  • Loading branch information
retronym committed Feb 20, 2018
1 parent 1cb115a commit 006cb12
Showing 1 changed file with 19 additions and 18 deletions.
37 changes: 19 additions & 18 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,23 +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 object PerRunCachesCompat {
def newAnyRefMap[K, V]() = perRunCaches.newMap[K, V]()
}
private implicit def perRunCachesCompat(p: perRunCaches.type): PerRunCachesCompat.type =
PerRunCachesCompat

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 @@ -159,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 @@ -171,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 @@ -335,9 +332,9 @@ class ExtractAPI[GlobalType <: Global](
}

private def structure(info: Type, s: Symbol): xsbti.api.Structure =
structureCache.getOrElseUpdate(s, mkStructure(info, s))
structureCache.computeIfAbsent(s, key => mkStructure(info, s))
private def structureWithInherited(info: Type, s: Symbol): xsbti.api.Structure =
structureCache.getOrElseUpdate(s, mkStructureWithInherited(info, s))
structureCache.computeIfAbsent(s, key => mkStructureWithInherited(info, s))

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

Expand Down Expand Up @@ -469,8 +466,9 @@ 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), (key) => makeType(in, t))
}
private def makeType(in: Symbol, t: Type): xsbti.api.Type = {

val dealiased = t match {
Expand Down Expand Up @@ -623,7 +621,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: java.util.function.Function[(Symbol, Symbol), ClassLikeDef] = {
case (in, c) => mkClassLike(in, c)
}
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

0 comments on commit 006cb12

Please sign in to comment.