Skip to content

Commit e8bd565

Browse files
committed
Cleanup
1 parent 8d7e31f commit e8bd565

File tree

2 files changed

+72
-74
lines changed

2 files changed

+72
-74
lines changed

compiler/src/dotty/tools/dotc/transform/LazyVals.scala

Lines changed: 40 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,23 @@
1-
package dotty.tools.dotc
2-
package transform
1+
package dotty.tools.dotc.transform
32

4-
import dotty.tools.dotc.core.Annotations.Annotation
3+
import java.util.IdentityHashMap
54

6-
import scala.collection.mutable
7-
import core._
8-
import Contexts._
9-
import Symbols._
10-
import Decorators._
11-
import NameKinds._
12-
import Types._
13-
import Flags._
14-
import StdNames.nme
15-
import dotty.tools.dotc.transform.MegaPhase._
165
import dotty.tools.dotc.ast.tpd
6+
import dotty.tools.dotc.core.Annotations.Annotation
177
import dotty.tools.dotc.core.Constants.Constant
18-
import dotty.tools.dotc.core.Types.MethodType
19-
import SymUtils._
8+
import dotty.tools.dotc.core.Contexts.Context
9+
import dotty.tools.dotc.core.Decorators._
2010
import dotty.tools.dotc.core.DenotTransformers.IdentityDenotTransformer
21-
import Erasure.Boxing.adaptToType
11+
import dotty.tools.dotc.core.Flags._
12+
import dotty.tools.dotc.core.NameKinds.{LazyBitMapName, LazyLocalInitName, LazyLocalName}
13+
import dotty.tools.dotc.core.StdNames.nme
14+
import dotty.tools.dotc.core.Symbols._
15+
import dotty.tools.dotc.core.Types._
16+
import dotty.tools.dotc.core.{Names, StdNames}
17+
import dotty.tools.dotc.transform.MegaPhase.MiniPhase
18+
import dotty.tools.dotc.transform.SymUtils._
2219

23-
import java.util.IdentityHashMap
20+
import scala.collection.mutable
2421

