fastmash 0.6.0

Fast command-line statistics on text data, compatible with GNU datamash
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
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
//! Owned segments with byte-encoded offsets and reusable projection ranges.
use super::{Failure, allocation};
use crate::locale;
use std::{cmp::Ordering, ops::Range};

/// Recent collation sort keys by key text. Grouping keys usually repeat, and a
/// sort key depends only on its text, so reuse is exact. At most 256 slots of
/// keys up to 256 bytes, within a caller's byte budget. Admission is optional:
/// a cache that cannot grow still computes the same key directly.
pub(super) struct SortKeys {
    slots: Vec<(Vec<u8>, Vec<u8>)>,
    bytes: usize,
    budget: usize,
}

impl Default for SortKeys {
    fn default() -> Self {
        Self::with_budget(Self::MAX_BYTES)
    }
}

impl SortKeys {
    const SLOTS: usize = 256;
    const LONGEST: usize = 256;
    pub(super) const MAX_BYTES: usize = 128 << 10;

    /// A budget too small for the slot table does not allocate a cache.
    pub(super) fn budget(available: usize) -> usize {
        let budget = available.min(Self::MAX_BYTES);
        if budget < Self::SLOTS * std::mem::size_of::<(Vec<u8>, Vec<u8>)>() {
            0
        } else {
            budget
        }
    }

    pub(super) fn with_budget(available: usize) -> Self {
        Self {
            slots: Vec::new(),
            bytes: 0,
            budget: Self::budget(available),
        }
    }

    /// Actual cache allocations, including unused slot capacity.
    pub(super) fn retained_bytes(&self) -> Option<usize> {
        Some(self.bytes)
    }

    /// Append the sort key of `text` to `out`.
    pub(super) fn write(
        &mut self,
        collator: &locale::Collator,
        text: &str,
        out: &mut Vec<u8>,
    ) -> Result<(), Failure> {
        if self.budget == 0 || text.len() > Self::LONGEST {
            return sort_key(collator, text, out);
        }
        if self.slots.is_empty() {
            if self.slots.try_reserve_exact(Self::SLOTS).is_err() {
                return sort_key(collator, text, out);
            }
            let bytes = self
                .slots
                .capacity()
                .checked_mul(std::mem::size_of::<(Vec<u8>, Vec<u8>)>());
            let Some(bytes) = bytes.filter(|&bytes| bytes <= self.budget) else {
                self.slots = Vec::new();
                return sort_key(collator, text, out);
            };
            self.bytes = bytes;
            self.slots.resize_with(Self::SLOTS, Default::default);
        }
        let at = Self::hash(text.as_bytes()) as usize % Self::SLOTS;
        let (cached, key) = &self.slots[at];
        // An empty slot holds no key: every sort key has at least one byte.
        if !key.is_empty() && crate::text_order::equal_bytes(cached, text.as_bytes()) {
            out.try_reserve(key.len()).map_err(|_| allocation())?;
            out.extend_from_slice(key);
            return Ok(());
        }
        let start = out.len();
        sort_key(collator, text, out)?;
        self.remember(at, text, &out[start..]);
        Ok(())
    }

    fn hash(bytes: &[u8]) -> u64 {
        let fold = |hash: u64, bytes: &[u8]| {
            bytes.iter().fold(hash, |hash, &byte| {
                (hash ^ u64::from(byte)).wrapping_mul(0x0100_0000_01b3)
            })
        };
        let initial = 0xcbf2_9ce4_8422_2325;
        if bytes.len() <= 64 {
            fold(initial, bytes)
        } else {
            // Bound work on misses. A hit still compares the complete text,
            // including the unsampled middle and its length.
            fold(fold(initial, &bytes[..32]), &bytes[bytes.len() - 32..]) ^ bytes.len() as u64
        }
    }

    fn remember(&mut self, at: usize, text: &str, computed: &[u8]) {
        let (mut cached, mut key) = std::mem::take(&mut self.slots[at]);
        // These capacities were admitted together within budget.
        let previous = cached.capacity() + key.capacity();
        self.bytes -= previous;
        let available = self.budget - self.bytes;
        let needed = cached
            .capacity()
            .max(text.len())
            .checked_add(key.capacity().max(computed.len()));
        if needed.is_none_or(|needed| needed > available) {
            self.slots[at] = (cached, key);
            self.bytes += previous;
            return;
        }
        cached.clear();
        key.clear();
        if cached.try_reserve_exact(text.len()).is_ok()
            && key.try_reserve_exact(computed.len()).is_ok()
            && let Some(bytes) = cached.capacity().checked_add(key.capacity())
            && bytes <= available
        {
            cached.extend_from_slice(text.as_bytes());
            key.extend_from_slice(computed);
            self.bytes += bytes;
            self.slots[at] = (cached, key);
        }
    }
}

