concinnity-memory 0.18.64

Allocation layer for Concinnity: tracking allocator, tagged budgets, arenas, pools
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
// concinnity-memory/src/inline_vec.rs
//
// A sequence that keeps its first element inline.
//
// Some populations hold exactly one item almost everywhere: an entity's draw
// slots (one per mesh, and most props are one mesh), a parent's children. A
// `Vec` charges each of those a heap block -- twenty thousand entities, twenty
// thousand blocks of four bytes -- and a pointer chase to reach it on every
// frame that walks them. Holding the first element inline removes both for the
// common case while keeping the heap for the rest, so a caller does not have to
// know which case it is in.
//
// Capacity one rather than a general N: one is expressible as an enum, which is
// why this file contains no `unsafe`. A general inline capacity needs
// `MaybeUninit` and a hand-written drop, and nothing has asked for it.
//
// Equality and ordering read the contents, never the representation: a
// one-element inline value equals a one-element spilled one.

use alloc::vec::Vec;
use core::fmt;
use core::ops::{Deref, DerefMut};

/// A sequence holding its first element inline and spilling to the heap beyond
/// that.
///
/// Derefs to `[T]`, so slice methods (`iter`, `len`, `contains`, indexing) are
/// available directly.
///
/// Spilled storage is kept once acquired: removing elements does not move the
/// remainder back inline, so a value that has grown never silently reallocates
/// when it shrinks.
pub struct InlineVec<T>(Repr<T>);

#[derive(Clone)]
enum Repr<T> {
    Empty,
    One(T),
    Spilled(Vec<T>),
}

impl<T> InlineVec<T> {
    /// An empty sequence, holding no allocation.
    pub const fn new() -> Self {
        Self(Repr::Empty)
    }

    /// A sequence holding exactly `value`, inline.
    pub const fn one(value: T) -> Self {
        Self(Repr::One(value))
    }

    // Whether the contents live on the heap rather than inline.
    #[cfg(test)]
    pub(crate) fn spilled(&self) -> bool {
        matches!(self.0, Repr::Spilled(_))
    }

    /// The contents as a slice.
    pub fn as_slice(&self) -> &[T] {
        match &self.0 {
            Repr::Empty => &[],
            Repr::One(value) => core::slice::from_ref(value),
            Repr::Spilled(values) => values,
        }
    }

    /// The contents as a mutable slice.
    pub fn as_mut_slice(&mut self) -> &mut [T] {
        match &mut self.0 {
            Repr::Empty => &mut [],
            Repr::One(value) => core::slice::from_mut(value),
            Repr::Spilled(values) => values,
        }
    }

    /// Append `value`, spilling to the heap if this is the second element.
    pub fn push(&mut self, value: T) {
        match &mut self.0 {
            Repr::Empty => self.0 = Repr::One(value),
            Repr::Spilled(values) => values.push(value),
            Repr::One(_) => {
                let Repr::One(first) = core::mem::replace(&mut self.0, Repr::Empty) else {
                    unreachable!("the arm above matched One")
                };
                self.0 = Repr::Spilled(alloc::vec![first, value]);
            }
        }
    }

    /// Remove and return the last element, or `None` when empty.
    pub fn pop(&mut self) -> Option<T> {
        match &mut self.0 {
            Repr::Empty => None,
            Repr::Spilled(values) => values.pop(),
            Repr::One(_) => match core::mem::replace(&mut self.0, Repr::Empty) {
                Repr::One(value) => Some(value),
                _ => unreachable!("the arm above matched One"),
            },
        }
    }

    /// Drop every element for which `keep` returns false.
    pub fn retain(&mut self, mut keep: impl FnMut(&T) -> bool) {
        match &mut self.0 {
            Repr::Empty => {}
            Repr::Spilled(values) => values.retain(|value| keep(value)),
            Repr::One(value) => {
                if !keep(value) {
                    self.0 = Repr::Empty;
                }
            }
        }
    }

    /// Drop every element and release any heap storage.
    pub fn clear(&mut self) {
        self.0 = Repr::Empty;
    }

    // The contents as an owned `Vec`, allocating when they were held inline.
    pub(crate) fn into_vec(self) -> Vec<T> {
        match self.0 {
            Repr::Empty => Vec::new(),
            Repr::One(value) => alloc::vec![value],
            Repr::Spilled(values) => values,
        }
    }
}

