From c3fcd4e5b0c8f03862ecf4f92fd43cf2fb8f5066 Mon Sep 17 00:00:00 2001 From: Jason Zaugg Date: Tue, 9 Feb 2016 22:12:26 +1000 Subject: 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. --- src/reflect/scala/reflect/internal/Symbols.scala | 38 ++++++++++++++++-------- 1 file changed, 25 insertions(+), 13 deletions(-) (limited to 'src/reflect') 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. -- cgit v1.2.3