[Internals] Optimise ZStream - Add readInput/readInputUnit and succeedChannelFn to ZChannel#10577
Open
guizmaii wants to merge 47 commits into
Open
[Internals] Optimise ZStream - Add readInput/readInputUnit and succeedChannelFn to ZChannel#10577guizmaii wants to merge 47 commits into
readInput/readInputUnit and succeedChannelFn to ZChannel#10577guizmaii wants to merge 47 commits into
Conversation
Add `refailCauseChannelFn` and `succeedChannelFn` shared constants in the `ZChannel` companion object, following the existing `unitChannelFn` pattern. This eliminates per-call-site lambda allocations for identity error and done handlers across `ZStream`, `ZPipeline`, `ZSink`, and related files. - `refailCauseChannelFn[E]` replaces `err => ZChannel.refailCause(err)` and bare `ZChannel.refailCause` eta-expansions - `succeedChannelFn[Z]` replaces `done => ZChannel.succeedNow(done)` and `ZChannel.succeed(_)` identity pass-throughs
Introduce `readInput` as a specialized `readWithCause` that only takes the `in` handler, defaulting error to `refailCause` and done to `unit`. A pre-cached `ReadInputFoldK` avoids allocating a new `Fold.K` on every call site. Replace 18 matching `readWithCause(..., refailCauseChannelFn, unitChannelFn)` call sites across `ZStream`, `ZPipeline`, and `ZSink` with the new `readInput` method.
…h` semantics - Rename `readInput` → `readInputCause` and change its done handler from `unitChannelFn` (discard) to `succeedChannelFn` (pass-through), making the method more general. - Add new `readInput` that uses `readWith` semantics (fail on errors, pass-through done value). Since `readWith(in, fail(_), done)` is semantically equivalent to `readWithCause(in, refailCause, done)`, it delegates to `readInputCause` — no separate `Fold.K` needed. - Replace all applicable `readWithCause(..., refailCauseChannelFn, succeedChannelFn)` and `readWith(..., fail(_), succeedChannelFn)` call sites across `ZChannel`, `ZStream`, `ZPipeline`, and `ZSink`. - Sites with `ZNothing` as `InErr` are left as `readWithCause` due to Scala 2 type inference limitations.
1671975 to
637eb33
Compare
ZChannel done-handler lambdas into shared constantsZChannel handler lambdas and add readInput/readInputCause
`readInputCause` uses `succeedChannelFn` which allocates a `SucceedNow(done)` when the upstream completes. For sites that discard the done value (all `ZStream`/`ZPipeline` usages), `readInputCauseUnit` uses the pre-cached `unitChannelFn` instead.
The shared constant provided no meaningful optimization: the JVM already caches eta-expanded method references per call site, and the real allocation (`Fail(() => cause)`) happens on invocation regardless. Also adds `readInputCauseUnit` which uses `unitChannelFn` (pre-cached `ZChannel.unit` singleton) instead of `succeedChannelFn` (allocates `SucceedNow(done)`). All `ZStream`/`ZPipeline` sites now use `readInputCauseUnit` since they discard the done value.
ZChannel handler lambdas and add readInput/readInputCauseZChannel\ handler lambdas and add \readInputCause\/\readInputCauseUnit\
44ee434 to
a9b86c8
Compare
`readInputCauseUnit` now correctly returns `ZChannel[..., Unit]` instead of `Any`, matching the `unitChannelFn` done handler. Also inlines `refailCause` directly in `Fold.K` constructors.
ZChannel\ handler lambdas and add \readInputCause\/\readInputCauseUnit\readInputCause/readInputCauseUnit and succeedChannelFn to ZChannel
5eb5d1f to
9cd8f7b
Compare
readInputCause/readInputCauseUnit and succeedChannelFn to ZChannelreadInputCause/readInputCauseUnit and succeedChannelFn to ZChannel
readInputCause/readInputCauseUnit and succeedChannelFn to ZChannelreadInputCause/readInputCauseUnit and succeedChannelFn to ZChannel
readInputCause/readInputCauseUnit and succeedChannelFn to ZChannelreadInputCause/readInputCauseUnit and succeedChannelFn to ZChannel
readInputCause/readInputCauseUnit and succeedChannelFn to ZChannelreadInput/readInputUnit and succeedChannelFn to ZChannel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Reduces boilerplate and avoids
Fold.Kallocation for the most commonreadWithCausepattern acrossZStream,ZPipeline,ZSink, andZChannel.1.
succeedChannelFnAdds
succeedChannelFn[Z]shared constant toZChannel, following the existingunitChannelFnpattern. Returns a pre-cached val viaasInstanceOf— one object instead of a per-call-site singleton.2.
readInput/readInputUnitTwo
private[zio]convenience methods that replace the most commonreadWithCauseboilerplate — reading input, refailing on error, with a standard done handler. Both use a pre-cachedFold.Kinstance, avoiding the per-callnew Fold.K(...)allocation.readInput— passes through the done value (succeedChannelFn). Used byZChannelinternal combinators (collect,contramap,drain,mapOut, etc.).readInputUnit— discards the done value (unitChannelFn, returnsUnit). Zero allocation on done sinceZChannel.unitis pre-cached. Used byZStream/ZPipelinesites.Test plan
streamsJVM/compilepassesstreamsTestsJVMtests pass (0 failures)