rapidproto 0.1.0-alpha.2

Runtime for the Protobuf decoders that rapidproto-build generates.
Documentation
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
//! SWAR packed-varint decoder -- the Rust mirror of the C++ decoder's `decode_packed_varints`
//! (include/rapidproto/runtime.hpp). A large packed-varint span is decoded word-at-a-time with a
//! width-classified kernel instead of the per-element byte loop: on irregular/narrow varints the
//! branchless kernels avoid the byte loop's mispredictions, which is where the win comes from.
//!
//! The hot kernels use raw-pointer loads that deliberately read a full 8 (or 64) bytes at a time,
//! including bytes past the current varint. That is sound only inside a window the caller has already
//! bounds-checked (`end - q >= 8` / `>= 64` / `>= 72`); every such read is guarded. The non-hot
//! paths (the scalar tail, and the 9/10-byte cases) reuse the validated `wire::read_varint_full`, so the
//! malformed-input rules (truncation, overflow) live in one place. The kernel never aborts mid-span
//! on a *store* -- only a malformed varint stops it -- so the bulk stores stay vectorizable.
//!
//! This is arena-only (the streaming decoder can't vectorize a per-element callback store, matching
//! the C++ split). Decoding into a pre-sized [`DecodeVec`] via [`DecodeVec::fill_packed_varints`].

use core::ptr;

use crate::decode_vec::DecodeVec;
use crate::error::{DecodeError, ErrorKind};
use crate::wire::read_varint_full;

/// Span floor below which the kernels never engage: below this the classify scan and kernel dispatch
/// are not amortized, so tiny spans take the plain scalar loop. Matches C++ `kPackedKernelMinSpan`.
const KERNEL_MIN_SPAN: usize = 256;

// ── SWAR primitives (mirror of C++ `swar_detail`) ────────────────────────────────────────────────
mod swar {
    /// MSB of each byte.
    pub const MSB: u64 = 0x8080_8080_8080_8080;
    /// Low 7 bits of each byte.
    pub const LOW7: u64 = 0x7F7F_7F7F_7F7F_7F7F;

    /// Mask of the low `n` bytes (`n` in 1..=8).
    #[inline]
    pub const fn low_byte_mask(n: usize) -> u64 {
        if n >= 8 {
            u64::MAX
        } else {
            (1u64 << (8 * n)) - 1
        }
    }

    /// Load 8 bytes at `p` as a little-endian-logical `u64` (byte i in bits 8i..8i+7).
    ///
    /// # Safety
    /// `p` must be valid for an 8-byte read (the caller guarantees `>= 8` bytes remain).
    #[inline]
    pub unsafe fn load64(p: *const u8) -> u64 {
        // SAFETY: caller guarantees 8 readable bytes at `p`; `read_unaligned` needs no alignment.
        let w = unsafe { core::ptr::read_unaligned(p.cast::<u64>()) };
        // On a little-endian host the raw load IS the logical value; otherwise swap the bytes back
        // into logical order so the masks/compaction below are correct on any host.
        if cfg!(target_endian = "little") {
            w
        } else {
            w.swap_bytes()
        }
    }

    /// Gather the low 7 bits of each byte into a contiguous value (three SWAR compaction rounds).
    #[inline]
    pub fn compact7(x: u64) -> u64 {
        let x = ((x & 0x7F00_7F00_7F00_7F00) >> 1) | (x & 0x007F_007F_007F_007F);
        let x = ((x & 0x3FFF_0000_3FFF_0000) >> 2) | (x & 0x0000_3FFF_0000_3FFF);
        ((x & 0x0FFF_FFFF_0000_0000) >> 4) | (x & 0x0000_0000_0FFF_FFFF)
    }

    /// Movemask without SIMD: gather the MSB of each byte into 8 bits (bit i = MSB of byte i).
    #[inline]
    pub fn bytemask8(w: u64) -> u64 {
        ((w & MSB).wrapping_mul(0x0002_0408_1020_4081)) >> 56
    }

    /// 64-bit terminator mask over the 64 bytes at `p` (bit i set = byte i terminates a varint).
    ///
    /// # Safety
    /// `p` must be valid for a 64-byte read.
    #[inline]
    pub unsafe fn stopmask64(p: *const u8) -> u64 {
        let mut m = 0u64;
        for j in 0..8usize {
            // SAFETY: caller guarantees 64 readable bytes, so each `p + 8j` (j < 8) has 8 bytes.
            let w = unsafe { load64(p.add(8 * j)) };
            m |= bytemask8(!w) << (8 * j);
        }
        m
    }
}

