Skip to content

Commit af75a7c

Browse files
committed
reusing TypeNames interning for GenBCode
1 parent 4bacb4f commit af75a7c

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

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

100644100755
Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ object Names {
197197
private final val fillFactor = 0.7
198198

199199
/** Memory to store all names sequentially. */
200-
private var chrs: Array[Char] = new Array[Char](InitialNameSize)
200+
var chrs: Array[Char] = new Array[Char](InitialNameSize)
201201

202202
/** The number of characters filled. */
203203
private var nc = 0
@@ -287,6 +287,36 @@ object Names {
287287
}
288288
}
289289

290+
/**
291+
* Used only by the GenBCode backend, to represent bytecode-level types in a way that makes equals() and hashCode() efficient.
292+
* For bytecode-level types of OBJECT sort, its internal name (not its descriptor) is stored.
293+
* For those of ARRAY sort, its descriptor is stored ie has a leading '['
294+
* For those of METHOD sort, its descriptor is stored ie has a leading '('
295+
*
296+
* can-multi-thread
297+
* TODO SI-6240 !!! JZ Really? the constructors TermName and TypeName publish unconstructed `this` references
298+
* into the hash tables; we could observe them here before the subclass constructor completes.
299+
*/
300+
final def lookupTypeName(cs: Array[Char]): TypeName = lookupTypeNameIfExisting(cs, true)
301+
302+
final def lookupTypeNameIfExisting(cs: Array[Char], failOnNotFound: Boolean): TypeName = {
303+
val h = hashValue(cs, 0, cs.length) & (table.size - 1)
304+
synchronized {
305+
val next = table(h)
306+
var name = next
307+
while (name ne null) {
308+
if (name.length == len && equals(name.start, cs, 0, cs.length))
309+
return name.toTypeName
310+
name = name.next
311+
}
312+
if (failOnNotFound) {
313+
throw new RuntimeException(s"lookup of non-existing TypeName: ${new String(cs)}")
314+
} else {
315+
return null
316+
}
317+
}
318+
}
319+
290320
/** Create a type name from the characters in cs[offset..offset+len-1].
291321
* Assume they are already encoded.
292322
*/

0 commit comments

Comments
 (0)