Skip to content

Commit a35b7a4

Browse files
committed
Avoid creating unnecessary strings in adjustExtension
1 parent 8b790a6 commit a35b7a4

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

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

+11-5
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,9 @@ object Names {
146146
def startsWith(str: String, start: Int = 0): Boolean = firstPart.startsWith(str, start)
147147

148148
/** Does (the last part of) this name end with `str`? */
149-
def endsWith(str: String): Boolean = lastPart.endsWith(str)
149+
def endsWith(suffix: String): Boolean = lastPart.endsWith(suffix)
150+
151+
def endsWith(suffix: SimpleName): Boolean = lastPart.endsWith(suffix)
150152

151153
override def hashCode: Int = System.identityHashCode(this)
152154
override def equals(that: Any): Boolean = this eq that.asInstanceOf[AnyRef]
@@ -363,11 +365,15 @@ object Names {
363365
i == str.length
364366
}
365367

366-
override def endsWith(str: String): Boolean = {
368+
override def endsWith(suffix: String): Boolean =
367369
var i = 1
368-
while (i <= str.length && i <= length && apply(length - i) == str(str.length - i)) i += 1
369-
i > str.length
370-
}
370+
while i <= suffix.length && i <= length && apply(length - i) == suffix(suffix.length - i) do i += 1
371+
i > suffix.length
372+
373+
override def endsWith(suffix: SimpleName): Boolean =
374+
var i = 1
375+
while i <= suffix.length && i <= length && apply(length - i) == suffix(suffix.length - i) do i += 1
376+
i > suffix.length
371377

372378
override def replace(from: Char, to: Char): SimpleName = {
373379
val cs = new Array[Char](length)

compiler/src/dotty/tools/dotc/typer/Typer.scala

+4-1
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,10 @@ class Typer extends Namer
216216
val termName = name.toTermName
217217

218218
def adjustExtension(name: Name) =
219-
if required.is(Extension) then name.toExtensionName else name
219+
if required.is(Extension) && termName.endsWith(name.lastPart)
220+
// pre-check to avoid forming a new string; form extension only if it has a chance of matching `termName`
221+
then name.toExtensionName
222+
else name
220223

221224
def recur(selectors: List[untpd.ImportSelector]): Type = selectors match
222225
case selector :: rest =>

0 commit comments

Comments
 (0)