-
Notifications
You must be signed in to change notification settings - Fork 90
Performance improvements: avoid boxing and needless string to name conversion #194
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
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package scala.async.internal | ||
|
||
import java.util.concurrent.atomic.AtomicInteger | ||
|
||
import scala.collection.mutable | ||
import scala.collection.mutable.ArrayBuffer | ||
import scala.reflect.api.Names | ||
|
||
/** | ||
* A per-global cache of names needed by the Async macro. | ||
*/ | ||
final class AsyncNames[U <: Names with Singleton](val u: U) { | ||
self => | ||
import u._ | ||
|
||
abstract class NameCache[N <: U#Name](base: String) { | ||
val cached = new ArrayBuffer[N]() | ||
protected def newName(s: String): N | ||
def apply(i: Int): N = { | ||
if (cached.isDefinedAt(i)) cached(i) | ||
else { | ||
assert(cached.length == i) | ||
val name = newName(freshenString(base, i)) | ||
cached += name | ||
name | ||
} | ||
} | ||
} | ||
|
||
final class TermNameCache(base: String) extends NameCache[U#TermName](base) { | ||
override protected def newName(s: String): U#TermName = newTermName(s) | ||
} | ||
final class TypeNameCache(base: String) extends NameCache[U#TypeName](base) { | ||
override protected def newName(s: String): U#TypeName = newTypeName(s) | ||
} | ||
private val matchRes: TermNameCache = new TermNameCache("match") | ||
private val ifRes: TermNameCache = new TermNameCache("if") | ||
private val await: TermNameCache = new TermNameCache("await") | ||
|
||
private val resume = newTermName("resume") | ||
private val completed: TermName = newTermName("completed$async") | ||
private val apply = newTermName("apply") | ||
private val stateMachine = newTermName("stateMachine$async") | ||
private val stateMachineT = stateMachine.toTypeName | ||
private val state: u.TermName = newTermName("state$async") | ||
private val execContext = newTermName("execContext$async") | ||
private val tr: u.TermName = newTermName("tr$async") | ||
private val t: u.TermName = newTermName("throwable$async") | ||
|
||
final class NameSource[N <: U#Name](cache: NameCache[N]) { | ||
private val count = new AtomicInteger(0) | ||
def apply(): N = cache(count.getAndIncrement()) | ||
} | ||
|
||
class AsyncName { | ||
final val matchRes = new NameSource[U#TermName](self.matchRes) | ||
final val ifRes = new NameSource[U#TermName](self.matchRes) | ||
final val await = new NameSource[U#TermName](self.await) | ||
final val completed = self.completed | ||
final val result = self.resume | ||
final val apply = self.apply | ||
final val stateMachine = self.stateMachine | ||
final val stateMachineT = self.stateMachineT | ||
final val state: u.TermName = self.state | ||
final val execContext = self.execContext | ||
final val tr: u.TermName = self.tr | ||
final val t: u.TermName = self.t | ||
|
||
private val seenPrefixes = mutable.AnyRefMap[Name, AtomicInteger]() | ||
private val freshened = mutable.HashSet[Name]() | ||
|
||
final def freshenIfNeeded(name: TermName): TermName = { | ||
seenPrefixes.getOrNull(name) match { | ||
case null => | ||
seenPrefixes.put(name, new AtomicInteger()) | ||
name | ||
case counter => | ||
freshen(name, counter) | ||
} | ||
} | ||
final def freshenIfNeeded(name: TypeName): TypeName = { | ||
seenPrefixes.getOrNull(name) match { | ||
case null => | ||
seenPrefixes.put(name, new AtomicInteger()) | ||
name | ||
case counter => | ||
freshen(name, counter) | ||
} | ||
} | ||
final def freshen(name: TermName): TermName = { | ||
val counter = seenPrefixes.getOrElseUpdate(name, new AtomicInteger()) | ||
freshen(name, counter) | ||
} | ||
final def freshen(name: TypeName): TypeName = { | ||
val counter = seenPrefixes.getOrElseUpdate(name, new AtomicInteger()) | ||
freshen(name, counter) | ||
} | ||
private def freshen(name: TermName, counter: AtomicInteger): TermName = { | ||
if (freshened.contains(name)) name | ||
else TermName(freshenString(name.toString, counter.incrementAndGet())) | ||
} | ||
private def freshen(name: TypeName, counter: AtomicInteger): TypeName = { | ||
if (freshened.contains(name)) name | ||
else TypeName(freshenString(name.toString, counter.incrementAndGet())) | ||
} | ||
} | ||
|
||
private def freshenString(name: String, counter: Int): String = name.toString + "$async$" + counter | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why synchronized + computeIfAbsent?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I got rid of this map altogether #194 as I think this was leaky anyway.