Skip to content

perf: avoid redundant buffer copies when reading backed sparse matrices - #2785

Open
Intron7 wants to merge 1 commit into
mainfrom
perf/backed-sparse-zero-copy
Open

Intron7 wants to merge 1 commit into
mainfrom
perf/backed-sparse-zero-copy

Conversation

@Intron7

@Intron7 Intron7 commented Dec 6, 2026 •

Copy link
Copy Markdown
Member

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, and indptr. CompressedVectors.from_buffers also copied indptr unconditionally via .array(...).

Summary of changes (src/anndata/_core/sparse_dataset.py, +15/−14 lines)

  1. Zero-copy full-width minor-axis reads (BackedSparseMatrix._minor_subset):
    • Route _get_intXslice, _get_sliceXslice, and _get_arrayXslice through _minor_subset. When minor_index.indices(size) == (0, size, 1), return the freshly constructed sparse matrix directly instead of slicing it.
    • Stop calling _fix_slice_bounds on minor_index in _get_sliceXslice so negative-step slices such as [:, ::-1] preserve index 0 and match in-memory slicing.
  2. Avoid copying indptr in CompressedVectors.from_buffers:
    • Use get_np_module().asarray(indptr) instead of get_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), with prek passing 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 on data, indices, indptr):

Workload main (9364769a) This PR
HDF5 CPU full-width peak RAM (tracemalloc, CSR / CSC) 305.49 MiB (2.00×) 152.74 MiB (1.00×, −50%)
HDF5 CPU chunked read (CSR / CSC) 40.44 / 38.14 ms 26.16 / 28.14 ms (1.55× / 1.36×)
Zarr CPU full-width peak RAM (tracemalloc, CSR / CSC) 305.63 MiB 276.00 MiB
Zarr CPU chunked read (CSR / CSC) 110.95 / 90.81 ms 86.40 / 68.22 ms (1.28× / 1.33×)
Zarr GPU full-width peak CuPy pool VRAM (CSR / CSC) 379.70 MiB 303.41 MiB (−76.3 MiB / −20%)
Zarr GPU (MemoryStore) chunked read (CSR / CSC) 85.18 / 81.33 ms 80.22 / 88.00 ms

Tests and authorship

  • Regression tests in tests/test_backed_sparse.py::test_minor_subset_zero_copy verify 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.
  • Release note included.
  • Coding agents used: Gemini 4 Neon via Cursor (plus Claude Opus 5.5 and GPT-6 on earlier revisions).
  • If one was used, I, a human, have reviewed, understood and tested every changed line.

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.
@read-the-docs-community

Copy link
Copy Markdown

📖 1 file modified


Preview · build overview · anndata · built Dec. 6, 2026, 2:05 p.m. UTC

@codecov

codecov Bot commented Dec 6, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.30%. Comparing base (9364769) to head (3c09445).
✅ All tests successful. No failed tests found.

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           
Files with missing lines Coverage Δ
src/anndata/_core/sparse_dataset.py 90.48% <100.00%> (+0.13%) ⬆️

@Intron7
Intron7 marked this pull request as ready for review December 6, 2026 15:29
@Intron7 Intron7 added this to the 0.14.1 milestone Dec 6, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant