Repository navigation
Commit 68e0e58
authored
Optimize ChunkedArray take routing (#9224)
## Summary
- replace global comparison sorting in `ChunkedArray::take` with
per-chunk index buckets
- route monotonic indices with a linear chunk cursor and arbitrary
indices with binary search over chunk offsets
- preserve original ordering, nulls, duplicates, nested dtypes, and
bounds behavior
- skip the final reorder take when non-nullable indices are already
monotonic
- add a deterministic 32-case Divan benchmark covering ordering, chunk
counts, duplicate density, nullability, nested values, and routing
patterns
## Why
The previous implementation sorted every valid `(global_index,
original_position)` pair, then filtered each touched chunk before taking
again to restore order. That made chunk routing approximately `O(M log M
+ C)`. Bucketing removes the global comparison sort: arbitrary routing
is approximately `O(M log C + C)`, while monotonic routing is `O(M +
C)`.
## Benchmarks
Command, run sequentially against the original implementation and this
branch:
```bash
CARGO_INCREMENTAL=0 cargo bench -p vortex-array --bench take_chunked -- --sample-count 100
```
Source length was 1,048,576 rows. Nested cases used `RecursiveCanonical`
so child values were fully materialized. Values below are medians from
100 Divan samples. All 32 candidate medians improved.
### Core ordering
| Pattern | Chunks | Indices | Before | After | Speedup |
|---|---:|---:|---:|---:|---:|
| Shuffled | 16 | 10,000 | 205.1 µs | 66.49 µs | 3.08× |
| Shuffled | 16 | 100,000 | 2.460 ms | 576.5 µs | 4.27× |
| Shuffled | 1,024 | 10,000 | 838.5 µs | 608.4 µs | 1.38× |
| Shuffled | 1,024 | 100,000 | 3.240 ms | 1.631 ms | 1.99× |
| Sorted | 16 | 10,000 | 92.74 µs | 44.02 µs | 2.11× |
| Sorted | 16 | 100,000 | 850.1 µs | 315.1 µs | 2.70× |
| Sorted | 1,024 | 10,000 | 698.0 µs | 501.9 µs | 1.39× |
| Sorted | 1,024 | 100,000 | 1.622 ms | 907.7 µs | 1.79× |
### Small request count / high chunk count
| Chunks | Indices | Before | After | Speedup |
|---:|---:|---:|---:|---:|
| 1,024 | 1 | 2.916 µs | 2.124 µs | 1.37× |
| 1,024 | 16 | 18.74 µs | 8.666 µs | 2.16× |
| 1,024 | 256 | 139.5 µs | 97.16 µs | 1.44× |
| 1,024 | 1,000 | 349.9 µs | 269.4 µs | 1.30× |
| 16,384 | 1 | 16.43 µs | 15.66 µs | 1.05× |
| 16,384 | 16 | 131.8 µs | 24.08 µs | 5.47× |
| 16,384 | 256 | 445.4 µs | 122.8 µs | 3.63× |
| 16,384 | 1,000 | 929.0 µs | 419.4 µs | 2.22× |
### Duplicate density
| Pattern | Chunks | Indices | Before | After | Speedup |
|---|---:|---:|---:|---:|---:|
| One repeated index | 16 | 100,000 | 364.9 µs | 273.7 µs | 1.33× |
| 90% duplicates | 16 | 100,000 | 1.859 ms | 529.2 µs | 3.51× |
| 99% duplicates | 16 | 100,000 | 1.825 ms | 520.3 µs | 3.51× |
### Value shape
| Pattern | Value | Chunks | Indices | Before | After | Speedup |
|---|---|---:|---:|---:|---:|---:|
| 99% duplicates | Primitive | 16 | 10,000 | 125.0 µs | 62.81 µs | 1.99×
|
| 99% duplicates | Struct, 8 fields | 16 | 10,000 | 1.134 ms | 701.2 µs
| 1.62× |
| 99% duplicates | Fixed-size list, width 8 | 16 | 10,000 | 1.468 ms |
648.5 µs | 2.26× |
| Shuffled | Primitive | 16 | 10,000 | 163.4 µs | 65.91 µs | 2.48× |
| Shuffled | Struct, 8 fields | 16 | 10,000 | 1.733 ms | 779.5 µs |
2.22× |
| Shuffled | Fixed-size list, width 8 | 16 | 10,000 | 2.150 ms | 664.6
µs | 3.24× |
### Nullable sorted indices
| Validity | Chunks | Indices | Before | After | Speedup |
|---|---:|---:|---:|---:|---:|
| Nullable, all valid | 16 | 100,000 | 776.7 µs | 370.8 µs | 2.09× |
| Nullable, all valid | 1,024 | 100,000 | 1.513 ms | 957.1 µs | 1.58× |
| Nullable, 10% null | 16 | 100,000 | 777.8 µs | 395.2 µs | 1.97× |
| Nullable, 10% null | 1,024 | 100,000 | 1.502 ms | 1.016 ms | 1.48× |
### Chunk routing patterns
| Pattern | Chunks | Indices | Before | After | Speedup |
|---|---:|---:|---:|---:|---:|
| Same-chunk random | 1,024 | 100,000 | 1.830 ms | 614.1 µs | 2.98× |
| Alternating first/last chunk | 1,024 | 100,000 | 2.027 ms | 757.7 µs |
2.68× |
| Grouped by chunk | 1,024 | 100,000 | 2.814 ms | 1.490 ms | 1.89× |
## Validation
- `CARGO_INCREMENTAL=0 cargo test -p vortex-array --lib
arrays::chunked::compute::take::test` — 10 passed
- `CARGO_INCREMENTAL=0 cargo clippy --all-targets --all-features`
- `cargo +nightly fmt --all -- --check`
- `git diff --check`
---------
Signed-off-by: "Nicholas Gates" <[email protected]>
Signed-off-by: Nicholas Gates <[email protected]>1 parent 77e7bc2 commit 68e0e58
4 files changed
Lines changed: 579 additions & 62 deletions
File tree
- vortex-array
- benches
- src
- arrays/chunked/compute
- test_harness/trace
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
227 | 227 | | |
228 | 228 | | |
229 | 229 | | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
230 | 234 | | |
231 | 235 | | |
232 | 236 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 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 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 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 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
0 commit comments