/// Appends the collation sort key of `text` to `out`, refusing if `out`
/// cannot grow.
fn sort_key(collator: &locale::Collator, text: &str, out: &mut Vec<u8>) -> Result<(), Failure> {
    collator
        .write_key(text, out)
        .map_err(|locale::KeyTooLarge| allocation())
}

/// Segments whose first `key_count` are a record's grouping keys, as sorting
/// stores them.
pub(super) trait Keys {
    fn key_count(&self) -> usize;
    fn segment(&self, at: usize) -> &[u8];
    /// Orders by the key segments in turn, then by key count.
    fn compare_keys(&self, other: &Self) -> Ordering {
        let (a, b) = (self.key_count(), other.key_count());
        for at in 0..a.min(b) {
            let order = crate::text_order::compare(self.segment(at), other.segment(at), false);
            if order != Ordering::Equal {
                return order;
            }
        }
        a.cmp(&b)
    }
}

/// A record's segments: its grouping keys, then its projected fields (packed)
/// or its original bytes.
pub(super) trait Storage: Keys + Sized {
    const ORIGINAL: bool;
    fn original(&self) -> Option<&[u8]>;
}

/// Storage built from a projection: the records of the reference sorts that
/// tests compare sort chunks with.
#[cfg(test)]
pub(super) trait Build: Storage {
    fn build(
        raw: &[u8],
        data: &[u8],
        spans: &[Range<usize>],
        keys: usize,
        fold: bool,
    ) -> Result<Self, Failure>;
    /// Storage whose key segments are `keys`, split at `ends`, followed by
    /// `original`.
    fn language(_keys: &[u8], _ends: &[usize], _original: &[u8]) -> Result<Self, Failure> {
        Err(super::unsupported(
            "internal language sorter requires original records",
        ))
    }
}

/// A record in a sort chunk's arena: a table of the segments' cumulative end
/// offsets, `width` bytes each (little-endian), then the segments. The width
/// is the smallest of 1, 2, 4 and 8 bytes that holds the segments' total
/// length, so a small record costs one byte per segment and a record of any
/// size or segment count fits.
#[derive(Clone, Copy, Debug)]
pub(super) struct View<'a> {
    /// The end table.
    table: &'a [u8],
    /// The segments.
    payload: &'a [u8],
    /// The first segment's end, which is read most.
    first: usize,
    width: u8,
    count: u32,
    keys: u32,
}

impl<'a> View<'a> {
    /// The end-table width for segments of `length` bytes in total.
    pub(super) fn width(length: usize) -> u8 {
        if length <= 0xff {
            1
        } else if length <= 0xffff {
            2
        } else if length <= 0xffff_ffff {
            4
        } else {
            8
        }
    }
    /// Appends `end` to an end table of width `width`.
    pub(super) fn put_end(out: &mut Vec<u8>, end: usize, width: u8) {
        match width {
            1 => out.push(end as u8),
            2 => out.extend_from_slice(&(end as u16).to_le_bytes()),
            4 => out.extend_from_slice(&(end as u32).to_le_bytes()),
            _ => out.extend_from_slice(&(end as u64).to_le_bytes()),
        }
    }
    #[inline]
    fn read(table: &[u8], width: u8, at: usize) -> usize {
        match width {
            1 => usize::from(table[at]),
            2 => usize::from(u16::from_le_bytes([table[2 * at], table[2 * at + 1]])),
            4 => {
                let bytes = &table[4 * at..4 * at + 4];
                u32::from_le_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]) as usize
            }
            _ => u64::from_le_bytes(table[8 * at..8 * at + 8].try_into().unwrap()) as usize,
        }
    }
    /// The record of `count` segments (the first `keys` of them keys) whose
    /// end table, of `width`-byte entries, starts `bytes`.
    #[inline]
    pub(super) fn new(bytes: &'a [u8], width: u8, count: usize, keys: usize) -> Self {
        let (table, rest) = bytes.split_at(count * usize::from(width));
        let (first, length) = match count {
            0 => (0, 0),
            1 => {
                let end = Self::read(table, width, 0);
                (end, end)
            }
            _ => (
                Self::read(table, width, 0),
                Self::read(table, width, count - 1),
            ),
        };
        Self {
            table,
            payload: &rest[..length],
            first,
            width,
            count: count as u32,
            keys: keys as u32,
        }
    }
    /// The end of segment `at`.
    #[inline]
    fn end(&self, at: usize) -> usize {
        if at == 0 {
            self.first
        } else if at + 1 == self.count as usize {
            self.payload.len()
        } else {
            Self::read(self.table, self.width, at)
        }
    }
    /// Segment `at`, borrowed from the arena rather than from the view.
    #[inline]
    pub(super) fn bytes(&self, at: usize) -> &'a [u8] {
        let start = if at == 0 { 0 } else { self.end(at - 1) };
        &self.payload[start..self.end(at)]
    }
    /// Every segment's bytes.
    pub(super) fn payload(&self) -> &'a [u8] {
        self.payload
    }
}

impl Keys for View<'_> {
    #[inline]
    fn key_count(&self) -> usize {
        self.keys as usize
    }
    #[inline]
    fn segment(&self, at: usize) -> &[u8] {
        self.bytes(at)
    }
}

/// A sorted record's storage: decoded from a run, or viewed in the chunk
/// that stayed in memory.
pub(super) enum Mixed<'a, S> {
    Owned(S),
    View(View<'a>),
}

impl<S: Storage> Keys for Mixed<'_, S> {
    #[inline]
    fn key_count(&self) -> usize {
        match self {
            Self::Owned(storage) => storage.key_count(),
            Self::View(view) => view.key_count(),
        }
    }
    #[inline]
    fn segment(&self, at: usize) -> &[u8] {
        match self {
            Self::Owned(storage) => storage.segment(at),
            Self::View(view) => view.bytes(at),
        }
    }
}

impl<S: Storage> Storage for Mixed<'_, S> {
    const ORIGINAL: bool = S::ORIGINAL;
    #[inline]
    fn original(&self) -> Option<&[u8]> {
        S::ORIGINAL.then(|| self.segment(self.key_count()))
    }
}

/// Per-thread buffers for building language records.
#[derive(Default)]
pub(super) struct LanguageScratch {
    /// Each key's field span within the data.
    pub(super) identities: Vec<Range<usize>>,
    /// The key segments ([`language_segments`]).
    pub(super) bytes: Vec<u8>,
    /// The end of each key segment in `bytes`.
    pub(super) ends: Vec<usize>,
    /// Key texts as they are read.
    pub(super) text: KeyText,
}

/// Buffers for a key text as language sorting reads it.
#[derive(Default)]
pub(super) struct KeyText {
    /// The text case-folded.
    pub(super) folded: String,
    /// The text with glibc's contractions composed.
    pub(super) composed: String,
}

