Skip to content
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

New lazy vals implementation #15296

Merged
merged 7 commits into from
Oct 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package dotty.tools.benchmarks.lazyvals

import org.openjdk.jmh.annotations._
import LazyVals.LazyHolder
import org.openjdk.jmh.infra.Blackhole
import java.util.concurrent.TimeUnit
import java.util.concurrent.{Executors, ExecutorService}

@BenchmarkMode(Array(Mode.AverageTime))
@Fork(2)
@Threads(1)
@Warmup(iterations = 5)
@Measurement(iterations = 5)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@State(Scope.Benchmark)
class ContendedInitialization {

@Param(Array("2000000", "5000000"))
var size: Int = _

@Param(Array("2", "4", "8"))
var nThreads: Int = _

var executor: ExecutorService = _

@Setup
def prepare: Unit = {
executor = Executors.newFixedThreadPool(nThreads)
}

@TearDown
def cleanup: Unit = {
executor.shutdown()
executor = null
}

@Benchmark
def measureContended(bh: Blackhole): Unit = {
val array = Array.fill(size)(new LazyHolder)
val task: Runnable = () =>
for (elem <- array) bh.consume(elem.value)

val futures =
for (_ <- 0 until nThreads) yield
executor.submit(task)

futures.foreach(_.get())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package dotty.tools.benchmarks.lazyvals

import org.openjdk.jmh.annotations._
import LazyVals.LazyHolder
import org.openjdk.jmh.infra.Blackhole
import java.util.concurrent.TimeUnit

@BenchmarkMode(Array(Mode.AverageTime))
@Fork(2)
@Threads(1)
@Warmup(iterations = 5)
@Measurement(iterations = 5)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@State(Scope.Benchmark)
class InitializedAccess {

var holder: LazyHolder = _

@Setup
def prepare: Unit = {
holder = new LazyHolder
holder.value
}

@Benchmark
def measureInitialized(bh: Blackhole) = {
bh.consume(holder)
bh.consume(holder.value)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package dotty.tools.benchmarks.lazyvals

import org.openjdk.jmh.annotations._
import LazyVals.LazyAnyHolder
import org.openjdk.jmh.infra.Blackhole
import java.util.concurrent.TimeUnit

@BenchmarkMode(Array(Mode.AverageTime))
@Fork(2)
@Threads(1)
@Warmup(iterations = 5)
@Measurement(iterations = 5)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@State(Scope.Benchmark)
class InitializedAccessAny {

var holder: LazyAnyHolder = _

@Setup
def prepare: Unit = {
holder = new LazyAnyHolder
holder.value
}

@Benchmark
def measureInitialized(bh: Blackhole) = {
bh.consume(holder)
bh.consume(holder.value)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package dotty.tools.benchmarks.lazyvals

import org.openjdk.jmh.annotations._
import LazyVals.LazyGenericHolder
import org.openjdk.jmh.infra.Blackhole
import java.util.concurrent.TimeUnit

@BenchmarkMode(Array(Mode.AverageTime))
@Fork(2)
@Threads(1)
@Warmup(iterations = 5)
@Measurement(iterations = 5)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@State(Scope.Benchmark)
class InitializedAccessGeneric {

var holder: LazyGenericHolder[String] = _

@Setup
def prepare: Unit = {
holder = new LazyGenericHolder[String]("foo")
holder.value
}

@Benchmark
def measureInitialized(bh: Blackhole) = {
bh.consume(holder)
bh.consume(holder.value)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package dotty.tools.benchmarks.lazyvals

import org.openjdk.jmh.annotations._
import LazyVals.LazyHolder
import org.openjdk.jmh.infra.Blackhole
import java.util.concurrent.TimeUnit

@BenchmarkMode(Array(Mode.AverageTime))
@Fork(2)
@Threads(1)
@Warmup(iterations = 5)
@Measurement(iterations = 5)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@State(Scope.Benchmark)
class InitializedAccessMultiple {

var holders: Array[LazyHolder] = _

@Setup
def prepare: Unit = {
holders = Array.fill(100){ new LazyHolder }
}

@Benchmark
def measureInitialized(bh: Blackhole) = {
var i = 0
while(i < 100) {
val currentHolder = holders(i)
bh.consume(currentHolder)
bh.consume(currentHolder.value)
i = i + 1
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package dotty.tools.benchmarks.lazyvals

import org.openjdk.jmh.annotations._
import LazyVals.LazyStringHolder
import org.openjdk.jmh.infra.Blackhole
import java.util.concurrent.TimeUnit

@BenchmarkMode(Array(Mode.AverageTime))
@Fork(2)
@Threads(1)
@Warmup(iterations = 5)
@Measurement(iterations = 5)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@State(Scope.Benchmark)
class InitializedAccessString {

var holder: LazyStringHolder = _

@Setup
def prepare: Unit = {
holder = new LazyStringHolder
holder.value
}

@Benchmark
def measureInitialized(bh: Blackhole) = {
bh.consume(holder)
bh.consume(holder.value)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package dotty.tools.benchmarks.lazyvals
import java.util.concurrent.CountDownLatch
object LazyVals {

trait Foo
class Bar1 extends Foo
class Bar2 extends Foo
class Bar3 extends Foo
class Bar4 extends Foo
class Bar5 extends Bar4

class LazyStringHolder {

lazy val value: String = {
System.nanoTime() % 5 match {
case 0 => "abc"
case 1 => "def"
case 2 => "ghi"
case 3 => "jkl"
case 4 => "mno"
}
}
}

class LazyHolder {

lazy val value: List[Int] = {
System.nanoTime() % 5 match {
case 0 => 1 :: 2 :: Nil
case 1 => Nil
case 2 => 1 :: Nil
case 3 => Nil
case 4 => 1 :: 2 :: 3 :: Nil
}
}
}

class LazyGenericHolder[A](v: => A) {
lazy val value: A = v
}

class LazyAnyHolder {
lazy val value: Any = {
System.nanoTime() % 5 match {
case 0 => new Bar1
case 1 => new Bar2
case 2 => new Bar3
case 3 => new Bar4
case 4 => new Bar4
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package dotty.tools.benchmarks.lazyvals

import org.openjdk.jmh.annotations._
import LazyVals.LazyHolder
import org.openjdk.jmh.infra.Blackhole
import java.util.concurrent.TimeUnit

@BenchmarkMode(Array(Mode.AverageTime))
@Fork(2)
@Threads(1)
@Warmup(iterations = 5)
@Measurement(iterations = 5)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@State(Scope.Benchmark)
class UninitializedAccess {

@Benchmark
def measureInitialized(bh: Blackhole) = {
var i = 0
val holder = new LazyHolder
bh.consume(holder)
bh.consume(holder.value)
i = i + 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package dotty.tools.benchmarks.lazyvals

import org.openjdk.jmh.annotations._
import LazyVals.LazyHolder
import org.openjdk.jmh.infra.Blackhole
import java.util.concurrent.TimeUnit

@BenchmarkMode(Array(Mode.AverageTime))
@Fork(2)
@Threads(1)
@Warmup(iterations = 5)
@Measurement(iterations = 5)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@State(Scope.Benchmark)
class UninitializedAccessMultiple {

@Benchmark
def measureInitialized(bh: Blackhole) = {
var i = 0
while(i < 100) {
val holder = new LazyHolder
bh.consume(holder)
bh.consume(holder.value)
i = i + 1
}
}
}
1 change: 1 addition & 0 deletions compiler/src/dotty/tools/dotc/config/ScalaSettings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ private sealed trait YSettings:
val YrecheckTest: Setting[Boolean] = BooleanSetting("-Yrecheck-test", "Run basic rechecking (internal test only)")
val YccDebug: Setting[Boolean] = BooleanSetting("-Ycc-debug", "Used in conjunction with captureChecking language import, debug info for captured references")
val YccNoAbbrev: Setting[Boolean] = BooleanSetting("-Ycc-no-abbrev", "Used in conjunction with captureChecking language import, suppress type abbreviations")
val YlightweightLazyVals: Setting[Boolean] = BooleanSetting("-Ylightweight-lazy-vals", "Use experimental lightweight implementation of lazy vals")

/** Area-specific debug output */
val YexplainLowlevel: Setting[Boolean] = BooleanSetting("-Yexplain-lowlevel", "When explaining type errors, show types at a lower level.")
Expand Down
6 changes: 6 additions & 0 deletions compiler/src/dotty/tools/dotc/core/Definitions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1990,6 +1990,12 @@ class Definitions {
addSyntheticSymbolsComments
}

/** Definitions used in Lazy Vals implementation */
val LazyValsModuleName = "scala.runtime.LazyVals"
@tu lazy val LazyValsModule = requiredModule(LazyValsModuleName)
@tu lazy val LazyValsWaitingState = requiredClass(s"$LazyValsModuleName.Waiting")
@tu lazy val LazyValsControlState = requiredClass(s"$LazyValsModuleName.LazyValControlState")

def addSyntheticSymbolsComments(using Context): Unit =
def add(sym: Symbol, doc: String) = ctx.docCtx.foreach(_.addDocstring(sym, Some(Comment(NoSpan, doc))))

Expand Down
Loading