@@ -42,8 +42,18 @@ object Contexts {
42
42
def nextTreeId : Int
43
43
}
44
44
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
+ */
45
50
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
+ }
47
57
}
48
58
49
59
private val (compilerCallbackLoc, store1) = Store .empty.newLocation[CompilerCallback ]()
@@ -236,6 +246,19 @@ object Contexts {
236
246
implicitsCache
237
247
}
238
248
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
+
239
262
/** Sourcefile corresponding to given abstract file, memoized */
240
263
def getSource (file : AbstractFile , codec : => Codec = Codec (settings.encoding.value)) =
241
264
base.sources.getOrElseUpdate(file, new SourceFile (file, codec))
@@ -466,7 +489,6 @@ object Contexts {
466
489
def uniqueNamedTypes : Uniques .NamedTypeUniques = base.uniqueNamedTypes
467
490
def uniques : util.HashSet [Type ] = base.uniques
468
491
def nextSymId : Int = base.nextSymId
469
- def nextTreeId : Int = base.nextTreeId
470
492
471
493
def initialize ()(implicit ctx : Context ): Unit = base.initialize()(ctx)
472
494
}
@@ -578,7 +600,6 @@ object Contexts {
578
600
579
601
@ sharable object NoContext extends Context {
580
602
val base : ContextBase = null
581
- override def nextTreeId = 0
582
603
override val implicits : ContextualImplicits = new ContextualImplicits (Nil , null )(this )
583
604
}
584
605
@@ -641,8 +662,8 @@ object Contexts {
641
662
private [core] var _nextSymId : Int = 0
642
663
def nextSymId : Int = { _nextSymId += 1 ; _nextSymId }
643
664
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 )
646
667
647
668
/** Sources that were loaded */
648
669
val sources : mutable.HashMap [AbstractFile , SourceFile ] = new mutable.HashMap [AbstractFile , SourceFile ]
@@ -720,6 +741,8 @@ object Contexts {
720
741
for ((_, set) <- uniqueSets) set.clear()
721
742
errorTypeMsg.clear()
722
743
sources.clear()
744
+ nextTreeIdBySource.clear()
745
+ sourceOfChunk.clear()
723
746
}
724
747
725
748
// Test that access is single threaded
0 commit comments