Skip to content

Commit

Permalink
Avoid creating unnecessary strings in adjustExtension
Browse files Browse the repository at this point in the history
  • Loading branch information
odersky committed Jul 23, 2020
1 parent 8b790a6 commit a35b7a4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
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
5 changes: 4 additions & 1 deletion compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,10 @@ class Typer extends Namer
val termName = name.toTermName

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

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

0 comments on commit a35b7a4

Please sign in to comment.