Skip to content

Commit 87824e8

Browse files
committed
Updated invoker to use a set of written ids
1 parent 55f4242 commit 87824e8

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

src/main/scala/scoverage/Invoker.scala

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package scoverage
22

33
import java.io.FileWriter
4+
import scala.collection.concurrent.TrieMap
45

56
/** @author Stephen Samuel */
67
object Invoker {
78

8-
val threadFile = new ThreadLocal[FileWriter]
9+
private val threadFile = new ThreadLocal[FileWriter]
10+
private val invoked = TrieMap.empty[Int, Any]
911

1012
/**
1113
* We record that the given id has been invoked by appending its id to the coverage
@@ -23,14 +25,20 @@ object Invoker {
2325
* @param dataDir the directory where the measurement data is held
2426
*/
2527
def invoked(id: Int, dataDir: String) = {
26-
// Each thread writes to a separate measurement file, to reduce contention
27-
// and because file appends via FileWriter are not atomic on Windows.
28-
var writer = threadFile.get()
29-
if (writer == null) {
30-
val file = IOUtils.measurementFile(dataDir)
31-
writer = new FileWriter(file, true)
32-
threadFile.set(writer)
28+
// [sam] we can do this simple check to save writing out to a file.
29+
// This won't work across JVMs but since there's no harm in writing out the same id multiple
30+
// times (it just slows things down), anything we can do to help is good.
31+
if (!invoked.contains(id)) {
32+
// Each thread writes to a separate measurement file, to reduce contention
33+
// and because file appends via FileWriter are not atomic on Windows.
34+
var writer = threadFile.get()
35+
if (writer == null) {
36+
val file = IOUtils.measurementFile(dataDir)
37+
writer = new FileWriter(file, true)
38+
threadFile.set(writer)
39+
}
40+
writer.append(id.toString + '\n').flush()
41+
invoked.put(id, ())
3342
}
34-
writer.append(id.toString + '\n').flush()
3543
}
3644
}

0 commit comments

Comments
 (0)