merc_aterm 3.0.0

A thread-safe library to manipulate and store first-order terms.
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
use std::cell::UnsafeCell;
use std::fmt;
use std::sync::Arc;
use std::sync::LazyLock;
use std::sync::Mutex;
use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering;
use std::time::Instant;

use log::debug;
use rustc_hash::FxHashMap;
use rustc_hash::FxHashSet;

use merc_io::LargeFormatter;
use merc_sharedmutex::GlobalBfSharedMutex;
use merc_sharedmutex::RecursiveLockReadGuard;
use merc_unsafety::ProtectionSet;
use merc_unsafety::StablePointer;
use merc_utilities::debug_trace;

use crate::ATermIndex;
use crate::ATermRef;
use crate::Markable;
use crate::Symb;
use crate::Symbol;
use crate::SymbolIndex;
use crate::SymbolRef;
use crate::Term;
use crate::storage::ATermStorage;
use crate::storage::SharedTerm;
use crate::storage::SymbolPool;

/// This is the global set of protection sets that are managed by the [crate::storage::ThreadTermPool].
pub(crate) static GLOBAL_TERM_POOL: LazyLock<GlobalBfSharedMutex<GlobalTermPool>> =
    LazyLock::new(|| GlobalBfSharedMutex::new(GlobalTermPool::new()));

/// Enables aggressive garbage collection, which is used for testing.
pub(crate) const AGGRESSIVE_GC: bool = false;

/// The number of terms a thread pool reserves from the shared budget at a time, see
/// [crate::storage::ThreadTermPool].
pub(crate) const GC_BUDGET_CHUNK: usize = 4096;

/// A type alias for the global term pool guard
pub(crate) type GlobalTermPoolGuard<'a> = RecursiveLockReadGuard<'a, GlobalTermPool>;

/// The `Mutex`-guarded, globally-scanned protection set. One is created per
/// registered thread pool.
pub(crate) type SendContainerProtectionSet = Arc<Mutex<ProtectionSet<Arc<dyn Markable + Sync + Send>>>>;

/// The single global (singleton) term pool, accessed via [GLOBAL_TERM_POOL].
pub(crate) struct GlobalTermPool {
    /// Unique table of all terms with stable pointers for references
    terms: ATermStorage,
    /// The symbol pool for managing function symbols.
    symbol_pool: SymbolPool,
    /// The thread-specific protection sets.
    thread_pools: ThreadPoolList,
    /// A separate protection set for sendable terms.
    send_term_protection_sets: Vec<Option<Arc<Mutex<ProtectionSet<ATermIndex>>>>>,
    /// A separate protection set for sendable containers.
    send_container_protection_sets: Vec<Option<SendContainerProtectionSet>>,
    /// Term roots adopted from thread-local protection sets when their owning thread exits while
    /// the terms are still reachable. Deduplicated so repeatedly leaking the same shared term
    /// (for example the default data symbols) does not grow this set unboundedly.
    orphan_term_protection_set: FxHashSet<ATermIndex>,
    /// Symbol roots adopted from thread-local protection sets during thread teardown, deduplicated.
    orphan_symbol_protection_set: FxHashSet<SymbolIndex>,
    /// Container roots adopted from thread-local protection sets during thread teardown, keyed by
    /// the container's [Arc] pointer address so the same container is retained at most once.
    orphan_container_protection_set: FxHashMap<usize, Arc<dyn Markable + Sync + Send>>,

    // Data structures used for garbage collection
    /// A stack used to mark terms recursively.
    stack: Vec<ATermIndex>,

    /// Indicates whether automatic garbage collection is enabled.
    garbage_collection: bool,

    /// The number of terms that may still be created before garbage collection should be
    /// triggered. Set to roughly `capacity - len` after each collection and consumed by the
    /// thread pools in [GC_BUDGET_CHUNK] sized chunks to avoid contention on this counter.
    gc_budget: AtomicUsize,

    /// Default terms
    int_symbol: SymbolRef<'static>,
    empty_list_symbol: SymbolRef<'static>,
    list_symbol: SymbolRef<'static>,
}

