Skip to content

Commit 1fd0844

Browse files
committed
Generalize buffering for Name construction from substring
1 parent cfd2937 commit 1fd0844

File tree

4 files changed

+20
-20
lines changed

4 files changed

+20
-20
lines changed

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

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -89,16 +89,6 @@ object Contexts {
8989
inline def withoutMode[T](mode: Mode)(inline op: Context ?=> T)(using ctx: Context): T =
9090
inMode(ctx.mode &~ mode)(op)
9191

92-
private[dotc] inline def withNameBuffer(inline op: Array[Char] => Int)(using ctx: Context): Names.TermName = {
93-
val base = ctx.base
94-
var len = op(base.nameCharBuffer)
95-
while(len == -1) {
96-
base.nameCharBuffer = new Array[Char](base.nameCharBuffer.length * 2)
97-
len = op(base.nameCharBuffer)
98-
}
99-
Names.termName(base.nameCharBuffer, 0, len)
100-
}
101-
10292
/** A context is passed basically everywhere in dotc.
10393
* This is convenient but carries the risk of captured contexts in
10494
* objects that turn into space leaks. To combat this risk, here are some

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,19 @@ object Names {
644644
/** Create a type name from a string, without encoding operators */
645645
def typeName(s: String): TypeName = typeName(s.toCharArray, 0, s.length)
646646

647+
def termName(s: String, from: Int, len: Int)(using ctx: Contexts.Context): TermName = {
648+
val base = ctx.base
649+
650+
while (len > base.nameCharBuffer.length)
651+
base.nameCharBuffer = new Array[Char](base.nameCharBuffer.length * 2)
652+
653+
s.getChars(from, from + len, base.nameCharBuffer, 0)
654+
termName(base.nameCharBuffer, 0, len)
655+
}
656+
657+
def typeName(s: String, from: Int, len: Int)(using ctx: Contexts.Context): TypeName =
658+
termName(s, from, len).toTypeName
659+
647660
table(0) = new SimpleName(-1, 0, null)
648661

649662
/** The term name represented by the empty string */

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ object SymbolLoaders {
205205
src.lastModified >= bin.lastModified
206206

207207
private def nameOf(classRep: ClassRepresentation)(using Context): TermName =
208-
withNameBuffer(classRep.nameChars)
208+
termName(classRep.fileName, 0, classRep.nameLength)
209209

210210
/** Load contents of a package
211211
*/

compiler/src/dotty/tools/io/ClassPath.scala

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -175,16 +175,13 @@ trait ClassRepresentation {
175175
def binary: Option[AbstractFile]
176176
def source: Option[AbstractFile]
177177

178-
/** Low level way to extract the entry name without allocation. */
179-
final def nameChars(buffer: Array[Char]): Int = {
178+
/** returns the length of `name` by stripping the extension of `fileName`
179+
*
180+
* Used to avoid creating String instance of `name`.
181+
*/
182+
final def nameLength: Int = {
180183
val ix = fileName.lastIndexOf('.')
181-
val nameLength = if (ix < 0) fileName.length else ix
182-
if (nameLength > buffer.length)
183-
-1
184-
else {
185-
fileName.getChars(0, nameLength, buffer, 0)
186-
nameLength
187-
}
184+
if (ix < 0) fileName.length else ix
188185
}
189186
}
190187

0 commit comments

Comments
 (0)