2522
class LazyVals extends MiniPhase with IdentityDenotTransformer {
2623
import LazyVals._
@@ -41,10 +38,10 @@ class LazyVals extends MiniPhase with IdentityDenotTransformer {
4138

4239
def transformer: LazyVals = new LazyVals
4340

44-
val containerFlags: FlagSet = Flags.Synthetic | Flags.Mutable | Flags.Lazy
45-
val initFlags: FlagSet = Flags.Synthetic | Flags.Method
41+
val containerFlags: FlagSet = Synthetic | Mutable | Lazy
42+
val initFlags: FlagSet = Synthetic | Method
4643

47-
val containerFlagsMask: FlagSet = Flags.Method | Flags.Lazy | Flags.Accessor | Flags.Module
44+
val containerFlagsMask: FlagSet = Method | Lazy | Accessor | Module
4845

4946
/** A map of lazy values to the fields they should null after initialization. */
5047
private[this] var lazyValNullables: IdentityHashMap[Symbol, mutable.ListBuffer[Symbol]] = _
@@ -72,22 +69,22 @@ class LazyVals extends MiniPhase with IdentityDenotTransformer {
7269

7370
def transformLazyVal(tree: ValOrDefDef)(implicit ctx: Context): Tree = {
7471
val sym = tree.symbol
75-
if (!(sym is Flags.Lazy) ||
76-
sym.owner.is(Flags.Trait) || // val is accessor, lazy field will be implemented in subclass
77-
(sym.isStatic && sym.is(Flags.Module, butNot = Flags.Method))) // static module vals are implemented in the JVM by lazy loading
72+
if (!(sym is Lazy) ||
73+
sym.owner.is(Trait) || // val is accessor, lazy field will be implemented in subclass
74+
(sym.isStatic && sym.is(Module, butNot = Method))) // static module vals are implemented in the JVM by lazy loading
7875
tree
7976
else {
8077
val isField = sym.owner.isClass
8178
if (isField) {
8279
if (sym.isVolatile ||
83-
(sym.is(Flags.Module)/* || ctx.scala2Mode*/) &&
80+
(sym.is(Module)/* || ctx.scala2Mode*/) &&
8481
// TODO assume @volatile once LazyVals uses static helper constructs instead of
8582
// ones in the companion object.
86-
!sym.is(Flags.Synthetic))
83+
!sym.is(Synthetic))
8784
// module class is user-defined.
8885
// Should be threadsafe, to mimic safety guaranteed by global object
8986
transformMemberDefVolatile(tree)
90-
else if (sym.is(Flags.Module)) // synthetic module
87+
else if (sym.is(Module)) // synthetic module
9188
transformSyntheticModule(tree)
9289
else
9390
transformMemberDefNonVolatile(tree)
@@ -123,7 +120,7 @@ class LazyVals extends MiniPhase with IdentityDenotTransformer {
123120
def transformSyntheticModule(tree: ValOrDefDef)(implicit ctx: Context): Thicket = {
124121
val sym = tree.symbol
125122
val holderSymbol = ctx.newSymbol(sym.owner, LazyLocalName.fresh(sym.asTerm.name),
126-
Flags.Synthetic, sym.info.widen.resultType).enteredAfter(this)
123+
Synthetic, sym.info.widen.resultType).enteredAfter(this)
127124
val field = ValDef(holderSymbol, tree.rhs.changeOwnerAfter(sym, holderSymbol, this))
128125
val getter = DefDef(sym.asTerm, ref(holderSymbol))
129126
Thicket(field, getter)
@@ -187,8 +184,8 @@ class LazyVals extends MiniPhase with IdentityDenotTransformer {
187184
// need to bring containers to start of method
188185
val (holders, stats) =
189186
trees.partition {
190-
_.symbol.flags.&~(Flags.Touched) == containerFlags
191-
// Filtering out Flags.Touched is not required currently, as there are no LazyTypes involved here
187+
_.symbol.flags.&~(Touched) == containerFlags
188+
// Filtering out Touched is not required currently, as there are no LazyTypes involved here
192189
// but just to be more safe
193190
}
194191
holders:::stats
@@ -198,7 +195,7 @@ class LazyVals extends MiniPhase with IdentityDenotTransformer {
198195
val nullConst = Literal(Constant(null))
199196
nullables.map { field =>
200197
assert(field.isField)
201-
field.setFlag(Flags.Mutable)
198+
field.setFlag(Mutable)
202199
ref(field).becomes(nullConst)
203200
}
204201
}
@@ -252,10 +249,10 @@ class LazyVals extends MiniPhase with IdentityDenotTransformer {
252249
def transformMemberDefNonVolatile(x: ValOrDefDef)(implicit ctx: Context): Thicket = {
253250
val claz = x.symbol.owner.asClass
254251
val tpe = x.tpe.widen.resultType.widen
255-
assert(!(x.symbol is Flags.Mutable))
252+
assert(!(x.symbol is Mutable))
256253
val containerName = LazyLocalName.fresh(x.name.asTermName)
257254
val containerSymbol = ctx.newSymbol(claz, containerName,
258-
x.symbol.flags &~ containerFlagsMask | containerFlags | Flags.Private,
255+
x.symbol.flags &~ containerFlagsMask | containerFlags | Private,
259256
tpe, coord = x.symbol.coord
260257
).enteredAfter(this)
261258

@@ -266,7 +263,7 @@ class LazyVals extends MiniPhase with IdentityDenotTransformer {
266263
}
267264
else {
268265
val flagName = LazyBitMapName.fresh(x.name.asTermName)
269-
val flagSymbol = ctx.newSymbol(x.symbol.owner, flagName, containerFlags | Flags.Private, defn.BooleanType).enteredAfter(this)
266+
val flagSymbol = ctx.newSymbol(x.symbol.owner, flagName, containerFlags | Private, defn.BooleanType).enteredAfter(this)
270267
val flag = ValDef(flagSymbol, Literal(Constant(false)))
271268
Thicket(containerTree, flag, mkNonThreadSafeDef(x.symbol, flagSymbol, containerSymbol, x.rhs))
272269
}
@@ -314,13 +311,12 @@ class LazyVals extends MiniPhase with IdentityDenotTransformer {
314311
stateMask: Tree,
315312
casFlag: Tree,
316313
setFlagState: Tree,
317-
waitOnLock: Tree,
318-
nullables: List[Symbol])(implicit ctx: Context): DefDef = {
314+
waitOnLock: Tree)(implicit ctx: Context): DefDef = {
319315
val initState = Literal(Constant(0))
320316
val computeState = Literal(Constant(1))
321317
val computedState = Literal(Constant(3))
322318

323-
val thiz = This(claz)(ctx.fresh.setOwner(claz))
319+
val thiz = This(claz)
324320
val fieldId = Literal(Constant(ord))
325321

326322
val flagSymbol = ctx.newSymbol(methodSymbol, lazyNme.flag, Synthetic, defn.LongType)
@@ -344,7 +340,7 @@ class LazyVals extends MiniPhase with IdentityDenotTransformer {
344340
}
345341

346342
val retryCase = {
347-
val caseSymbol = ctx.newSymbol(methodSymbol, nme.DEFAULT_EXCEPTION_NAME, Flags.Synthetic, defn.ThrowableType)
343+
val caseSymbol = ctx.newSymbol(methodSymbol, nme.DEFAULT_EXCEPTION_NAME, Synthetic, defn.ThrowableType)
348344
val triggerRetry = setFlagState.appliedTo(thiz, offset, initState, fieldId)
349345
CaseDef(
350346
Bind(caseSymbol, ref(caseSymbol)),
@@ -374,7 +370,7 @@ class LazyVals extends MiniPhase with IdentityDenotTransformer {
374370
}
375371

376372
def transformMemberDefVolatile(x: ValOrDefDef)(implicit ctx: Context): Thicket = {
377-
assert(!(x.symbol is Flags.Mutable))
373+
assert(!(x.symbol is Mutable))
378374

379375
val tpe = x.tpe.widen.resultType.widen
380376
val claz = x.symbol.owner.asClass
@@ -385,9 +381,9 @@ class LazyVals extends MiniPhase with IdentityDenotTransformer {
385381
var flag: Tree = EmptyTree
386382
var ord = 0
387383

388-
def offsetName(id: Int) = (StdNames.nme.LAZY_FIELD_OFFSET + (if(x.symbol.owner.is(Flags.Module)) "_m_" else "") + id.toString).toTermName
384+
def offsetName(id: Int) = (StdNames.nme.LAZY_FIELD_OFFSET + (if (x.symbol.owner.is(Module)) "_m_" else "") + id.toString).toTermName
389385

390-
// compute or create appropriate offsetSymol, bitmap and bits used by current ValDef
386+
// compute or create appropriate offsetSymbol, bitmap and bits used by current ValDef
391387
appendOffsetDefs.get(claz) match {
392388
case Some(info) =>
393389
val flagsPerLong = (64 / dotty.runtime.LazyVals.BITS_PER_LAZY_VAL).toInt
@@ -397,10 +393,10 @@ class LazyVals extends MiniPhase with IdentityDenotTransformer {
397393
val offsetById = offsetName(id)
398394
if (ord != 0) { // there are unused bits in already existing flag
399395
offsetSymbol = claz.info.decl(offsetById)
400-
.suchThat(sym => (sym is Flags.Synthetic) && sym.isTerm)
396+
.suchThat(sym => (sym is Synthetic) && sym.isTerm)
401397
.symbol.asTerm
402398
} else { // need to create a new flag
403-
offsetSymbol = ctx.newSymbol(claz, offsetById, Flags.Synthetic, defn.LongType).enteredAfter(this)
399+
offsetSymbol = ctx.newSymbol(claz, offsetById, Synthetic, defn.LongType).enteredAfter(this)
404400
offsetSymbol.addAnnotation(Annotation(defn.ScalaStaticAnnot))
405401
val flagName = (StdNames.nme.BITMAP_PREFIX + id.toString).toTermName
406402
val flagSymbol = ctx.newSymbol(claz, flagName, containerFlags, defn.LongType).enteredAfter(this)
@@ -410,7 +406,7 @@ class LazyVals extends MiniPhase with IdentityDenotTransformer {
410406
}
411407

412408
case None =>
413-
offsetSymbol = ctx.newSymbol(claz, offsetName(0), Flags.Synthetic, defn.LongType).enteredAfter(this)
409+
offsetSymbol = ctx.newSymbol(claz, offsetName(0), Synthetic, defn.LongType).enteredAfter(this)
414410
offsetSymbol.addAnnotation(Annotation(defn.ScalaStaticAnnot))
415411
val flagName = (StdNames.nme.BITMAP_PREFIX + "0").toTermName
416412
val flagSymbol = ctx.newSymbol(claz, flagName, containerFlags, defn.LongType).enteredAfter(this)
@@ -430,9 +426,8 @@ class LazyVals extends MiniPhase with IdentityDenotTransformer {
430426
val wait = Select(ref(helperModule), lazyNme.RLazyVals.wait4Notification)
431427
val state = Select(ref(helperModule), lazyNme.RLazyVals.state)
432428
val cas = Select(ref(helperModule), lazyNme.RLazyVals.cas)
433-
val nullables = nullableFor(x.symbol)
434429

435-
val accessor = mkThreadSafeDef(x.symbol.asTerm, claz, ord, containerSymbol, x.rhs, tpe, offset, getFlag, state, cas, setFlag, wait, nullables)
430+
val accessor = mkThreadSafeDef(x.symbol.asTerm, claz, ord, containerSymbol, x.rhs, tpe, offset, getFlag, state, cas, setFlag, wait)
436431
if (flag eq EmptyTree)
437432
Thicket(containerTree, accessor)
438433
else Thicket(containerTree, flag, accessor)

library/src/dotty/runtime/LazyVals.scala

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
package dotty.runtime
22

3-
import scala.forceInline
4-
53
/**
64
* Helper methods used in thread-safe lazy vals.
75
*/
86
object LazyVals {
9-
private val unsafe: sun.misc.Unsafe =
7+
private[this] val unsafe: sun.misc.Unsafe =
108
classOf[sun.misc.Unsafe].getDeclaredFields.find { field =>
119
field.getType == classOf[sun.misc.Unsafe] && {
1210
field.setAccessible(true)
@@ -20,24 +18,45 @@ object LazyVals {
2018
}
2119
}
2220

21+
private[this] val base: Int = {
22+
val processors = java.lang.Runtime.getRuntime.availableProcessors()
23+
8 * processors * processors
24+
}
25+
private[this] val monitors: Array[Object] =
26+
Array.tabulate(base)(_ => new Object)
27+
28+
private def getMonitor(obj: Object, fieldId: Int = 0) = {
29+
var id = (
30+
/*java.lang.System.identityHashCode(obj) + */ // should be here, but #548
31+
fieldId) % base
32+
33+
if (id < 0) id += base
34+
monitors(id)
35+
}
36+
37+
private final val LAZY_VAL_MASK = 3L
38+
private final val debug = false
39+
40+
/* ------------- Start of public API ------------- */
41+
2342
final val BITS_PER_LAZY_VAL = 2L
24-
final val LAZY_VAL_MASK = 3L
25-
final val debug = false
2643

27-
@forceInline def STATE(cur: Long, ord: Int) = {
44+
def STATE(cur: Long, ord: Int): Long = {
2845
val r = (cur >> (ord * BITS_PER_LAZY_VAL)) & LAZY_VAL_MASK
2946
if (debug)
3047
println(s"STATE($cur, $ord) = $r")
3148
r
3249
}
33-
@forceInline def CAS(t: Object, offset: Long, e: Long, v: Int, ord: Int) = {
50+
51+
def CAS(t: Object, offset: Long, e: Long, v: Int, ord: Int): Boolean = {
3452
if (debug)
3553
println(s"CAS($t, $offset, $e, $v, $ord)")
3654
val mask = ~(LAZY_VAL_MASK << ord * BITS_PER_LAZY_VAL)
3755
val n = (e & mask) | (v.toLong << (ord * BITS_PER_LAZY_VAL))
38-
compareAndSet(t, offset, e, n)
56+
unsafe.compareAndSwapLong(t, offset, e, n)
3957
}
40-
@forceInline def setFlag(t: Object, offset: Long, v: Int, ord: Int) = {
58+
59+
def setFlag(t: Object, offset: Long, v: Int, ord: Int): Unit = {
4160
if (debug)
4261
println(s"setFlag($t, $offset, $v, $ord)")
4362
var retry = true
@@ -56,7 +75,8 @@ object LazyVals {
5675
}
5776
}
5877
}
59-
@forceInline def wait4Notification(t: Object, offset: Long, cur: Long, ord: Int) = {
78+
79+
def wait4Notification(t: Object, offset: Long, cur: Long, ord: Int): Unit = {
6080
if (debug)
6181
println(s"wait4Notification($t, $offset, $cur, $ord)")
6282
var retry = true
@@ -75,29 +95,13 @@ object LazyVals {
7595
}
7696
}
7797

78-
@forceInline def compareAndSet(t: Object, off: Long, e: Long, v: Long) = unsafe.compareAndSwapLong(t, off, e, v)
79-
@forceInline def get(t: Object, off: Long) = {
98+
def get(t: Object, off: Long): Long = {
8099
if (debug)
81100
println(s"get($t, $off)")
82101
unsafe.getLongVolatile(t, off)
83102
}
84103

85-
val processors: Int = java.lang.Runtime.getRuntime.availableProcessors()
86-
val base: Int = 8 * processors * processors
87-
val monitors: Array[Object] = (0 to base).map {
88-
x => new Object()
89-
}.toArray
90-
91-
@forceInline def getMonitor(obj: Object, fieldId: Int = 0) = {
92-
var id = (
93-
/*java.lang.System.identityHashCode(obj) + */ // should be here, but #548
94-
fieldId) % base
95-
96-
if (id < 0) id += base
97-
monitors(id)
98-
}
99-
100-
@forceInline def getOffset(clz: Class[_], name: String) = {
104+
def getOffset(clz: Class[_], name: String): Long = {
101105
val r = unsafe.objectFieldOffset(clz.getDeclaredField(name))
102106
if (debug)
103107
println(s"getOffset($clz, $name) = $r")
@@ -109,7 +113,6 @@ object LazyVals {
109113
final val cas = "CAS"
110114
final val setFlag = "setFlag"
111115
final val wait4Notification = "wait4Notification"
112-
final val compareAndSet = "compareAndSet"
113116
final val get = "get"
114117
final val getOffset = "getOffset"
115118
}

0 commit comments

Comments
 (0)