Skip to content

Commit 2e99976

Browse files
committed
Allocate tree ids globally
Tree ids survive compilation runs, need to be allocated globally.
1 parent 4625098 commit 2e99976

File tree

14 files changed

+231
-217
lines changed

14 files changed

+231
-217
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,23 +43,23 @@ object desugar {
4343

4444
// ----- DerivedTypeTrees -----------------------------------
4545

46-
class SetterParamTree(implicit @transientParam ids: TreeIds) extends DerivedTypeTree {
46+
class SetterParamTree(implicit @transientParam src: SourceInfo) extends DerivedTypeTree {
4747
def derivedTree(sym: Symbol)(implicit ctx: Context): tpd.TypeTree = tpd.TypeTree(sym.info.resultType)
4848
}
4949

50-
class TypeRefTree(implicit @transientParam ids: TreeIds) extends DerivedTypeTree {
50+
class TypeRefTree(implicit @transientParam src: SourceInfo) extends DerivedTypeTree {
5151
def derivedTree(sym: Symbol)(implicit ctx: Context): tpd.TypeTree = tpd.TypeTree(sym.typeRef)
5252
}
5353

54-
class TermRefTree(implicit @transientParam ids: TreeIds) extends DerivedTypeTree {
54+
class TermRefTree(implicit @transientParam src: SourceInfo) extends DerivedTypeTree {
5555
def derivedTree(sym: Symbol)(implicit ctx: Context): tpd.Tree = tpd.ref(sym)
5656
}
5757

5858
/** A type tree that computes its type from an existing parameter.
5959
* @param suffix String difference between existing parameter (call it `P`) and parameter owning the
6060
* DerivedTypeTree (call it `O`). We have: `O.name == P.name + suffix`.
6161
*/
62-
class DerivedFromParamTree(suffix: String)(implicit @transientParam ids: TreeIds) extends DerivedTypeTree {
62+
class DerivedFromParamTree(suffix: String)(implicit @transientParam src: SourceInfo) extends DerivedTypeTree {
6363

6464
/** Make sure that for all enclosing module classes their companion classes
6565
* are completed. Reason: We need the constructor of such companion classes to
@@ -1025,7 +1025,7 @@ object desugar {
10251025
mayNeedSetter
10261026
}
10271027

1028-
private def derivedDefDef(original: Tree, named: NameTree, tpt: Tree, rhs: Tree, mods: Modifiers)(implicit ids: TreeIds) =
1028+
private def derivedDefDef(original: Tree, named: NameTree, tpt: Tree, rhs: Tree, mods: Modifiers)(implicit src: SourceInfo) =
10291029
DefDef(named.name.asTermName, Nil, Nil, tpt, rhs)
10301030
.withMods(mods)
10311031
.withPos(original.pos.withPoint(named.pos.start))
@@ -1389,6 +1389,6 @@ object desugar {
13891389
buf.toList
13901390
}
13911391

1392-
private class IrrefutableGenFrom(pat: Tree, expr: Tree)(implicit @transientParam ids: TreeIds)
1392+
private class IrrefutableGenFrom(pat: Tree, expr: Tree)(implicit @transientParam src: SourceInfo)
13931393
extends GenFrom(pat, expr)
13941394
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ object DesugarEnums {
7373
else if (isEnumCase(cdef)) cdef.withMods(cdef.mods.withFlags(cdef.mods.flags | Final))
7474
else cdef
7575

76-
private def valuesDot(name: String)(implicit ids: TreeIds) =
76+
private def valuesDot(name: String)(implicit src: SourceInfo) =
7777
Select(Ident(nme.DOLLAR_VALUES), name.toTermName)
7878
private def registerCall(implicit ctx: Context): List[Tree] =
7979
if (enumClass.typeParams.nonEmpty) Nil

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package dotty.tools.dotc
22
package ast
33

44
import util.Positions._
5-
import core.Contexts.{Context, TreeIds}
5+
import core.Contexts.{Context, SourceInfo}
66
import core.Decorators._
77
import core.Flags.{JavaDefined, Extension}
88
import core.StdNames.nme
@@ -27,26 +27,26 @@ abstract class Positioned extends Product {
2727
if (pos.exists) setChildPositions(pos.toSynthetic)
2828
}
2929

30-
def cloned(implicit ids: TreeIds): Positioned
30+
def cloned(implicit src: SourceInfo): Positioned
3131

3232
/** A positioned item like this one with the position set to `pos`.
3333
* if the positioned item is source-derived, a clone is returned.
3434
* If the positioned item is synthetic, the position is updated
3535
* destructively and the item itself is returned.
3636
*/
37-
def withPos(pos: Position)(implicit ids: TreeIds): this.type = {
37+
def withPos(pos: Position)(implicit src: SourceInfo): this.type = {
3838
val newpd = if (pos == curPos || curPos.isSynthetic) this else cloned
3939
newpd.setPos(pos)
4040
newpd.asInstanceOf[this.type]
4141
}
4242

43-
def withPos(posd: Positioned)(implicit ids: TreeIds): this.type =
43+
def withPos(posd: Positioned)(implicit src: SourceInfo): this.type =
4444
if (posd == null) this else withPos(posd.pos)
4545

4646
/** This item with a position that's the union of the given `pos` and the
4747
* current position.
4848
*/
49-
def addPos(pos: Position)(implicit ids: TreeIds): this.type = withPos(pos union this.pos)
49+
def addPos(pos: Position)(implicit src: SourceInfo): this.type = withPos(pos union this.pos)
5050

5151
/** Set position of this tree only, without performing
5252
* any checks of consistency with - or updates of - other positions.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package dotty.tools
2+
package dotc
3+
package ast
4+
5+
import core.Contexts.SourceInfo
6+
import io.AbstractFile
7+
import scala.annotation.internal.sharable
8+
import scala.collection.mutable
9+
import java.util.concurrent.ConcurrentHashMap
10+
import java.util.concurrent.atomic.AtomicInteger
11+
12+
object TreeIds {
13+
14+
private final val ChunkSize = 1024
15+
16+
@sharable private[this] val counters = new ConcurrentHashMap[AbstractFile, AtomicInteger]
17+
@sharable private[this] val fileOfChunk = mutable.ArrayBuffer[AbstractFile]()
18+
19+
def nextId(implicit src: SourceInfo): Int = {
20+
val file = src.source.file
21+
var ctr = counters.get(file)
22+
if (ctr == null) {
23+
counters.putIfAbsent(file, new AtomicInteger)
24+
ctr = counters.get(file)
25+
}
26+
def recur(): Int = {
27+
val id = ctr.get
28+
if (id % ChunkSize == 0) newChunk(file, ctr)
29+
else if (ctr.compareAndSet(id, id + 1)) id
30+
else recur()
31+
}
32+
recur()
33+
}
34+
35+
private def newChunk(file: AbstractFile, ctr: AtomicInteger): Int = synchronized {
36+
val id = fileOfChunk.length * ChunkSize
37+
fileOfChunk += file
38+
ctr.set(id + 1)
39+
id
40+
}
41+
42+
def fileOfId(id: Int): AbstractFile =
43+
fileOfChunk(id / ChunkSize)
44+
}

0 commit comments

Comments
 (0)