Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: Fold should accept an array of events #409

Closed
wants to merge 5 commits into from

Conversation

nordfjord
Copy link
Contributor

No description provided.

@@ -75,7 +75,7 @@ type private Category<'event, 'state, 'context, 'Format>(store: VolatileStore<'F
member _.Load(_log, _categoryName, _streamId, streamName, _maxAge, _requireLeader, _ct) =
match store.Load(streamName) with
| null -> struct (Token.ofEmpty, initial) |> Task.FromResult
| xs -> struct (Token.ofValue xs, fold initial (Seq.chooseV codec.TryDecode xs)) |> Task.FromResult
| xs -> struct (Token.ofValue xs, fold initial (Array.chooseV codec.TryDecode xs)) |> Task.FromResult
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this one is ok but its an example of the waste there can be

@@ -81,12 +81,12 @@ let verifyCorrectEventGenerationWhenAppropriate command (originState: State) =
/// (and hence potentially-conflicting) changes
let verifyIdempotency (cmd: Command) (originState: State) =
// Put the aggregate into the state where the command should not trigger an event
let establish: Events.Event list = cmd |> function
| AddItem (_, skuId, qty, waive) -> [ mkAddQty skuId qty waive]
let establish: Events.Event[] = cmd |> function
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can become an array comprehension given V6 not requiring yield

let state = fold originState initialEvents
let events = interpret command state
let state' = fold state events
let state' = fold state (Seq.toArray events)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this would be bad. But make the decide fn return arrays and see if its ok

?load, ?attempts): Async<'view> = Async.call <| fun ct ->
let inline decide' c ct = task { let! r, es = Async.StartImmediateAsTask(decide c, ct) in return struct (r, Seq.ofList es) }
let inline decide' c ct = task { let! r, es = Async.StartImmediateAsTask(decide c, ct) in return struct (r, es) }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hm tempting to require struct tuple results - then no task expr would be required

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One thing at at time?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess so. But the non-struct tuples is the only/key remaining wart vs DeciderCore
And if there is going to be an open Equinox.V3Interop, then it can map from non-struct tuples to struct ones...
But you're probably correct too

@@ -38,8 +38,8 @@ type Command =
let interpret command (state: Fold.State) =
match command with
| Update ({ preferences = preferences } as value) ->
if state = preferences then [] else
[ Events.Updated value ]
if state = preferences then [||] else
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[| if state <> preferences then Events.Updated value |]

yield Events.Favorited { date = date; skuId = skuId } ]
[| for skuId in Seq.distinct skuIds do
if state |> doesntHave skuId then
yield Events.Favorited { date = date; skuId = skuId } |]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no yield

if state |> doesntHave skuId then [] else
[ Events.Unfavorited { skuId = skuId } ]
if state |> doesntHave skuId then [||] else
[| Events.Unfavorited { skuId = skuId } |]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wrap whole thing as a array comprehension

Comment on lines +41 to +42
if state = preferences then [||] else
[| Events.Updated value |]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if state = preferences then [||] else
[| Events.Updated value |]
[| if state <> preferences then Events.Updated value |]

match command with
// a request to set quantity of `0` represents a removal request
| SyncItem (Context c, skuId, Some 0, _) ->
if itemExistsWithSkuId skuId then [ Events.ItemRemoved { context = c; skuId = skuId } ]
else []
if itemExistsWithSkuId skuId then [| Events.ItemRemoved { context = c; skuId = skuId } |]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same stuff here, either individual cases of the match, or maybe the whole thing should
wrap whole thing as a array comprehension

otherwise the noise is a bit much with that forest of hard to type symbols

| Merge items, Choice1Of2 randomSkus -> mkAppend randomSkus @ mkMerged items
| Merge (TakeHalf items), Choice2Of2 randomSkus -> mkAppend randomSkus @ mkMerged items
| Add (d,TakeHalf skus), Choice2Of2 moreSkus -> Array.append <| mkAppendDated d skus <| mkAppendDated (let n = DateTimeOffset.Now in n.AddDays -1.) moreSkus
| Merge items, Choice1Of2 randomSkus -> Array.append <| mkAppend randomSkus <| mkMerged items
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

array comprehension with yield!
And you know better than to do do <| !

@bartelink
Copy link
Collaborator

Superseded by #411 - thanks for paving the way for this excellent simplification to the (new) user experience

@bartelink bartelink closed this Jul 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants