intern-mint 0.4.0

byte slice interning
Documentation
use std::sync::LazyLock;

use hashbrown::HashTable;
use parking_lot::{Mutex, MutexGuard};

use crate::entry::Entry;

#[derive(Default)]
pub(crate) struct LockedShard {
    table: HashTable<Entry>,
    /// Sum of the allocation sizes of the entries in `table`
    bytes: usize,
}

type Shard = Mutex<LockedShard>;

#[derive(Debug, Default, Clone, Copy)]
#[non_exhaustive]
pub struct MemoryUsage {
    pub len: usize,
    pub capacity: usize,
    /// Bytes allocated for the entries themselves, each a small header followed by the data, as
    /// requested from the allocator
    pub bytes: usize,
}

pub(crate) struct ShardedSet {
    pub(crate) shift: usize,
    pub(crate) hash_builder: ahash::RandomState,
    pub(crate) shards: Box<[Shard]>,
}

impl ShardedSet {
    fn get_shard(&self, hash: u64) -> MutexGuard<'_, LockedShard> {
        // copied from https://github.com/xacrimon/dashmap/blob/366ce7e7872866a06de66eb95002fa6cf2c117a7/src/lib.rs#L419
        let idx = ((hash << 7) >> self.shift) as usize;
        self.shards[idx].lock()
    }

    fn get_hash_and_shard(&self, value: &[u8]) -> (u64, MutexGuard<'_, LockedShard>) {
        // hash before locking
        let hash = self.hash_builder.hash_one(value);
        (hash, self.get_shard(hash))
    }

    pub(crate) fn get_from_existing_ref(&self, value: &[u8]) -> Option<Entry> {
        let (hash, shard) = self.get_hash_and_shard(value);
        shard
            .table
            .find(hash, |o| std::ptr::eq(o.data_ptr(), value.as_ptr()))
            // Safety: the shard is locked
            .map(|o| unsafe { o.acquire() })
    }

    pub(crate) fn get_or_insert(&self, value: &[u8]) -> Entry {
        let (hash, mut shard) = self.get_hash_and_shard(value);
        let LockedShard { table, bytes } = &mut *shard;

        // Safety: entries are only accessed while the shard is locked
        let entry = table
            .entry(
                hash,
                |o| unsafe { o.data() } == value,
                |o| unsafe { o.hash() },
            )
            .or_insert_with(|| {
                let entry = Entry::new(value, hash);
                *bytes += Entry::allocation_size_for(value.len());
                entry
            });

        // Safety: the shard is locked
        unsafe { entry.get().acquire() }
    }

    /// Gives up a handle, removing `entry` from the pool if it was the last one
    ///
    /// # Safety
    ///
    /// The caller must own the handle being given up, and must not use it after this call
    pub(crate) unsafe fn release(&self, entry: Entry) {
        // read before releasing, since another thread may free the entry right after
        // Safety: the handle being given up keeps the entry alive until then
        let hash = unsafe { entry.hash() };

        // Safety: guaranteed by the caller
        if !unsafe { entry.release() } {
            return;
        }

        // `entry` may be dangling from here on, so it's only compared by address
        let mut shard = self.get_shard(hash);
        let LockedShard { table, bytes } = &mut *shard;

        let Ok(found) = table.find_entry(hash, |o| *o == entry) else {
            // another thread already removed it
            return;
        };

        // check again in case a new handle was acquired before the shard was locked
        // Safety: the shard is locked
        if !unsafe { found.get().is_unused() } {
            return;
        }

        let (removed, _) = found.remove();
        // Safety: the shard is still locked, so the entry hasn't been freed
        *bytes -= unsafe { removed.allocation_size() };
        // free outside of the lock
        drop(shard);
        // Safety: the entry was removed with no handles left, so nothing can reach it anymore
        unsafe { removed.free() };
    }

    pub(crate) fn is_empty(&self) -> bool {
        self.shards.iter().all(|s| s.lock().table.is_empty())
    }

    pub(crate) fn len(&self) -> usize {
        self.shards.iter().map(|o| o.lock().table.len()).sum()
    }

    pub(crate) fn capacity(&self) -> usize {
        self.shards.iter().map(|o| o.lock().table.capacity()).sum()
    }

    pub(crate) fn get_memory_usage(&self) -> MemoryUsage {
        self.shards
            .iter()
            .map(|o| {
                let o = o.lock();
                MemoryUsage {
                    len: o.table.len(),
                    capacity: o.table.capacity(),
                    bytes: o.bytes,
                }
            })
            .reduce(|acc, o| MemoryUsage {
                len: acc.len + o.len,
                capacity: acc.capacity + o.capacity,
                bytes: acc.bytes + o.bytes,
            })
            .unwrap_or_default()
    }

    pub(crate) fn shrink_to_fit(&self) {
        for shard in self.shards.iter() {
            // Safety: the shard is locked
            shard.lock().table.shrink_to_fit(|o| unsafe { o.hash() });
        }
    }
}

impl Default for ShardedSet {
    fn default() -> Self {
        // copied from https://github.com/xacrimon/dashmap/blob/366ce7e7872866a06de66eb95002fa6cf2c117a7/src/lib.rs#L63
        static DEFAULT_SHARDS_COUNT: LazyLock<usize> = LazyLock::new(|| {
            (std::thread::available_parallelism().map_or(1, usize::from) * 4).next_power_of_two()
        });

        // based on https://github.com/xacrimon/dashmap/blob/366ce7e7872866a06de66eb95002fa6cf2c117a7/src/lib.rs#L269
        // using the width of the u64 hash rather than usize, which is narrower on 32-bit targets
        let shift = (u64::BITS - DEFAULT_SHARDS_COUNT.trailing_zeros()) as usize;

        Self {
            shift,
            hash_builder: Default::default(),
            shards: (0..*DEFAULT_SHARDS_COUNT)
                .map(|_| Default::default())
                .collect(),
        }
    }
}

pub(crate) static POOL: LazyLock<ShardedSet> = LazyLock::new(Default::default);

pub fn is_empty() -> bool {
    POOL.is_empty()
}

pub fn len() -> usize {
    POOL.len()
}

pub fn capacity() -> usize {
    POOL.capacity()
}

pub fn get_memory_usage() -> MemoryUsage {
    POOL.get_memory_usage()
}

pub fn shrink_to_fit() {
    POOL.shrink_to_fit();
}