/// Appends a record's language key segments to `out`, ending each in `ends`:
/// per key its collation sort key and then its identity, so that keys glibc's
/// `strcoll` ties leave the order to the next key, as GNU `sort` does.
///
/// Key `at` is sorted by the sort key of `text(at)`, ASCII-uppercased with
/// `fold`. Its identity is `identity(at)`, the key field (ASCII-lowercased
/// with `fold`), which orders the spellings the collator ties as glibc's
/// fourth level does: by code point, except for the letters glibc places
/// otherwise against their decomposition ([`locale::Collator::write_identity`]).
/// Both are read as the sort reads them (uppercased with `fold`) with
/// glibc's contractions composed ([`locale::compose`]), which glibc ties with
/// the letters they spell, so keys spelled so tie on every segment and stay
/// in input order, as in the stable sort GNU runs.
#[allow(clippy::too_many_arguments)]
pub(super) fn language_segments<'a>(
    count: usize,
    mut text: impl FnMut(usize) -> Result<&'a str, Failure>,
    identity: impl Fn(usize) -> &'a [u8],
    fold: bool,
    collator: &locale::Collator,
    sort_keys: &mut SortKeys,
    scratch: &mut KeyText,
    out: &mut Vec<u8>,
    ends: &mut Vec<usize>,
) -> Result<(), Failure> {
    let KeyText { folded, composed } = scratch;
    out.clear();
    ends.clear();
    ends.try_reserve_exact(2 * count)
        .map_err(|_| allocation())?;
    for at in 0..count {
        let original = text(at)?;
        let mut text = original;
        if fold {
            folded.clear();
            folded.try_reserve(text.len()).map_err(|_| allocation())?;
            folded.push_str(text);
            folded.make_ascii_uppercase();
            text = folded.as_str();
        }
        if compose(text, composed)? {
            text = composed.as_str();
        }
        sort_keys.write(collator, text, out)?;
        ends.push(out.len());
        let field = identity(at);
        let start = out.len();
        // The identity is usually the key text, read just so; otherwise it is
        // read as the sort reads it: GNU `sort -f` uppercases.
        let read = if field == original.as_bytes() {
            Some(text)
        } else if field.is_ascii() {
            None
        } else if let Ok(mut written) = std::str::from_utf8(field) {
            if fold {
                folded.clear();
                folded
                    .try_reserve(written.len())
                    .map_err(|_| allocation())?;
                folded.push_str(written);
                folded.make_ascii_uppercase();
                written = folded.as_str();
            }
            if compose(written, composed)? {
                written = composed.as_str();
            }
            Some(written)
        } else {
            None
        };
        let placed = match read {
            Some(read) => collator
                .write_identity(read, out)
                .map_err(|locale::KeyTooLarge| allocation())?,
            None => false,
        };
        if !placed {
            let bytes = read.map_or(field, str::as_bytes);
            out.try_reserve(bytes.len()).map_err(|_| allocation())?;
            out.extend_from_slice(bytes);
        }
        if fold {
            out[start..].make_ascii_lowercase();
        }
        ends.push(out.len());
    }
    Ok(())
}

/// [`locale::compose`], refusing if `out` cannot grow.
fn compose(text: &str, out: &mut String) -> Result<bool, Failure> {
    locale::compose(text, out).map_err(|locale::KeyTooLarge| allocation())
}

/// Short records keep their bytes inline, with one-byte end offsets, so the
/// common small projected record needs no allocation of its own. On 64-bit
/// targets the inline form fits around the heap form's `Vec` capacity niche,
/// so `Segments` stays as small as the heap form alone.
const INLINE: usize = 22;
#[cfg(target_pointer_width = "64")]
const _: () = assert!(std::mem::size_of::<Segments>() == 32);

fn heap_end(bytes: &[u8], at: usize) -> usize {
    let start = at * std::mem::size_of::<usize>();
    usize::from_ne_bytes(
        bytes[start..start + std::mem::size_of::<usize>()]
            .try_into()
            .unwrap(),
    )
}

