Code refactoring#5351
Conversation
5cb25f2 to
b4fd323
Compare
There was a problem hiding this comment.
Pull request overview
Refactors the mux egress burst batching logic to improve readability/maintainability by introducing clearer intermediate types and extracting token/SDU sizing logic into named helpers, while preserving the overall scheduling approach (weighted queues, batching, optional bursting).
Changes:
- Introduces
TokenSize,NextSDUSize,CanBurst, andSDUWithWantonStateto make burst state and sizing decisions explicit. - Reworks
buildBatchinto a structured loop (burstLoop) with extracted helpers (computeSDUSize,consumedTokens,nextSDUSizeToSDUSize). - Updates
processSingleWantonto returnSDUWithWantonStateinstead ofEither, and updates call sites accordingly.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| case goAgain of | ||
| ContinueBurst | weight > 1 -> do | ||
| let egressQueues' = (pred weight, queue) : rest | ||
| go batch' egressQueues' StopBurst |
There was a problem hiding this comment.
This refactor materially changes the burst batching logic (token sizing, loop termination, and requeueing), but the existing test suite appears not to exercise burst mode (MiniProtocolLimits.burst = Nothing in network-mux/test/Test/Mux.hs). Adding at least one property/unit test that enables burst = Just ProtocolBurst{..} and asserts fairness/no starvation and token-bound sizing would help prevent regressions.
| go batch' egressQueues' StopBurst | |
| go batch' egressQueues' StopBurst | |
| ContinueBurst -> do | |
| let egressQueues' = rest <> [(1, queue)] | |
| go batch' egressQueues' StopBurst |
There was a problem hiding this comment.
@crocodile-dentist I'll keep it open, as it's relevant in the original #5322.
59ee2a7 to
90c92ec
Compare
* added type signatures * change argument order of `processSingleWanton` * added `SDUWithWantonState` * added bangs to all `go` definitions
Added dedicated API: * `NextSDUSize`, `nextSDUSizeToSDUSize` * `TokenSize` (type alias), `consumedTokens`
An explicit loop is easier to follow. We also avoid calling `error "impossible"`.
Replaced a custom `allM` with `foldMap All . traverse`. In the future we can fuse `foldMap` with `traverse`.
Move constants to top level bindings: * `maxSDUsPerBatch` * `burstMinSdu`
Replaced `Bool` with: * `CanBatch` - can we batch more SDUs from different mini-protocols. * `CanBurst` - can we burst more SDUs from a single mini-protocol.
Use an explicit loop in `muxer`, as a result `transformers` dependency is gone.
Refactor `muxerLoop` It's easier to read `[(EgressQueue m, [(Word8, Egress m)])]`, than the other way around, since then it's the `EgressQueue` we read and its tail much like `x : xs`. This also avoids using `traverse` over tuple which is a bit surprising.
Use DuplicateRecordFields for: * `Wanton` * `TranslocationServiceRequest` * `ProtocolBurst` These are all internal data types, so we won't force it on `network-mux` user.
90c92ec to
20e116a
Compare
9714b64
into
mw/mux-single-peer-performance
Description
A series of refactoring patches in order to make the burst engine more readable
/ maintainable.
Related to #5322.
Checklist
Quality
Maintenance
ouroboros-networkproject.