Repository navigation
Conversation
Skip the redundant minor-axis slice in BackedSparseMatrix._get_*Xslice when the minor slice spans the full minor axis, and use asarray instead of array in CompressedVectors.from_buffers so freshly read sparse buffers are returned directly instead of being copied.
3 of 4 tasks
Intron7
force-pushed
the
perf/backed-sparse-zero-copy
branch
from
December 6, 2026 14:04
2d1cfcb to
3c09445
Compare
|
📖 1 file modified Preview · build overview · anndata · built Dec. 6, 2026, 2:05 p.m. UTC |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2785 +/- ##
=======================================
Coverage 89.29% 89.30%
=======================================
Files 51 51
Lines 8821 8826 +5
=======================================
+ Hits 7877 7882 +5
Misses 944 944
|
This branch has not been deployed
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.
Supersedes #2676. The lazy Dask metadata fixes from #2676 already landed in #2770; this PR focuses strictly on avoiding the 2× memory overhead from redundant minor-axis slicing copies when reading backed sparse matrices while keeping read runtime neutral or faster.
Reading complete rows of a backed CSR matrix (or complete columns of a backed CSC matrix) constructed a sparse matrix from freshly read buffers and then immediately sliced its entire minor axis, allocating a duplicate copy of
data,indices, andindptr.CompressedVectors.from_buffersalso copiedindptrunconditionally via.array(...).Summary of changes (
src/anndata/_core/sparse_dataset.py, +15/−14 lines)BackedSparseMatrix._minor_subset):_get_intXslice,_get_sliceXslice, and_get_arrayXslicethrough_minor_subset. Whenminor_index.indices(size) == (0, size, 1), return the freshly constructed sparse matrix directly instead of slicing it._fix_slice_boundsonminor_indexin_get_sliceXsliceso negative-step slices such as[:, ::-1]preserve index0and match in-memory slicing.indptrinCompressedVectors.from_buffers:get_np_module().asarray(indptr)instead ofget_np_module().array(indptr).Verification & Benchmarks
Tested on CPU (
hatch-test.stable --strict-warnings) and GPU (RTX PRO 6000,zarr 3.3.0,cupy 14.2.0), withprekpassing across all modified files.A/B vs
main(9364769a) on a 40,000 × 10,000 sparse matrix (20M nonzeros,float32, 152.74 MiB per matrix; chunked timing = 4 × 10,000 rows/cols, median of 7 warm rounds; exact SHA-256 parity ondata,indices,indptr):main(9364769a)tracemalloc, CSR / CSC)tracemalloc, CSR / CSC)MemoryStore) chunked read (CSR / CSC)Tests and authorship
tests/test_backed_sparse.py::test_minor_subset_zero_copyverify zero-copy return identity on full-width minor slices and exact parity on partial/reversed minor slices across CSR/CSC and CPU HDF5, CPU Zarr, and GPU Zarr.