Commit fad0159
committed
feat(hpc): LazyLock frozen SIMD dispatch table — detect once, keep CPU choice forever
simd_dispatch.rs (300+ lines, 7 tests):
SimdDispatch: struct of function pointers, frozen at first access via LazyLock.
Each field is a fn pointer to the best available implementation for this CPU.
After initialization: one pointer deref + one indirect call. Zero branching.
SimdTier enum: Avx512 / Avx2 / Sse2 / Scalar / WasmSimd128 (future).
Selected once based on simd_caps() detection. Frozen forever.
Before: if simd_caps().avx512f { avx512_fn() } else { scalar_fn() } → ~1ns + branch
After: (SIMD_DISPATCH.fn_ptr)(args) → ~0.3ns, no branch
Dispatch targets (6 free functions across 4 modules):
byte_scan: byte_find_all, byte_count (AVX-512 / AVX2 / scalar)
distance: squared_distances_f32 (AVX2 / scalar)
nibble: nibble_unpack, nibble_above_threshold (AVX2 / scalar)
spatial_hash: batch_sq_dist (AVX2 / scalar)
NOTE: aabb.rs and cam_pq.rs dispatch on &self methods (not free functions)
so they keep inline simd_caps() branching. The dispatch table covers
the free function hot paths.
Visibility: internal SIMD functions promoted from pub(super)/private
to pub(crate) so the dispatch table can reference them as fn pointers.
The 8 existing per-call dispatch sites in nibble/byte_scan/distance/
spatial_hash/aabb/cam_pq still work — the dispatch table is additive.
Consumers can migrate to simd_dispatch().fn_ptr() incrementally.
TODO (separate PR): Rust 1.94 stabilized safe #[target_feature] on
safe functions. The `unsafe` on SIMD functions is legacy debt that
should be removed. The dispatch wrappers currently bridge this with
SAFETY comments; once unsafe is removed, the wrappers simplify to
direct function pointer assignment.
https://claude.ai/code/session_01Y69Vnw751w75iVSBRws7o71 parent a1c1cf4 commit fad0159
6 files changed
Lines changed: 348 additions & 13 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
12 | | - | |
| 12 | + | |
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
20 | | - | |
| 20 | + | |
21 | 21 | | |
22 | 22 | | |
23 | 23 | | |
| |||
52 | 52 | | |
53 | 53 | | |
54 | 54 | | |
55 | | - | |
| 55 | + | |
56 | 56 | | |
57 | 57 | | |
58 | 58 | | |
| |||
84 | 84 | | |
85 | 85 | | |
86 | 86 | | |
87 | | - | |
| 87 | + | |
88 | 88 | | |
89 | 89 | | |
90 | 90 | | |
| |||
111 | 111 | | |
112 | 112 | | |
113 | 113 | | |
114 | | - | |
| 114 | + | |
115 | 115 | | |
116 | 116 | | |
117 | 117 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
29 | 29 | | |
30 | 30 | | |
31 | 31 | | |
32 | | - | |
| 32 | + | |
33 | 33 | | |
34 | 34 | | |
35 | 35 | | |
| |||
39 | 39 | | |
40 | 40 | | |
41 | 41 | | |
42 | | - | |
| 42 | + | |
43 | 43 | | |
44 | 44 | | |
45 | 45 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
21 | 21 | | |
22 | 22 | | |
23 | 23 | | |
| 24 | + | |
| 25 | + | |
24 | 26 | | |
25 | 27 | | |
26 | 28 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
40 | 40 | | |
41 | 41 | | |
42 | 42 | | |
43 | | - | |
| 43 | + | |
44 | 44 | | |
45 | 45 | | |
46 | 46 | | |
| |||
54 | 54 | | |
55 | 55 | | |
56 | 56 | | |
57 | | - | |
| 57 | + | |
58 | 58 | | |
59 | 59 | | |
60 | 60 | | |
| |||
252 | 252 | | |
253 | 253 | | |
254 | 254 | | |
255 | | - | |
| 255 | + | |
256 | 256 | | |
257 | 257 | | |
258 | 258 | | |
| |||
272 | 272 | | |
273 | 273 | | |
274 | 274 | | |
275 | | - | |
| 275 | + | |
276 | 276 | | |
277 | 277 | | |
278 | 278 | | |
| |||
0 commit comments