any-intern 0.1.5

An interner for various types
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
use super::common::{self, Interned, RawInterned, UnsafeLock};
use bumpalo::Bump;
use hashbrown::{hash_table::Entry, HashTable};
use std::{
    alloc::Layout,
    any::TypeId,
    borrow,
    cell::Cell,
    hash::Hasher,
    hash::{BuildHasher, Hash},
    mem,
    ptr::NonNull,
};

/// A type-erased interner for storing and deduplicating values of a single type.
///
/// This interner is simply a wrapper of [`AnyInternSet`] with interior mutability. If you need a
/// collection of interners for various types like a hash map of interners, then consider using the
/// `AnyInterSet` with a container providing interior mutability such as [`ManualMutex`].
///
/// # Examples
///
/// ```
/// use any_intern::AnyInterner;
///
/// #[derive(PartialEq, Eq, Hash, Debug)]
/// struct A(u32);
///
/// let interner = AnyInterner::of::<A>();
///
/// unsafe {
///     let a1 = interner.intern(A(42));
///     let a2 = interner.intern(A(42));
///     assert_eq!(a1, a2); // Same value, same reference
///
///     let a3 = interner.intern(A(99));
///     assert_ne!(a1, a3); // Different values, different references
/// }
/// ```
///
/// # Safety
///
/// Many methods in `AnyInterner` are marked as `unsafe` because they rely on the caller to ensure
/// that the correct type is used when interacting with the interner. Using an incorrect type can
/// lead to undefined behavior.
pub struct AnyInterner<S = fxhash::FxBuildHasher> {
    inner: UnsafeLock<AnyInternSet<S>>,
}

impl AnyInterner {
    pub fn of<K: 'static>() -> Self {
        // Safety: Only one instance
        let inner = unsafe { UnsafeLock::new(AnyInternSet::of::<K>()) };
        Self { inner }
    }
}

impl<S: BuildHasher> AnyInterner<S> {
    pub fn with_hasher<K: 'static>(hash_builder: S) -> Self {
        // Safety: Only one instance
        let inner = unsafe { UnsafeLock::new(AnyInternSet::with_hasher::<K>(hash_builder)) };
        Self { inner }
    }

    /// Returns number of values the interner contains.
    pub fn len(&self) -> usize {
        self.with_inner(|set| set.len())
    }

    /// Returns true if the interner is empty.
    pub fn is_empty(&self) -> bool {
        self.with_inner(|set| set.is_empty())
    }

    /// Stores a value in the interner, returning a reference to the interned value.
    ///
    /// This method inserts the given value into the interner if it does not already exist. If the
    /// value already exists, a reference to the existing value is returned.
    ///
    /// # Examples
    ///
    /// ```
    /// use any_intern::AnyInterner;
    ///
    /// #[derive(PartialEq, Eq, Hash, Debug)]
    /// struct A(u32);
    ///
    /// let interner = AnyInterner::of::<A>();
    ///
    /// unsafe {
    ///     let a1 = interner.intern(A(42));
    ///     let a2 = interner.intern(A(42));
    ///     assert_eq!(a1, a2); // Same value, same reference
    ///     assert_eq!(a1.raw().as_ptr(), a2.raw().as_ptr());
    /// }
    /// ```
    ///
    /// # Safety
    ///
    /// Undefined behavior if incorrect type `K` is given.
    pub unsafe fn intern<K>(&self, value: K) -> Interned<'_, K>
    where
        K: Hash + Eq + 'static,
    {
        self.with_inner(|set| unsafe { set.intern(value) })
    }

    /// Stores a value in the interner, creating it only if it does not already exist.
    ///
    /// This method allows you to provide a key and a closure to generate the value. If the key
    /// already exists in the interner, the closure is not called, and a reference to the existing
    /// value is returned. If the key does not exist, the closure is called to create the value,
    /// which is then stored in the interner.
    ///
    /// This method is useful when the value is expensive to compute, as it avoids unnecessary
    /// computation if the value already exists.
    ///
    /// # Examples
    ///
    /// ```
    /// use any_intern::AnyInterner;
    ///
    ///
    /// #[derive(PartialEq, Eq, Hash, Debug)]
    /// struct A(i32);
    ///
    /// impl std::borrow::Borrow<i32> for A {
    ///     fn borrow(&self) -> &i32 {
    ///         &self.0
    ///     }
    /// }
    ///
    /// let interner = AnyInterner::of::<A>();
    ///
    /// unsafe {
    ///     let a = interner.intern_with(&42, || A(42));
    ///     assert_eq!(interner.len(), 1);
    ///     assert_eq!(*a, A(42));
    ///
    ///     let b = interner.intern_with(&42, || A(99)); // Closure is not called
    ///     assert_eq!(interner.len(), 1);
    ///     assert_eq!(*b, A(42));
    ///
    ///     let c = interner.intern_with(&43, || A(43));
    ///     assert_eq!(interner.len(), 2);
    ///     assert_eq!(*c, A(43));
    /// }
    /// ```
    ///
    /// # Safety
    ///
    /// Undefined behavior if incorrect type `K` is given.
    pub unsafe fn intern_with<K, Q, F>(&self, key: &Q, make_value: F) -> Interned<'_, K>
    where
        K: borrow::Borrow<Q> + 'static,
        Q: Hash + Eq + ?Sized,
        F: FnOnce() -> K,
    {
        self.with_inner(|set| unsafe { set.intern_with(key, make_value) })
    }

    /// Retrieves a reference to a value in the interner based on the provided key.
    ///
    /// This method checks if a value corresponding to the given key exists in the interner. If it
    /// exists, a reference to the interned value is returned. Otherwise, `None` is returned.
    ///
    /// # Eaxmples
    ///
    /// ```
    /// use any_intern::AnyInterner;
    ///
    /// let interner = AnyInterner::of::<i32>();
    /// unsafe {
    ///     interner.intern(42);
    ///     assert_eq!(interner.get::<i32, _>(&42).as_deref(), Some(&42));
    ///     assert!(interner.get::<i32, _>(&99).is_none());
    /// }
    /// ```
    ///
    /// # Safety
    ///
    /// Undefined behavior if incorrect type `K` is given.
    pub unsafe fn get<K, Q>(&self, key: &Q) -> Option<Interned<'_, K>>
    where
        K: borrow::Borrow<Q> + 'static,
        Q: Hash + Eq + ?Sized,
    {
        self.with_inner(|set| unsafe { set.get(key) })
    }

    /// Returns true if the interner contains values of the given type.
    pub fn is_type_of<K: 'static>(&self) -> bool {
        self.with_inner(|set| set.is_type_of::<K>())
    }

    /// Removes all items in the interner.
    ///
    /// Although the interner support interior mutability, clear method requires mutable access
    /// to the interner to invalidate all [`Interned`]s referencing the interner.
    pub fn clear(&mut self) {
        self.with_inner(|set| set.clear())
    }

    fn with_inner<'this, F, R>(&'this self, f: F) -> R
    where
        F: FnOnce(&'this mut AnyInternSet<S>) -> R,
        R: 'this,
    {
        // Safety: Mutex unlocking is paired with the locking.
        unsafe {
            let set = self.inner.lock().as_mut();
            let ret = f(set);
            self.inner.unlock();
            ret
        }
    }
}

/// A type-erased interning set for storing and deduplicating values of a single type without
/// interior mutability.
///
/// # Examples
///
/// ```
/// use any_intern::AnyInternSet;
///
/// #[derive(PartialEq, Eq, Hash, Debug)]
/// struct A(u32);
///
/// let mut set = AnyInternSet::of::<A>();
///
/// unsafe {
///     let a1 = set.intern(A(42)).raw();
///     let a2 = set.intern(A(42)).raw();
///     assert_eq!(a1, a2); // Same value, same reference
///
///     let a3 = set.intern(A(99)).raw();
///     assert_ne!(a1, a3); // Different values, different references
/// }
/// ```
///
/// # Safety
///
/// Many methods in `AnyInternSet` are marked as `unsafe` because they rely on the caller to ensure
/// that the correct type is used when interacting with the interner. Using an incorrect type can
/// lead to undefined behavior.
pub struct AnyInternSet<S = fxhash::FxBuildHasher> {
    arena: AnyArena,
    set: HashTable<RawInterned>,
    hash_builder: S,
}

impl AnyInternSet {
    pub fn of<K: 'static>() -> Self {
        Self {
            arena: AnyArena::of::<K>(),
            set: HashTable::new(),
            hash_builder: Default::default(),
        }
    }
}

