Repository navigation
Keep ChainedVector folds in order and reduce empty vectors like Vector - #133
Open
quinnj-bot[bot] wants to merge 2 commits into
Open
quinnj-bot[bot] wants to merge 2 commits into
quinnj-bot[bot] wants to merge 2 commits into
Conversation
Fails on main; passes with the next commit. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
foldl, foldr, mapfoldl, and mapfoldr folded each chunk and then the
chunk results, which changes the answer for non-associative operators.
Remove them so Base's in-order folds iterate the vector. Give
IndexIterator an Iterators.reverse so Iterators.reverse(x) and Base's
foldr work. With no chunks, reduce and mapreduce reduce an empty
Vector{T}, so sum returns zero as it does for Vector.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This was referenced Nov 19, 2026
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.
Why
A
ChainedVectorshould give the same answers as the same data in aVector. Folds did not.foldl,foldr,mapfoldl, andmapfoldrfolded each chunk on its own and then folded the chunk results, which changes the answer for any operator that is not associative:Two related gaps in the same reduction code also differed from
Vector.sum,prod,reduce, andmapreducethrew on aChainedVectorwith no chunks (for example afterempty!orfilter!removed every element), wheresum(Int[])is0.Iterators.reverse(x)threw aMethodErrorfor everyChainedVector, which is also why Base'sfoldrcould not be used.What changed
foldl,foldr,mapfoldl, andmapfoldrmethods. Base's in-order methods iterate theChainedVectorinstead.reverse(eachindex(x))returns linear indices, soIterators.reverse(x)works.length(eachindex(x))no longer throws whenxhas no chunks.reduceandmapreducereduce an emptyVector{T}, so they return what aVectorreturns.Scope
reduceandmapreducestill reduce chunk by chunk, which Base allows because they may reassociate.Vector.Tradeoffs
A chunk-by-chunk
mapfoldrthat passes each result asinitto the next chunk would keepfoldrnear 7 ms, for about ten more lines.foldron aChainedVectoris rare, so this PR deletes instead. The faster version is a follow-up if it matters.Blast radius
Code that called
foldl/foldrwith an associative operator gets the same values. Code that relied on an emptyChainedVectorthrowing fromsumorreducenow gets the empty value, the same asVector.Verification
mainand passes on this branch (Julia 1.12.6).srccalls the removed fold methods. Base'sfindmaxandargmaxfold overpairs, so they never used them.ChainedVector,SentinelVector, andMissingVector, compared withVector) reports this mismatch onmainand not with the fixes applied.Float64values in 8 chunks, Julia 1.12.6, Chairmarks minimum of 2 interleaved runs per side on a loaded shared machine):foldl(+, x)7.2 → 7.0 ms.foldr(+, x)6.8 → 17.8 ms, because reverse iteration looks up the chunk for each element. The oldfoldrtime came from the wrong chunk-wise fold.🤖 Generated with Claude Code