/// Which kernel to decode a packed-varint span with. A wrong guess is only slower, never incorrect.
#[derive(Clone, Copy, PartialEq, Eq)]
enum Kernel {
    ByteLoop, // small span or overlong/unclassifiable: the scalar loop
    Swar,     // wide-mixed / 1-byte-dominant: per-element word-at-a-time read (1-byte fast path)
    Struct,   // narrow-mixed (max width <= 5): one stopmask per block, then independent compact7s
    Bulk1,    // homogeneous 1-byte: widen 64 bytes at once
    Bulk2,    // homogeneous 2-byte: four varints per 8-byte word
    Fixed,    // homogeneous 3..=10-byte: fixed-stride decode
}

/// Classify the width regime from the first 64-byte window at `p` (`span` bytes remain). For a
/// `Fixed` result the homogeneous width is returned; it is unspecified otherwise. Mirror of C++
/// `packed_strategy`.
///
/// # Safety
/// `p` must be valid for a 64-byte read when `span >= KERNEL_MIN_SPAN` (guaranteed by the floor).
unsafe fn classify(p: *const u8, span: usize) -> (Kernel, usize) {
    if span < KERNEL_MIN_SPAN {
        return (Kernel::ByteLoop, 0);
    }
    // SAFETY: span >= KERNEL_MIN_SPAN (256) >= 64, so 64 bytes are readable at `p`.
    let stop = unsafe { swar::stopmask64(p) };
    if stop == 0 {
        // No terminator in 64 bytes: a varint longer than the window (overlong/malformed) -> the
        // validating scalar loop rejects it.
        return (Kernel::ByteLoop, 0);
    }
    // Scan every terminator: track the first varint's width (homogeneity) and the max width.
    let mut m = stop;
    let mut last: i32 = -1;
    let mut g0: i32 = -1;
    let mut maxw = 0i32;
    let mut homo = true;
    while m != 0 {
        let b = m.trailing_zeros() as i32;
        m &= m - 1;
        let w = b - last;
        last = b;
        if w > maxw {
            maxw = w;
        }
        if g0 < 0 {
            g0 = w;
        } else if w != g0 {
            homo = false;
        }
    }
    if !homo {
        return if maxw <= 5 {
            (Kernel::Struct, 0)
        } else {
            (Kernel::Swar, 0)
        };
    }
    match g0 {
        1 => (Kernel::Bulk1, 0),
        2 => (Kernel::Bulk2, 0),
        3..=10 => (Kernel::Fixed, g0 as usize),
        _ => (Kernel::ByteLoop, 0), // g0 > 10: an overlong (malformed) varint -> scalar loop
    }
}

/// Read one varint from `[q, end)` via the validated scalar reader. Returns the value and the
/// advanced pointer, or the error kind.
///
/// # Safety
/// `q <= end` and both point into one allocation (the packed span).
///
/// `#[inline(always)]`, not `#[inline]`: inlining the full varint decode (`read_varint_full`) grows
/// this body past LLVM's inline threshold, so a plain hint lets it be out-of-lined -- and then every
/// scalar element (packed small-span fill, SWAR tail) pays a per-element call, which regresses large
/// packed spans (e.g. `big_osm`). Forcing the inline keeps the element read folded into the caller's
/// loop (as it was before the full-varint change, and as the C++ RP_FLATTENed small kernel does).
#[inline(always)]
unsafe fn read_one(q: *const u8, end: *const u8) -> Result<(u64, *const u8), ErrorKind> {
    // SAFETY: q <= end in the same span, so this is a valid subslice of the input.
    let rest = unsafe { core::slice::from_raw_parts(q, end.offset_from(q) as usize) };
    let mut raw = 0u64;
    // The fully-inlined reader: this is a tight per-element loop over a packed span, so inlining the
    // whole multibyte decode (no out-of-line #[cold] tail) avoids a per-element call on multibyte
    // values -- the dominant cost on a multibyte-heavy packed field (e.g. the `wide` bench).
    let after = read_varint_full(rest, &mut raw)?;
    Ok((raw, after.as_ptr()))
}

/// Read one varint from `[q, end)` word-at-a-time: a 1-byte value takes a straight-line fast path, a
/// 2..=8-byte value is decoded branchlessly from one 8-byte load, and a 9/10-byte value (all 8 low
/// bytes continue) falls to [`read_one`], which also reports overflow and truncation. Mirror of C++
/// `read_varint_packed`.
///
/// # Safety
/// `q <= end` in one span, with at least 8 bytes in `[q, end)`.
#[inline(always)]
unsafe fn read_one_swar(q: *const u8, end: *const u8) -> Result<(u64, *const u8), ErrorKind> {
    // SAFETY: >= 8 bytes remain at q (the caller's contract).
    let b = unsafe { *q };
    if b < 0x80 {
        // SAFETY: as above, `q + 1` is within the span.
        return Ok((u64::from(b), unsafe { q.add(1) }));
    }
    // SAFETY: >= 8 bytes remain at q.
    let w = unsafe { swar::load64(q) };
    let stop = !w & swar::MSB; // the MSB of each terminating byte
    if stop == 0 {
        // SAFETY: `q <= end` in one span, `read_one`'s contract.
        return unsafe { read_one(q, end) };
    }
    let len = (stop.trailing_zeros() >> 3) as usize + 1;
    // Every bit up to and including the first terminator's MSB.
    let mask = stop ^ (stop - 1);
    // SAFETY: the terminator is among the 8 bytes at q, so `q + len <= q + 8 <= end`.
    Ok((swar::compact7(w & mask & swar::LOW7), unsafe { q.add(len) }))
}

