Skip to content

perf: speed up backed sparse integer indexing#2506

Open
tippered1-debug wants to merge 2 commits into
scverse:mainfrom
tippered1-debug:perf-backed-sparse-integer-indexing
Open

perf: speed up backed sparse integer indexing#2506
tippered1-debug wants to merge 2 commits into
scverse:mainfrom
tippered1-debug:perf-backed-sparse-integer-indexing

Conversation

@tippered1-debug

Copy link
Copy Markdown

Summary

Integer indexing of backed sparse datasets currently reads every selected row or column separately, even when most of the indices form contiguous runs.

This PR groups those runs and reads them as slices. Shuffled indices and duplicates are restored to their original order after reading. Highly fragmented indexers continue to use the existing path, since slicing does not help in that case.

Related to #1224.

Benchmark

Benchmarked on a 10000 x 10000 sparse matrix with density 0.01. Each indexer contains 2048 elements. Results are medians of three runs.

HDF5 CSR

  • Single run: 7.10 ms0.73 ms9.7x faster
  • Multiple runs: 6.81 ms0.74 ms9.2x faster
  • Clustered, shuffled: 7.04 ms1.03 ms6.8x faster
  • Clustered with duplicates: 7.02 ms0.83 ms8.5x faster

Zarr CSR

  • Single run: 16.60 ms3.70 ms4.5x faster
  • Multiple runs: 16.27 ms13.92 ms1.2x faster
  • Clustered, shuffled: 18.91 ms14.30 ms1.3x faster
  • Clustered with duplicates: 19.19 ms8.53 ms2.2x faster

Fragmented indexers continue to use the existing path and stayed close to the previous timings.

  • Closes #
  • Tests added
  • Release note not necessary because:

@codecov

codecov Bot commented Jun 17, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 85.71%. Comparing base (6d70d77) to head (653f5b6).
⚠️ Report is 8 commits behind head on main.
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2506      +/-   ##
==========================================
- Coverage   87.61%   85.71%   -1.91%     
==========================================
  Files          49       49              
  Lines        7693     7719      +26     
==========================================
- Hits         6740     6616     -124     
- Misses        953     1103     +150     
Files with missing lines Coverage Δ
src/anndata/_core/sparse_dataset.py 92.15% <100.00%> (+0.01%) ⬆️

... and 7 files with indirect coverage changes

@ilan-gold ilan-gold left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can this reuse subset_by_major_axis_mask? It looks eerily familiar. Or could that method be removed in favor of this code path? I'm not sure what the performance considerations here are but I think they are probably trivial i.e., operations on a small 1d array.

Thanks for the contribution!

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.

2 participants