Skip to content

Commit

Permalink
avoid choice overhead
Browse files Browse the repository at this point in the history
  • Loading branch information
luithefirst committed Sep 23, 2024
1 parent aa8b799 commit 916cb3c
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 78 deletions.
20 changes: 12 additions & 8 deletions src/FSharp.Data.Adaptive/AdaptiveHashMap/AdaptiveHashMap.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1071,9 +1071,9 @@ module AdaptiveHashMapImplementation =
override x.Compute (token : AdaptiveToken) =
let cmp = DefaultEqualityComparer<'Key>.Instance
let mutable delta = null
for op in reader.GetChanges token do
match op with
| Add(_, (k, v)) ->
for d in reader.GetChanges token do
let (k, v) = d.Value
if d.Count = 1 then // >= 1 ??
match state.TryGetValue k with
| (true, set) ->
let newSet = HashSet.add v set
Expand All @@ -1083,7 +1083,7 @@ module AdaptiveHashMapImplementation =
let newSet = HashSet.single v
state.[k] <- newSet
delta <- MapNode.addInPlace' cmp k (Set (view newSet)) delta
| Rem(_, (k, v)) ->
elif d.Count = -1 then // <= -1 ??
match state.TryGetValue k with
| (true, set) ->
let newSet = HashSet.remove v set
Expand All @@ -1095,6 +1095,8 @@ module AdaptiveHashMapImplementation =
delta <- MapNode.addInPlace' cmp k (Set (view newSet)) delta
| _ ->
()
else
unexpected()

HashMap(cmp, delta) |> HashMapDelta.ofHashMap

Expand All @@ -1112,9 +1114,9 @@ module AdaptiveHashMapImplementation =
override x.Compute (token : AdaptiveToken) =
let cmp = DefaultEqualityComparer<'Key>.Instance
let mutable delta = null
for op in reader.GetChanges token do
match op with
| Add(_, v) ->
for d in reader.GetChanges token do
let v = d.Value
if d.Count = 1 then // >= 1 ??
let k = cache.Invoke v
match state.TryGetValue k with
| (true, set) ->
Expand All @@ -1125,7 +1127,7 @@ module AdaptiveHashMapImplementation =
let newSet = HashSet.single v
state.[k] <- newSet
delta <- MapNode.addInPlace' cmp k (Set (view newSet)) delta
| Rem(_, v) ->
elif d.Count = -1 then // <= -1 ??
let k = cache.Revoke v
match state.TryGetValue k with
| (true, set) ->
Expand All @@ -1138,6 +1140,8 @@ module AdaptiveHashMapImplementation =
delta <- MapNode.addInPlace' cmp k (Set (view newSet)) delta
| _ ->
()
else
unexpected()

HashMap(cmp, delta) |> HashMapDelta.ofHashMap

Expand Down
135 changes: 65 additions & 70 deletions src/FSharp.Data.Adaptive/AdaptiveHashSet/AdaptiveHashSet.fs
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,11 @@ module SetReductions =
let mutable e = ops.GetEnumerator()
while working && e.MoveNext() do
let op = e.Current
match op with
| Add(_, a) ->
sum <- reduction.add sum a

| Rem(_, old) ->
match reduction.sub sum old with
let v = op.Value
if op.Count = 1 then // >= 1 ??
sum <- reduction.add sum v
elif op.Count = -1 then // <= -1 ??
match reduction.sub sum v with
| ValueSome s -> sum <- s
| ValueNone -> working <- false

Expand Down Expand Up @@ -414,18 +413,18 @@ module AdaptiveHashSetImplementation =
static member DeltaMapping (mapping : 'A -> 'B) =
let cache = Cache mapping
HashSetDelta.map (fun d ->
match d with
| Add(1, v) -> Add(cache.Invoke v)
| Rem(1, v) -> Rem(cache.Revoke v)
| _ -> unexpected()
let v = d.Value
if d.Count = 1 then Add(cache.Invoke v)
elif d.Count = -1 then Rem(cache.Revoke v)
else unexpected()
)

override x.Compute(token) =
reader.GetChanges token |> HashSetDelta.map (fun d ->
match d with
| Add(1, v) -> Add(cache.Invoke v)
| Rem(1, v) -> Rem(cache.Revoke v)
| _ -> unexpected()
let v = d.Value
if d.Count = 1 then Add(cache.Invoke v)
elif d.Count = -1 then Rem(cache.Revoke v)
else unexpected()
)


Expand Down Expand Up @@ -460,17 +459,17 @@ module AdaptiveHashSetImplementation =
d
else
reader.GetChanges token |> HashSetDelta.choose (fun d ->
match d with
| Add(1, v) ->
let v = d.Value
if d.Count = 1 then
Add(cache.Invoke v) |> Some
| Rem(1, v) ->
elif d.Count = -1 then
match cache.RevokeAndGetDeletedTotal v with
| ValueSome (del, v) ->
if del then (v :> IDisposable).Dispose()
Rem v |> Some
| ValueNone ->
None
| _ ->
else
unexpected()
)

Expand Down Expand Up @@ -511,35 +510,31 @@ module AdaptiveHashSetImplementation =
static member DeltaMapping (mapping : 'A -> option<'B>) =
let cache = Cache mapping
HashSetDelta.choose (fun d ->
match d with
| Add(1, v) ->
let v = d.Value
if d.Count = 1 then
match cache.Invoke v with
| Some v -> Some (Add v)
| None -> None

| Rem(1, v) ->
elif d.Count = -1 then
match cache.Revoke v with
| Some v -> Some (Rem v)
| None -> None

| _ ->
else
unexpected()
)

override x.Compute(token) =
r.GetChanges token |> HashSetDelta.choose (fun d ->
match d with
| Add(1, v) ->
let v = d.Value
if d.Count = 1 then
match cache.Invoke v with
| Some v -> Some (Add v)
| None -> None

| Rem(1, v) ->
elif d.Count = -1 then
match cache.Revoke v with
| Some v -> Some (Rem v)
| None -> None

| _ ->
else
unexpected()
)