impl<T> Default for InlineVec<T> {
    fn default() -> Self {
        Self::new()
    }
}

impl<T: Clone> Clone for InlineVec<T> {
    fn clone(&self) -> Self {
        Self(self.0.clone())
    }
}

impl<T> Deref for InlineVec<T> {
    type Target = [T];

    fn deref(&self) -> &[T] {
        self.as_slice()
    }
}

impl<T> DerefMut for InlineVec<T> {
    fn deref_mut(&mut self) -> &mut [T] {
        self.as_mut_slice()
    }
}

// Contents, not representation: `One(x)` and `Spilled(vec![x])` hold the same
// sequence and must not be distinguishable.
impl<T: PartialEq> PartialEq for InlineVec<T> {
    fn eq(&self, other: &Self) -> bool {
        self.as_slice() == other.as_slice()
    }
}

impl<T: Eq> Eq for InlineVec<T> {}

impl<T: PartialEq<U>, U, const N: usize> PartialEq<[U; N]> for InlineVec<T> {
    fn eq(&self, other: &[U; N]) -> bool {
        self.as_slice() == other.as_slice()
    }
}

impl<T: PartialEq<U>, U> PartialEq<[U]> for InlineVec<T> {
    fn eq(&self, other: &[U]) -> bool {
        self.as_slice() == other
    }
}

impl<T: PartialEq<U>, U> PartialEq<Vec<U>> for InlineVec<T> {
    fn eq(&self, other: &Vec<U>) -> bool {
        self.as_slice() == other.as_slice()
    }
}

impl<T: fmt::Debug> fmt::Debug for InlineVec<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.as_slice().fmt(f)
    }
}

impl<T> From<Vec<T>> for InlineVec<T> {
    // Normalizes: a `Vec` of at most one element gives up its allocation.
    fn from(values: Vec<T>) -> Self {
        let mut values = values;
        match values.len() {
            0 => Self::new(),
            1 => Self(Repr::One(values.pop().expect("length is one"))),
            _ => Self(Repr::Spilled(values)),
        }
    }
}

impl<T, const N: usize> From<[T; N]> for InlineVec<T> {
    fn from(values: [T; N]) -> Self {
        values.into_iter().collect()
    }
}

impl<T> From<InlineVec<T>> for Vec<T> {
    fn from(inline: InlineVec<T>) -> Self {
        inline.into_vec()
    }
}

impl<T> FromIterator<T> for InlineVec<T> {
    fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
        let mut iter = iter.into_iter();
        let Some(first) = iter.next() else {
            return Self::new();
        };
        let Some(second) = iter.next() else {
            return Self(Repr::One(first));
        };
        let mut values = Vec::with_capacity(iter.size_hint().0 + 2);
        values.push(first);
        values.push(second);
        values.extend(iter);
        Self(Repr::Spilled(values))
    }
}

impl<T> Extend<T> for InlineVec<T> {
    fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
        for value in iter {
            self.push(value);
        }
    }
}

/// By-value iterator over an [`InlineVec`], produced by `into_iter`.
pub enum IntoIter<T> {
    #[doc(hidden)]
    Inline(core::option::IntoIter<T>),
    #[doc(hidden)]
    Spilled(alloc::vec::IntoIter<T>),
}

impl<T> Iterator for IntoIter<T> {
    type Item = T;

    fn next(&mut self) -> Option<T> {
        match self {
            Self::Inline(iter) => iter.next(),
            Self::Spilled(iter) => iter.next(),
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        match self {
            Self::Inline(iter) => iter.size_hint(),
            Self::Spilled(iter) => iter.size_hint(),
        }
    }
}

impl<T> ExactSizeIterator for IntoIter<T> {}

impl<T> IntoIterator for InlineVec<T> {
    type Item = T;
    type IntoIter = IntoIter<T>;

    fn into_iter(self) -> IntoIter<T> {
        match self.0 {
            Repr::Empty => IntoIter::Inline(None.into_iter()),
            Repr::One(value) => IntoIter::Inline(Some(value).into_iter()),
            Repr::Spilled(values) => IntoIter::Spilled(values.into_iter()),
        }
    }
}

impl<'a, T> IntoIterator for &'a InlineVec<T> {
    type Item = &'a T;
    type IntoIter = core::slice::Iter<'a, T>;

