Skip to content

Commit b37b9e3

Browse files
committed
Extend synchronized scope in newTermName
Just noted that the previous scope might have been too small. We compute the bucket index with the table size before going into the synchronized. But that might mean we see a stale table size. Let's see what this gives us.
1 parent 17a5066 commit b37b9e3

File tree

1 file changed

+45
-48
lines changed

1 file changed

+45
-48
lines changed

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

Lines changed: 45 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -240,64 +240,61 @@ object Names {
240240
/** Create a term name from the characters in cs[offset..offset+len-1].
241241
* Assume they are already encoded.
242242
*/
243-
def termName(cs: Array[Char], offset: Int, len: Int): TermName = {
243+
def termName(cs: Array[Char], offset: Int, len: Int): TermName = synchronized {
244244
util.Stats.record("termName")
245245
val h = hashValue(cs, offset, len) & (table.size - 1)
246246

247-
synchronized {
248-
249-
/** Make sure the capacity of the character array is at least `n` */
250-
def ensureCapacity(n: Int) =
251-
if (n > chrs.length) {
252-
val newchrs = new Array[Char](chrs.length * 2)
253-
chrs.copyToArray(newchrs)
254-
chrs = newchrs
255-
}
256-
257-
/** Enter characters into chrs array. */
258-
def enterChars(): Unit = {
259-
ensureCapacity(nc + len)
260-
var i = 0
261-
while (i < len) {
262-
chrs(nc + i) = cs(offset + i)
263-
i += 1
264-
}
265-
nc += len
247+
/** Make sure the capacity of the character array is at least `n` */
248+
def ensureCapacity(n: Int) =
249+
if (n > chrs.length) {
250+
val newchrs = new Array[Char](chrs.length * 2)
251+
chrs.copyToArray(newchrs)
252+
chrs = newchrs
266253
}
267254

268-
/** Rehash chain of names */
269-
def rehash(name: TermName): Unit =
270-
if (name != null) {
271-
val oldNext = name.next
272-
val h = hashValue(chrs, name.start, name.length) & (table.size - 1)
273-
name.next = table(h)
274-
table(h) = name
275-
rehash(oldNext)
276-
}
255+
/** Enter characters into chrs array. */
256+
def enterChars(): Unit = {
257+
ensureCapacity(nc + len)
258+
var i = 0
259+
while (i < len) {
260+
chrs(nc + i) = cs(offset + i)
261+
i += 1
262+
}
263+
nc += len
264+
}
277265

278-
/** Make sure the hash table is large enough for the given load factor */
279-
def incTableSize() = {
280-
size += 1
281-
if (size.toDouble / table.size > fillFactor) {
282-
val oldTable = table
283-
table = new Array[TermName](table.size * 2)
284-
for (i <- 0 until oldTable.size) rehash(oldTable(i))
285-
}
266+
/** Rehash chain of names */
267+
def rehash(name: TermName): Unit =
268+
if (name != null) {
269+
val oldNext = name.next
270+
val h = hashValue(chrs, name.start, name.length) & (table.size - 1)
271+
name.next = table(h)
272+
table(h) = name
273+
rehash(oldNext)
286274
}
287275

288-
val next = table(h)
289-
var name = next
290-
while (name ne null) {
291-
if (name.length == len && equals(name.start, cs, offset, len))
292-
return name
293-
name = name.next
276+
/** Make sure the hash table is large enough for the given load factor */
277+
def incTableSize() = {
278+
size += 1
279+
if (size.toDouble / table.size > fillFactor) {
280+
val oldTable = table
281+
table = new Array[TermName](table.size * 2)
282+
for (i <- 0 until oldTable.size) rehash(oldTable(i))
294283
}
295-
name = new TermName(nc, len, next)
296-
enterChars()
297-
table(h) = name
298-
incTableSize()
299-
name
300284
}
285+
286+
val next = table(h)
287+
var name = next
288+
while (name ne null) {
289+
if (name.length == len && equals(name.start, cs, offset, len))
290+
return name
291+
name = name.next
292+
}
293+
name = new TermName(nc, len, next)
294+
enterChars()
295+
table(h) = name
296+
incTableSize()
297+
name
301298
}
302299

303300
/** Create a type name from the characters in cs[offset..offset+len-1].

0 commit comments

Comments
 (0)