impl<S: Default> AnyInternSet<S> {
    pub fn default_of<K: 'static>() -> Self {
        Self {
            arena: AnyArena::of::<K>(),
            set: HashTable::new(),
            hash_builder: Default::default(),
        }
    }
}

impl<S: BuildHasher> AnyInternSet<S> {
    pub fn with_hasher<K: 'static>(hash_builder: S) -> Self {
        Self {
            arena: AnyArena::of::<K>(),
            set: HashTable::new(),
            hash_builder,
        }
    }

    /// Returns number of values in the set.
    pub fn len(&self) -> usize {
        self.arena.len()
    }

    /// Returns true if the set is empty.
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Stores a value in the set, returning a reference to the value.
    ///
    /// This method inserts the given value into the set if it does not already exist. If the value
    /// already exists, a reference to the existing value is returned.
    ///
    /// # Examples
    ///
    /// ```
    /// use any_intern::AnyInternSet;
    ///
    /// #[derive(PartialEq, Eq, Hash, Debug)]
    /// struct A(u32);
    ///
    /// let mut set = AnyInternSet::of::<A>();
    ///
    /// unsafe {
    ///     let a1 = set.intern(A(42)).raw();
    ///     let a2 = set.intern(A(42)).raw();
    ///     assert_eq!(a1, a2); // Same value, same reference
    /// }
    /// ```
    ///
    /// # Safety
    ///
    /// Undefined behavior if incorrect type `K` is given.
    pub unsafe fn intern<K>(&mut self, value: K) -> Interned<'_, K>
    where
        K: Hash + Eq + 'static,
    {
        debug_assert!(self.is_type_of::<K>());

        unsafe {
            let hash = Self::hash(&self.hash_builder, &value);
            let eq = Self::table_eq::<K, K>(&value);
            let hasher = Self::table_hasher::<K, K>(&self.hash_builder);
            match self.set.entry(hash, eq, hasher) {
                Entry::Occupied(entry) => Interned::from_erased_raw(*entry.get()),
                Entry::Vacant(entry) => {
                    let ref_ = self.arena.alloc(value);
                    let interned = Interned::unique(ref_);
                    let raw = interned.erased_raw();
                    entry.insert(raw);
                    interned
                }
            }
        }
    }

    /// Stores a value in the set, creating it only if it does not already exist.
    ///
    /// This method allows you to provide a key and a closure to generate the value. If the key
    /// already exists in the set, the closure is not called, and a reference to the existing value
    /// is returned. If the key does not exist, the closure is called to create the value, which is
    /// then stored in the set.
    ///
    /// This method is useful when the value is expensive to compute, as it avoids unnecessary
    /// computation if the value already exists.
    ///
    /// # Examples
    ///
    /// ```
    /// use any_intern::AnyInternSet;
    ///
    ///
    /// #[derive(PartialEq, Eq, Hash, Debug)]
    /// struct A(i32);
    ///
    /// impl std::borrow::Borrow<i32> for A {
    ///     fn borrow(&self) -> &i32 {
    ///         &self.0
    ///     }
    /// }
    ///
    /// let mut set = AnyInternSet::of::<A>();
    ///
    /// unsafe {
    ///     let a = set.intern_with(&42, || A(42));
    ///     assert_eq!(*a, A(42));
    ///     assert_eq!(set.len(), 1);
    ///
    ///     let b = set.intern_with(&42, || A(99)); // Closure is not called
    ///     assert_eq!(*b, A(42));
    ///     assert_eq!(set.len(), 1);
    ///
    ///     let c = set.intern_with(&43, || A(43));
    ///     assert_eq!(*c, A(43));
    ///     assert_eq!(set.len(), 2);
    /// }
    /// ```
    ///
    /// # Safety
    ///
    /// Undefined behavior if incorrect type `K` is given.
    pub unsafe fn intern_with<K, Q, F>(&mut self, key: &Q, make_value: F) -> Interned<'_, K>
    where
        K: borrow::Borrow<Q> + 'static,
        Q: Hash + Eq + ?Sized,
        F: FnOnce() -> K,
    {
        debug_assert!(self.is_type_of::<K>());

        unsafe {
            let hash = Self::hash(&self.hash_builder, key);
            let eq = Self::table_eq::<K, Q>(key);
            let hasher = Self::table_hasher::<K, Q>(&self.hash_builder);
            match self.set.entry(hash, eq, hasher) {
                Entry::Occupied(entry) => Interned::from_erased_raw(*entry.get()),
                Entry::Vacant(entry) => {
                    let value = make_value();
                    let ref_ = self.arena.alloc(value);
                    let interned = Interned::unique(ref_);
                    let raw = interned.erased_raw();
                    entry.insert(raw);
                    interned
                }
            }
        }
    }

    /// Retrieves a reference to a value in the set based on the provided key.
    ///
    /// This method checks if a value corresponding to the given key exists in the set. If it
    /// exists, a reference to the value is returned. Otherwise, `None` is returned.
    ///
    /// # Eaxmples
    ///
    /// ```
    /// use any_intern::AnyInternSet;
    ///
    /// let mut set = AnyInternSet::of::<i32>();
    /// unsafe {
    ///     set.intern(42);
    ///     assert_eq!(set.get::<i32, _>(&42).as_deref(), Some(&42));
    ///     assert!(set.get::<i32, _>(&99).is_none());
    /// }
    /// ```
    ///
    /// # Safety
    ///
    /// Undefined behavior if incorrect type `K` is given.
    pub unsafe fn get<K, Q>(&self, key: &Q) -> Option<Interned<'_, K>>
    where
        K: borrow::Borrow<Q> + 'static,
        Q: Hash + Eq + ?Sized,
    {
        debug_assert!(self.is_type_of::<K>());

        unsafe {
            let hash = Self::hash(&self.hash_builder, key);
            let eq = Self::table_eq::<K, Q>(key);
            self.set
                .find(hash, eq)
                .map(|raw| Interned::from_erased_raw(*raw))
        }
    }

    /// Returns true if the set contains values of the given type.
    pub fn is_type_of<K: 'static>(&self) -> bool {
        self.arena.is_type_of::<K>()
    }

    /// Removes all items in the set.
    pub fn clear(&mut self) {
        self.arena.clear();
        self.set.clear();
    }

    /// Returns `eq` closure that is used for some methods on the [`HashTable`].
    ///
    /// # Safety
    ///
    /// Undefined behavior if incorrect type `K` is given.
    unsafe fn table_eq<'a, K, Q>(key: &'a Q) -> impl FnMut(&RawInterned) -> bool + 'a
    where
        K: borrow::Borrow<Q>,
        Q: Hash + Eq + ?Sized,
    {
        move |entry: &RawInterned| unsafe {
            let value = entry.cast::<K>().as_ref();
            value.borrow() == key
        }
    }

    /// Returns `hasher` closure that is used for some methods on the [`HashTable`].
    ///
    /// # Safety
    ///
    /// Undefined behavior if incorrect type `K` is given.
    unsafe fn table_hasher<'a, K, Q>(hash_builder: &'a S) -> impl Fn(&RawInterned) -> u64 + 'a
    where
        K: borrow::Borrow<Q>,
        Q: Hash + Eq + ?Sized,
    {
        |entry: &RawInterned| unsafe {
            let value = entry.cast::<K>().as_ref();
            Self::hash(hash_builder, value.borrow())
        }
    }

    fn hash<K: Hash + ?Sized>(hash_builder: &S, value: &K) -> u64 {
        let mut hasher = hash_builder.build_hasher();
        value.hash(&mut hasher);
        hasher.finish()
    }
}