/// End offsets are encoded as bytes so the allocation requires no alignment or
/// unsafe conversions. Payload bytes start after the complete offset table:
/// one byte per offset inline, `usize` offsets on the heap.
pub(super) enum Segments {
    Inline {
        count: u8,
        len: u8,
        data: [u8; INLINE],
    },
    Heap {
        bytes: Vec<u8>,
        count: usize,
    },
}
impl Segments {
    pub(super) fn new(count: usize, payload: usize) -> Result<Self, Failure> {
        if count
            .checked_add(payload)
            .is_some_and(|size| size <= INLINE)
        {
            return Ok(Self::Inline {
                count: count as u8,
                len: count as u8,
                data: [0; INLINE],
            });
        }
        Self::heap(count, payload)
    }
    /// Heap storage for `count` segments, with room for `payload` bytes.
    pub(super) fn heap(count: usize, payload: usize) -> Result<Self, Failure> {
        let header = count
            .checked_mul(std::mem::size_of::<usize>())
            .ok_or_else(allocation)?;
        let capacity = header.checked_add(payload).ok_or_else(allocation)?;
        let mut bytes = Vec::new();
        bytes
            .try_reserve_exact(capacity)
            .map_err(|_| allocation())?;
        bytes.resize(header, 0);
        Ok(Self::Heap { bytes, count })
    }
    /// Storage for `count` segments whose lengths `length` yields in order, with
    /// a zeroed payload for `payload_mut` to fill. Small records stay inline, so
    /// decoding them allocates nothing; a record of more than `INLINE` segments
    /// is always on the heap and takes its ends directly.
    pub(super) fn with_lengths(
        count: usize,
        mut length: impl FnMut() -> Result<usize, Failure>,
    ) -> Result<Self, Failure> {
        let mut segments = if count <= INLINE {
            let mut lengths = [0; INLINE];
            let mut total = 0usize;
            for slot in &mut lengths[..count] {
                *slot = length()?;
                total = total.checked_add(*slot).ok_or_else(allocation)?;
            }
            // `new` checks that the offsets and payload fit, inline or on the heap.
            let mut segments = Self::new(count, total)?;
            let mut end = segments.header();
            for (at, slot) in lengths[..count].iter().enumerate() {
                end += slot;
                segments.set_end(at, end);
            }
            segments
        } else {
            let mut segments = Self::heap(count, 0)?;
            let mut end = segments.header();
            for at in 0..count {
                end = end.checked_add(length()?).ok_or_else(allocation)?;
                segments.set_end(at, end);
            }
            segments
        };
        let end = if segments.count() == 0 {
            segments.header()
        } else {
            segments.end(segments.count() - 1)
        };
        match &mut segments {
            Self::Inline { len, .. } => *len = end as u8,
            Self::Heap { bytes, .. } => {
                bytes
                    .try_reserve_exact(end - bytes.len())
                    .map_err(|_| allocation())?;
                bytes.resize(end, 0);
            }
        }
        Ok(segments)
    }
    /// Every segment's bytes, after the offset table, for filling in place.
    pub(super) fn payload_mut(&mut self) -> &mut [u8] {
        let header = self.header();
        match self {
            Self::Inline { len, data, .. } => &mut data[header..usize::from(*len)],
            Self::Heap { bytes, .. } => &mut bytes[header..],
        }
    }
    pub(super) fn count(&self) -> usize {
        match self {
            Self::Inline { count, .. } => usize::from(*count),
            Self::Heap { count, .. } => *count,
        }
    }
    fn all(&self) -> &[u8] {
        match self {
            Self::Inline { len, data, .. } => &data[..usize::from(*len)],
            Self::Heap { bytes, .. } => bytes,
        }
    }
    pub(super) fn header(&self) -> usize {
        match self {
            Self::Inline { count, .. } => usize::from(*count),
            Self::Heap { count, .. } => count * std::mem::size_of::<usize>(),
        }
    }
    pub(super) fn end(&self, at: usize) -> usize {
        match self {
            Self::Inline { data, .. } => usize::from(data[at]),
            Self::Heap { bytes, .. } => heap_end(bytes, at),
        }
    }
    pub(super) fn set_end(&mut self, at: usize, end: usize) {
        match self {
            Self::Inline { data, .. } => data[at] = end as u8,
            Self::Heap { bytes, .. } => {
                let start = at * std::mem::size_of::<usize>();
                bytes[start..start + std::mem::size_of::<usize>()]
                    .copy_from_slice(&end.to_ne_bytes());
            }
        }
    }
    pub(super) fn segment(&self, at: usize) -> &[u8] {
        // One dispatch per call: this is the sort comparison's hot path.
        match self {
            Self::Inline { count, data, .. } => {
                let start = if at == 0 {
                    usize::from(*count)
                } else {
                    usize::from(data[at - 1])
                };
                &data[start..usize::from(data[at])]
            }
            Self::Heap { bytes, count } => {
                let start = if at == 0 {
                    count * std::mem::size_of::<usize>()
                } else {
                    heap_end(bytes, at - 1)
                };
                &bytes[start..heap_end(bytes, at)]
            }
        }
    }
    /// Every segment's bytes, after the offset table.
    pub(super) fn payload(&self) -> &[u8] {
        &self.all()[self.header()..]
    }
    /// Appends segment `at`; inline records were sized for it by `new`.
    #[cfg(test)]
    fn append(&mut self, at: usize, value: &[u8], fold: bool) {
        let start = self.all().len();
        let end = start + value.len();
        let bytes = match self {
            Self::Inline { len, data, .. } => {
                debug_assert!(end <= INLINE, "inline record sized too small");
                data[start..end].copy_from_slice(value);
                *len = end as u8;
                &mut data[start..end]
            }
            Self::Heap { bytes, .. } => {
                bytes.extend_from_slice(value);
                &mut bytes[start..]
            }
        };
        if fold {
            bytes.make_ascii_uppercase();
        }
        self.set_end(at, end);
    }
}

