Skip to content

Commit c8e13d2

Browse files
committed
Avoid use @volatile in toTypeName
1 parent 0918782 commit c8e13d2

File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-14
lines changed

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

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -165,16 +165,15 @@ object Names {
165165
override def asTermName: TermName = this
166166

167167
@sharable // because it is only modified in the synchronized block of toTypeName.
168-
@volatile private var _typeName: TypeName = null
168+
private var myTypeName: TypeName = null
169+
// Note: no @volatile needed since type names are immutable and therefore safely published
169170

170-
override def toTypeName: TypeName = {
171-
if (_typeName == null)
171+
override def toTypeName: TypeName =
172+
if myTypeName == null then
172173
synchronized {
173-
if (_typeName == null)
174-
_typeName = new TypeName(this)
174+
if myTypeName == null then myTypeName = new TypeName(this)
175175
}
176-
_typeName
177-
}
176+
myTypeName
178177

179178
override def likeSpaced(name: Name): TermName = name.toTermName
180179

@@ -535,25 +534,25 @@ object Names {
535534

536535
def enterIfNew(cs: Array[Char], offset: Int, len: Int): SimpleName =
537536
Stats.record(statsItem("put"))
538-
val table = currentTable
539-
var idx = hashValue(cs, offset, len) & (table.length - 1)
540-
var name = table(idx).asInstanceOf[SimpleName]
537+
val myTable = currentTable // could be outdated under parallel execution
538+
var idx = hashValue(cs, offset, len) & (myTable.length - 1)
539+
var name = myTable(idx).asInstanceOf[SimpleName]
541540
while name != null do
542541
if name.length == len && Names.equals(name.start, cs, offset, len) then
543542
return name
544543
Stats.record(statsItem("miss"))
545-
idx = (idx + 1) & (table.length - 1)
546-
name = table(idx).asInstanceOf[SimpleName]
544+
idx = (idx + 1) & (myTable.length - 1)
545+
name = myTable(idx).asInstanceOf[SimpleName]
547546
Stats.record(statsItem("addEntryAt"))
548547
synchronized {
549-
if (table eq currentTable) && table(idx) == null then
548+
if (myTable eq currentTable) && myTable(idx) == null then
550549
// Our previous unsynchronized computation of the next free index is still correct.
551550
// This relies on the fact that table entries go from null to non-null, and then
552551
// stay the same. Note that we do not need the table or the entry in it to be
553552
// volatile since SimpleNames are immutable, and hence safely published.
554553
// The same holds for the chrs array. We might miss before the synchronized
555554
// on published characters but that would make name comparison false, which
556-
// means we end up in the synchronized block here, where we get the correct state
555+
// means we end up in the synchronized block here, where we get the correct state.
557556
name = SimpleName(nc, len)
558557
ensureCapacity(nc + len)
559558
Array.copy(cs, offset, chrs, nc, len)

0 commit comments

Comments
 (0)