impl GlobalTermPool {
    fn new() -> GlobalTermPool {
        // Insert the default symbols, mirrors the symbols defined in mCRL2.
        // Only aterm_int has to be reserved since it behaves special with the
        // integer terms, the other two are just normal symbols.
        let mut symbol_pool = SymbolPool::new();

        // SAFETY: the default symbols are marked on every collection (see `collect_garbage`),
        // so these indices stay valid for the lifetime of the pool.
        let int_symbol = unsafe { SymbolRef::from_index(&symbol_pool.create_reserved("<aterm_int>", 0)) };
        let list_symbol = unsafe { SymbolRef::from_index(&symbol_pool.create("<list_constructor>", 2)) };
        let empty_list_symbol = unsafe { SymbolRef::from_index(&symbol_pool.create("<empty_list>", 0)) };

        let pool = GlobalTermPool {
            terms: ATermStorage::new(),
            symbol_pool,
            thread_pools: ThreadPoolList(Vec::new()),
            send_term_protection_sets: Vec::new(),
            send_container_protection_sets: Vec::new(),
            orphan_term_protection_set: FxHashSet::default(),
            orphan_symbol_protection_set: FxHashSet::default(),
            orphan_container_protection_set: FxHashMap::default(),
            stack: Vec::new(),
            garbage_collection: true,
            gc_budget: AtomicUsize::new(0),
            int_symbol,
            list_symbol,
            empty_list_symbol,
        };

        // Initialise the budget from the free capacity of the freshly created storage.
        pool.reset_gc_budget();
        pool
    }

    /// Returns the number of terms in the pool.
    pub fn len(&self) -> usize {
        self.terms.len()
    }

    /// Returns whether the term pool is empty.
    #[allow(dead_code)]
    pub(crate) fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Creates a term storing a single integer value.
    ///
    /// Crate-private: the returned pointer is unprotected, so the caller must protect it
    /// (or hand it to a [crate::Return]) before the read guard it was created under drops.
    pub(crate) fn create_int(&self, value: usize) -> (StablePointer<SharedTerm>, bool) {
        // SAFETY: `int_symbol` is one of the default symbols, which are marked on every
        // collection, so the index stays valid for the pool's lifetime.
        let (index, inserted) = unsafe {
            self.terms
                .insert_int_term(SymbolRef::from_index(self.int_symbol.shared()), value)
        };

        (index, inserted)
    }

