wazabin-jstd 0.2.0

Typed identifiers, registries, graph containers with dominator/SESE analysis, string interning, and stable arenas
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
//! Dense payload storage with stable, never-reused logical identifiers.
//!
//! [`StableArena`] separates identity from physical position. Removing an entry
//! eagerly drops its payload with `swap_remove`, while a location table keeps
//! every surviving identifier valid. Unlike [`Registry`](crate::registry::Registry),
//! payload addresses are not stable across mutation.

use std::{
    fmt::{Debug, Formatter},
    iter::Zip,
    ops::{Index, IndexMut},
    slice, vec,
};

use serde::{Deserialize, Deserializer, Serialize, Serializer};

use crate::registry::{Identified, Identifier};

const DEAD: usize = usize::MAX;

/// Stable logical IDs over compact, dense physical payload storage.
#[derive(Clone, PartialEq, Eq)]
pub struct StableArena<Id: Identifier, T> {
    values: Vec<T>,
    slot_ids: Vec<Id>,
    locations: Vec<usize>,
}

impl<Id: Identifier, T> StableArena<Id, T> {
    /// Inserts `value` under a fresh, monotonic identifier.
    pub fn push(&mut self, value: T) -> Id {
        let raw = self.locations.len();
        assert!(raw < DEAD, "StableArena identifier space exhausted");
        let id = Id::from(raw);
        assert_eq!(
            Into::<usize>::into(id),
            raw,
            "StableArena identifier does not round-trip through its backing type"
        );

        let slot = self.values.len();
        assert!(slot < DEAD, "StableArena physical slot space exhausted");
        self.values.push(value);
        self.slot_ids.push(id);
        self.locations.push(slot);
        id
    }

    /// Returns the number of live payloads.
    pub fn len(&self) -> usize {
        self.values.len()
    }

    /// Returns the number of logical IDs issued, including removed IDs.
    pub fn issued_len(&self) -> usize {
        self.locations.len()
    }

    /// Returns the payload capacity reserved by the dense value vector.
    pub fn capacity(&self) -> usize {
        self.values.capacity()
    }

    /// Returns bytes reserved by the arena's three structural vectors.
    ///
    /// Allocations owned by `T` itself are deliberately excluded.
    pub fn structural_bytes(&self) -> usize {
        self.values
            .capacity()
            .saturating_mul(std::mem::size_of::<T>())
            .saturating_add(
                self.slot_ids
                    .capacity()
                    .saturating_mul(std::mem::size_of::<Id>()),
            )
            .saturating_add(
                self.locations
                    .capacity()
                    .saturating_mul(std::mem::size_of::<usize>()),
            )
    }

    /// Returns `true` when no live payload remains.
    pub fn is_empty(&self) -> bool {
        self.values.is_empty()
    }

    /// Releases structural capacity retained beyond the current live/issued
    /// lengths.
    ///
    /// Identifiers, liveness, and dense physical order are unchanged; only
    /// allocator capacity is returned. Intended for explicit end-of-mutation
    /// boundaries — shrinking after every removal would reallocate on the next
    /// growth.
    pub fn shrink_to_fit(&mut self) {
        self.values.shrink_to_fit();
        self.slot_ids.shrink_to_fit();
        self.locations.shrink_to_fit();
    }

    /// Returns whether `id` currently names a live payload.
    pub fn contains(&self, id: Id) -> bool {
        self.live_slot(id).is_some()
    }

    /// Returns the live payload identified by `id`, if any.
    pub fn get(&self, id: Id) -> Option<Identified<Id, &T>> {
        let slot = self.live_slot(id)?;
        Some(Identified::new(id, &self.values[slot]))
    }

    /// Returns the live payload identified by `id` mutably, if any.
    pub fn get_mut(&mut self, id: Id) -> Option<Identified<Id, &mut T>> {
        let slot = self.live_slot(id)?;
        Some(Identified::new(id, &mut self.values[slot]))
    }

    /// Removes and returns `id`'s payload while preserving every other ID.
    ///
    /// # Panics
    ///
    /// Panics when `id` is unknown or was already removed.
    pub fn remove(&mut self, id: Id) -> T {
        let raw = id.into();
        let slot = self.live_slot(id).unwrap_or_else(|| {
            panic!(
                "StableArena::remove: dead or unknown id {id:?} (issued {}, live {})",
                self.issued_len(),
                self.len()
            )
        });

        self.locations[raw] = DEAD;
        let removed = self.values.swap_remove(slot);
        let removed_id = self.slot_ids.swap_remove(slot);
        debug_assert_eq!(removed_id, id);

        if slot < self.values.len() {
            let moved_id = self.slot_ids[slot];
            self.locations[Into::<usize>::into(moved_id)] = slot;
        }
        removed
    }

    /// Iterates over live entries in dense physical order.
    pub fn iter(&self) -> Iter<'_, Id, T> {
        Iter {
            inner: self.slot_ids.iter().zip(self.values.iter()),
        }
    }

    /// Iterates mutably over live entries in dense physical order.
    pub fn iter_mut(&mut self) -> IterMut<'_, Id, T> {
        IterMut {
            inner: self.slot_ids.iter().zip(self.values.iter_mut()),
        }
    }

    #[inline]
    fn live_slot(&self, id: Id) -> Option<usize> {
        let slot = *self.locations.get(Into::<usize>::into(id))?;
        (slot != DEAD).then_some(slot)
    }
}

impl<Id: Identifier, T> Default for StableArena<Id, T> {
    fn default() -> Self {
        Self {
            values: Vec::new(),
            slot_ids: Vec::new(),
            locations: Vec::new(),
        }
    }
}

impl<Id: Identifier, T: Debug> Debug for StableArena<Id, T> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("StableArena")
            .field("issued", &self.issued_len())
            .field(
                "entries",
                &self
                    .iter()
                    .map(|entry| (entry.id, entry.inner))
                    .collect::<Vec<_>>(),
            )
            .finish()
    }
}

impl<Id: Identifier, T> Index<Id> for StableArena<Id, T> {
    type Output = T;

    fn index(&self, id: Id) -> &Self::Output {
        self.get(id).map(|entry| entry.inner).unwrap_or_else(|| {
            panic!(
                "StableArena index: dead or unknown id {id:?} (issued {}, live {})",
                self.issued_len(),
                self.len()
            )
        })
    }
}

impl<Id: Identifier, T> IndexMut<Id> for StableArena<Id, T> {
    fn index_mut(&mut self, id: Id) -> &mut Self::Output {
        let issued = self.issued_len();
        let live = self.len();
        self.get_mut(id)
            .map(|entry| entry.inner)
            .unwrap_or_else(|| {
                panic!(
                    "StableArena mutable index: dead or unknown id {id:?} (issued {issued}, live {live})"
                )
            })
    }
}

/// Immutable dense-order iterator.
pub struct Iter<'a, Id: Identifier, T> {
    inner: Zip<slice::Iter<'a, Id>, slice::Iter<'a, T>>,
}

impl<'a, Id: Identifier, T> Iterator for Iter<'a, Id, T> {
    type Item = Identified<Id, &'a T>;

    fn next(&mut self) -> Option<Self::Item> {
        let (id, value) = self.inner.next()?;
        Some(Identified::new(*id, value))
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        self.inner.size_hint()
    }
}

impl<Id: Identifier, T> ExactSizeIterator for Iter<'_, Id, T> {}

/// Mutable dense-order iterator.
pub struct IterMut<'a, Id: Identifier, T> {
    inner: Zip<slice::Iter<'a, Id>, slice::IterMut<'a, T>>,
}

impl<'a, Id: Identifier, T> Iterator for IterMut<'a, Id, T> {
    type Item = Identified<Id, &'a mut T>;

    fn next(&mut self) -> Option<Self::Item> {
        let (id, value) = self.inner.next()?;
        Some(Identified::new(*id, value))
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        self.inner.size_hint()
    }
}

impl<Id: Identifier, T> ExactSizeIterator for IterMut<'_, Id, T> {}