pub struct AnyArena {
    bump: Bump,
    ty: TypeId,
    stride: usize,
    raw_drop_slice: Option<unsafe fn(*mut u8, usize)>,
    len: Cell<usize>,
}

impl AnyArena {
    pub fn of<T: 'static>() -> Self {
        Self {
            bump: Bump::new(),
            ty: TypeId::of::<T>(),
            stride: Layout::new::<T>().pad_to_align().size(),
            raw_drop_slice: if mem::needs_drop::<T>() {
                Some(common::cast_then_drop_slice::<T>)
            } else {
                None
            },
            len: Cell::new(0),
        }
    }

    pub fn is_type_of<T: 'static>(&self) -> bool {
        TypeId::of::<T>() == self.ty
    }

    /// Returns number of elements in this arena.
    pub fn len(&self) -> usize {
        self.len.get()
    }

    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    pub fn alloc<T: 'static>(&self, value: T) -> &mut T {
        debug_assert!(self.is_type_of::<T>());

        self.len.set(self.len() + 1);
        self.bump.alloc(value)
    }

    pub fn clear(&mut self) {
        self.drop_all();
        self.bump.reset();
        self.len.set(0);
    }

    fn drop_all(&mut self) {
        if let Some(raw_drop_slice) = self.raw_drop_slice {
            if self.stride > 0 {
                unsafe {
                    for (ptr, len) in self.bump.iter_allocated_chunks_raw() {
                        // Chunk would not be divisible by the `stride` especially when the stride
                        // is greater than 16. In that case, we should ignore the remainder.
                        let num_elems = len / self.stride;
                        raw_drop_slice(ptr, num_elems);
                    }
                }
            } else {
                // ZST, but it has drop impl, which means the drop could have side effects, so we
                // should call it.
                let ptr = NonNull::<()>::dangling(); // aligned dangling pointer for ZST
                unsafe {
                    raw_drop_slice(ptr.as_ptr().cast(), self.len());
                }
            }
        }
    }
}

impl Drop for AnyArena {
    fn drop(&mut self) {
        self.drop_all();
    }
}

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

    #[test]
    fn test_any_interner() {
        #[derive(PartialEq, Eq, Hash, Debug)]
        struct A(i32);

        let interner = AnyInterner::of::<A>();

        unsafe {
            let a = interner.intern(A(0));
            let b = interner.intern(A(0));
            let c = interner.intern(A(1));
            assert_eq!(a, b);
            assert_ne!(a, c);
        }
    }

    #[test]
    fn test_arena() {
        test_arena_alloc();
        test_arena_drop();
    }

    fn test_arena_alloc() {
        const START: u32 = 0;
        const END: u32 = 100;
        const EXPECTED: u32 = (END + START) * (END - START + 1) / 2;

        let arena = AnyArena::of::<u32>();
        let mut refs = Vec::new();
        for i in START..=END {
            let ref_ = arena.alloc(i);
            refs.push(ref_);
        }
        let acc = refs.into_iter().map(|ref_| *ref_).sum::<u32>();
        assert_eq!(acc, EXPECTED);
    }

    fn test_arena_drop() {
        macro_rules! test {
            ($arr_len:literal, $align:literal) => {{
                thread_local! {
                    static SUM: Cell<u32> = Cell::new(0);
                    static CNT: Cell<u32> = Cell::new(0);
                }

                #[repr(align($align))]
                struct A([u8; $arr_len]);

                // Restricted by `u8` and `A::new()`.
                const _: () = const { assert!($arr_len < 256) };

                impl A {
                    fn new() -> Self {
                        Self(std::array::from_fn(|i| i as u8))
                    }

                    fn sum() -> u32 {
                        ($arr_len - 1) * $arr_len / 2
                    }
                }

                impl Drop for A {
                    fn drop(&mut self) {
                        let sum = self.0.iter().map(|n| *n as u32).sum::<u32>();
                        SUM.set(SUM.get() + sum);
                        CNT.set(CNT.get() + 1);
                    }
                }

                struct Zst;

                impl Drop for Zst {
                    fn drop(&mut self) {
                        CNT.set(CNT.get() + 1);
                    }
                }

                const REPEAT: u32 = 10;

                // === Non-ZST type ===

                let arena = AnyArena::of::<A>();
                for _ in 0..REPEAT {
                    arena.alloc(A::new());
                }
                drop(arena);

                assert_eq!(SUM.get(), A::sum() * REPEAT);
                assert_eq!(CNT.get(), REPEAT);
                SUM.set(0);
                CNT.set(0);

                // === ZST type ===

                let arena = AnyArena::of::<Zst>();
                for _ in 0..REPEAT {
                    arena.alloc(Zst);
                }
                drop(arena);

                assert_eq!(CNT.get(), REPEAT);
                CNT.set(0);
            }};
        }

        // Array len, align
        test!(1, 1);
        test!(1, 2);
        test!(1, 4);
        test!(1, 8);
        test!(1, 16);
        test!(1, 32);
        test!(1, 64);
        test!(1, 128);
        test!(1, 256);

        test!(100, 1);
        test!(100, 2);
        test!(100, 4);
        test!(100, 8);
        test!(100, 16);
        test!(100, 32);
        test!(100, 64);
        test!(100, 128);
        test!(100, 256);
    }
}