Fuse mapWithIndex into a single lazy view, avoiding tuple allocation - #86
Open
cheeseng wants to merge 1 commit into
Open
Conversation
mapWithIndex was implemented as
new View.Map(new View.ZipWithIndex(this), (pair: (A, Int)) => f(pair._1, pair._2)),
which builds a real (A, Int) tuple per element via ZipWithIndex only
to have View.Map immediately unpack and discard it one step later. It
also allocates two View objects and, once traversed, two chained
Iterator objects instead of one.
Replace it with a single anonymous AbstractView[B] whose iterator
threads the index as a plain mutable field instead of pairing it with
each element.
`self` is captured explicitly since an unqualified `this` inside the
anonymous AbstractView body would refer to the view itself. The
inner iterator's return type is annotated Iterator[B]^{self, f} so
the capture checker accepts that it legitimately holds onto self and
f, matching how the outer method is already annotated CC[B]^{this, f}.
Preserves the original's laziness: no element of `this` is evaluated
until the resulting view is actually traversed.
No behavior changes; result values, traversal order, and laziness are
identical to the previous implementation.
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.
mapWithIndex composed two existing view combinators:
new View.Map(new View.ZipWithIndex(this), (pair: (A, Int)) => f(pair._1, pair._2))
ZipWithIndex's iterator constructs a real (A, Int) tuple per element;
View.Map's function then immediately destructures that tuple and
discards it. This also means two View instances and, once traversed,
two chained Iterator instances wrap
this, where one fused instancewould do.
This PR replaces that with a single anonymous AbstractView[B] that
threads the index as a mutable field on its own iterator rather than
pairing it with each element.
selfis captured explicitly, since an unqualifiedthisinside theanonymous AbstractView body refers to the view itself rather than the
enclosing collection. The inner iterator method is annotated
Iterator[B]^{self, f} so the capture checker accepts that it holds
onto self and f, mirroring the outer method's own CC[B]^{this, f}
annotation.
Laziness is preserved: nothing in
thisis evaluated until theresulting view is traversed, matching existing tests asserting zero
element evaluations immediately after calling mapWithIndex.
No behavior changes: same result values, same traversal order, same
laziness guarantees as before.