Skip to content

Keep ChainedVector folds in order and reduce empty vectors like Vector - #133

Open
quinnj-bot[bot] wants to merge 2 commits into
mainfrom
claude/chainedvector-fold-order
Open

quinnj-bot[bot] wants to merge 2 commits into
mainfrom
claude/chainedvector-fold-order

Conversation

@quinnj-bot

@quinnj-bot quinnj-bot Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Why

A ChainedVector should give the same answers as the same data in a Vector. Folds did not. foldl, foldr, mapfoldl, and mapfoldr folded each chunk on its own and then folded the chunk results, which changes the answer for any operator that is not associative:

julia> foldl(-, ChainedVector([[1, 2], [3, 4]]))
0      # Vector gives -8

Two related gaps in the same reduction code also differed from Vector. sum, prod, reduce, and mapreduce threw on a ChainedVector with no chunks (for example after empty! or filter! removed every element), where sum(Int[]) is 0. Iterators.reverse(x) threw a MethodError for every ChainedVector, which is also why Base's foldr could not be used.

What changed

  • Remove the chunk-wise foldl, foldr, mapfoldl, and mapfoldr methods. Base's in-order methods iterate the ChainedVector instead.
  • reverse(eachindex(x)) returns linear indices, so Iterators.reverse(x) works. length(eachindex(x)) no longer throws when x has no chunks.
  • With no chunks, reduce and mapreduce reduce an empty Vector{T}, so they return what a Vector returns.

Scope

  • reduce and mapreduce still reduce chunk by chunk, which Base allows because they may reassociate.
  • The existing tests that expected the empty folds and reductions to throw now compare against Vector.

Tradeoffs

A chunk-by-chunk mapfoldr that passes each result as init to the next chunk would keep foldr near 7 ms, for about ten more lines. foldr on a ChainedVector is rare, so this PR deletes instead. The faster version is a follow-up if it matters.

Blast radius

Code that called foldl/foldr with an associative operator gets the same values. Code that relied on an empty ChainedVector throwing from sum or reduce now gets the empty value, the same as Vector.

Verification

  • The new test fails on main and passes on this branch (Julia 1.12.6).
  • The full test suite passes locally on Julia 1.12.6 and 1.10.11, on this branch alone and with all twelve fix PRs (Keep ChainedVector folds in order and reduce empty vectors like Vector #133 to Reverse SentinelVectors with missing values and an undef sentinel #144) merged together.
  • Nothing in src calls the removed fold methods. Base's findmax and argmax fold over pairs, so they never used them.
  • The seeded differential harness from the sweep (179 operations on ChainedVector, SentinelVector, and MissingVector, compared with Vector) reports this mismatch on main and not with the fixes applied.
  • Timing (10M Float64 values 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 old foldr time came from the wrong chunk-wise fold.

🤖 Generated with Claude Code

quinnj and others added 2 commits November 18, 2026 23:04
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant