Skip to content

Commit 3ef3a19

Browse files
committed
New version of Lazy Vals implementation
1 parent b9d5565 commit 3ef3a19

File tree

24 files changed

+518
-196
lines changed

24 files changed

+518
-196
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package dotty.tools.benchmarks.lazyvals
2+
3+
import org.openjdk.jmh.annotations._
4+
import LazyVals.LazyHolder
5+
import org.openjdk.jmh.infra.Blackhole
6+
import java.util.concurrent.TimeUnit
7+
import java.util.concurrent.{Executors, ExecutorService}
8+
9+
@BenchmarkMode(Array(Mode.AverageTime))
10+
@Fork(2)
11+
@Threads(1)
12+
@Warmup(iterations = 5)
13+
@Measurement(iterations = 5)
14+
@OutputTimeUnit(TimeUnit.MILLISECONDS)
15+
@State(Scope.Benchmark)
16+
class ContendedInitialization {
17+
18+
@Param(Array("2000000", "5000000"))
19+
var size: Int = _
20+
21+
@Param(Array("2", "4", "8"))
22+
var nThreads: Int = _
23+
24+
var executor: ExecutorService = _
25+
26+
@Setup
27+
def prepare: Unit = {
28+
executor = Executors.newFixedThreadPool(nThreads)
29+
}
30+
31+
@TearDown
32+
def cleanup: Unit = {
33+
executor.shutdown()
34+
executor = null
35+
}
36+
37+
@Benchmark
38+
def measureContended(bh: Blackhole): Unit = {
39+
val array = Array.fill(size)(new LazyHolder)
40+
val task: Runnable = () =>
41+
for (elem <- array) bh.consume(elem.value)
42+
43+
val futures =
44+
for (_ <- 0 until nThreads) yield
45+
executor.submit(task)
46+
47+
futures.foreach(_.get())
48+
}
49+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package dotty.tools.benchmarks.lazyvals
2+
3+
import org.openjdk.jmh.annotations._
4+
import LazyVals.LazyHolder
5+
import org.openjdk.jmh.infra.Blackhole
6+
import java.util.concurrent.TimeUnit
7+
8+
@BenchmarkMode(Array(Mode.AverageTime))
9+
@Fork(2)
10+
@Threads(1)
11+
@Warmup(iterations = 5)
12+
@Measurement(iterations = 5)
13+
@OutputTimeUnit(TimeUnit.NANOSECONDS)
14+
@State(Scope.Benchmark)
15+
class InitializedAccess {
16+
17+
var holder: LazyHolder = _
18+
19+
@Setup
20+
def prepare: Unit = {
21+
holder = new LazyHolder
22+
holder.value
23+
}
24+
25+
@Benchmark
26+
def measureInitialized(bh: Blackhole) = {
27+
bh.consume(holder.value)
28+
}
29+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package dotty.tools.benchmarks.lazyvals
2+
3+
object LazyVals {
4+
class LazyHolder {
5+
lazy val value = 1
6+
}
7+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package dotty.tools.benchmarks.lazyvals
2+
3+
import org.openjdk.jmh.annotations._
4+
import LazyVals.LazyHolder
5+
import org.openjdk.jmh.infra.Blackhole
6+
import java.util.concurrent.TimeUnit
7+
8+
@BenchmarkMode(Array(Mode.AverageTime))
9+
@Fork(2)
10+
@Threads(1)
11+
@Warmup(iterations = 5)
12+
@Measurement(iterations = 5)
13+
@OutputTimeUnit(TimeUnit.NANOSECONDS)
14+
@State(Scope.Benchmark)
15+
class UninitializedAccess {
16+
17+
var holder: LazyHolder = _
18+
19+
@Setup
20+
def prepare: Unit = {
21+
holder = new LazyHolder
22+
}
23+
24+
@Benchmark
25+
def measureInitialized(bh: Blackhole) = {
26+
bh.consume(holder)
27+
bh.consume(holder.value)
28+
}
29+
}

compiler/src/dotty/tools/dotc/ast/TreeTypeMap.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import Types._, Contexts._, Flags._
77
import Symbols._, Annotations._, Trees._, Symbols._, Constants.Constant
88
import Decorators._
99
import dotty.tools.dotc.transform.SymUtils._
10+
import transform.LazyVals
1011

1112
/** A map that applies three functions and a substitution together to a tree and
1213
* makes sure they are coordinated so that the result is well-typed. The functions are

compiler/src/dotty/tools/dotc/config/ScalaSettings.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ private sealed trait XSettings:
241241
val XcheckMacros: Setting[Boolean] = BooleanSetting("-Xcheck-macros", "Check some invariants of macro generated code while expanding macros", aliases = List("--Xcheck-macros"))
242242
val XmainClass: Setting[String] = StringSetting("-Xmain-class", "path", "Class for manifest's Main-Class entry (only useful with -d <jar>)", "")
243243
val XimplicitSearchLimit: Setting[Int] = IntSetting("-Ximplicit-search-limit", "Maximal number of expressions to be generated in an implicit search", 50000)
244+
val XlegacyLazyVals: Setting[Boolean] = BooleanSetting("-Xlegacy-lazy-values", "Use legacy lazy vals implementations")
244245

245246
val XmixinForceForwarders = ChoiceSetting(
246247
name = "-Xmixin-force-forwarders",

0 commit comments

Comments
 (0)