    /// Create a term from a head symbol and an iterator over its arguments
    ///
    /// Crate-private: the returned pointer is unprotected, see [Self::create_int].
    pub(crate) fn create_term_array<'a, 'b, 'c, S: Symb<'a, 'b>>(
        &'c self,
        symbol: &'b S,
        args: &'c [ATermRef<'c>],
    ) -> (StablePointer<SharedTerm>, bool) {
        self.terms.insert(symbol, args)
    }

    /// Create a term of arity at most [crate::storage::aterm_storage::MAX_FIXED_ARITY] directly
    /// from the given argument slice, without an intermediate `ATermRef` buffer.
    ///
    /// Crate-private: the returned pointer is unprotected, see [Self::create_int].
    pub(crate) fn create_term_fixed<'a, 'b, 'c, 'd, S, T>(
        &self,
        symbol: &'b S,
        args: &[T],
    ) -> (StablePointer<SharedTerm>, bool)
    where
        S: Symb<'a, 'b>,
        T: Term<'c, 'd>,
    {
        self.terms.insert_fixed(symbol, args)
    }

    /// Create a term of arity at most [crate::storage::aterm_storage::MAX_FIXED_ARITY] straight
    /// from an iterator over argument indices, see [Self::create_term_fixed].
    ///
    /// Crate-private: the returned pointer is unprotected, see [Self::create_int].
    pub(crate) fn create_term_fixed_iter<'a, 'b, S, I>(
        &self,
        symbol: &'b S,
        args: I,
    ) -> (StablePointer<SharedTerm>, bool)
    where
        S: Symb<'a, 'b>,
        I: Iterator<Item = ATermIndex>,
    {
        self.terms.insert_fixed_iter(symbol, args)
    }

    /// Create a function symbol
    ///
    /// Crate-private: `protect` receives an unprotected index, see [Self::create_int].
    ///
    /// # Panics
    ///
    /// Panics if `name`/`arity` collide with one of the pool's own reserved
    /// marker symbols (e.g. `<aterm_int>`) — this should never happen for a
    /// legitimate caller, since those names are internal implementation details.
    pub(crate) fn create_symbol<P>(&self, name: &str, arity: usize, protect: P) -> Symbol
    where
        P: FnOnce(SymbolIndex) -> Symbol,
    {
        protect(self.symbol_pool.create(name, arity))
    }

    /// Create a function symbol that did not exist yet, see [SymbolPool::create_unused].
    ///
    /// Crate-private: `protect` receives an unprotected index, as for [Self::create_symbol].
    pub(crate) fn create_unused_symbol<P>(&self, name: &str, arity: usize, protect: P) -> Option<Symbol>
    where
        P: FnOnce(SymbolIndex) -> Symbol,
    {
        self.symbol_pool.create_unused(name, arity).map(protect)
    }

    /// Registers a new thread term pool.
    ///
    /// # Safety
    ///
    /// Note that the returned `Arc<UnsafeCell<...>>` is not Send or Sync, so it
    /// *must* be protected through other means.
    #[allow(clippy::arc_with_non_send_sync)]
    #[allow(clippy::type_complexity)]
    pub(crate) fn register_thread_term_pool(
        &mut self,
    ) -> (
        Arc<UnsafeCell<SharedTermProtection>>,
        Arc<Mutex<ProtectionSet<ATermIndex>>>,
        SendContainerProtectionSet,
    ) {
        let protection = Arc::new(UnsafeCell::new(SharedTermProtection {
            term_protection_set: ProtectionSet::new(),
            symbol_protection_set: ProtectionSet::new(),
            container_protection_set: ProtectionSet::new(),
            index: self.thread_pools.len(),
        }));

        debug!("Registered thread_local protection set(s) {}", self.thread_pools.len());
        self.thread_pools.push(Some(protection.clone()));

        let send_term_protection_set = Arc::new(Mutex::new(ProtectionSet::new()));
        self.send_term_protection_sets
            .push(Some(send_term_protection_set.clone()));

        let send_container_protection_set = Arc::new(Mutex::new(ProtectionSet::new()));
        self.send_container_protection_sets
            .push(Some(send_container_protection_set.clone()));

        (protection, send_term_protection_set, send_container_protection_set)
    }

    /// Deregisters a thread pool.
    ///
    /// The `send_term_protection_sets` and `send_container_protection_sets` slots are
    /// deliberately left in place: a still-live `ATermSend` or `ProtectedSend` created on this
    /// thread may outlive it and must keep being marked.
    pub(crate) fn deregister_thread_pool(&mut self, index: usize) {
        debug!("Removed thread_local protection set(s) {index}");
        if let Some(entry) = self.thread_pools.get_mut(index) {
            *entry = None;
        }
    }

    /// Adopts the term, symbol, and container roots that were still protected in a thread-local
    /// set when its thread exited. Deduplicated, so repeatedly leaking the same shared roots does
    /// not grow the orphan sets unboundedly.
    pub(crate) fn protect_orphan_roots(&mut self, protection: &SharedTermProtection) {
        for (_root, term) in protection.term_protection_set.iter() {
            // SAFETY: the value is copied from a currently protected root; the copy is owned by
            // the orphan set, which keeps the term alive as an additional GC root.
            self.orphan_term_protection_set.insert(unsafe { term.copy() });
        }

        for (_root, symbol) in protection.symbol_protection_set.iter() {
            // SAFETY: the value is copied from a currently protected symbol root; the copy is
            // owned by the orphan set, which keeps the symbol alive as an additional GC root.
            self.orphan_symbol_protection_set.insert(unsafe { symbol.copy() });
        }

        for (_root, container) in protection.container_protection_set.iter() {
            let key = Arc::as_ptr(container) as *const () as usize;
            self.orphan_container_protection_set
                .entry(key)
                .or_insert_with(|| container.clone());
        }
    }

    /// Triggers garbage collection if necessary and refreshes the global budget.
    pub(crate) fn trigger_garbage_collection(&mut self) {
        if self.garbage_collection {
            // Garbage collection is enabled.
            self.collect_garbage();
        }

        self.reset_gc_budget();
    }

    /// Recomputes the global GC budget from the free storage capacity.
    pub(crate) fn reset_gc_budget(&self) {
        let budget = if AGGRESSIVE_GC {
            1
        } else {
            self.terms.capacity().saturating_sub(self.len()).max(1)
        };

        self.gc_budget.store(budget, Ordering::Relaxed);
    }

    /// Subtracts `amount` from the global GC budget, saturating at zero so it never wraps, and
    /// returns the budget as it was *before* the subtraction. A returned value not greater than
    /// `amount` means the budget is exhausted and a collection should be triggered.
    pub(crate) fn consume_gc_budget(&self, amount: usize) -> usize {
        #[allow(deprecated)] // Requires MSRV 1.99.0
        self.gc_budget
            .fetch_update(Ordering::Relaxed, Ordering::Relaxed, |current| {
                Some(current.saturating_sub(amount))
            })
            .expect("the update closure always returns Some")
    }

    /// Enables or disables automatic garbage collection.
    pub fn automatic_garbage_collection(&mut self, enabled: bool) {
        self.garbage_collection = enabled;
    }

    /// Collects garbage terms.
    pub fn collect_garbage(&mut self) {
        let mark_time = Instant::now();
        self.mark_roots();
        let mark_time_elapsed = mark_time.elapsed();
        let collect_time = Instant::now();

        let (removed_terms, removed_symbols) = self.sweep_terms_and_symbols();

        debug!(
            "Garbage collection: marking took {}ms, collection took {}ms, {} terms and {} symbols removed",
            mark_time_elapsed.as_millis(),
            collect_time.elapsed().as_millis(),
            removed_terms,
            removed_symbols
        );

        debug!("{}", self.metrics());

        // Print information from the protection sets.
        for pool in self.thread_pools.iter().flatten() {
            // SAFETY: We have exclusive access to the global term pool, so no other thread can modify the protection sets.
            let pool = unsafe { &mut *pool.get() };
            debug!("{}", pool.metrics());
        }

        // Clear marking data structures
        self.stack.clear();
    }

    /// Marks the default symbols and every root in every protection set as reachable,
    /// and reclaims protection sets of threads that have exited.
    fn mark_roots(&mut self) {
        // Mark the default symbols
        let mut marker = Marker { stack: &mut self.stack };
        marker.mark_symbol(&self.int_symbol);
        marker.mark_symbol(&self.list_symbol);
        marker.mark_symbol(&self.empty_list_symbol);

        // Loop through all protection sets and mark the terms.
        for pool in self.thread_pools.iter().flatten() {
            // SAFETY: We have exclusive access to the global term pool, so no other thread can modify the protection sets.
            let pool = unsafe { &mut *pool.get() };

            for (_root, symbol) in pool.symbol_protection_set.iter() {
                debug_trace!("Marking root {_root} symbol {symbol:?}");
                // SAFETY: the protection set keeps the symbol alive.
                unsafe { symbol.deref() }.mark();
            }

            for (_root, term) in pool.term_protection_set.iter() {
                debug_trace!("Marking root {_root} term {term:?}");
                unsafe {
                    ATermRef::from_index(term).mark(&mut marker);
                }
            }

            // Marking a `Protected` container goes through `GcMutex::lock`, which takes a
            // `read_recursive` on the same lock we already hold for writing here.
            for (_, container) in pool.container_protection_set.iter() {
                container.mark(&mut marker);
            }
        }

        for pool in self.send_term_protection_sets.iter().flatten() {
            let pool = pool.lock().expect("Lock poisoned!");
            for (_root, term) in pool.iter() {
                debug_trace!("Marking sendable term {term:?}");
                unsafe {
                    ATermRef::from_index(term).mark(&mut marker);
                }
            }
        }

        // As for the per-thread `container_protection_set` above, marking a `ProtectedSend`
        // container goes through `GcMutex::lock`, which takes a `read_recursive` on the same
        // lock we already hold for writing here.
        for pool in self.send_container_protection_sets.iter().flatten() {
            let pool = pool.lock().expect("Lock poisoned!");
            for (_, container) in pool.iter() {
                debug_trace!("Marking sendable container");
                container.mark(&mut marker);
            }
        }

        for term in &self.orphan_term_protection_set {
            debug_trace!("Marking orphaned term {term:?}");
            unsafe {
                ATermRef::from_index(term).mark(&mut marker);
            }
        }

        for symbol in &self.orphan_symbol_protection_set {
            debug_trace!("Marking orphaned symbol {symbol:?}");
            // SAFETY: the orphan set keeps the symbol alive.
            unsafe { symbol.deref() }.mark();
        }

        for container in self.orphan_container_protection_set.values() {
            debug_trace!("Marking orphaned container");
            container.mark(&mut marker);
        }

        // Reclaim send-term protection sets whose owning thread has exited and that hold no
        // outstanding ATermSend.
        for slot in self.send_term_protection_sets.iter_mut() {
            if slot.as_ref().is_some_and(|set| Arc::strong_count(set) == 1) {
                *slot = None;
            }
        }

        // Same, for send-container protection sets with no outstanding ProtectedSend.
        for slot in self.send_container_protection_sets.iter_mut() {
            if slot.as_ref().is_some_and(|set| Arc::strong_count(set) == 1) {
                *slot = None;
            }
        }
    }

    /// Removes every term and symbol that was not marked by [`Self::mark_roots`], and
    /// returns how many of each were removed.
    fn sweep_terms_and_symbols(&mut self) -> (usize, usize) {
        let num_of_terms = self.len();
        let num_of_symbols = self.symbol_pool.len();

        // Delete all terms that are not marked.
        // SAFETY: Marking visited every root in every protection set while holding the exclusive
        // lock, so unmarked terms have no live references that could be dereferenced afterwards.
        unsafe {
            self.terms.retain(|term| {
                let shared = term.deref();
                if !shared.is_marked() {
                    debug_trace!("Dropping term: {:?}", term);
                    return false;
                }

                // Leave no marks behind for the next collection.
                shared.unmark();
                true
            });
        }

        // We ensure that every removed symbol is not used anymore.
        // SAFETY: Unmarked symbols are not referenced by any marked term or protection set root.
        unsafe {
            self.symbol_pool.retain(|symbol| {
                let shared = symbol.deref();
                if !shared.is_marked() {
                    debug_trace!("Dropping symbol: {:?}", symbol);
                    return false;
                }

                // Leave no marks behind for the next collection.
                shared.unmark();
                true
            });
        }

        (num_of_terms - self.len(), num_of_symbols - self.symbol_pool.len())
    }

    /// Returns the metrics of the term pool, can be formatted and written to output.
    pub fn metrics(&self) -> TermPoolMetrics<'_> {
        TermPoolMetrics(self)
    }

    /// Returns integer function symbol.
    pub(crate) fn get_int_symbol(&self) -> &SymbolRef<'static> {
        &self.int_symbol
    }

    /// Returns integer function symbol.
    pub(crate) fn get_list_symbol(&self) -> &SymbolRef<'static> {
        &self.list_symbol
    }

    /// Returns integer function symbol.
    pub(crate) fn get_empty_list_symbol(&self) -> &SymbolRef<'static> {
        &self.empty_list_symbol
    }
}

