Skip to content

Commit f9b2df5

Browse files
committed
Avoid use @volatile in toTypeName
1 parent 09fc620 commit f9b2df5

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

@@ -528,25 +527,25 @@ object Names {
528527

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

0 commit comments

Comments
 (0)