/// Store `conv(raw)` at `out[i]`.
///
/// # Safety
/// `i < cap` for the reserved array `out` (the caller sized `out` to the span's element upper bound).
#[inline]
unsafe fn put<T, F: FnMut(u64) -> T>(out: *mut T, i: usize, raw: u64, conv: &mut F) {
    // SAFETY: `i` is within the reserved capacity; the slot is uninitialized, so `write` (no drop).
    unsafe { ptr::write(out.add(i), conv(raw)) };
}

/// Fixed-stride decode of homogeneous `W`-byte varints (`W` in 3..=8). Guards each element with a
/// full `W`-byte continuation-pattern compare and stops on the first width change. Mirror of C++
/// `fixed_fill`. Returns the advanced `q`; `*n` is bumped per element written.
///
/// # Safety
/// `[q, end)` is a valid range in the span; `out` has room for every index written (`*n` stays below
/// the reserved capacity because element count never exceeds span bytes).
unsafe fn fixed_fill<const W: usize, T, F: FnMut(u64) -> T>(
    mut q: *const u8,
    end: *const u8,
    n: &mut usize,
    out: *mut T,
    conv: &mut F,
) -> *const u8 {
    let len_mask = swar::low_byte_mask(W);
    let msb_w = swar::MSB & len_mask;
    // The one accepted pattern: bytes 0..W-2 continue (MSB set), byte W-1 terminates (MSB clear).
    let one_varint = msb_w ^ (0x80u64 << (8 * (W - 1)));
    if W <= 4 {
        // Two W-byte varints (2W <= 8) fit in one load -> decode both per load.
        let msb2 = msb_w | (msb_w << (8 * W));
        let two_varints = one_varint | (one_varint << (8 * W));
        while end.addr().wrapping_sub(q.addr()) >= 8 {
            // SAFETY: >= 8 bytes remain at q.
            let w = unsafe { swar::load64(q) };
            if (w & msb2) == two_varints {
                let r0 = swar::compact7(w & len_mask & swar::LOW7);
                let r1 = swar::compact7((w >> (8 * W)) & len_mask & swar::LOW7);
                // SAFETY: *n and *n+1 are within capacity (<= span bytes).
                unsafe {
                    put(out, *n, r0, conv);
                    put(out, *n + 1, r1, conv);
                }
                *n += 2;
                // SAFETY: the two varints span `2W <= 8` of the bytes left.
                q = unsafe { q.add(2 * W) };
            } else if (w & msb_w) == one_varint {
                let r = swar::compact7(w & len_mask & swar::LOW7);
                // SAFETY: *n is within capacity (<= span bytes).
                unsafe { put(out, *n, r, conv) };
                *n += 1;
                // SAFETY: the varint spans `W <= 8` of the bytes left.
                q = unsafe { q.add(W) };
            } else {
                break;
            }
        }
    } else {
        while end.addr().wrapping_sub(q.addr()) >= 8 {
            // SAFETY: >= 8 bytes remain at q.
            let w = unsafe { swar::load64(q) };
            if (w & msb_w) != one_varint {
                break;
            }
            let r = swar::compact7(w & len_mask & swar::LOW7);
            // SAFETY: *n is within capacity (<= span bytes).
            unsafe { put(out, *n, r, conv) };
            *n += 1;
            // SAFETY: the varint spans `W <= 8` of the bytes left.
            q = unsafe { q.add(W) };
        }
    }
    q
}

/// Fixed-stride decode of homogeneous 9/10-byte varints. These span more than one word, so the value
/// is assembled from the low 8 bytes plus the high byte(s), applying `read_varint`'s overflow rule.
/// Mirror of C++ `fixed_fill_wide`.
///
/// # Safety
/// As [`fixed_fill`].
unsafe fn fixed_fill_wide<const W: usize, T, F: FnMut(u64) -> T>(
    mut q: *const u8,
    end: *const u8,
    n: &mut usize,
    out: *mut T,
    conv: &mut F,
) -> *const u8 {
    debug_assert!(W == 9 || W == 10);
    while end.addr().wrapping_sub(q.addr()) >= W {
        // SAFETY: >= W >= 9 bytes remain, so the 8-byte load is in range.
        let w0 = unsafe { swar::load64(q) };
        if (w0 & swar::MSB) != swar::MSB {
            break; // a byte in 0..7 terminates -> narrower than W
        }
        // Bits 0..55.
        let mut value = swar::compact7(w0 & swar::LOW7);
        // SAFETY: >= 9 bytes remain, so q[8] is in range.
        let b8 = unsafe { *q.add(8) };
        if W == 9 {
            if (b8 & 0x80) != 0 {
                break; // byte 8 continues -> wider than 9
            }
            value |= u64::from(b8 & 0x7F) << 56;
        } else {
            // SAFETY: >= W = 10 bytes remain, so q[9] is in range.
            let b9 = unsafe { *q.add(9) };
            if (b8 & 0x80) == 0 || (b9 & 0x80) != 0 {
                break; // not exactly 10 bytes
            }
            if b9 > 1 {
                break; // 10th byte > 1 overflows -> let the scalar tail report it
            }
            value |= u64::from(b8 & 0x7F) << 56;
            value |= u64::from(b9) << 63;
        }
        // SAFETY: *n is within capacity (<= span bytes).
        unsafe { put(out, *n, value, conv) };
        *n += 1;
        // SAFETY: the varint spans the W bytes checked.
        q = unsafe { q.add(W) };
    }
    q
}

/// Decode a whole packed-varint span into `out`, applying `conv` per raw value. Returns the element
/// count, or `(kind, offset-within-span)` on a malformed varint. Mirror of C++
/// `decode_packed_varints`. Kept out of line, as C++ does with `RP_NOINLINE`: the kernel is large,
/// and only spans `>= KERNEL_MIN_SPAN` reach it. Smaller spans take [`decode_scalar`], inlined into
/// `fill_packed_varints`.
///
/// # Safety
/// `out` must have room for at least `span.len()` elements (the element-count upper bound).
#[inline(never)]
unsafe fn decode_kernel<T, F: FnMut(u64) -> T>(
    span: &[u8],
    out: *mut T,
    conv: &mut F,
) -> Result<usize, (ErrorKind, usize)> {
    let core::ops::Range { start: begin, end } = span.as_ptr_range();
    let mut q = begin;
    let mut n = 0usize;
    let avail = |q: *const u8| end.addr().wrapping_sub(q.addr());

    loop {
        let q_start = q;
        // SAFETY: avail(q) >= 256 here (loop re-entry guards below keep it so), 64 bytes readable.
        let (kern, width) = unsafe { classify(q, avail(q)) };
        match kern {
            Kernel::Bulk1 => {
                while avail(q) >= 64 {
                    let mut any = 0u64;
                    for j in 0..8usize {
                        // SAFETY: >= 64 bytes remain at q, so 8 bytes at `q + 8j` (j < 8).
                        any |= unsafe { swar::load64(q.add(8 * j)) } & swar::MSB;
                    }
                    if any == 0 {
                        for i in 0..64usize {
                            // SAFETY: byte `i < 64` of the 64 left is in range; the 64 1-byte
                            // varints fill slots `n..n + 64`, within capacity (<= span bytes).
                            unsafe { put(out, n + i, u64::from(*q.add(i)), conv) };
                        }
                        // SAFETY: the 64 bytes just decoded remain at q.
                        q = unsafe { q.add(64) };
                        n += 64;
                    } else {
                        break;
                    }
                }
            }
            Kernel::Bulk2 => {
                while avail(q) >= 8 {
                    // SAFETY: >= 8 bytes remain at q.
                    let w = unsafe { swar::load64(q) };
                    if (w & swar::MSB) == 0x0080_0080_0080_0080 {
                        let x = w & swar::LOW7;
                        let y = (x & 0x007F_007F_007F_007F) | ((x & 0x7F00_7F00_7F00_7F00) >> 1);
                        // SAFETY: the four 2-byte varints fill slots `n..n + 4`, within capacity
                        // (<= span bytes), and span the 8 bytes left at q.
                        unsafe {
                            put(out, n, u64::from(y as u16), conv);
                            put(out, n + 1, u64::from((y >> 16) as u16), conv);
                            put(out, n + 2, u64::from((y >> 32) as u16), conv);
                            put(out, n + 3, u64::from((y >> 48) as u16), conv);
                            q = q.add(8);
                        }
                        n += 4;
                    } else {
                        break;
                    }
                }
            }
            Kernel::Fixed => {
                // SAFETY: `[q, end)` is the rest of the span and `out` has a slot per span byte,
                // the fill functions' contract.
                q = unsafe {
                    match width {
                        3 => fixed_fill::<3, _, _>(q, end, &mut n, out, conv),
                        4 => fixed_fill::<4, _, _>(q, end, &mut n, out, conv),
                        5 => fixed_fill::<5, _, _>(q, end, &mut n, out, conv),
                        6 => fixed_fill::<6, _, _>(q, end, &mut n, out, conv),
                        7 => fixed_fill::<7, _, _>(q, end, &mut n, out, conv),
                        8 => fixed_fill::<8, _, _>(q, end, &mut n, out, conv),
                        9 => fixed_fill_wide::<9, _, _>(q, end, &mut n, out, conv),
                        _ => fixed_fill_wide::<10, _, _>(q, end, &mut n, out, conv),
                    }
                };
            }
            Kernel::Struct => {
                while avail(q) >= 72 {
                    // SAFETY: >= 72 bytes remain at q, so 64 are readable.
                    let mut t = unsafe { swar::stopmask64(q) };
                    if t == 0 {
                        break;
                    }
                    let mut start = 0i32;
                    loop {
                        let b = t.trailing_zeros() as i32;
                        t &= t - 1;
                        let len = (b - start + 1) as usize;
                        if len <= 8 {
                            let lenmask = swar::low_byte_mask(len);
                            // SAFETY: `start <= b < 64` and 72 bytes remain at q, so 8 remain at
                            // `q + start`.
                            let w = unsafe { swar::load64(q.add(start as usize)) };
                            let r = swar::compact7(w & lenmask & swar::LOW7);
                            // SAFETY: slot `n` is within capacity (<= span bytes).
                            unsafe { put(out, n, r, conv) };
                            n += 1;
                        } else {
                            // 9/10-byte varint: decode and validate with the scalar reader.
                            // SAFETY: `start <= b < 64`, so `q + start` is among the 72 bytes left.
                            let p = unsafe { q.add(start as usize) };
                            // SAFETY: `p <= end` in the span, `read_one`'s contract.
                            let (raw, _) = unsafe { read_one(p, end) }
                                .map_err(|k| (k, p.addr() - begin.addr()))?;
                            // SAFETY: slot `n` is within capacity (<= span bytes).
                            unsafe { put(out, n, raw, conv) };
                            n += 1;
                        }
                        start = b + 1;
                        if t == 0 {
                            break;
                        }
                    }
                    // SAFETY: `start <= 64` and 72 bytes remain at q.
                    q = unsafe { q.add(start as usize) };
                }
            }
            Kernel::Swar => {
                while avail(q) >= 8 {
                    // SAFETY: `q <= end` with >= 8 bytes between, `read_one_swar`'s contract.
                    let (raw, next) = unsafe { read_one_swar(q, end) }
                        .map_err(|k| (k, q.addr() - begin.addr()))?;
                    q = next;
                    // SAFETY: slot `n` is within capacity (<= span bytes).
                    unsafe { put(out, n, raw, conv) };
                    n += 1;
                }
            }
            Kernel::ByteLoop => {}
        }
        if avail(q) < 512 {
            break; // little left -> the scalar tail finishes it
        }
        if q.addr().wrapping_sub(q_start.addr()) < 4096 {
            break; // kernel got no traction (churny data / ByteLoop pick) -> scalar tail
        }
        // a homogeneous kernel ran a long stretch then hit a width change: re-classify remainder
    }

    // Finish the remainder after the kernel (or a ByteLoop pick) with the inline scalar loop.
    let consumed = q.addr() - begin.addr();
    // SAFETY: `out + n` has room for the remaining elements (<= span.len() total); `consumed` bytes
    // were consumed, so the remaining span is `span[consumed..]`.
    let tail = unsafe { decode_scalar(&span[consumed..], out.add(n), conv) }
        .map_err(|(k, off)| (k, consumed + off))?;
    Ok(n + tail)
}

