Skip to content

Convert range views of a sparse vector from their stored entries - #1134

Merged
ViralBShah merged 1 commit into
mainfrom
vs/range-view-convert
Oct 27, 2026
Merged

ViralBShah merged 1 commit into
mainfrom
vs/range-view-convert

Conversation

@ViralBShah

@ViralBShah ViralBShah commented Oct 27, 2026 •

Copy link
Copy Markdown
Member

A range view of a sparse vector, view(x, 2:6), is converted to a SparseVector by reading it as a dense vector, so SparseVector, sparse, sparsevec, convert and sparse vcat, hcat and hvcat take O(n), drop stored zeros and give Int indices, and kron of a range view is dense.

using SparseArrays
x = sparsevec(Int32[2, 4, 5], [1.0, 0.0, 3.0], 7); v = view(x, 2:6)

Before

SparseVector(v) = sparsevec([1, 4], [1.0, 3.0], 5)
vcat(v, v) = sparsevec([1, 4, 6, 9], [1.0, 3.0, 1.0, 3.0], 10)
typeof(kron(v, x)) = Vector{Float64}

After

SparseVector(v) = sparsevec(Int32[1, 3, 4], [1.0, 0.0, 3.0], 5)
vcat(v, v) = sparsevec(Int32[1, 3, 4, 6, 8, 9], [1.0, 0.0, 3.0, 1.0, 0.0, 3.0], 10)
typeof(kron(v, x)) = SparseVector{Float64, Int32}

For w = view(sparsevec([1, n], [1.0, 2.0], n), 1:n) with n = 10^7 and y = sparsevec([1], [1.0], 2), the minimum of three runs and the @allocated of a warm call:

f(w) main branch
SparseVector(w) 0.030 s, 19.1 MiB 2.3e-7 s, 240 B
vcat(w, w) 0.056 s, 38.2 MiB 3.8e-7 s, 1.1 KiB
kron(w, y) 0.067 s, 152.6 MiB 3.8e-7 s, 976 B

The SparseVector copy constructors and sparsevec, which #1119 widened to whole views, also take SparseVectorPartialView, whose nonzeroinds and nonzeros give its stored entries relative to the range. convert and the concatenations reach them through _makesparse. kron takes range views in _SparseKronGroup and in its vector methods, so they go through the same conversion. The _sparsevector helper that sparse map uses for views now calls SparseVector instead of repeating it.

This touches dispatch.

Potential breakage: for a range view, these conversions and concatenations keep its stored zeros and the parent's index type, and kron of one is sparse rather than dense, as for x[2:6].

Products with a range view are in #1135.

🤖 Generated with Claude Code

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov

codecov Bot commented Oct 27, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.69%. Comparing base (61f75e0) to head (eebacf8).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1134   +/-   ##
=======================================
  Coverage   94.68%   94.69%           
=======================================
  Files          16       16           
  Lines       10433    10434    +1     
=======================================
+ Hits         9879     9880    +1     
  Misses        554      554           

☔ 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.

@ViralBShah
ViralBShah merged commit 3d92185 into main Oct 27, 2026
12 checks passed
@ViralBShah
ViralBShah deleted the vs/range-view-convert branch October 27, 2026 03:58
@ViralBShah ViralBShah mentioned this pull request Oct 27, 2026
30 tasks done
ViralBShah added a commit that referenced this pull request Oct 27, 2026
Backports of the merged PRs labelled `backport 1.14`, cherry-picked with
`-x` in the order they were merged on `main`. `release-1.14` starts at
b0aa455 (#1104), the commit JuliaLang/julia's `release-1.14` pins, and
every PR merged on `main` since then is labelled, so this branch's tree
is identical to `main`'s at 9bfad27.

- [x] #1105
- [x] #1011
- [x] #1107
- [x] #1108
- [x] #1110
- [x] #1111
- [x] #1114
- [x] #1112
- [x] #1115
- [x] #1117
- [x] #1118
- [x] #1109
- [x] #1113
- [x] #1122
- [x] #1123
- [x] #1120
- [x] #1124
- [x] #1125
- [x] #1116
- [x] #1121
- [x] #1126
- [x] #1128
- [x] #1130
- [x] #1131
- [x] #1119
- [x] #1133
- [x] #1127
- [x] #1135
- [x] #1134
- [x] #1137

#1137 removes the first-call compile time that #1128 added, which the
TTFX check flagged on this PR.

#1132 merged into the branch of #1131 and is part of its commit.

Not bug fixes: #1011 adds a deprecation, #1109 changes the `sprand`
value stream for a value function, #1113 changes the result shape of
`SparseMatrixCSC` of a vector view, and #1119, #1133, #1134 and #1135
add methods for views. They are included because they are labelled and
1.14 is not released yet.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Viral B. Shah <ViralBShah@users.noreply.github.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