From a09e4b2d59e943439d79f5969f443ca526297ea3 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Thu, 23 Jul 2020 19:13:08 +0200 Subject: [PATCH 1/2] Avoid creating unnecessary strings in adjustExtension # Conflicts: # compiler/src/dotty/tools/dotc/typer/Typer.scala --- compiler/src/dotty/tools/dotc/core/Names.scala | 16 +++++++++++----- compiler/src/dotty/tools/dotc/typer/Typer.scala | 5 ++++- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/core/Names.scala b/compiler/src/dotty/tools/dotc/core/Names.scala index cd58977f9dda..9799418484d4 100644 --- a/compiler/src/dotty/tools/dotc/core/Names.scala +++ b/compiler/src/dotty/tools/dotc/core/Names.scala @@ -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] @@ -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) diff --git a/compiler/src/dotty/tools/dotc/typer/Typer.scala b/compiler/src/dotty/tools/dotc/typer/Typer.scala index 405e450a4691..cdb236e2cded 100644 --- a/compiler/src/dotty/tools/dotc/typer/Typer.scala +++ b/compiler/src/dotty/tools/dotc/typer/Typer.scala @@ -216,7 +216,10 @@ class Typer extends Namer val termName = name.toTermName def adjustExtension(name: Name) = - if required.is(ExtensionMethod) then name.toExtensionName else name + if required.is(ExtensionMethod) && 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 => From 7e45458159f540e41396f7e637ea0bfd52eb25bd Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Thu, 20 Aug 2020 12:53:42 +0200 Subject: [PATCH 2/2] Rename parameter for clarity --- compiler/src/dotty/tools/dotc/typer/Typer.scala | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/typer/Typer.scala b/compiler/src/dotty/tools/dotc/typer/Typer.scala index cdb236e2cded..b1df2c376bda 100644 --- a/compiler/src/dotty/tools/dotc/typer/Typer.scala +++ b/compiler/src/dotty/tools/dotc/typer/Typer.scala @@ -215,11 +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) && termName.endsWith(name.lastPart) + 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` - then name.toExtensionName - else name + then n.toExtensionName + else n def recur(selectors: List[untpd.ImportSelector]): Type = selectors match case selector :: rest =>