Skip to content

Commit

Permalink
Merge branch 'release-3.2.2' into language-reference-stable
Browse files Browse the repository at this point in the history
  • Loading branch information
Kordyjan committed Jan 23, 2023
2 parents d26c3ca + 22193a3 commit a3321cc
Show file tree
Hide file tree
Showing 396 changed files with 8,007 additions and 2,517 deletions.
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
}
}
}
Loading

0 comments on commit a3321cc

Please sign in to comment.