Skip to content

Specialize mapreduce + sparsevec on function arguments - #753

Open
topolarity wants to merge 1 commit into
JuliaSparse:mainfrom
topolarity:ct/trim-sparse-mapreduce
Open

Specialize mapreduce + sparsevec on function arguments#753
topolarity wants to merge 1 commit into
JuliaSparse:mainfrom
topolarity:ct/trim-sparse-mapreduce

Conversation

@topolarity

Copy link
Copy Markdown
Contributor

Another small trim fix-up, follow-up to #751 . Expect ~1 or 2 more PRs in this series.

I do not think it is easy to add a test here since the problem this fixes has to do with the "compileable" specializations chosen by the compiler - those affect codegen and cause --trim to fail but they don't generally impact inference.

`Base._mapreduce`, `_mapreducezeros` and `Base._mapreducedim!` for sparse
matrices, their sparse-vector counterparts, and the `sparsevec` methods with
a `combine` argument took their function arguments without a type
parameter. Where such an argument is only passed through, Julia compiles the
method for `Function` rather than for the concrete function, and every call
below that point becomes dynamic. Under juliac's `--trim` this made
`sum(A)` for a sparse matrix, and the other reductions with it, unbuildable.

Give each of these function arguments a type parameter, as JuliaSparse#751 did for the
`combine` argument of `sparse`, so the methods specialize and the reduction
call graph resolves statically.

This commit was written with the assistance of generative AI (Claude).

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@codecov

codecov Bot commented Sep 2, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 91.66667% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 83.84%. Comparing base (88fa040) to head (a5c5feb).

Files with missing lines Patch % Lines
src/sparsevector.jl 83.33% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #753      +/-   ##
==========================================
- Coverage   83.85%   83.84%   -0.01%     
==========================================
  Files          13       13              
  Lines        9387     9386       -1     
==========================================
- Hits         7871     7870       -1     
  Misses       1516     1516              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@araujoms

araujoms commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

You could write a comment explaining why the extra type parameters are needed - they look otherwise useless and somebody else might remove them.

@fredrikekre fredrikekre left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could write a comment explaining why the extra type parameters are needed

I don't think that is needed. This is a well known pattern and is used all over the place (https://docs.julialang.org/en/v1/manual/performance-tips/#Be-aware-of-when-Julia-avoids-specializing).

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.

3 participants