/// Consuming dense-order iterator.
pub struct IntoIter<Id: Identifier, T> {
    inner: Zip<vec::IntoIter<Id>, vec::IntoIter<T>>,
}

impl<Id: Identifier, T> Iterator for IntoIter<Id, T> {
    type Item = Identified<Id, T>;

    fn next(&mut self) -> Option<Self::Item> {
        let (id, value) = self.inner.next()?;
        Some(Identified::new(id, value))
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        self.inner.size_hint()
    }
}

impl<Id: Identifier, T> ExactSizeIterator for IntoIter<Id, T> {}

impl<'a, Id: Identifier, T> IntoIterator for &'a StableArena<Id, T> {
    type Item = Identified<Id, &'a T>;
    type IntoIter = Iter<'a, Id, T>;

    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}

impl<'a, Id: Identifier, T> IntoIterator for &'a mut StableArena<Id, T> {
    type Item = Identified<Id, &'a mut T>;
    type IntoIter = IterMut<'a, Id, T>;

    fn into_iter(self) -> Self::IntoIter {
        self.iter_mut()
    }
}

impl<Id: Identifier, T> IntoIterator for StableArena<Id, T> {
    type Item = Identified<Id, T>;
    type IntoIter = IntoIter<Id, T>;

    fn into_iter(self) -> Self::IntoIter {
        IntoIter {
            inner: self.slot_ids.into_iter().zip(self.values),
        }
    }
}

impl<Id: Identifier, T> FromIterator<T> for StableArena<Id, T> {
    fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
        let mut arena = Self::default();
        for value in iter {
            arena.push(value);
        }
        arena
    }
}

#[derive(Serialize)]
struct StableArenaWireRef<'a, T> {
    next_id: usize,
    entries: Vec<(usize, &'a T)>,
}

#[derive(Serialize, Deserialize)]
struct StableArenaWire<T> {
    next_id: usize,
    entries: Vec<(usize, T)>,
}

impl<Id: Identifier, T: Serialize> Serialize for StableArena<Id, T> {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        StableArenaWireRef {
            next_id: self.issued_len(),
            entries: self
                .slot_ids
                .iter()
                .copied()
                .map(Into::<usize>::into)
                .zip(self.values.iter())
                .collect(),
        }
        .serialize(serializer)
    }
}