pub(super) struct Packed {
    pub(super) segments: Segments,
    pub(super) keys: usize,
}
impl Keys for Packed {
    fn key_count(&self) -> usize {
        self.keys
    }
    fn segment(&self, at: usize) -> &[u8] {
        self.segments.segment(at)
    }
}
impl Storage for Packed {
    const ORIGINAL: bool = false;
    fn original(&self) -> Option<&[u8]> {
        None
    }
}
#[cfg(test)]
impl Build for Packed {
    fn build(
        raw: &[u8],
        _: &[u8],
        spans: &[Range<usize>],
        keys: usize,
        fold: bool,
    ) -> Result<Self, Failure> {
        let length = spans.iter().try_fold(0usize, |n, span| {
            n.checked_add(span.len()).ok_or_else(allocation)
        })?;
        let mut segments = Segments::new(spans.len(), length)?;
        for (at, span) in spans.iter().enumerate() {
            segments.append(at, &raw[span.clone()], fold && at < keys);
        }
        Ok(Self { segments, keys })
    }
}

pub(super) struct Original {
    pub(super) segments: Segments,
}
impl Keys for Original {
    fn key_count(&self) -> usize {
        self.segments.count() - 1
    }
    fn segment(&self, at: usize) -> &[u8] {
        self.segments.segment(at)
    }
}
impl Storage for Original {
    const ORIGINAL: bool = true;
    fn original(&self) -> Option<&[u8]> {
        Some(self.segment(self.key_count()))
    }
}
#[cfg(test)]
impl Build for Original {
    fn build(
        raw: &[u8],
        data: &[u8],
        spans: &[Range<usize>],
        keys: usize,
        fold: bool,
    ) -> Result<Self, Failure> {
        let length = spans[..keys].iter().try_fold(data.len(), |n, span| {
            n.checked_add(span.len()).ok_or_else(allocation)
        })?;
        let mut segments = Segments::new(keys.checked_add(1).ok_or_else(allocation)?, length)?;
        for (at, span) in spans[..keys].iter().enumerate() {
            segments.append(at, &raw[span.clone()], fold);
        }
        segments.append(keys, data, false);
        Ok(Self { segments })
    }
    fn language(keys: &[u8], ends: &[usize], original: &[u8]) -> Result<Self, Failure> {
        let count = ends.len().checked_add(1).ok_or_else(allocation)?;
        let length = keys
            .len()
            .checked_add(original.len())
            .ok_or_else(allocation)?;
        let mut segments = Segments::new(count, length)?;
        let mut start = 0;
        for (at, &end) in ends.iter().enumerate() {
            segments.append(at, &keys[start..end], false);
            start = end;
        }
        segments.append(ends.len(), original, false);
        Ok(Self { segments })
    }
}

#[cfg(test)]
// One-element span lists are single-field projections, not whole ranges.
#[allow(clippy::single_range_in_vec_init)]
mod tests {
    use super::*;

    #[test]
    fn a_sort_key_that_cannot_grow_refuses_and_otherwise_is_the_collators() {
        let mut policy = crate::locale::Policy::default();
        policy.collation = crate::locale::Collation::Language("de-DE");
        let collator = policy.collator().ok().unwrap().unwrap();
        let text = "\u{d6}lfeld und Stra\u{df}e, ".repeat(64);
        let mut expected = Vec::new();
        assert!(collator.write_key(&text, &mut expected).is_ok());
        let mut out = Vec::new();
        assert!(sort_key(&collator, &text, &mut out).is_ok());
        assert_eq!(out, expected);
        // Any write of the key can fail to grow the buffer; each refuses.
        for fail_at in 0..4 {
            crate::locale::KEY_WRITES_BEFORE_FAILURE.with(|count| count.set(Some(fail_at)));
            let mut out = Vec::new();
            let error = sort_key(&collator, &text, &mut out).err().unwrap();
            assert_eq!(error.message, allocation().message, "{fail_at}");
        }
        crate::locale::KEY_WRITES_BEFORE_FAILURE.with(|count| count.set(None));
    }

