Skip to content

Commit 188b99c

Browse files
authored
Merge pull request #9598 from dotty-staging/optimize-adjustExtension
Avoid creating unnecessary strings in adjustExtension
2 parents 6149e92 + 7e45458 commit 188b99c

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

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

Lines changed: 11 additions & 5 deletions
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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,11 @@ class Typer extends Namer
215215
def namedImportRef(imp: ImportInfo)(using Context): Type = {
216216
val termName = name.toTermName
217217

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

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

0 commit comments

Comments
 (0)