/// The validating scalar varint loop over a whole span: the small-span path of `fill_packed_varints`,
/// which it inlines into, and the remainder tail of [`decode_kernel`]. Returns the element count or
/// `(kind, offset-within-span)`.
///
/// # Safety
/// `out` must have room for at least `span.len()` elements (the element-count upper bound).
#[inline(always)]
unsafe fn decode_scalar<T, F: FnMut(u64) -> T>(
    span: &[u8],
    out: *mut T,
    conv: &mut F,
) -> Result<usize, (ErrorKind, usize)> {
    let core::ops::Range { start: begin, end } = span.as_ptr_range();
    let mut q = begin;
    let mut n = 0usize;
    while q < end {
        // A 1-byte value is read here directly; `read_one`'s slice-based reader would re-check the
        // remaining length and bound the advance on every element.
        // SAFETY: `q < end`, so one byte is readable.
        let b = unsafe { *q };
        let raw = if b < 0x80 {
            // SAFETY: `q < end`, so `q + 1 <= end`.
            q = unsafe { q.add(1) };
            u64::from(b)
        } else {
            // SAFETY: `q < end` in the span, `read_one`'s contract.
            let (raw, next) =
                unsafe { read_one(q, end) }.map_err(|k| (k, q.addr() - begin.addr()))?;
            q = next;
            raw
        };
        // SAFETY: slot `n` is within capacity (<= span bytes, each element takes at least one).
        unsafe { put(out, n, raw, conv) };
        n += 1;
    }
    Ok(n)
}