    #[test]
    fn a_sort_key_that_outgrows_its_room_is_the_collators_and_any_write_can_refuse() {
        let mut policy = crate::locale::Policy::default();
        policy.collation = crate::locale::Collation::Language("de-DE");
        let collator = policy.collator().ok().unwrap().unwrap();
        // An expanding ligature: far more than three key bytes per text byte.
        let text = "\u{fdfa}".repeat(8);
        let mut expected = Vec::new();
        assert!(collator.write_key(&text, &mut expected).is_ok());
        assert!(expected.len() > 3 * text.len() + 16, "{}", expected.len());
        // Appended after what the buffer holds, as a projection does.
        let mut out = b"held".to_vec();
        assert!(sort_key(&collator, &text, &mut out).is_ok());
        assert_eq!(out[..4], *b"held");
        assert_eq!(out[4..], expected);
        // Count the key's writes, then fail the first, a middle and the last.
        crate::locale::KEY_WRITES_BEFORE_FAILURE.with(|count| count.set(Some(usize::MAX)));
        assert!(sort_key(&collator, &text, &mut Vec::new()).is_ok());
        let writes = usize::MAX
            - crate::locale::KEY_WRITES_BEFORE_FAILURE.with(|count| count.get().unwrap());
        for fail_at in [0, writes / 2, writes - 1] {
            crate::locale::KEY_WRITES_BEFORE_FAILURE.with(|count| count.set(Some(fail_at)));
            let error = sort_key(&collator, &text, &mut Vec::new()).err().unwrap();
            assert_eq!(error.message, allocation().message, "{fail_at}");
        }
        crate::locale::KEY_WRITES_BEFORE_FAILURE.with(|count| count.set(None));
    }

    #[test]
    fn cached_sort_keys_equal_direct_keys() {
        let long = "k".repeat(SortKeys::LONGEST + 1);
        let mut texts: Vec<String> = (0..300).map(|n| format!("k{n:03}")).collect();
        // Precomposed and decomposed spellings of the same letter, and repeats.
        let extra = [
            "",
            "a-b",
            "A",
            "k001",
            "k001",
            "\u{d6}lfeld",
            "O\u{308}lfeld",
            &long,
            &long,
        ];
        texts.extend(extra.map(String::from));
        texts.extend([64, 65, 256, 257].map(|len| "x".repeat(len)));
        texts.push("\u{fdfa}".repeat(64));
        for collation in [
            crate::locale::Collation::Language("en-US"),
            crate::locale::Collation::Language("de-DE"),
        ] {
            let mut policy = crate::locale::Policy::default();
            policy.collation = collation;
            let collator = policy.collator().ok().unwrap().unwrap();
            let mut cache = SortKeys::default();
            for round in 0..2 {
                for text in &texts {
                    let mut direct = Vec::new();
                    assert!(collator.write_key(text, &mut direct).is_ok());
                    let mut cached = b"prefix".to_vec();
                    cache.write(&collator, text, &mut cached).ok().unwrap();
                    assert_eq!(&cached[6..], direct, "{text:?} round {round}");
                }
            }
        }
    }

    #[test]
    fn repeated_long_sort_keys_do_not_recompute_collation() {
        let mut policy = crate::locale::Policy::default();
        policy.collation = crate::locale::Collation::Language("en-US");
        let collator = policy.collator().ok().unwrap().unwrap();
        let text = "école".repeat(32);
        let mut cache = SortKeys::default();
        let mut first = Vec::new();
        cache.write(&collator, &text, &mut first).ok().unwrap();
        crate::locale::KEY_WRITES_BEFORE_FAILURE.with(|count| count.set(Some(0)));
        let mut second = b"prefix".to_vec();
        let result = cache.write(&collator, &text, &mut second);
        crate::locale::KEY_WRITES_BEFORE_FAILURE.with(|count| count.set(None));
        assert!(result.is_ok(), "a cached key must not call the collator");
        assert_eq!(&second[6..], first);
    }

    #[test]
    fn sampled_hash_collisions_compare_the_unsampled_middle() {
        let mut policy = crate::locale::Policy::default();
        policy.collation = crate::locale::Collation::Language("en-US");
        let collator = policy.collator().ok().unwrap().unwrap();
        let a = format!("{}a{}", "x".repeat(80), "é".repeat(40));
        let b = format!("{}b{}", "x".repeat(80), "é".repeat(40));
        assert_eq!(SortKeys::hash(a.as_bytes()), SortKeys::hash(b.as_bytes()));
        let mut cache = SortKeys::default();
        let mut keys = Vec::new();
        for text in [&a, &b, &b, &a, &a] {
            let mut direct = Vec::new();
            collator.write_key(text, &mut direct).ok().unwrap();
            let mut cached = Vec::new();
            cache.write(&collator, text, &mut cached).ok().unwrap();
            assert_eq!(cached, direct);
            keys.push(cached);
        }
        assert_ne!(keys[0], keys[1]);
    }