    fn into_iter(self) -> core::slice::Iter<'a, T> {
        self.as_slice().iter()
    }
}

impl<'a, T> IntoIterator for &'a mut InlineVec<T> {
    type Item = &'a mut T;
    type IntoIter = core::slice::IterMut<'a, T>;

    fn into_iter(self) -> core::slice::IterMut<'a, T> {
        self.as_mut_slice().iter_mut()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use alloc::vec;

    #[test]
    fn an_empty_value_holds_nothing_inline() {
        let empty = InlineVec::<u32>::new();
        assert!(empty.is_empty());
        assert_eq!(empty.len(), 0);
        assert!(!empty.spilled());
        assert_eq!(empty.as_slice(), &[] as &[u32]);
    }

    // The whole point: the common case must not reach the heap.
    #[test]
    fn a_single_element_stays_inline() {
        let mut one = InlineVec::new();
        one.push(7u32);
        assert!(!one.spilled(), "one element must not allocate");
        assert_eq!(one.as_slice(), [7]);
        assert_eq!(InlineVec::one(7u32).as_slice(), [7]);
    }

    #[test]
    fn a_second_element_spills_to_the_heap_in_order() {
        let mut many = InlineVec::new();
        many.push(1u32);
        many.push(2);
        many.push(3);
        assert!(many.spilled());
        assert_eq!(many.as_slice(), [1, 2, 3]);
    }

    #[test]
    fn collecting_picks_the_representation_from_the_length() {
        let none: InlineVec<u32> = core::iter::empty().collect();
        assert!(none.is_empty() && !none.spilled());

        let one: InlineVec<u32> = core::iter::once(5).collect();
        assert!(!one.spilled());
        assert_eq!(one.as_slice(), [5]);

        let many: InlineVec<u32> = (0..4).collect();
        assert!(many.spilled());
        assert_eq!(many.as_slice(), [0, 1, 2, 3]);
    }

    // A `Vec` handed in gives up an allocation it no longer needs, so a caller
    // building through `Vec` is not stuck with the block forever.
    #[test]
    fn a_short_vec_gives_up_its_allocation() {
        assert!(!InlineVec::from(vec![9u32]).spilled());
        assert!(InlineVec::from(vec![9u32, 10]).spilled());
        assert!(!InlineVec::<u32>::from(vec![]).spilled());
        assert_eq!(InlineVec::from(vec![9u32, 10]).as_slice(), [9, 10]);
    }

    // Equality is over contents. Both representations can hold one element, and
    // a caller must never be able to tell which it got.
    #[test]
    fn the_two_representations_compare_equal_on_equal_contents() {
        let inline = InlineVec::one(4u32);
        let mut spilled = InlineVec::new();
        spilled.push(4u32);
        spilled.push(5);
        spilled.pop();

        assert!(spilled.spilled() && !inline.spilled());
        assert_eq!(inline, spilled);
        assert_eq!(
            alloc::format!("{inline:?}"),
            alloc::format!("{spilled:?}"),
            "the representation must not show in Debug"
        );
    }

    #[test]
    fn retain_drops_the_inline_element_and_filters_the_spilled_one() {
        let mut one = InlineVec::one(1u32);
        one.retain(|&v| v != 1);
        assert!(one.is_empty());

        let mut many: InlineVec<u32> = (0..6).collect();
        many.retain(|v| v % 2 == 0);
        assert_eq!(many.as_slice(), [0, 2, 4]);
    }

    // Documented behaviour: shrinking keeps the block rather than reallocating.
    #[test]
    fn a_shrunk_value_keeps_its_heap_storage_until_cleared() {
        let mut many: InlineVec<u32> = (0..3).collect();
        many.retain(|&v| v == 0);
        assert!(many.spilled());
        assert_eq!(many.as_slice(), [0]);

        many.clear();
        assert!(!many.spilled());
        assert!(many.is_empty());
    }