impl<'a, T> DecodeVec<'a, T> {
    /// Decode a whole packed-varint `span` into `self`, applying `conv` to each raw value, with
    /// `base_off` the span's byte offset within the message body (for error reporting).
    ///
    /// Reserves the span's element upper bound (each varint is >= 1 byte), fills it with the SWAR
    /// kernel (large spans) or the scalar loop (small spans), then trims the over-estimate -- the
    /// Rust mirror of the C++ decoder's `emit_packed_fill` + `decode_packed_varints`. Generated
    /// decoders call this for every packed-varint field; the only `unsafe` stays here, not in them.
    // A plain `#[inline]`: forcing this into the generated decoders measured slower on small packed
    // arrays.
    #[inline]
    pub fn fill_packed_varints<F>(
        &mut self,
        span: &[u8],
        base_off: usize,
        mut conv: F,
    ) -> Result<(), DecodeError>
    where
        F: FnMut(u64) -> T,
    {
        if span.is_empty() {
            return Ok(());
        }
        self.reserve(span.len())
            .map_err(|_| DecodeError::new(ErrorKind::ArenaExhausted, 0))?;
        let tail = self.spare_capacity_ptr();
        // Small spans take the inline scalar loop (nothing to amortize an out-of-line call); large
        // spans go through the out-of-line SWAR kernel. Mirrors C++ `emit_packed_fill`'s span-size
        // split. `reserve(span.len())` left `span.len()` free slots at `tail`, and the element count
        // never exceeds `span.len()` (>= 1 byte each), so every write is in bounds.
        let written = if span.len() >= KERNEL_MIN_SPAN {
            // SAFETY: `tail` has room for `span.len()` elements (above).
            unsafe { decode_kernel(span, tail, &mut conv) }
        } else {
            // SAFETY: `tail` has room for `span.len()` elements (above).
            unsafe { decode_scalar(span, tail, &mut conv) }
        }
        .map_err(|(k, off)| DecodeError::new(k, base_off + off))?;
        // SAFETY: `written` elements were initialized contiguously at `tail`.
        unsafe { self.advance_len(written) };
        self.shrink_to_fit();
        Ok(())
    }
}

#[cfg(all(test, feature = "alloc"))]
mod tests {
    use super::*;
    use crate::arena::Arena;

    // Encode `v` as a LEB128 varint into `out`.
    fn put_varint(out: &mut alloc::vec::Vec<u8>, mut v: u64) {
        loop {
            let b = (v & 0x7F) as u8;
            v >>= 7;
            if v != 0 {
                out.push(b | 0x80);
            } else {
                out.push(b);
                break;
            }
        }
    }

    fn decode_u64(span: &[u8]) -> alloc::vec::Vec<u64> {
        let arena = Arena::new();
        let mut v = DecodeVec::<u64>::new(&arena);
        v.fill_packed_varints(span, 0, |raw| raw).unwrap();
        v.into_slice().to_vec()
    }

    fn check(values: &[u64]) {
        let mut span = alloc::vec::Vec::new();
        for &x in values {
            put_varint(&mut span, x);
        }
        assert_eq!(decode_u64(&span), values, "span len {}", span.len());
    }

    // Miri runs these tests orders of magnitude slower than native code, so under it the long runs
    // are cut to a tenth, still long enough for the kernel each test targets.
    fn runs(n: u64) -> u64 {
        if cfg!(miri) {
            n / 10
        } else {
            n
        }
    }

    #[test]
    fn empty_span() {
        assert_eq!(decode_u64(&[]), alloc::vec::Vec::<u64>::new());
    }

    #[test]
    fn small_span_uses_scalar_loop() {
        check(&[0, 1, 127, 128, 300, 16384, u64::MAX]);
    }

    #[test]
    fn homogeneous_1_byte_bulk1() {
        // > KERNEL_MIN_SPAN one-byte varints -> Bulk1.
        let vals: alloc::vec::Vec<u64> = (0..2000u64).map(|i| i % 128).collect();
        check(&vals);
    }

    #[test]
    fn homogeneous_2_byte_bulk2() {
        // Values 128..=16383 all encode in 2 bytes -> Bulk2.
        let vals: alloc::vec::Vec<u64> = (0..runs(2000)).map(|i| 128 + (i % 16000)).collect();
        check(&vals);
    }

    #[test]
    fn homogeneous_fixed_widths_3_through_10() {
        // Pick a value whose encoding is exactly W bytes for each W in 3..=10.
        let per_width = [
            (3usize, 1u64 << 14), // 16384 -> 3 bytes
            (4, 1 << 21),
            (5, 1 << 28),
            (6, 1 << 35),
            (7, 1 << 42),
            (8, 1 << 49),
            (9, 1 << 56),
            (10, 1 << 63),
        ];
        for (w, base) in per_width {
            let vals: alloc::vec::Vec<u64> = (0..runs(1500)).map(|i| base + (i & 0x3F)).collect();
            check(&vals);
            let _ = w;
        }
    }

