summaryrefslogtreecommitdiff
path: root/src/reflect/scala/reflect/internal/Symbols.scala
diff options
context:
space:
mode:
authorJason Zaugg <jzaugg@gmail.com>2016-02-09 22:12:26 +1000
committerJason Zaugg <jzaugg@gmail.com>2016-02-09 22:12:26 +1000
commitc3fcd4e5b0c8f03862ecf4f92fd43cf2fb8f5066 (patch)
treeb7f6f0fbbbf514687bc20e18c94317cb9905c673 /src/reflect/scala/reflect/internal/Symbols.scala
parentef4ed496c59e4b54266aaf20db917fb41bb9f1e4 (diff)
downloadscala-c3fcd4e5b0c8f03862ecf4f92fd43cf2fb8f5066.tar.gz
scala-c3fcd4e5b0c8f03862ecf4f92fd43cf2fb8f5066.tar.bz2
scala-c3fcd4e5b0c8f03862ecf4f92fd43cf2fb8f5066.zip
Micro optimise Symbol#fullName
The old approach of recursively calling `fullNameAsName` creates a lot of garbage for intermediate results, in addition to needless interning of those results into the name table. This commit instead creates a string buffer of the correct capacity and writes the component names directly into this. I compared old and new approaches and this shows a 2x speedup. ``` scala> val th = ichi.bench.Thyme.warmed(verbose = print) th: ichi.bench.Thyme = ichi.bench.Thyme@1643e817 scala> val w_old = th.Warm(sym.fullNameAsNameOld('.')) w_old: th.Warm[$r.intp.global.Name] = ichi.bench.Thyme$Warm@7a8d001b scala> val w_new = th.Warm(sym.fullNameAsName('.')) w_new: th.Warm[$r.intp.global.Name] = ichi.bench.Thyme$Warm@1ec14586 scala> th.pbenchOffWarm("", x => println(x))(w_old, 10, "old")(w_new, 10, "new") Benchmark comparison (in 4.084 s) old vs new Significantly different (p ~= 0) Time ratio: 0.53572 95% CI 0.51618 - 0.55525 (n=20) old 64.54 ns 95% CI 62.41 ns - 66.67 ns new 34.57 ns 95% CI 34.04 ns - 35.11 ns res3: $r.intp.global.Name = scala.collection.parallel.mutable.ParSeq ``` It is still expensive enough that we should still consider caching. The call to full name in `classBTypeFromSymbol` in the new backed is a prime candidate for optimization.
Diffstat (limited to 'src/reflect/scala/reflect/internal/Symbols.scala')
-rw-r--r--src/reflect/scala/reflect/internal/Symbols.scala38
1 files changed, 25 insertions, 13 deletions
diff --git a/src/reflect/scala/reflect/internal/Symbols.scala b/src/reflect/scala/reflect/internal/Symbols.scala
index 11e740117d..3b9ee9048a 100644
--- a/src/reflect/scala/reflect/internal/Symbols.scala
+++ b/src/reflect/scala/reflect/internal/Symbols.scala
@@ -1256,8 +1256,9 @@ trait Symbols extends api.Symbols { self: SymbolTable =>
/** These should be moved somewhere like JavaPlatform.
*/
def javaSimpleName: Name = addModuleSuffix(simpleName.dropLocal)
- def javaBinaryName: Name = addModuleSuffix(fullNameInternal('/'))
- def javaClassName: String = addModuleSuffix(fullNameInternal('.')).toString
+ def javaBinaryName: Name = name.newName(javaBinaryNameString)
+ def javaBinaryNameString: String = fullName('/', moduleSuffix)
+ def javaClassName: String = fullName('.', moduleSuffix)
/** The encoded full path name of this symbol, where outer names and inner names
* are separated by `separator` characters.
@@ -1265,18 +1266,29 @@ trait Symbols extends api.Symbols { self: SymbolTable =>
* Never adds id.
* Drops package objects.
*/
- final def fullName(separator: Char): String = fullNameAsName(separator).toString
-
- /** Doesn't drop package objects, for those situations (e.g. classloading)
- * where the true path is needed.
- */
- private def fullNameInternal(separator: Char): Name = (
- if (isRoot || isRootPackage || this == NoSymbol) name
- else if (owner.isEffectiveRoot) name
- else effectiveOwner.enclClass.fullNameAsName(separator) append (separator, name)
- )
+ final def fullName(separator: Char): String = fullName(separator, "")
+
+ private def fullName(separator: Char, suffix: CharSequence): String = {
+ var b: java.lang.StringBuffer = null
+ def loop(size: Int, sym: Symbol): Unit = {
+ val symName = sym.name
+ val nSize = symName.length - (if (symName.endsWith(nme.LOCAL_SUFFIX_STRING)) 1 else 0)
+ if (sym.isRoot || sym.isRootPackage || sym == NoSymbol || sym.owner.isEffectiveRoot) {
+ val capacity = size + nSize
+ b = new java.lang.StringBuffer(capacity)
+ b.append(chrs, symName.start, nSize)
+ } else {
+ loop(size + nSize + 1, sym.effectiveOwner.enclClass)
+ b.append(separator)
+ b.append(chrs, symName.start, nSize)
+ }
+ }
+ loop(suffix.length(), this)
+ b.append(suffix)
+ b.toString
+ }
- def fullNameAsName(separator: Char): Name = fullNameInternal(separator).dropLocal
+ def fullNameAsName(separator: Char): Name = name.newName(fullName(separator, ""))
/** The encoded full path name of this symbol, where outer names and inner names
* are separated by periods.