Commit 2f9b0b3
perf: Vectorize get_chunk_slice for faster sharded writes (#3713)
* perf: Skip bounds check for initial elements in 2^n hypercube
* lint:Use a list comprehension rather than a for loop
* pref:Add decode_morton_vectorized
* perf:Replace math.log2() with bit_length()
* perf:Use magic numbers for 2D and 3D
* perf:Add 4D Morton magic numbers
* perf:Add Morton magic numbers for 5D
* perf:Remove singleton dimensions to reduce ndims
* Add changes
* fix:Address type annotation and linting issues
* perf:Remove magic number functions
* test:Add power of 2 sharding indexing tests
* test: Add Morton order benchmarks with cache clearing
Add benchmarks that clear the _morton_order LRU cache before each
iteration to measure the full Morton computation cost:
- test_sharded_morton_indexing: 512-4096 chunks per shard
- test_sharded_morton_indexing_large: 32768 chunks per shard
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* fix:Bound LRU cache of _morton_order to 16
* test:Add a single chunk test for a large shard
* test:Add indexing benchmarks for writing
* tests:Add single chunk write test for sharding
* perf: Vectorize get_chunk_slice for faster sharded writes
Add vectorized methods to _ShardIndex and _ShardReader for batch
chunk slice lookups, reducing per-chunk function call overhead
when writing to shards.
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* refactor: Return ndarray from _morton_order, simplify to_dict_vectorized
_morton_order now returns a read-only npt.NDArray[np.intp] (annotated as
Iterable[Sequence[int]]) instead of a tuple of tuples, eliminating the
intermediate list-of-tuples allocation. morton_order_iter converts rows to
tuples on the fly. to_dict_vectorized no longer requires a redundant
chunk_coords_tuples argument; tuple conversion happens inline during dict
population. get_chunk_slices_vectorized accepts any integer array dtype
(npt.NDArray[np.integer[Any]]) and casts to uint64 internally.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
* perf: Cache tuple keys separately from ndarray in _morton_order_keys
Add _morton_order_keys() as a second lru_cache that converts the ndarray
returned by _morton_order into a tuple of tuples. This restores cached
access to hashable chunk coordinate keys without reverting to the old
dual-argument interface. morton_order_iter now uses _morton_order_keys,
and to_dict_vectorized derives its keys from _morton_order_keys internally
using the shard index shape, keeping the call site single-argument.
Result: test_sharded_morton_write_single_chunk[(32,32,32)] improves from
~33ms to ~7ms (~5x speedup over prior to this PR's changes).
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
* tests: Clear _morton_order_keys cache alongside _morton_order in benchmarks
All benchmark functions that call _morton_order.cache_clear() now also
call _morton_order_keys.cache_clear() to ensure both caches are reset
before each benchmark iteration.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
* refactor: Use npt.NDArray[np.intp] as return type for _morton_order
More precise than Iterable[Sequence[int]] and accurately reflects the
actual return value. Remove the now-unused Iterable import.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Co-authored-by: Davis Bennett <davis.v.bennett@gmail.com>1 parent f8b3d38 commit 2f9b0b3
File tree
4 files changed
+114
-35
lines changed- changes
- src/zarr
- codecs
- core
- tests/benchmarks
4 files changed
+114
-35
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
46 | 46 | | |
47 | 47 | | |
48 | 48 | | |
| 49 | + | |
| 50 | + | |
49 | 51 | | |
50 | 52 | | |
51 | 53 | | |
| |||
144 | 146 | | |
145 | 147 | | |
146 | 148 | | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
147 | 188 | | |
148 | 189 | | |
149 | 190 | | |
| |||
225 | 266 | | |
226 | 267 | | |
227 | 268 | | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
228 | 297 | | |
229 | 298 | | |
230 | 299 | | |
| |||
511 | 580 | | |
512 | 581 | | |
513 | 582 | | |
514 | | - | |
| 583 | + | |
| 584 | + | |
515 | 585 | | |
516 | 586 | | |
517 | 587 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1504 | 1504 | | |
1505 | 1505 | | |
1506 | 1506 | | |
1507 | | - | |
| 1507 | + | |
1508 | 1508 | | |
| 1509 | + | |
1509 | 1510 | | |
1510 | | - | |
| 1511 | + | |
| 1512 | + | |
| 1513 | + | |
1511 | 1514 | | |
1512 | 1515 | | |
1513 | 1516 | | |
1514 | 1517 | | |
1515 | 1518 | | |
1516 | 1519 | | |
1517 | 1520 | | |
1518 | | - | |
1519 | | - | |
1520 | | - | |
1521 | | - | |
1522 | | - | |
1523 | | - | |
1524 | | - | |
1525 | | - | |
1526 | | - | |
1527 | | - | |
1528 | | - | |
1529 | | - | |
1530 | | - | |
1531 | | - | |
1532 | | - | |
| 1521 | + | |
| 1522 | + | |
| 1523 | + | |
| 1524 | + | |
| 1525 | + | |
| 1526 | + | |
| 1527 | + | |
| 1528 | + | |
1533 | 1529 | | |
1534 | 1530 | | |
1535 | | - | |
1536 | | - | |
1537 | | - | |
| 1531 | + | |
| 1532 | + | |
| 1533 | + | |
1538 | 1534 | | |
1539 | 1535 | | |
1540 | 1536 | | |
| |||
1547 | 1543 | | |
1548 | 1544 | | |
1549 | 1545 | | |
1550 | | - | |
1551 | 1546 | | |
1552 | 1547 | | |
1553 | | - | |
1554 | | - | |
| 1548 | + | |
1555 | 1549 | | |
1556 | | - | |
| 1550 | + | |
1557 | 1551 | | |
1558 | | - | |
| 1552 | + | |
| 1553 | + | |
1559 | 1554 | | |
1560 | | - | |
| 1555 | + | |
1561 | 1556 | | |
1562 | 1557 | | |
1563 | | - | |
| 1558 | + | |
1564 | 1559 | | |
1565 | 1560 | | |
1566 | | - | |
| 1561 | + | |
| 1562 | + | |
| 1563 | + | |
| 1564 | + | |
| 1565 | + | |
| 1566 | + | |
| 1567 | + | |
| 1568 | + | |
| 1569 | + | |
1567 | 1570 | | |
1568 | 1571 | | |
1569 | 1572 | | |
1570 | | - | |
| 1573 | + | |
1571 | 1574 | | |
1572 | 1575 | | |
1573 | 1576 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
74 | 74 | | |
75 | 75 | | |
76 | 76 | | |
77 | | - | |
| 77 | + | |
78 | 78 | | |
79 | 79 | | |
80 | 80 | | |
| |||
98 | 98 | | |
99 | 99 | | |
100 | 100 | | |
| 101 | + | |
101 | 102 | | |
102 | 103 | | |
103 | 104 | | |
| |||
122 | 123 | | |
123 | 124 | | |
124 | 125 | | |
125 | | - | |
| 126 | + | |
126 | 127 | | |
127 | 128 | | |
128 | 129 | | |
| |||
145 | 146 | | |
146 | 147 | | |
147 | 148 | | |
| 149 | + | |
148 | 150 | | |
149 | 151 | | |
150 | 152 | | |
| |||
164 | 166 | | |
165 | 167 | | |
166 | 168 | | |
167 | | - | |
| 169 | + | |
168 | 170 | | |
169 | 171 | | |
170 | 172 | | |
| |||
187 | 189 | | |
188 | 190 | | |
189 | 191 | | |
| 192 | + | |
190 | 193 | | |
191 | 194 | | |
192 | 195 | | |
| |||
211 | 214 | | |
212 | 215 | | |
213 | 216 | | |
214 | | - | |
| 217 | + | |
215 | 218 | | |
216 | 219 | | |
217 | 220 | | |
| 221 | + | |
218 | 222 | | |
219 | 223 | | |
220 | 224 | | |
| |||
239 | 243 | | |
240 | 244 | | |
241 | 245 | | |
242 | | - | |
| 246 | + | |
243 | 247 | | |
244 | 248 | | |
245 | 249 | | |
| |||
262 | 266 | | |
263 | 267 | | |
264 | 268 | | |
| 269 | + | |
265 | 270 | | |
266 | 271 | | |
267 | 272 | | |
0 commit comments