Expand All @@ -554,18 +549,18 @@ module AdaptiveHashSetImplementation =
static member DeltaMapping (predicate : 'T -> bool) =
let cache = Cache predicate
HashSetDelta.filter (fun d ->
match d with
| Add(1, v) -> cache.Invoke v
| Rem(1, v) -> cache.Revoke v
| _ -> unexpected()
let v = d.Value
if d.Count = 1 then cache.Invoke v
elif d.Count = -1 then cache.Revoke v
else unexpected()
)

override x.Compute(token) =
r.GetChanges token |> HashSetDelta.filter (fun d ->
match d with
| Add(1, v) -> cache.Invoke v
| Rem(1, v) -> cache.Revoke v
| _ -> unexpected()
let v = d.Value
if d.Count = 1 then cache.Invoke v
elif d.Count = -1 then cache.Revoke v
else unexpected()
)

/// Reader for fully dynamic union operations.
Expand All @@ -584,14 +579,14 @@ module AdaptiveHashSetImplementation =
override x.Compute(token, dirty) =
let mutable deltas =
reader.GetChanges token |> HashSetDelta.collect (fun d ->
match d with
| Add(1, v) ->
let v = d.Value
if d.Count = 1 then
// r is no longer dirty since we pull it here.
let r = cache.Invoke v
dirty.Remove r |> ignore
r.GetChanges token

| Rem(1, v) ->
elif d.Count = -1 then
// r is no longer dirty since we either pull or destroy it here.
let struct(deleted, r) = cache.RevokeAndGetDeleted v
dirty.Remove r |> ignore
Expand All @@ -603,7 +598,8 @@ module AdaptiveHashSetImplementation =
else
r.GetChanges token

| _ -> unexpected()
else
unexpected()
)

// finally pull all the dirty readers and accumulate the deltas.
Expand Down Expand Up @@ -817,10 +813,10 @@ module AdaptiveHashSetImplementation =

override x.Compute(token) =
reader.GetChanges token |> HashSetDelta.collect (fun d ->
match d with
| Add(1, v) -> HashSet.addAll v
| Rem(1, v) -> HashSet.removeAll v
| _ -> unexpected()
let v = d.Value
if d.Count = 1 then HashSet.addAll v
elif d.Count = -1 then HashSet.removeAll v
else unexpected()
)

/// Reader for collect operations.
Expand All @@ -839,14 +835,14 @@ module AdaptiveHashSetImplementation =
override x.Compute(token,dirty) =
let mutable deltas =
reader.GetChanges token |> HashSetDelta.collect (fun d ->
match d with
| Add(1, value) ->
let value = d.Value
if d.Count = 1 then
// r is no longer dirty since we pull it here.
let r = cache.Invoke value
dirty.Remove r |> ignore
r.GetChanges token

| Rem(1, value) ->
elif d.Count = -1 then
match cache.RevokeAndGetDeletedTotal value with
| ValueSome (deleted, r) ->
// r is no longer dirty since we either pull or destroy it here.
Expand All @@ -862,7 +858,8 @@ module AdaptiveHashSetImplementation =
// weird
HashSetDelta.empty

| _ -> unexpected()
else
unexpected()
)

// finally pull all the dirty readers and accumulate the deltas.
Expand Down Expand Up @@ -957,10 +954,10 @@ module AdaptiveHashSetImplementation =
override x.Compute(token, dirty) =
let mutable deltas =
r.GetChanges token |> HashSetDelta.map (fun d ->
match d with
| Add(1,m) -> Add(x.Invoke(token, m))
| Rem(1,m) -> Rem(x.Revoke(m, dirty))
| _ -> unexpected()
let m = d.Value
if d.Count = 1 then Add(x.Invoke(token, m))
elif d.Count = -1 then Rem(x.Revoke(m, dirty))
else unexpected()
)

for d in dirty do
Expand Down Expand Up @@ -1019,10 +1016,10 @@ module AdaptiveHashSetImplementation =
override x.Compute(token, dirty) =
let mutable deltas =
reader.GetChanges token |> HashSetDelta.map (fun d ->
match d with
| Add(1,m) -> Add(x.Invoke(token,m))
| Rem(1,m) -> Rem(x.Revoke(m, dirty))
| _ -> unexpected()
let m = d.Value
if d.Count = 1 then Add(x.Invoke(token,m))
elif d.Count = -1 then Rem(x.Revoke(m, dirty))
else unexpected()
)

for d in dirty do
Expand Down Expand Up @@ -1092,18 +1089,16 @@ module AdaptiveHashSetImplementation =
override x.Compute(token, dirty) =
let mutable deltas =
r.GetChanges token |> HashSetDelta.chooseV (fun d ->
match d with
| Add(1,m) ->
let m = d.Value
if d.Count = 1 then
match x.Invoke(token,m) with
| Some v -> ValueSome (Add v)
| None -> ValueNone

| Rem(1,m) ->
elif (d.Count = -1) then
match x.Revoke m with
| Some v -> ValueSome (Rem v)
| None -> ValueNone

| _ ->
else
unexpected()
)

Expand Down Expand Up @@ -1145,8 +1140,9 @@ module AdaptiveHashSetImplementation =
override x.Compute(token, dirty) =
let mutable deltas =
r.GetChanges token |> HashSetDelta.chooseV (fun d ->
match d with
| Add(1, m) ->

let m = d.Value
if d.Count = 1 then
let v = predicate m
let p = v.GetValue token
state.[m] <- struct(v, p)
Expand All @@ -1166,8 +1162,7 @@ module AdaptiveHashSetImplementation =
ValueSome (Add m)
else
ValueNone

| Rem(1, m) ->
elif (d.Count = -1) then
match state.TryGetValue m with
| (true, x) ->
let struct(v, p) = x
Expand Down Expand Up @@ -1199,8 +1194,8 @@ module AdaptiveHashSetImplementation =

| _ -> unexpected()

| _ ->
unexpected()
else
unexpected() // "SetOperation Count is not 1 or -1"
)

for d in dirty do
Expand Down

0 comments on commit 916cb3c

Please sign in to comment.