Skip to content

Commit d1008fc

Browse files
committed
Reduce concurrent hashmap lookups
Reduce concurrent hashmap lookups in TreeIds by caching the last result in SourceInfo. This reduces lookups for typer/*.scala from 500K+ to 7.6K.
1 parent cb3515b commit d1008fc

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

compiler/src/dotty/tools/dotc/ast/TreeIds.scala

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,21 @@ object TreeIds {
1616
@sharable private[this] val counters = new ConcurrentHashMap[AbstractFile, AtomicInteger]
1717
@sharable private[this] val fileOfChunk = mutable.ArrayBuffer[AbstractFile]()
1818

19-
def nextId(implicit src: SourceInfo): Int = nextIdFor(src.srcfile)
20-
21-
def nextIdFor(file: AbstractFile): Int = {
22-
var ctr = counters.get(file)
19+
def nextIdFor(file: AbstractFile)(implicit src: SourceInfo): Int = {
20+
def getCounter: AtomicInteger =
21+
if (file `eq` src.srcfile) {
22+
var cachedCtr = src.counter
23+
if (cachedCtr == null) {
24+
cachedCtr = counters.get(file)
25+
src.counter = cachedCtr
26+
}
27+
cachedCtr
28+
}
29+
else counters.get(file)
30+
var ctr = getCounter
2331
if (ctr == null) {
2432
counters.putIfAbsent(file, new AtomicInteger)
25-
ctr = counters.get(file)
33+
ctr = getCounter
2634
}
2735
def recur(): Int = {
2836
val id = ctr.get

compiler/src/dotty/tools/dotc/core/Contexts.scala

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,16 @@ import util.Property.Key
3434
import util.Store
3535
import xsbti.AnalysisCallback
3636
import plugins._
37+
import java.util.concurrent.atomic.AtomicInteger
3738

3839
object Contexts {
3940

4041
trait SourceInfo {
4142
def srcfile: AbstractFile
43+
44+
private[this] var _counter: AtomicInteger = null
45+
def counter = _counter
46+
def counter_=(c: AtomicInteger) = _counter = c
4247
}
4348
object SourceInfo {
4449
def apply(src: AbstractFile) = new SourceInfo {
@@ -170,7 +175,10 @@ object Contexts {
170175

171176
/** The current source file */
172177
private[this] var _source: SourceFile = _
173-
protected def source_=(source: SourceFile): Unit = _source = source
178+
protected def source_=(source: SourceFile): Unit = {
179+
_source = source
180+
counter = null
181+
}
174182
def source: SourceFile = _source
175183

176184
def srcfile = source.file
@@ -247,8 +255,10 @@ object Contexts {
247255
getSource(TreeIds.fileOfId(id))
248256

249257
/** Sourcefile corresponding to given abstract file, memoized */
250-
def getSource(file: AbstractFile, codec: => Codec = Codec(settings.encoding.value)) =
258+
def getSource(file: AbstractFile, codec: => Codec = Codec(settings.encoding.value)) = {
259+
util.Stats.record("getSource")
251260
base.sources.getOrElseUpdate(file, new SourceFile(file, codec))
261+
}
252262

253263
def getSource(fileName: String): SourceFile = {
254264
val f = new PlainFile(Path(fileName))

0 commit comments

Comments
 (0)