1
1
package scoverage
2
2
3
3
import java .io .FileWriter
4
+ import scala .collection .concurrent .TrieMap
4
5
5
6
/** @author Stephen Samuel */
6
7
object Invoker {
7
8
8
- val threadFile = new ThreadLocal [FileWriter ]
9
+ private val threadFile = new ThreadLocal [FileWriter ]
10
+ private val invoked = TrieMap .empty[Int , Any ]
9
11
10
12
/**
11
13
* We record that the given id has been invoked by appending its id to the coverage
@@ -23,14 +25,20 @@ object Invoker {
23
25
* @param dataDir the directory where the measurement data is held
24
26
*/
25
27
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, ())
33
42
}
34
- writer.append(id.toString + '\n ' ).flush()
35
43
}
36
44
}
0 commit comments