    #[test]
    fn pop_returns_elements_from_the_end_of_both_representations() {
        let mut one = InlineVec::one(1u32);
        assert_eq!(one.pop(), Some(1));
        assert_eq!(one.pop(), None);

        let mut many: InlineVec<u32> = (0..3).collect();
        assert_eq!(many.pop(), Some(2));
        assert_eq!(many.pop(), Some(1));
        assert_eq!(many.pop(), Some(0));
        assert_eq!(many.pop(), None);
    }

    #[test]
    fn iteration_covers_every_representation() {
        let cases = [
            InlineVec::<u32>::new(),
            InlineVec::one(1),
            (1..4).collect::<InlineVec<u32>>(),
        ];
        let expected: [&[u32]; 3] = [&[], &[1], &[1, 2, 3]];

        for (case, want) in cases.into_iter().zip(expected) {
            assert_eq!(case.iter().copied().collect::<Vec<_>>(), want);
            assert_eq!((&case).into_iter().copied().collect::<Vec<_>>(), want);
            assert_eq!(case.clone().into_vec(), want);
            assert_eq!(case.into_iter().collect::<Vec<_>>(), want);
        }
    }

    #[test]
    fn mutable_access_reaches_both_representations() {
        let mut one = InlineVec::one(1u32);
        for value in &mut one {
            *value += 1;
        }
        assert_eq!(one.as_slice(), [2]);

        let mut many: InlineVec<u32> = (0..3).collect();
        many.as_mut_slice()[1] = 9;
        assert_eq!(many.as_slice(), [0, 9, 2]);
    }

    #[test]
    fn extend_grows_through_the_same_spill_rule() {
        let mut values = InlineVec::new();
        values.extend([1u32]);
        assert!(!values.spilled());
        values.extend([2u32, 3]);
        assert_eq!(values.as_slice(), [1, 2, 3]);
    }

    // The inline element rides in the `Vec` pointer's niche, so a column of
    // these is no wider than a column of `Vec`s and the saved heap block is not
    // paid for in inline bytes. A representation change that broke the niche
    // would grow every component column that holds one, silently.
    #[test]
    fn the_inline_element_costs_no_more_than_the_vec_it_replaces() {
        assert_eq!(
            size_of::<InlineVec<u32>>(),
            size_of::<Vec<u32>>(),
            "the inline case must ride in the Vec's niche"
        );
        assert_eq!(size_of::<InlineVec<u64>>(), size_of::<Vec<u64>>());
        assert_eq!(
            size_of::<InlineVec<(u32, u32)>>(),
            size_of::<Vec<(u32, u32)>>()
        );
    }

    // Comparing against a literal is what call sites and their tests do, so it
    // reads the same whether the value spilled or not.
    #[test]
    fn arrays_and_vecs_convert_and_compare_across_both_representations() {
        let one: InlineVec<u32> = [1].into();
        let many: InlineVec<u32> = [1, 2].into();
        assert!(!one.spilled() && many.spilled());

        assert_eq!(one, [1]);
        assert_eq!(many, [1, 2]);
        assert_eq!(many, vec![1u32, 2]);
        assert_eq!(many, *[1u32, 2].as_slice());
        assert_ne!(one, [2]);
        assert_ne!(one, [1, 2]);
    }

    // Slice methods arrive through `Deref`, so call sites that held a `Vec`
    // keep reading the same way.
    #[test]
    fn slice_methods_are_available_through_deref() {
        let values: InlineVec<u32> = (10..13).collect();
        assert!(values.contains(&11));
        assert_eq!(values[2], 12);
        assert_eq!(values.first(), Some(&10));
        assert_eq!(values.to_vec(), vec![10, 11, 12]);
    }

    // Ownership is real on both paths: dropping must run element destructors
    // exactly once whether or not the value spilled.
    #[test]
    fn dropping_runs_element_destructors_once() {
        use alloc::rc::Rc;

        let witness = Rc::new(());
        let mut values = InlineVec::new();
        values.push(Rc::clone(&witness));
        assert_eq!(Rc::strong_count(&witness), 2);

        values.push(Rc::clone(&witness));
        assert_eq!(Rc::strong_count(&witness), 3, "the spill must not leak");

        drop(values);
        assert_eq!(Rc::strong_count(&witness), 1);
    }
}