Skip to content

Commit 0050ca9

Browse files
committed
Produce tree ids in chunks
Tree ids are now allocated in chunks. All ids in the same chunk were produced by contexts with the same source file.
1 parent 9aefa10 commit 0050ca9

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

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

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,18 @@ object Contexts {
4242
def nextTreeId: Int
4343
}
4444

45+
private final val TreeIdChunkSize = 1024
46+
47+
/** Id generator for compiler-global trees that are not created in a context.
48+
* We reserve the first chunk of ids for these
49+
*/
4550
object GlobalTreeIds extends TreeIds {
46-
def nextTreeId = 0
51+
private var _nextTreeId: Int = 0
52+
def nextTreeId: Int = synchronized {
53+
_nextTreeId += 1
54+
assert(_nextTreeId < TreeIdChunkSize)
55+
_nextTreeId
56+
}
4757
}
4858

4959
private val (compilerCallbackLoc, store1) = Store.empty.newLocation[CompilerCallback]()
@@ -466,7 +476,13 @@ object Contexts {
466476
def uniqueNamedTypes: Uniques.NamedTypeUniques = base.uniqueNamedTypes
467477
def uniques: util.HashSet[Type] = base.uniques
468478
def nextSymId: Int = base.nextSymId
469-
def nextTreeId: Int = base.nextTreeId
479+
480+
def nextTreeId: Int = {
481+
var id = base.nextTreeIdBySource.getOrElseUpdate(source, 0)
482+
if (id % TreeIdChunkSize == 0) id = base.reserveChunk()
483+
base.nextTreeIdBySource(source) = id + 1
484+
id
485+
}
470486

471487
def initialize()(implicit ctx: Context): Unit = base.initialize()(ctx)
472488
}
@@ -578,7 +594,6 @@ object Contexts {
578594

579595
@sharable object NoContext extends Context {
580596
val base: ContextBase = null
581-
override def nextTreeId = 0
582597
override val implicits: ContextualImplicits = new ContextualImplicits(Nil, null)(this)
583598
}
584599

@@ -641,8 +656,13 @@ object Contexts {
641656
private[core] var _nextSymId: Int = 0
642657
def nextSymId: Int = { _nextSymId += 1; _nextSymId }
643658

644-
private[dotc] var _nextTreeId: Int = 0
645-
def nextTreeId: Int = { _nextTreeId += 1; _nextTreeId }
659+
private[this] var chunksUsed: Int = 1
660+
private[core] var nextTreeIdBySource = new mutable.HashMap[SourceFile, Int]
661+
662+
/** Reserve a new chunk.
663+
* @return First id address in that chunk
664+
*/
665+
private[core] def reserveChunk(): Int = try chunksUsed * TreeIdChunkSize finally chunksUsed += 1
646666

647667
/** Sources that were loaded */
648668
val sources: mutable.HashMap[AbstractFile, SourceFile] = new mutable.HashMap[AbstractFile, SourceFile]
@@ -720,6 +740,7 @@ object Contexts {
720740
for ((_, set) <- uniqueSets) set.clear()
721741
errorTypeMsg.clear()
722742
sources.clear()
743+
nextTreeIdBySource.clear()
723744
}
724745

725746
// Test that access is single threaded

0 commit comments

Comments
 (0)