Specialize mapreduce + sparsevec on function arguments - #753
Open
topolarity wants to merge 1 commit into
Open
Conversation
`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 Report❌ Patch coverage is
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. 🚀 New features to boost your workflow:
|
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
approved these changes
Sep 3, 2026
fredrikekre
left a comment
Member
There was a problem hiding this comment.
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).
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.
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
--trimto fail but they don't generally impact inference.