impl<'de, Id: Identifier, T: Deserialize<'de>> Deserialize<'de> for StableArena<Id, T> {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let StableArenaWire { next_id, entries } = StableArenaWire::deserialize(deserializer)?;
        if next_id == DEAD {
            return Err(serde::de::Error::custom(
                "StableArena next ID exhausts the location sentinel",
            ));
        }
        if let Some(last_issued) = next_id.checked_sub(1)
            && Into::<usize>::into(Id::from(last_issued)) != last_issued
        {
            return Err(serde::de::Error::custom(format!(
                "StableArena next ID {next_id} exceeds its backing type"
            )));
        }

        let mut arena = Self {
            values: Vec::with_capacity(entries.len()),
            slot_ids: Vec::with_capacity(entries.len()),
            locations: vec![DEAD; next_id],
        };
        for (raw, value) in entries {
            if raw >= next_id {
                return Err(serde::de::Error::custom(format!(
                    "StableArena live ID {raw} is outside next ID {next_id}"
                )));
            }
            let id = Id::from(raw);
            if Into::<usize>::into(id) != raw {
                return Err(serde::de::Error::custom(format!(
                    "StableArena ID {raw} does not fit its backing type"
                )));
            }
            if arena.locations[raw] != DEAD {
                return Err(serde::de::Error::custom(format!(
                    "StableArena contains duplicate live ID {raw}"
                )));
            }
            let slot = arena.values.len();
            arena.locations[raw] = slot;
            arena.slot_ids.push(id);
            arena.values.push(value);
        }
        Ok(arena)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use jstd_derive::Identifier;

    #[derive(Identifier)]
    struct Id(u32);

    #[test]
    fn removal_repairs_moved_slot_and_never_reuses_ids() {
        let mut arena = StableArena::<Id, &str>::default();
        let a = arena.push("a");
        let b = arena.push("b");
        let c = arena.push("c");

        assert_eq!(arena.remove(b), "b");
        assert!(!arena.contains(b));
        assert_eq!(arena.get(b).map(|entry| **entry), None);
        assert_eq!(arena[c], "c");
        assert_eq!(arena[a], "a");
        assert_eq!(
            arena.iter().map(|entry| entry.id).collect::<Vec<_>>(),
            [a, c]
        );

        let d = arena.push("d");
        assert_eq!(usize::from(d), 3);
        assert_eq!(arena.issued_len(), 4);
        assert_eq!(arena.len(), 3);
    }

    #[test]
    fn removes_first_last_and_only_entries() {
        let mut arena = StableArena::<Id, i32>::default();
        let a = arena.push(1);
        let b = arena.push(2);
        let c = arena.push(3);
        assert_eq!(arena.remove(a), 1);
        assert_eq!(arena.remove(c), 3);
        assert_eq!(arena.remove(b), 2);
        assert!(arena.is_empty());
        assert_eq!(arena.issued_len(), 3);
    }

    #[test]
    #[should_panic(expected = "dead or unknown id")]
    fn indexing_removed_id_panics() {
        let mut arena = StableArena::<Id, i32>::default();
        let id = arena.push(1);
        arena.remove(id);
        let _ = arena[id];
    }

    #[test]
    #[should_panic(expected = "dead or unknown id")]
    fn double_remove_panics() {
        let mut arena = StableArena::<Id, i32>::default();
        let id = arena.push(1);
        arena.remove(id);
        arena.remove(id);
    }

    #[test]
    fn mutable_and_consuming_iteration_keep_stable_ids() {
        let mut arena = StableArena::<Id, i32>::default();
        let a = arena.push(1);
        let b = arena.push(2);
        let c = arena.push(3);
        arena.remove(b);
        for mut entry in &mut arena {
            **entry += 10;
        }
        let entries = arena
            .into_iter()
            .map(|entry| (entry.id, entry.inner))
            .collect::<Vec<_>>();
        assert_eq!(entries, [(a, 11), (c, 13)]);
    }

    #[test]
    fn shrink_to_fit_preserves_ids_order_and_liveness() {
        let mut arena = StableArena::<Id, u64>::default();
        let ids = (0..1_000).map(|step| arena.push(step)).collect::<Vec<_>>();
        for id in &ids[..900] {
            arena.remove(*id);
        }
        let before = arena.clone();
        arena.shrink_to_fit();
        assert_eq!(arena, before);
        assert!(arena.capacity() < 1_000);
        assert_eq!(arena.len(), 100);
        assert_eq!(arena.issued_len(), 1_000);
        assert_eq!(arena[ids[950]], 950);
        let next = arena.push(1_000);
        assert_eq!(usize::from(next), 1_000);
    }

    #[test]
    fn clone_and_equality_preserve_holes_and_physical_order() {
        let mut arena = StableArena::<Id, i32>::default();
        arena.push(1);
        let removed = arena.push(2);
        arena.push(3);
        arena.remove(removed);
        let clone = arena.clone();
        assert_eq!(clone, arena);
        assert_eq!(
            clone.iter().map(|entry| entry.id).collect::<Vec<_>>(),
            arena.iter().map(|entry| entry.id).collect::<Vec<_>>()
        );
    }

    #[test]
    fn serde_round_trip_preserves_holes_order_and_cursor() {
        let mut arena = StableArena::<Id, String>::default();
        let a = arena.push("a".into());
        let b = arena.push("b".into());
        let c = arena.push("c".into());
        arena.remove(a);
        arena.remove(b);
        let bytes = bincode::serde::encode_to_vec(&arena, bincode::config::standard()).unwrap();
        let (mut decoded, used): (StableArena<Id, String>, _) =
            bincode::serde::decode_from_slice(&bytes, bincode::config::standard()).unwrap();
        assert_eq!(used, bytes.len());
        assert_eq!(decoded, arena);
        assert_eq!(decoded[c], "c");
        let d = decoded.push("d".into());
        assert_eq!(usize::from(d), 3);

        decoded.remove(c);
        decoded.remove(d);
        let bytes = bincode::serde::encode_to_vec(&decoded, bincode::config::standard()).unwrap();
        let (empty, _): (StableArena<Id, String>, _) =
            bincode::serde::decode_from_slice(&bytes, bincode::config::standard()).unwrap();
        assert!(empty.is_empty());
        assert_eq!(empty.issued_len(), 4);
    }

    #[test]
    fn malformed_wire_is_rejected() {
        let duplicate = StableArenaWire {
            next_id: 2,
            entries: vec![(0, 1_i32), (0, 2)],
        };
        let bytes = bincode::serde::encode_to_vec(duplicate, bincode::config::standard()).unwrap();
        assert!(
            bincode::serde::decode_from_slice::<StableArena<Id, i32>, _>(
                &bytes,
                bincode::config::standard()
            )
            .is_err()
        );

        let outside = StableArenaWire {
            next_id: 1,
            entries: vec![(1, 1_i32)],
        };
        let bytes = bincode::serde::encode_to_vec(outside, bincode::config::standard()).unwrap();
        assert!(
            bincode::serde::decode_from_slice::<StableArena<Id, i32>, _>(
                &bytes,
                bincode::config::standard()
            )
            .is_err()
        );

        let too_wide = StableArenaWire {
            next_id: u32::MAX as usize + 2,
            entries: Vec::<(usize, i32)>::new(),
        };
        let bytes = bincode::serde::encode_to_vec(too_wide, bincode::config::standard()).unwrap();
        assert!(
            bincode::serde::decode_from_slice::<StableArena<Id, i32>, _>(
                &bytes,
                bincode::config::standard()
            )
            .is_err()
        );
    }

    #[test]
    fn deterministic_mixed_operations_match_option_vec_model() {
        let mut arena = StableArena::<Id, u64>::default();
        let mut model: Vec<Option<u64>> = Vec::new();
        let mut state = 0x1234_5678_9abc_def0_u64;

        for step in 0..2_000_u64 {
            state = state
                .wrapping_mul(6_364_136_223_846_793_005)
                .wrapping_add(1);
            let live = model
                .iter()
                .enumerate()
                .filter_map(|(id, value)| value.as_ref().map(|_| id))
                .collect::<Vec<_>>();
            if live.is_empty() || state & 3 != 0 {
                let id = arena.push(step);
                assert_eq!(usize::from(id), model.len());
                model.push(Some(step));
            } else {
                let raw = live[state as usize % live.len()];
                let expected = model[raw].take().unwrap();
                assert_eq!(arena.remove(Id::from(raw)), expected);
            }

            for (raw, expected) in model.iter().enumerate() {
                assert_eq!(
                    arena.get(Id::from(raw)).map(|entry| **entry),
                    *expected,
                    "model mismatch at step {step}, id {raw}"
                );
            }
        }
    }

    #[test]
    fn public_views_iteration_and_capacity_accounting_work() {
        let mut arena = StableArena::<Id, i32>::default();
        assert!(arena.is_empty());
        assert_eq!(arena.structural_bytes(), 0);

        let first = arena.push(1);
        let second = arena.push(2);
        let capacity_before = arena.capacity();
        assert!(arena.structural_bytes() >= capacity_before * std::mem::size_of::<i32>());
        assert_eq!(arena.get(first).unwrap().id, first);
        **arena.get_mut(second).unwrap() = 20;
        arena[first] = 10;
        assert_eq!(arena[first], 10);
        assert_eq!(arena[second], 20);

        let mut entries = arena.iter();
        assert_eq!(entries.size_hint(), (2, Some(2)));
        assert_eq!(entries.next().unwrap().id, first);
        assert_eq!(entries.next().unwrap().id, second);
        assert!(entries.next().is_none());
        assert!(format!("{arena:?}").contains("issued"));

        let rebuilt: StableArena<Id, i32> = [3, 4].into_iter().collect();
        assert_eq!(
            rebuilt.iter().map(|entry| *entry.inner).collect::<Vec<_>>(),
            [3, 4]
        );
    }
}