    #[test]
    fn narrow_mixed_struct() {
        // Mixed small widths (1..=5 bytes), max <= 5 -> Struct.
        let vals: alloc::vec::Vec<u64> = (0..runs(3000))
            .map(|i| match i % 5 {
                0 => i % 100,           // 1 byte
                1 => 200 + (i % 10000), // 2-3 bytes
                2 => 1 << 20,           // 3 bytes
                3 => 1 << 24,           // 4 bytes
                _ => 1 << 30,           // 5 bytes
            })
            .collect();
        check(&vals);
    }

    // Encode `values` as one packed span, asserting the classifier sends it to the Swar kernel.
    fn swar_span(values: &[u64]) -> alloc::vec::Vec<u8> {
        let mut span = alloc::vec::Vec::new();
        for &x in values {
            put_varint(&mut span, x);
        }
        // SAFETY: the whole span is readable at its start.
        assert!(unsafe { classify(span.as_ptr(), span.len()) }.0 == Kernel::Swar);
        span
    }

    // 1-byte values with every third one 10 bytes wide.
    fn swar_prefix_values() -> alloc::vec::Vec<u64> {
        (0..100u64)
            .map(|i| if i % 3 == 0 { u64::MAX } else { i })
            .collect()
    }

    // The Swar span of `swar_prefix_values`.
    fn swar_prefix() -> alloc::vec::Vec<u8> {
        swar_span(&swar_prefix_values())
    }

    #[test]
    fn wide_mixed_swar() {
        // Mix in wide values (max width > 5) -> Swar. The classifier only samples the first 64-byte
        // window, so a representative mix must appear up front for this to engage Swar.
        let vals: alloc::vec::Vec<u64> = (0..runs(3000))
            .map(|i| if i % 7 == 0 { u64::MAX - i } else { i % 50 })
            .collect();
        swar_span(&vals);
        check(&vals);
    }

    #[test]
    fn wide_mixed_swar_every_width() {
        // Widths 1..=10 in turn: the Swar kernel's word read decodes every 2..=8-byte width, and the
        // 9/10-byte ones fall back to the validating reader.
        let vals: alloc::vec::Vec<u64> = (0..runs(3000))
            .map(|i| match i % 10 {
                9 => u64::MAX - i,
                w => (1u64 << (7 * w)) + i % 7, // exactly w + 1 bytes
            })
            .collect();
        let span = swar_span(&vals);
        assert_eq!(decode_u64(&span), vals);
    }

    // The error `fill_packed_varints` reports for `span`, as (kind, offset).
    fn decode_err(span: &[u8]) -> (ErrorKind, usize) {
        let arena = Arena::new();
        let mut v = DecodeVec::<u64>::new(&arena);
        let err = v.fill_packed_varints(span, 0, |raw| raw).unwrap_err();
        (err.kind(), err.offset())
    }

    #[test]
    fn swar_overflow_reports_offset() {
        // A 10-byte varint whose last byte is > 1 overflows. At least 8 bytes remain at it, so the
        // Swar loop's fallback reads it and reports it at its own offset.
        let mut span = swar_prefix();
        let overflow_at = span.len();
        span.resize(span.len() + 9, 0x80);
        span.push(0x02);
        assert_eq!(decode_err(&span), (ErrorKind::VarintOverflow, overflow_at));
    }

    #[test]
    fn swar_truncation_reports_offset() {
        // A varint cut off at the end of the span: with 9 bytes left the Swar loop's fallback reads
        // it, with 7 the scalar tail does. Both report it at its start.
        for left in [9usize, 7] {
            let mut span = swar_prefix();
            let truncated_at = span.len();
            span.resize(span.len() + left, 0x80);
            assert_eq!(decode_err(&span), (ErrorKind::Truncated, truncated_at));
        }
    }

    #[test]
    fn scalar_loop_reports_offset_after_one_byte_values() {
        // A small span (the scalar loop) whose bad varint follows 1-byte values: it is reported at
        // its own offset.
        assert_eq!(decode_err(&[1, 2, 0x80]), (ErrorKind::Truncated, 2));
        let mut span = alloc::vec![5, 0x7f];
        span.resize(span.len() + 9, 0xff);
        span.push(0x02);
        assert_eq!(decode_err(&span), (ErrorKind::VarintOverflow, 2));
    }

    #[test]
    fn swar_non_canonical_padding() {
        // Zero-padded encodings (1 as 0x81 0x80 0x80 0x00, 300 as 0xac 0x82 0x00) decode to their
        // values.
        let mut span = swar_prefix();
        let mut expected = swar_prefix_values();
        for _ in 0..50 {
            span.extend_from_slice(&[0x81, 0x80, 0x80, 0x00, 0xac, 0x82, 0x00, 0x07]);
            expected.extend([1, 300, 7]);
        }
        assert_eq!(decode_u64(&span), expected);
    }

