Skip to content

Commit

Permalink
Rename ObjStack -> ListObjStack
Browse files Browse the repository at this point in the history
  • Loading branch information
durban committed Apr 28, 2024
1 parent 6e06d0f commit 995cfc7
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,21 @@ class InternalStackBench {
}

@Benchmark
def objStack(s: St, bh: Blackhole): Unit = {
s.objStack = new ObjStack[String]
def listObjStack(s: St, bh: Blackhole): Unit = {
s.listObjStack = new ListObjStack[String]
var i = 0
while (i < N) {
s.objStack.push("test")
s.listObjStack.push("test")
i += 1
}
i = 0
while (i < N) {
bh.consume(s.objStack.pop())
bh.consume(s.listObjStack.pop())
i += 1
}
i = 0
while (i < N) {
s.objStack.push("test")
s.listObjStack.push("test")
i += 1
}
}
Expand All @@ -74,6 +74,6 @@ private object InternalStackBench {
@State(Scope.Benchmark)
class St {
final var byteStack: ByteStack = null
final var objStack: ObjStack[String] = null
final var listObjStack: ListObjStack[String] = null
}
}
28 changes: 14 additions & 14 deletions bench/src/main/scala/dev/tauri/choam/core/ObjStackBench.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ class ObjStackBench {
import ObjStackBench._

@Benchmark
def pushPopObjStack(s: ObjSt, bh: Blackhole): Unit = {
if (ThreadLocalRandom.current().nextBoolean() || s.objStack.isEmpty) {
s.objStack.push("x")
def pushPopOListbjStack(s: ListObjSt, bh: Blackhole): Unit = {
if (ThreadLocalRandom.current().nextBoolean() || s.listObjStack.isEmpty) {
s.listObjStack.push("x")
} else {
bh.consume(s.objStack.pop())
bh.consume(s.listObjStack.pop())
}
}

Expand All @@ -50,8 +50,8 @@ class ObjStackBench {
}

@Benchmark
def toArrayObjStack(s: ObjSt, bh: Blackhole): Unit = {
bh.consume(s.objStack.takeSnapshot())
def toArrayListObjStack(s: ListObjSt, bh: Blackhole): Unit = {
bh.consume(s.listObjStack.takeSnapshot())
}

@Benchmark
Expand All @@ -60,9 +60,9 @@ class ObjStackBench {
}

@Benchmark
def addAllClearObjStack(s: ObjSt, r: RandomArray): Unit = {
s.objStack.loadSnapshot(r.randomList)
s.objStack.clear()
def addAllClearListObjStack(s: ListObjSt, r: RandomArray): Unit = {
s.listObjStack.loadSnapshot(r.randomList)
s.listObjStack.clear()
}

@Benchmark
Expand All @@ -77,9 +77,9 @@ private object ObjStackBench {
final val initSize = 8

@State(Scope.Thread)
class ObjSt {
val objStack: ObjStack[String] = {
val s = new ObjStack[String]
class ListObjSt {
val listObjStack: ListObjStack[String] = {
val s = new ListObjStack[String]
for (i <- 1 to 8) {
if (ThreadLocalRandom.current().nextBoolean()) {
s.push(i.toString())
Expand Down Expand Up @@ -109,11 +109,11 @@ private object ObjStackBench {
ThreadLocalRandom.current().nextLong().toString()
}
}
val randomList: ObjStack.Lst[String] = {
val randomList: ListObjStack.Lst[String] = {
val l = List.fill(ThreadLocalRandom.current().nextInt(16)) {
ThreadLocalRandom.current().nextLong().toString()
}
val s = new ObjStack[String]
val s = new ListObjStack[String]
s.pushAll(l)
s.takeSnapshot()
}
Expand Down
22 changes: 11 additions & 11 deletions core/jvm/src/main/scala/dev/tauri/choam/core/ExchangerImplJvm.scala
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,12 @@ private sealed trait ExchangerImplJvm[A, B]
val (newContT, newContK) = mergeConts[D](
selfContT = selfMsg.contT,
// we put `b` on top of contK; `FinishExchange` will pop it:
selfContK = ObjStack.Lst[Any](b, selfMsg.contK),
selfContK = ListObjStack.Lst[Any](b, selfMsg.contK),
otherContT = other.msg.contT,
otherContK = other.msg.contK,
hole = other.hole,
)
debugLog(s"merged conts: newContT = ${java.util.Arrays.toString(newContT)}; newContK = [${ObjStack.Lst.mkString(newContK)}] - thread#${Thread.currentThread().getId()}")
debugLog(s"merged conts: newContT = ${java.util.Arrays.toString(newContT)}; newContK = [${ListObjStack.Lst.mkString(newContK)}] - thread#${Thread.currentThread().getId()}")
val mergedDesc = try {
ctx.addAll(selfMsg.desc, other.msg.desc)
} catch {
Expand All @@ -221,43 +221,43 @@ private sealed trait ExchangerImplJvm[A, B]
contK = newContK,
contT = newContT,
desc = mergedDesc,
postCommit = ObjStack.Lst.concat(other.msg.postCommit, selfMsg.postCommit),
postCommit = ListObjStack.Lst.concat(other.msg.postCommit, selfMsg.postCommit),
// this thread will continue, so we use (and update) our data:
exchangerData = selfMsg.exchangerData.updated(this.key, Statistics.exchanged(stats, params))
)
debugLog(s"merged postCommit: ${ObjStack.Lst.mkString(resMsg.postCommit)} - thread#${Thread.currentThread().getId()}")
debugLog(s"merged postCommit: ${ListObjStack.Lst.mkString(resMsg.postCommit)} - thread#${Thread.currentThread().getId()}")
Right(resMsg)
}

private[this] final def mergeConts[D](
selfContT: Array[Byte],
selfContK: ObjStack.Lst[Any],
selfContK: ListObjStack.Lst[Any],
otherContT: Array[Byte],
otherContK: ObjStack.Lst[Any],
otherContK: ListObjStack.Lst[Any],
hole: Ref[NodeResult[D]],
): (Array[Byte], ObjStack.Lst[Any]) = {
): (Array[Byte], ListObjStack.Lst[Any]) = {
// otherContK: |-|-|-|-...-|COMMIT|-|-...-|
// \-----------/
// we'll need this first part (until the first commit)
// and also need an extra op (see below)
// after this, we'll continue with selfContK
// (extra op: to fill `other.hole` with the result
// and also the remaining part of otherContK and otherContT)
ObjStack.Lst.splitBefore[Any](lst = otherContK, item = Rxn.commitSingleton) match {
ListObjStack.Lst.splitBefore[Any](lst = otherContK, item = Rxn.commitSingleton) match {
case (prefix, rest) =>
val extraOp = Rxn.internal.finishExchange[D](
hole = hole,
restOtherContK = rest,
lenSelfContT = selfContT.length,
)
val newContK = ObjStack.Lst.concat(
val newContK = ListObjStack.Lst.concat(
prefix,
ObjStack.Lst(extraOp, selfContK),
ListObjStack.Lst(extraOp, selfContK),
)
val newContT = mergeContTs(selfContT = selfContT, otherContT = otherContT)
(newContT, newContK)
case null =>
val len = ObjStack.Lst.length(otherContK)
val len = ListObjStack.Lst.length(otherContK)
if (len == 0) impossible("empty otherContK")
else impossible(s"no commit in otherContK: ${otherContK.mkString()}")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,10 @@ private[choam] object Exchanger extends ExchangerCompanionPlatform { // TODO: sh

private[core] final case class Msg(
value: Any,
contK: ObjStack.Lst[Any],
contK: ListObjStack.Lst[Any],
contT: Array[Byte],
desc: Descriptor,
postCommit: ObjStack.Lst[Axn[Unit]],
postCommit: ListObjStack.Lst[Axn[Unit]],
exchangerData: Rxn.ExStatMap,
)

Expand All @@ -132,7 +132,7 @@ private[choam] object Exchanger extends ExchangerCompanionPlatform { // TODO: sh
contK = fx.contK,
contT = fx.contT,
desc = ctx.start(),
postCommit = ObjStack.Lst.empty[Axn[Unit]],
postCommit = ListObjStack.Lst.empty[Axn[Unit]],
exchangerData = newStats,
)
}
Expand All @@ -142,7 +142,7 @@ private[choam] object Exchanger extends ExchangerCompanionPlatform { // TODO: sh

private[core] final class FinishedEx[C](
val result: C,
val contK: ObjStack.Lst[Any],
val contK: ListObjStack.Lst[Any],
val contT: Array[Byte],
) extends NodeResult[C]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@
package dev.tauri.choam
package core

private final class ObjStack[A]() {
private final class ListObjStack[A]() {

private[this] var lst: ObjStack.Lst[A] =
private[this] var lst: ListObjStack.Lst[A] =
null

final override def toString: String = {
if (this.lst ne null) s"ObjStack(${this.lst.mkString(", ")})"
else "ObjStack()"
if (this.lst ne null) s"ListObjStack(${this.lst.mkString(", ")})"
else "ListObjStack()"
}

final def push(a: A): Unit = {
this.lst = new ObjStack.Lst(a, this.lst)
this.lst = new ListObjStack.Lst(a, this.lst)
}

final def pushAll(as: Iterable[A]): Unit = {
Expand Down Expand Up @@ -79,20 +79,20 @@ private final class ObjStack[A]() {
this.lst ne null
}

final def takeSnapshot(): ObjStack.Lst[A] = {
final def takeSnapshot(): ListObjStack.Lst[A] = {
this.lst
}

final def loadSnapshot(snapshot: ObjStack.Lst[A]): Unit = {
final def loadSnapshot(snapshot: ListObjStack.Lst[A]): Unit = {
this.lst = snapshot
}

final def loadSnapshotUnsafe(snapshot: ObjStack.Lst[Any]): Unit = {
this.lst = snapshot.asInstanceOf[ObjStack.Lst[A]]
final def loadSnapshotUnsafe(snapshot: ListObjStack.Lst[Any]): Unit = {
this.lst = snapshot.asInstanceOf[ListObjStack.Lst[A]]
}
}

private object ObjStack {
private object ListObjStack {

final class Lst[+A](final val head: A, final val tail: Lst[A]) {

Expand Down
20 changes: 10 additions & 10 deletions core/shared/src/main/scala/dev/tauri/choam/core/Rxn.scala
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ object Rxn extends RxnInstances0 {

final def finishExchange[D](
hole: Ref[Exchanger.NodeResult[D]],
restOtherContK: ObjStack.Lst[Any],
restOtherContK: ListObjStack.Lst[Any],
lenSelfContT: Int,
): Rxn[D, Unit] = new FinishExchange(hole, restOtherContK, lenSelfContT)
}
Expand Down Expand Up @@ -554,12 +554,12 @@ object Rxn extends RxnInstances0 {
/** Only the interpreter/exchanger can use this! */
private final class FinishExchange[D](
val hole: Ref[Exchanger.NodeResult[D]],
val restOtherContK: ObjStack.Lst[Any],
val restOtherContK: ListObjStack.Lst[Any],
val lenSelfContT: Int,
) extends Rxn[D, Unit] {
private[core] final override def tag = 18
final override def toString: String = {
val rockLen = ObjStack.Lst.length(this.restOtherContK)
val rockLen = ListObjStack.Lst.length(this.restOtherContK)
s"FinishExchange(${hole}, <ObjStack.Lst of length ${rockLen}>, ${lenSelfContT})"
}
}
Expand Down Expand Up @@ -618,7 +618,7 @@ object Rxn extends RxnInstances0 {
// Interpreter:

private[this] def newStack[A]() = {
new ObjStack[A]
new ListObjStack[A]
}

private[this] final class PostCommitResultMarker // TODO: make this a java enum?
Expand Down Expand Up @@ -682,18 +682,18 @@ object Rxn extends RxnInstances0 {
_desc = null
}

private[this] val alts: ObjStack[Any] = newStack[Any]()
private[this] val alts: ListObjStack[Any] = newStack[Any]()

private[this] val contT: ByteStack = new ByteStack(initSize = 8)
private[this] val contK: ObjStack[Any] = newStack[Any]()
private[this] val pc: ObjStack[Rxn[Any, Unit]] = newStack[Rxn[Any, Unit]]()
private[this] val contK: ListObjStack[Any] = newStack[Any]()
private[this] val pc: ListObjStack[Rxn[Any, Unit]] = newStack[Rxn[Any, Unit]]()
private[this] val commit = commitSingleton
contT.push(RxnConsts.ContAfterPostCommit)
contT.push(RxnConsts.ContAndThen)
contK.push(commit)

private[this] var contTReset: Array[Byte] = contT.takeSnapshot()
private[this] var contKReset: ObjStack.Lst[Any] = contK.takeSnapshot()
private[this] var contKReset: ListObjStack.Lst[Any] = contK.takeSnapshot()

private[this] var a: Any =
x
Expand Down Expand Up @@ -842,8 +842,8 @@ object Rxn extends RxnInstances0 {
private[this] final def loadAlt(): Rxn[Any, R] = {
val alts = this.alts
val res = alts.pop().asInstanceOf[Rxn[Any, R]]
pc.loadSnapshotUnsafe(alts.pop().asInstanceOf[ObjStack.Lst[Any]])
contK.loadSnapshot(alts.pop().asInstanceOf[ObjStack.Lst[Any]])
pc.loadSnapshotUnsafe(alts.pop().asInstanceOf[ListObjStack.Lst[Any]])
contK.loadSnapshot(alts.pop().asInstanceOf[ListObjStack.Lst[Any]])
contT.loadSnapshot(alts.pop().asInstanceOf[Array[Byte]])
a = alts.pop()
_desc = alts.pop().asInstanceOf[Descriptor]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,31 @@ import scala.util.Try

final class InternalStackSpec extends BaseSpec {

test("ObjStack") {
val s = new ObjStack[String]
test("ListObjStack") {
val s = new ListObjStack[String]
assert(s.isEmpty)
assert(!s.nonEmpty)
assertEquals(s.toString, "ObjStack()")
assertEquals(s.toString, "ListObjStack()")
val s0 = s.takeSnapshot()
s.push("a")
assertEquals(s.toString, "ObjStack(a)")
assertEquals(s.toString, "ListObjStack(a)")
assert(!s.isEmpty)
assert(s.nonEmpty)
s.push("b")
assertEquals(s.toString, "ObjStack(b, a)")
assertEquals(s.toString, "ListObjStack(b, a)")
assertSameInstance(s.pop(), "b")
assertEquals(s.toString, "ObjStack(a)")
assertEquals(s.toString, "ListObjStack(a)")
val s1 = s.takeSnapshot()
s.push("b")
assertEquals(s.toString, "ObjStack(b, a)")
assertEquals(s.toString, "ListObjStack(b, a)")
s.pushAll("c" :: "d" :: Nil)
assertEquals(s.toString, "ObjStack(d, c, b, a)")
assertEquals(s.toString, "ListObjStack(d, c, b, a)")
assertEquals(s.peek(), "d")
assertEquals(s.peek(), "d")
assertEquals(s.peekSecond(), "c")
assertEquals(s.peek(), "d")
assertEquals(s.peekSecond(), "c")
assertEquals(s.toString, "ObjStack(d, c, b, a)")
assertEquals(s.toString, "ListObjStack(d, c, b, a)")
val s2 = s.takeSnapshot()
s.loadSnapshot(s1)
assertSameInstance(s.pop(), "a")
Expand Down Expand Up @@ -88,7 +88,7 @@ final class InternalStackSpec extends BaseSpec {
}

test("ObjStack.Lst.length") {
import ObjStack.Lst
import ListObjStack.Lst
assertEquals(Lst.length(null), 0)
assertEquals(Lst.length(Lst(1, null)), 1)
assertEquals(Lst.length(Lst(1, Lst(2, null))), 2)
Expand All @@ -97,7 +97,7 @@ final class InternalStackSpec extends BaseSpec {
}

test("ObjStack.Lst.reversed") {
import ObjStack.Lst
import ListObjStack.Lst
assertEquals(Lst.reversed(null), null)
assertEquals(Lst.reversed(Lst(1, null)).mkString(), "1")
assertEquals(Lst.reversed(Lst(1, Lst(2, null))).mkString(), "2, 1")
Expand All @@ -106,7 +106,7 @@ final class InternalStackSpec extends BaseSpec {
}

test("ObjStack.Lst.concat") {
import ObjStack.Lst
import ListObjStack.Lst
assertEquals(Lst.concat(null, null), null)
assertEquals(Lst.concat(Lst(1, null), null).mkString(), "1")
assertEquals(Lst.concat(null, Lst(1, null)).mkString(), "1")
Expand All @@ -119,7 +119,7 @@ final class InternalStackSpec extends BaseSpec {
}

test("ObjStack.Lst.splitBefore") {
import ObjStack.Lst
import ListObjStack.Lst
assertEquals(Lst.splitBefore[String](null, "a"), null)
assertEquals(Lst.splitBefore[String](Lst("x", null), "a"), null)
assertEquals(Lst.splitBefore[String](Lst("x", Lst("y", null)), "a"), null)
Expand Down

0 comments on commit 995cfc7

Please sign in to comment.