Skip to content

Commit

Permalink
Make a benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
ysthakur committed Dec 13, 2024
1 parent 36afa04 commit 44abdad
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@ sbt-launch.jar
/.worksheet/

a.out
bench.bin
fred.jar
30 changes: 30 additions & 0 deletions bench.nu
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const jar = "fred.jar"
const outExe = "bench.bin"

export def run [ file: string, lazyMarkScan: bool ] {
let benchName = $file | path basename
if $lazyMarkScan {
java -jar $jar $file -o $outExe --lazy-mark-scan-only
} else {
java -jar $jar $file -o $outExe
}
let res = (
^$"./($outExe)"
| lines
| last
| parse "Time stamp counter diff: {tsc}, clock diff: {clock}"
| get 0)
{
name: $benchName,
tsc: $res.tsc,
clock: $res.clock,
lazyMarkScan: $lazyMarkScan
}
}

export def run-all [ ] {
let files = (ls benchmarks).name
let myAlgo = $files | each { |file| run $file false }
let lazyOnly = $files | each { |file| run $file true }
$myAlgo ++ $lazyOnly
}
50 changes: 50 additions & 0 deletions benchmarks/game.fred
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
data Player = Player {
mut friends: PlayerList,
store: Store
}

data PlayerList
= PlayerNil {}
| PlayerCons {
player: Player,
next: PlayerList
}

data Store = Store { datums: Data }

data Data
= DataCons {
value: int,
// This is mut only so the compiler thinks there can be a cycle at runtime
mut next: Data
}
| DataNil {}

fn createDummyData(length: int): Data =
if length == 0 then DataNil {}
else DataCons { value: length, next: createDummyData(length - 1) }

fn createPlayers(n: int, store: Store): PlayerList =
if n == 0 then PlayerNil {}
else
let player = Player { friends: PlayerNil {}, store: store } in
let next = createPlayers(n - 1, store) in
set player.friends PlayerCons { player: player, next: next };
player.friends

fn pointlessLoop(iters: int): int =
if iters == 0 then 0
else
createPlayers(1000, Store { datums: createDummyData(10000) });
c("processAllPCRs();");
pointlessLoop(iters - 1)

fn main(): int =
printf("Starting game benchmark\n");
c("float clockStart = (float) clock()/CLOCKS_PER_SEC;");
c("u_int64_t tscStart = rdtscp();");
pointlessLoop(5000);
c("printf(
\"Time stamp counter diff: %ld, clock diff: %lf\",
rdtscp() - tscStart,
((float) clock()/CLOCKS_PER_SEC) - clockStart);")
2 changes: 2 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ lazy val root = project

scalacOptions ++= Seq("-Wunused:all", "-deprecation"),

assembly / assemblyOutputPath := file("./fred.jar"),

libraryDependencies ++= Seq(
"org.typelevel" %% "cats-parse" % "0.3.9",
"com.github.scopt" %% "scopt" % "4.1.0",
Expand Down
1 change: 1 addition & 0 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.5.2")
addSbtPlugin("com.siriusxm" % "sbt-snapshot4s" % "0.1.5")
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "2.3.0")
9 changes: 9 additions & 0 deletions src/main/resources/runtime/runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

enum Color
{
Expand Down Expand Up @@ -186,3 +187,11 @@ void processAllPCRs()
pcrBuckets = next;
}
}

// From https://stackoverflow.com/a/14783909/11882002
static inline u_int64_t rdtscp() {
u_int64_t rax,rdx;
u_int32_t aux;
asm volatile ( "rdtscp\n" : "=a" (rax), "=d" (rdx), "=c" (aux) : : );
return (rdx << 32) + rax;
}
2 changes: 2 additions & 0 deletions src/main/scala/fred/Compiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ object Compiler {
if (settings.includeMemcheck) "-I /usr/include/valgrind" else ""

assert(s"gcc $extraIncludes -o $outExe -x c -".run(io).exitValue() == 0)
System.err.println(s"Created executable at $outExe")
File(outExe).setExecutable(true)

runtimeHeader.close()
}
Expand Down
11 changes: 7 additions & 4 deletions src/main/scala/fred/Parser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ object Parser {
}

private object ParserHelpers {
val comment = (P.string("//") *> P.repUntil0(P.anyChar.void, crlf | lf)).void
val comment = (P.string("//") *> P.repUntil0(P.anyChar.void, crlf | lf))
.void
val ws: P0[Unit] = (sp | crlf | lf | comment).rep0.void

extension [A](p1: P[A])
Expand Down Expand Up @@ -71,10 +72,12 @@ object Parser {
case (num, end) => IntLiteral(num.toInt, Span(end - num.length, end))
}.withContext("int literal")
val stringLiteral: P[StringLiteral] = spanned(
P.char('"') *> ((P.char('\\') ~ P.anyChar) | (P.charWhere(_ != '"'))).rep
P.char('"') *> ((P.char('\\') *> P.anyChar) | (P.charWhere(_ != '"'))).rep
.string <* P.char('"')
).map { case Spanned(text, span) => StringLiteral(text, span) }
.withContext("str literal")
).map { case Spanned(text, span) =>
// TODO make this work with multiple backslashes and stuff
StringLiteral(text.replace("\\\"", "\"").replace(raw"\\", "\\"), span)
}.withContext("str literal")
val literal = intLiteral | stringLiteral
val parenExpr = inParens(expr).withContext("paren expr")
val varRefOrFnCallOrCtorCall =
Expand Down

0 comments on commit 44abdad

Please sign in to comment.