    #[test]
    fn re_classify_across_regime_change() {
        // A long homogeneous run (forces a kernel) followed by a different regime, long enough to
        // re-classify (>= 4096 bytes of traction, then >= 512 remaining). The first run therefore
        // keeps its length under Miri.
        let mut vals: alloc::vec::Vec<u64> = (0..6000u64).map(|_| 1).collect(); // 1-byte run
        vals.extend((0..runs(3000)).map(|i| 128 + (i % 16000))); // 2-byte run
        vals.extend((0..runs(3000)).map(|i| if i % 7 == 0 { u64::MAX } else { i % 50 }));
        check(&vals);
    }

    #[test]
    fn conv_is_applied_per_element() {
        let arena = Arena::new();
        let mut v = DecodeVec::<i32>::new(&arena);
        let mut span = alloc::vec::Vec::new();
        for i in 0..1000u64 {
            put_varint(&mut span, i);
        }
        v.fill_packed_varints(&span, 0, |raw| raw as i32).unwrap();
        let got = v.into_slice();
        assert_eq!(got.len(), 1000);
        for (i, &x) in got.iter().enumerate() {
            assert_eq!(x, i as i32);
        }
    }

    #[test]
    fn truncated_varint_errors_without_panic() {
        // A valid large run, then a dangling continuation byte at the end.
        let mut span = alloc::vec::Vec::new();
        for _ in 0..500 {
            put_varint(&mut span, 300); // 2-byte each
        }
        span.push(0x80); // continuation with no terminator
        let arena = Arena::new();
        let mut v = DecodeVec::<u64>::new(&arena);
        let err = v.fill_packed_varints(&span, 10, |raw| raw).unwrap_err();
        assert_eq!(err.kind(), ErrorKind::Truncated);
        // Offset is base_off + position of the dangling byte within the span.
        assert_eq!(err.offset(), 10 + (span.len() - 1));
    }

    #[test]
    fn spans_around_the_kernel_min_span_boundary() {
        // 1-byte varints -> one byte each, so element count == span length in bytes. Exercise 255
        // (below the 256 floor: scalar loop), 256 (floor: kernels engage), and 257.
        for n in [255usize, 256, 257] {
            let vals: alloc::vec::Vec<u64> = (0..n as u64).map(|i| i % 128).collect();
            check(&vals);
        }
    }

    #[test]
    fn ten_byte_overflow_bails_to_tail_and_reports_overflow() {
        // A homogeneous 10-byte run (value with bit 63 set encodes in exactly 10 bytes) forces the
        // Fixed(10) kernel; then an over-long 10-byte varint whose 10th byte is > 1 encodes bits above
        // 63. fixed_fill_wide<10> must bail that element to the validating tail, which reports
        // VarintOverflow -- never a wrong value, never a panic.
        let mut span = alloc::vec::Vec::new();
        for _ in 0..40 {
            put_varint(&mut span, 1u64 << 63); // 10 bytes each -> >= 256 bytes, classifies Fixed(10)
        }
        let overflow_at = span.len();
        span.resize(span.len() + 9, 0x80); // nine continuation bytes
        span.push(0x02); // 10th byte > 1 -> overflow
        let arena = Arena::new();
        let mut v = DecodeVec::<u64>::new(&arena);
        let err = v.fill_packed_varints(&span, 0, |raw| raw).unwrap_err();
        assert_eq!(err.kind(), ErrorKind::VarintOverflow);
        assert_eq!(err.offset(), overflow_at);
    }

    #[test]
    fn two_segments_reserve_into_nonempty() {
        // A packed field can arrive in two wire segments: the second fill appends into a DecodeVec
        // that already holds elements (reserve-into-nonempty, then relocate-on-grow).
        let arena = Arena::new();
        let mut v = DecodeVec::<u64>::new(&arena);
        let seg1: alloc::vec::Vec<u64> = (0..300u64).map(|i| 128 + (i % 16000)).collect(); // 2-byte
        let seg2: alloc::vec::Vec<u64> = (0..400u64).map(|i| i % 64).collect(); // 1-byte
        let mut b1 = alloc::vec::Vec::new();
        for &x in &seg1 {
            put_varint(&mut b1, x);
        }
        let mut b2 = alloc::vec::Vec::new();
        for &x in &seg2 {
            put_varint(&mut b2, x);
        }
        v.fill_packed_varints(&b1, 0, |raw| raw).unwrap();
        v.fill_packed_varints(&b2, 0, |raw| raw).unwrap();
        let got = v.into_slice();
        let mut expected = seg1.clone();
        expected.extend(seg2);
        assert_eq!(got, &expected[..]);
    }
}