Skip to content

Commit c7f2f05

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 c7f2f05

File tree

1 file changed

+28
-5
lines changed

1 file changed

+28
-5
lines changed

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

Lines changed: 28 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+
@sharable 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]()
@@ -236,6 +246,19 @@ object Contexts {
236246
implicitsCache
237247
}
238248

249+
def nextTreeId: Int = {
250+
var id = base.nextTreeIdBySource.getOrElseUpdate(source, 0)
251+
if (id % TreeIdChunkSize == 0) {
252+
id = base.sourceOfChunk.length * TreeIdChunkSize
253+
base.sourceOfChunk += source
254+
}
255+
base.nextTreeIdBySource(source) = id + 1
256+
id
257+
}
258+
259+
def sourceOfTreeId(id: Int): SourceFile =
260+
base.sourceOfChunk(id / TreeIdChunkSize)
261+
239262
/** Sourcefile corresponding to given abstract file, memoized */
240263
def getSource(file: AbstractFile, codec: => Codec = Codec(settings.encoding.value)) =
241264
base.sources.getOrElseUpdate(file, new SourceFile(file, codec))
@@ -466,7 +489,6 @@ object Contexts {
466489
def uniqueNamedTypes: Uniques.NamedTypeUniques = base.uniqueNamedTypes
467490
def uniques: util.HashSet[Type] = base.uniques
468491
def nextSymId: Int = base.nextSymId
469-
def nextTreeId: Int = base.nextTreeId
470492

471493
def initialize()(implicit ctx: Context): Unit = base.initialize()(ctx)
472494
}
@@ -578,7 +600,6 @@ object Contexts {
578600

579601
@sharable object NoContext extends Context {
580602
val base: ContextBase = null
581-
override def nextTreeId = 0
582603
override val implicits: ContextualImplicits = new ContextualImplicits(Nil, null)(this)
583604
}
584605

@@ -641,8 +662,8 @@ object Contexts {
641662
private[core] var _nextSymId: Int = 0
642663
def nextSymId: Int = { _nextSymId += 1; _nextSymId }
643664

644-
private[dotc] var _nextTreeId: Int = 0
645-
def nextTreeId: Int = { _nextTreeId += 1; _nextTreeId }
665+
private[core] var nextTreeIdBySource = new mutable.HashMap[SourceFile, Int]
666+
private[core] var sourceOfChunk = mutable.ArrayBuffer[SourceFile](NoSource)
646667

647668
/** Sources that were loaded */
648669
val sources: mutable.HashMap[AbstractFile, SourceFile] = new mutable.HashMap[AbstractFile, SourceFile]
@@ -720,6 +741,8 @@ object Contexts {
720741
for ((_, set) <- uniqueSets) set.clear()
721742
errorTypeMsg.clear()
722743
sources.clear()
744+
nextTreeIdBySource.clear()
745+
sourceOfChunk.clear()
723746
}
724747

725748
// Test that access is single threaded

0 commit comments

Comments
 (0)