-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make lazy initialization of static Val.Obj thread-safe (#136)
Static Val.Obj instances are created by the optimizer and will end up in the parse cache. If the cache is shared by multiple threads, initialization must be sufficiently safe, i.e. computing a value multiple times in race conditions is allowed (and cheaper than ensuring that it doesn't happen), but object must never get into an invalid intermediate state.
- Loading branch information
Showing
3 changed files
with
66 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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
56 changes: 56 additions & 0 deletions
56
bench/src/main/scala/sjsonnet/MultiThreadedBenchmark.scala
This file contains 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,56 @@ | ||
package sjsonnet | ||
|
||
import java.util.concurrent.{ExecutorService, Executors, TimeUnit} | ||
|
||
import org.openjdk.jmh.annotations._ | ||
import org.openjdk.jmh.infra._ | ||
|
||
import scala.collection.mutable | ||
|
||
@BenchmarkMode(Array(Mode.AverageTime)) | ||
@Fork(4) | ||
@Threads(1) | ||
@Warmup(iterations = 30) | ||
@Measurement(iterations = 40) | ||
@OutputTimeUnit(TimeUnit.MILLISECONDS) | ||
@State(Scope.Benchmark) | ||
class MultiThreadedBenchmark { | ||
|
||
val threads = 8 | ||
|
||
@Benchmark | ||
def main(bh: Blackhole): Unit = { | ||
val cache: ParseCache = new ParseCache { | ||
val map = new mutable.HashMap[(Path, String), Either[Error, (Expr, FileScope)]]() | ||
override def getOrElseUpdate(key: (Path, String), defaultValue: => Either[Error, (Expr, FileScope)]): Either[Error, (Expr, FileScope)] = { | ||
var v = map.synchronized(map.getOrElse(key, null)) | ||
if(v == null) { | ||
v = defaultValue | ||
map.synchronized(map.put(key, v)) | ||
} | ||
v | ||
} | ||
} | ||
|
||
val pool: ExecutorService = Executors.newFixedThreadPool(threads) | ||
val futs = (1 to threads).map { _ => | ||
pool.submit { (() => | ||
if(SjsonnetMain.main0( | ||
MainBenchmark.mainArgs, | ||
cache, // new DefaultParseCache | ||
System.in, | ||
MainBenchmark.createDummyOut, | ||
System.err, | ||
os.pwd, | ||
None | ||
) != 0) throw new Exception): Runnable | ||
} | ||
} | ||
var err: Throwable = null | ||
bh.consume(futs.map { f => | ||
try f.get() catch { case e: Throwable => err = e } | ||
}) | ||
pool.shutdown() | ||
if(err != null) throw err | ||
} | ||
} |
This file contains 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