-
Notifications
You must be signed in to change notification settings - Fork 5
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
Avoid O(n^2) time broadcast' by replacing Writer #112
Conversation
The code looks ok to me but how did you test it? |
c204311
to
aa3320b
Compare
This prevents us from making sure the order doesn't change when `broadcast'` is modified.
I thought the SubscriptionTree tests already tested the icepeak/server/tests/Icepeak/Server/SubscriptionTreeSpec.hs Lines 109 to 114 in c204311
But, there's a sneaky sort right here:
I added another commit before this one that removes the sort so the order is also tested (if you change the orders in |
With the strict state monad we can prepend the items to a list and then reverse the list afterwards to maintain the original order. This brings the time complexity down from `O(n^2)` (because of the mappend/list concatenation used by Writer) to `O(n)`. Resolves #102.
bd83c69
to
c525dcc
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! 🚀
@@ -89,7 +88,7 @@ spec = do | |||
value_foo_bar = AE.Null | |||
value_baz = AE.object [] | |||
|
|||
broadcast'' path = sortOn fst $ broadcast' path value root | |||
broadcast'' path = broadcast' path value root |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice catch!
@OpsBotPrime merge |
Pull request approved for merge by @robbert-vdh, rebasing now. |
Approved-by: robbert-vdh Auto-deploy: false
Rebased as a24b517, waiting for CI … |
CI job 🟡 started. |
As mentioned by @fatho in #102, the Writer monad can be replaced with the strict state monad to avoid the use for mappend/list concatenation for every element added to the list.. This brings the time complexity down from
O(n^2)
toO(n)
. I added the reverse so the order stays the same before and after this change. If that's not needed the reverse can also be removed. Then order of the lists in the tests would then need to be changed.I thought about removing the monad entirely since it's only used for keeping track of the list and the return value is never used, but an alternative version that still runs on
O(n)
time would require passing the list around directly which gets very messy.Resolves #102.