Skip to content

Avoid creating unnecessary strings in adjustExtension #9598

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions compiler/src/dotty/tools/dotc/core/Names.scala
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,9 @@ object Names {
def startsWith(str: String, start: Int = 0): Boolean = firstPart.startsWith(str, start)

/** Does (the last part of) this name end with `str`? */
def endsWith(str: String): Boolean = lastPart.endsWith(str)
def endsWith(suffix: String): Boolean = lastPart.endsWith(suffix)

def endsWith(suffix: SimpleName): Boolean = lastPart.endsWith(suffix)

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

override def endsWith(str: String): Boolean = {
override def endsWith(suffix: String): Boolean =
var i = 1
while (i <= str.length && i <= length && apply(length - i) == str(str.length - i)) i += 1
i > str.length
}
while i <= suffix.length && i <= length && apply(length - i) == suffix(suffix.length - i) do i += 1
i > suffix.length

override def endsWith(suffix: SimpleName): Boolean =
var i = 1
while i <= suffix.length && i <= length && apply(length - i) == suffix(suffix.length - i) do i += 1
i > suffix.length

override def replace(from: Char, to: Char): SimpleName = {
val cs = new Array[Char](length)
Expand Down
7 changes: 5 additions & 2 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,11 @@ class Typer extends Namer
def namedImportRef(imp: ImportInfo)(using Context): Type = {
val termName = name.toTermName

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will termName.endsWith(name.lastPart) be always true, given that termName = name.toTermName?

then n.toExtensionName
else n

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