-
Notifications
You must be signed in to change notification settings - Fork 127
Reduce performance overhead of map lookups in Invoker.invoked() #202
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
Merged
sksamuel
merged 2 commits into
scoverage:master
from
JoshRosen:Invoker-map-lookup-performance-improvements
Jul 5, 2018
Merged
Changes from all commits
Commits
Show all changes
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,8 +7,12 @@ import scoverage.Platform._ | |
object Invoker { | ||
|
||
private val MeasurementsPrefix = "scoverage.measurements." | ||
private val threadFiles = new ThreadLocal[ThreadSafeMap[String, FileWriter]] | ||
private val ids = ThreadSafeMap.empty[(String, Int), Any] | ||
private val threadFiles = new ThreadLocal[mutable.HashMap[String, FileWriter]] | ||
|
||
// For each data directory we maintain a thread-safe set tracking the ids that we've already | ||
// seen and recorded. We're using a map as a set, so we only care about its keys and can ignore | ||
// its values. | ||
private val dataDirToIds = ThreadSafeMap.empty[String, ThreadSafeMap[Int, Any]] | ||
|
||
/** | ||
* We record that the given id has been invoked by appending its id to the coverage | ||
|
@@ -31,18 +35,27 @@ object Invoker { | |
// times since for coverage we only care about 1 or more, (it just slows things down to | ||
// do it more than once), anything we can do to help is good. This helps especially with code | ||
// that is executed many times quickly, eg tight loops. | ||
if (!ids.contains(dataDir, id)) { | ||
if (!dataDirToIds.contains(dataDir)) { | ||
// Guard against SI-7943: "TrieMap method getOrElseUpdate is not thread-safe". | ||
dataDirToIds.synchronized { | ||
if (!dataDirToIds.contains(dataDir)) { | ||
dataDirToIds(dataDir) = ThreadSafeMap.empty[Int, Any] | ||
} | ||
} | ||
} | ||
val ids = dataDirToIds(dataDir) | ||
if (!ids.contains(id)) { | ||
// Each thread writes to a separate measurement file, to reduce contention | ||
// and because file appends via FileWriter are not atomic on Windows. | ||
var files = threadFiles.get() | ||
if (files == null) { | ||
files = ThreadSafeMap.empty[String, FileWriter] | ||
files = mutable.HashMap.empty[String, FileWriter] | ||
threadFiles.set(files) | ||
} | ||
val writer = files.getOrElseUpdate(dataDir, new FileWriter(measurementFile(dataDir), true)) | ||
writer.append(id.toString + '\n').flush() | ||
writer.append(Integer.toString(id)).append("\n").flush() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm pretty sure that The key change on this line is to replace the string concatenation with two separate |
||
|
||
ids.put((dataDir, id), ()) | ||
ids.put(id, ()) | ||
} | ||
} | ||
|
||
|
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.
I noticed that this was also calling
getOrElseUpdate
on aTrieMap
down infiles.getOrElseUpdate
, but that case wasn't an issue because that map is used only in a single thread (since it comes from a non-inheritableThreadLocal
).To help make this clearer in the code, I went ahead and used a plain
mutable.HashMap
at that other site.