pub(crate) struct TermPoolMetrics<'a>(&'a GlobalTermPool);

impl fmt::Display for TermPoolMetrics<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "There are {} terms, and {} symbols",
            self.0.terms.len(),
            self.0.symbol_pool.len()
        )
    }
}

/// A newtype wrapping the per-thread protection-set list stored inside
/// [`GlobalTermPool`].
///
/// # Safety
///
/// Note that [`UnsafeCell`] is not [`Sync`], but we explicitly only use this in
/// `&mut self` contexts, so we can safely implement `Sync` for this wrapper.
struct ThreadPoolList(Vec<Option<Arc<UnsafeCell<SharedTermProtection>>>>);

// SAFETY: See the safety documentation on `ThreadPoolList`.
unsafe impl Sync for ThreadPoolList {}
unsafe impl Send for ThreadPoolList {}

impl std::ops::Deref for ThreadPoolList {
    type Target = Vec<Option<Arc<UnsafeCell<SharedTermProtection>>>>;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl std::ops::DerefMut for ThreadPoolList {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

/// A struct that contains the protection sets for a thread, as well as the
/// index of the thread pool in the global term pool.
pub(crate) struct SharedTermProtection {
    /// Protection set for terms
    pub term_protection_set: ProtectionSet<ATermIndex>,
    /// Protection set to prevent garbage collection of symbols
    pub symbol_protection_set: ProtectionSet<SymbolIndex>,
    /// Protection set for containers
    pub container_protection_set: ProtectionSet<Arc<dyn Markable + Sync + Send>>,
    /// Index in global pool's thread pools list
    pub index: usize,
}

impl SharedTermProtection {
    /// Returns the metrics of the term pool, can be formatted and written to output.
    pub fn metrics(&self) -> ProtectionMetrics<'_> {
        ProtectionMetrics(self)
    }
}

/// A struct that can be used to print the performance of the protection sets.
pub(crate) struct ProtectionMetrics<'a>(&'a SharedTermProtection);

impl fmt::Display for ProtectionMetrics<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        writeln!(
            f,
            "Protection set {} has {} roots, max {} and {} insertions",
            self.0.index,
            LargeFormatter(self.0.term_protection_set.len()),
            LargeFormatter(self.0.term_protection_set.maximum_size()),
            LargeFormatter(self.0.term_protection_set.number_of_insertions())
        )?;

        writeln!(
            f,
            "Containers: {} roots, max {} and {} insertions",
            LargeFormatter(self.0.container_protection_set.len()),
            LargeFormatter(self.0.container_protection_set.maximum_size()),
            LargeFormatter(self.0.container_protection_set.number_of_insertions()),
        )?;

        write!(
            f,
            "Symbols: {} roots, max {} and {} insertions",
            LargeFormatter(self.0.symbol_protection_set.len()),
            LargeFormatter(self.0.symbol_protection_set.maximum_size()),
            LargeFormatter(self.0.symbol_protection_set.number_of_insertions()),
        )
    }
}

/// Helper struct to pass private data required to mark term recursively.
pub struct Marker<'a> {
    stack: &'a mut Vec<ATermIndex>,
}

impl Marker<'_> {
    // Marks the given term as being reachable.
    pub fn mark(&mut self, term: &ATermRef<'_>) {
        // SAFETY: all copies below go into the work stack, which is drained before the
        // collection pass ends, and a marked term or symbol is by definition retained by
        // the sweep; `term` itself is alive for the borrow.
        unsafe {
            // A term is marked when it is pushed, so that a shared subterm is pushed only once.
            if term.shared().deref().mark() {
                self.stack.push(term.shared().copy());

                while let Some(term) = self.stack.pop() {
                    // Reconstruct the wide reference once; each `deref` rebuilds it from the
                    // symbol header, so we reuse the borrow for both the symbol and arguments.
                    let shared = term.deref();

                    // Mark the function symbol.
                    shared.symbol().shared().deref().mark();

                    // The arguments slice already has exactly the symbol's arity as its length
                    // (integer terms reconstruct to an empty slice), so iterating it is correct.
                    for arg in shared.arguments().iter() {
                        if arg.shared().deref().mark() {
                            self.stack.push(arg.shared().copy());
                        }
                    }
                }
            }
        }
    }

    /// Marks the given symbol as being reachable.
    pub fn mark_symbol(&mut self, symbol: &SymbolRef<'_>) {
        // SAFETY: `symbol` is alive for the borrow.
        unsafe { symbol.shared().deref() }.mark();
    }
}

