Skip to content

Commit

Permalink
RefLike#upd now doesn't have a default impl (we almost always overrid…
Browse files Browse the repository at this point in the history
…e it)

With this, we essentially went back to how things were in v0.4.5,
since that seems to be the most performant.
  • Loading branch information
durban committed Dec 2, 2024
1 parent c50365d commit b1203f0
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 6 deletions.
8 changes: 5 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 @@ -29,10 +29,12 @@ trait RefLike[A] {

def updWith[B, C](f: (A, B) => Axn[(A, C)]): Rxn[B, C]

// derived (but overridden for performance):
// derivable (but primitive for performance):

def upd[B, C](f: (A, B) => (A, C)): Rxn[B, C] =
updWith[B, C] { (a, b) => Axn.pure(f(a, b)) }
def upd[B, C](f: (A, B) => (A, C)): Rxn[B, C]
// Note: in theory this could be implemented as
// `updWith[B, C] { (a, b) => Axn.pure(f(a, b)) }`,
// but we rather have it as a primitive.

// derived:

Expand Down
3 changes: 2 additions & 1 deletion data/jvm/src/main/scala/dev/tauri/choam/data/Ttrie.scala
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,8 @@ private final class Ttrie[K, V] private (
final def get: Axn[V] =
self.get.provide(key).map(_.getOrElse(default))

// TODO: maybe override `upd` and/or `getAndSet` if we can make it faster than the default impl.
final def upd[B, C](f: (V, B) => (V, C)): B =#> C =
updWith[B, C] { (v, b) => Axn.pure(f(v, b)) }

final def updWith[B, C](f: (V, B) => Axn[(V, C)]): B =#> C = {
getRefWithKey(key).flatMap { ref =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ private final class SimpleMap[K, V] private (
final def get: Axn[V] =
self.get.provide(key).map(_.getOrElse(default))

final override def upd[B, C](f: (V, B) => (V, C)): B =#> C = {
final def upd[B, C](f: (V, B) => (V, C)): B =#> C = {
Rxn.computed[B, C] { (b: B) =>
repr.modify { hm =>
val currVal = hm.getOrElse(key, default)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ private final class SimpleOrderedMap[K, V] private (
final def get: Axn[V] =
self.get.provide(key).map(_.getOrElse(default))

final override def upd[B, C](f: (V, B) => (V, C)): B =#> C = {
final def upd[B, C](f: (V, B) => (V, C)): B =#> C = {
Rxn.computed[B, C] { (b: B) =>
repr.modify { am =>
val currVal = am.get(key).getOrElse(default)
Expand Down

0 comments on commit b1203f0

Please sign in to comment.