Skip to content

Commit

Permalink
Remove the GetAndSet primitive, because it doesn't seem to help perfo…
Browse files Browse the repository at this point in the history
…rmance
  • Loading branch information
durban committed Nov 25, 2024
1 parent d790ebc commit 72a6aca
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 50 deletions.
50 changes: 6 additions & 44 deletions core/shared/src/main/scala/dev/tauri/choam/core/Rxn.scala
Original file line number Diff line number Diff line change
Expand Up @@ -412,9 +412,6 @@ object Rxn extends RxnInstances0 {
private[choam] final def get[A](r: Ref[A]): Axn[A] =
new Read(r.loc)

private[choam] final def getAndSet[A](r: Ref[A]): Rxn[A, A] =
new GetAndSet(r.loc)

private[choam] final def upd[A, B, C](r: Ref[A])(f: (A, B) => (A, C)): Rxn[B, C] =
new Upd(r.loc, f)

Expand Down Expand Up @@ -564,11 +561,6 @@ object Rxn extends RxnInstances0 {
final override def toString: String = s"Read(${ref})"
}

private final class GetAndSet[A](val ref: MemoryLocation[A]) extends Rxn[A, A] {
private[core] final override def tag = 9
final override def toString: String = s"GetAndSet(${ref})"
}

private final class Upd[A, B, X](val ref: MemoryLocation[X], val f: (X, A) => (X, B)) extends Rxn[A, B] {
private[core] final override def tag = 10
final override def toString: String = s"Upd(${ref}, <function>)"
Expand Down Expand Up @@ -941,16 +933,8 @@ object Rxn extends RxnInstances0 {
val res: LogEntry[Any] = (curr.tag : @switch) match {
case 8 => // Read
this.ctx.readIntoHwd(ref)
case 9 => // GetAndSet
val c = curr.asInstanceOf[GetAndSet[Any]]
val hwd = this.ctx.readIntoHwd(c.ref)
if (this.desc.isValidHwd(hwd)) {
val res = hwd.withNv(this.a)
this.a = hwd.nv
res
} else {
hwd
}
case 9 => // was GetAndSet
sys.error("GetAndSet")
case 10 => // Upd
val c = curr.asInstanceOf[Upd[Any, Any, Any]]
val hwd = this.ctx.readIntoHwd(c.ref)
Expand Down Expand Up @@ -978,10 +962,8 @@ object Rxn extends RxnInstances0 {
val res: LogEntry[Any] = (curr.tag : @switch) match {
case 8 => // Read
hwd
case 9 => // GetAndSet
val res = hwd.withNv(this.a)
this.a = hwd.nv
res
case 9 => // was GetAndSet
sys.error("GetAndSet")
case 10 => // Upd
val ox = hwd.nv
val (nx, b) = curr.asInstanceOf[Upd[Any, Any, Any]].f(ox, this.a)
Expand Down Expand Up @@ -1472,28 +1454,8 @@ object Rxn extends RxnInstances0 {
a = hwd2.nv
loop(next())
}
case 9 => // GetAndSet
val c = curr.asInstanceOf[GetAndSet[Any]]
assert(this._entryHolder eq null) // just to be sure
desc = desc.computeOrModify(c.ref, tok = c, visitor = this)
val hwd = this._entryHolder
this._entryHolder = null // cleanup
val nxt = if (!desc.isValidHwd(hwd)) {
if (forceValidate(hwd)) {
// OK, `desc` was extended;
// but need to finish `GetAndSet`:
val newHwd = hwd.withNv(this.a)
this.a = hwd.nv
desc = desc.overwrite(newHwd)
next()
} else {
assert(this._desc eq null)
retry()
}
} else {
next()
}
loop(nxt)
case 9 => // was GetAndSet
sys.error("GetAndSet")
case 10 => // Upd
val c = curr.asInstanceOf[Upd[A, B, Any]]
assert(this._entryHolder eq null) // just to be sure
Expand Down
3 changes: 0 additions & 3 deletions core/shared/src/main/scala/dev/tauri/choam/refs/Ref.scala
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ sealed trait Ref[A] extends RefLike[A] { this: MemoryLocation[A] =>
final override def get: Axn[A] =
Rxn.ref.get(this)

final override def getAndSet: Rxn[A, A] =
Rxn.ref.getAndSet(this)

// TODO: needs better name (it's like `modify`)
final override def upd[B, C](f: (A, B) => (A, C)): Rxn[B, C] =
Rxn.ref.upd(this)(f)
Expand Down
6 changes: 3 additions & 3 deletions core/shared/src/main/scala/dev/tauri/choam/refs/RefLike.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ trait RefLike[A] {
def upd[B, C](f: (A, B) => (A, C)): Rxn[B, C] =
updWith[B, C] { (a, b) => Axn.pure(f(a, b)) }

def getAndSet: Rxn[A, A] =
upd[A, A] { (oa, na) => (na, oa) }

// derived:

final def set: Rxn[A, Unit] =
getAndSet.void

final def getAndSet: Rxn[A, A] =
upd[A, A] { (oa, na) => (na, oa) }

final def update(f: A => A): Axn[Unit] =
upd[Any, Unit] { (oa, _) => (f(oa), ()) }

Expand Down

0 comments on commit 72a6aca

Please sign in to comment.