#[cfg(test)]
mod tests {
    use std::collections::HashMap;

    use merc_utilities::random_test;

    use crate::ATerm;
    use crate::Symb;
    use crate::Symbol;
    use crate::Term;
    use crate::random_term;
    use crate::storage::THREAD_TERM_POOL;

    #[test]
    #[cfg_attr(miri, ignore)]
    fn test_maximal_sharing() {
        random_test(100, |rng| {
            let mut terms = HashMap::new();

            for _ in 0..1000 {
                let term = random_term(rng, &[("f".into(), 2), ("g".into(), 1)], &["a".to_string()], 10);

                let representation = format!("{}", term);
                if let Some(entry) = terms.get(&representation) {
                    assert_eq!(term, *entry, "There is another term with the same representation");
                } else {
                    terms.insert(representation, term);
                }
            }
        });
    }

    #[test]
    fn test_symbol_reachable_through_term_survives_collection() {
        let term = {
            let f = Symbol::new("only_reachable_through_term", 1);
            ATerm::with_args(&f, &[ATerm::constant(&Symbol::new("a", 0))]).protect()
        };

        // Collect twice, so the second collection also sees the marks cleared by the first.
        THREAD_TERM_POOL.with(|tp| tp.force_collect_garbage());
        THREAD_TERM_POOL.with(|tp| tp.force_collect_garbage());

        let head = term.get_head_symbol();
        assert_eq!(head.name(), "only_reachable_through_term");
        assert_eq!(head.arity(), 1, "the mark must not leak into the arity");
        assert_eq!(head, Symbol::new("only_reachable_through_term", 1).copy());
    }

    #[test]
    #[should_panic]
    fn test_term_out_of_bound_arity() {
        let c = ATerm::constant(&Symbol::new("a", 0));

        let t = ATerm::with_args(&Symbol::new("f", 1), &[c.copy(), c.copy()]);

        // Currently we check on access
        let _ = t.arg(1);
    }
}