    #[test]
    fn cache_capacity_stays_within_its_budget_and_never_limits_a_key() {
        let mut policy = crate::locale::Policy::default();
        policy.collation = crate::locale::Collation::Language("en-US");
        let collator = policy.collator().ok().unwrap().unwrap();
        let metadata = SortKeys::SLOTS * std::mem::size_of::<(Vec<u8>, Vec<u8>)>();
        for budget in [
            0,
            metadata - 1,
            metadata,
            metadata + 128,
            SortKeys::MAX_BYTES,
        ] {
            let mut cache = SortKeys::with_budget(budget);
            for at in 0..768 {
                let text = format!("{at:03}{}", "\u{fdfa}".repeat(at % 86));
                let mut direct = Vec::new();
                collator.write_key(&text, &mut direct).ok().unwrap();
                let mut cached = Vec::new();
                cache.write(&collator, &text, &mut cached).ok().unwrap();
                assert_eq!(cached, direct, "budget {budget}, key {at}");
                let actual = cache.slots.capacity() * std::mem::size_of::<(Vec<u8>, Vec<u8>)>()
                    + cache
                        .slots
                        .iter()
                        .map(|(text, key)| text.capacity() + key.capacity())
                        .sum::<usize>();
                assert_eq!(cache.retained_bytes(), Some(actual));
                assert!(actual <= budget, "budget {budget}, retained {actual}");
            }
        }
    }

    fn heap_copy(packed: &Packed) -> Packed {
        let count = packed.segments.count();
        let mut segments = Segments::heap(count, packed.segments.payload().len())
            .ok()
            .unwrap();
        for at in 0..count {
            segments.append(at, packed.segment(at), false);
        }
        Packed {
            segments,
            keys: packed.keys,
        }
    }

    /// Records of every count and payload size around the inline limit read
    /// and compare exactly like their heap equivalents.
    #[test]
    fn inline_and_heap_segments_are_interchangeable() {
        let mut inline = 0;
        for count in 1..=INLINE + 1 {
            for payload in 0..=INLINE + 2 {
                let raw: Vec<u8> = (0..payload as u8).map(|b| b'a' + b % 26).collect();
                let cut = |at: usize| payload * at / count;
                let spans: Vec<_> = (0..count).map(|at| cut(at)..cut(at + 1)).collect();
                let keys = count.min(2);
                let packed = Packed::build(&raw, &raw, &spans, keys, true).ok().unwrap();
                let fits = count + payload <= INLINE;
                inline += usize::from(fits);
                assert_eq!(matches!(packed.segments, Segments::Inline { .. }), fits);
                for (at, span) in spans.iter().enumerate() {
                    let expected = &raw[span.clone()];
                    if at < keys {
                        assert_eq!(packed.segment(at), expected.to_ascii_uppercase());
                    } else {
                        assert_eq!(packed.segment(at), expected);
                    }
                }
                let heap = heap_copy(&packed);
                assert_eq!(packed.compare_keys(&heap), Ordering::Equal);
                assert_eq!(packed.segments.payload(), heap.segments.payload());
            }
        }
        // Most small counts and payloads fit inline; guard against a zero limit.
        assert!(inline > 200, "{inline}");
    }

    #[test]
    fn inline_and_heap_records_order_by_keys_then_key_count() {
        let build = |raw: &[u8], spans: &[Range<usize>], keys| {
            Packed::build(raw, raw, spans, keys, false).ok().unwrap()
        };
        let a = build(b"ab", &[0..1, 1..2], 1);
        let b = build(b"b", &[0..1], 1);
        let long = build(&[b'a'; 40], &[0..1, 1..40], 1);
        let two_keys = build(b"ab", &[0..1, 1..2], 2);
        assert!(matches!(long.segments, Segments::Heap { .. }));
        assert_eq!(a.compare_keys(&b), Ordering::Less);
        assert_eq!(heap_copy(&b).compare_keys(&a), Ordering::Greater);
        assert_eq!(a.compare_keys(&long), Ordering::Equal);
        assert_eq!(a.compare_keys(&two_keys), Ordering::Less);
        assert_eq!(heap_copy(&two_keys).compare_keys(&a), Ordering::Greater);
    }
}