Skip to content
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
2 changes: 1 addition & 1 deletion .github/workflows/benchmarks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
with:
jvm: temurin:1.${{ matrix.jvm }}
apps: mill
- run: mill j${{ matrix.jvm }}.benchmarks.test -f1 -o j${{ matrix.jvm }}-${{ matrix.os }}.bench -rff j${{ matrix.jvm }}-${{ matrix.os }}.json -rf json .*${{ matrix.benchmark }}${{ matrix.jit }}.*
- run: mill j${{ matrix.jvm }}.benchmarks.test -f1 -wi 2 -i 2 -o j${{ matrix.jvm }}-${{ matrix.os }}.bench -rff j${{ matrix.jvm }}-${{ matrix.os }}.json -rf json .*${{ matrix.benchmark }}${{ matrix.jit }}.*
- run: scala-cli run scripts/PublishBenchmarkReport.sc -- "Java ${{ matrix.jvm}}" ${{ matrix.os }} out/j${{ matrix.jvm }}/benchmarks/test/jmhRun.dest/j${{ matrix.jvm }}-${{ matrix.os }}.json ${{ matrix.benchmark }} ${{ matrix.jit }} >> $GITHUB_STEP_SUMMARY
- uses: actions/upload-artifact@v3
with:
Expand Down
104 changes: 38 additions & 66 deletions core/benchmarks/test/src/fr/hammons/slinc/BindingsBenchmarkShape.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,59 +3,44 @@ package fr.hammons.slinc
import org.openjdk.jmh.annotations.{Scope as _, *}
import java.util.concurrent.TimeUnit
import scala.util.Random
import fr.hammons.slinc.types.*
import scala.annotation.nowarn
import fr.hammons.slinc.modules.LibModule
import org.openjdk.jmh.infra.Blackhole

case class div_t(quot: Int, rem: Int)
case class div_t(quot: CInt, rem: CInt)

@nowarn("msg=unused import")
trait BindingsBenchmarkShape(val s: Slinc):
import scala.language.unsafeNulls
trait Cstd derives Lib:
def abs(i: CInt): CInt
def div(numer: CInt, denom: CInt): div_t
def labs(l: CLong): CLong
def qsort(
array: Ptr[Nothing],
num: SizeT,
size: SizeT,
fn: Ptr[(Ptr[Nothing], Ptr[Nothing]) => Int]
): Unit

trait Cstd3 derives Lib:
def abs(i: Int): Int

import s.given LibModule

@Benchmark
def abs2 =
Lib.instance[Cstd3].abs(-4)

lazy val cstd3 = Lib.instance[Cstd3]

@Benchmark
def abs3 =
cstd3.abs(-4)
trait Cstd2 derives Lib:
def labs(l: Long): Long

import s.{given, *}
import s.given

object Cstd derives Library:
def abs(i: Int): Int = Library.binding
def div(numer: Int, denom: Int): div_t = Library.binding
def labs(l: CLong): CLong = Library.binding
// todo: needs SizeT
def qsort[A](
array: Ptr[A],
num: Long,
size: SizeT,
fn: Ptr[(Ptr[A], Ptr[A]) => A]
): Unit = Library.binding

object Cstd2 derives Library:
def labs(l: Long): Long = Library.binding
val cstd = Lib.instance[Cstd]
val cstd2 = Lib.instance[Cstd2]

given Struct[div_t] = Struct.derived

val lib = summon[Library[Cstd.type]]
val absHandle = lib.handles(0)

val base = Seq.fill(10000)(Random.nextInt)
val baseArr = base.toArray

val upcall: Ptr[(Ptr[Int], Ptr[Int]) => Int] = Scope.global {
import s.{given, *}

val upcall: Ptr[(Ptr[Nothing], Ptr[Nothing]) => Int] = Scope.global {
Ptr.upcall((a, b) =>
val aVal = !a
val bVal = !b
val aVal = !a.castTo[Int]
val bVal = !b.castTo[Int]
if aVal < bVal then -1
else if aVal == bVal then 0
else 1
Expand All @@ -64,50 +49,37 @@ trait BindingsBenchmarkShape(val s: Slinc):

val path = Random.nextBoolean()

@Benchmark
def ifmark(blackhole: Blackhole) =
if path then blackhole.consume(1 + 2)
else blackhole.consume(2 + 3)

@Benchmark
def noifmark(blackhole: Blackhole) =
blackhole.consume(1 + 2)

@Benchmark
def abs =
Cstd.abs(6)
cstd.abs(6)

@Benchmark
def labs =
Cstd.labs(-15.as[CLong])
cstd.labs(CLong(-15))

@Benchmark
def labs2 =
Cstd2.labs(-15)
cstd2.labs(-15)

@Benchmark
def div =
Cstd.div(5, 2)
cstd.div(5, 2)

@Benchmark
@OutputTimeUnit(TimeUnit.MILLISECONDS)
def qsort =
Scope.confined {
val sortingArr = Ptr.copy(baseArr)
Cstd.qsort(
sortingArr,
10000,
4.as[SizeT],
Ptr.upcall((a, b) =>
val aVal = !a
val bVal = !b
if aVal < bVal then -1
else if aVal == bVal then 0
else 1
for size <- SizeT.maybe(10000)
do
Scope.confined {
val sortingArr = Ptr.copy(baseArr).castTo[Nothing]
cstd.qsort(
sortingArr,
size,
IntDescriptor.size.toSizeT,
upcall
)
)
sortingArr.asArray(10000)
}
sortingArr.castTo[Int].asArray(10000)
}

@Benchmark
@OutputTimeUnit(TimeUnit.MILLISECONDS)
Expand Down
10 changes: 8 additions & 2 deletions core/src/fr/hammons/slinc/Bytes.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package fr.hammons.slinc

import scala.quoted.{ToExpr, Quotes}
import types.SizeT
import fr.hammons.slinc.types.SizeT

opaque type Bytes = Long

Expand All @@ -16,7 +16,13 @@ object Bytes:
inline def -(b: Bytes): Bytes = a - b
inline def toLong: Long = a
inline def toBits: Long = a * 8
def sizeT = SizeT.maybe(a).get
def toSizeT = SizeT
.maybe(a)
.getOrElse(
throw new Exception(
s"The bytes described was too big for the current platform $a"
)
)

given Numeric[Bytes] = Numeric.LongIsIntegral
given ToExpr[Bytes] with
Expand Down
4 changes: 2 additions & 2 deletions core/test/src/fr/hammons/slinc/StdlibSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ trait StdlibSpec(val slinc: Slinc) extends ScalaCheckSuite:
cstd.qsort(
cArr,
len,
DescriptorOf[Int].size.sizeT,
DescriptorOf[Int].size.toSizeT,
Ptr.upcall((a: Ptr[Nothing], b: Ptr[Nothing]) =>
val aVal = !a.castTo[CInt]
val bVal = !b.castTo[CInt]
Expand All @@ -131,7 +131,7 @@ trait StdlibSpec(val slinc: Slinc) extends ScalaCheckSuite:
cstd.qsort(
arr,
size,
DescriptorOf[CInt].size.sizeT,
DescriptorOf[CInt].size.toSizeT,
Ptr.upcall: (a, b) =>
val aVal = !a.castTo[CInt]
val bVal = !b.castTo[CInt]
Expand Down