planus/
builder_cache.rs

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
use core::{
    convert::{TryFrom, TryInto},
    hash::{BuildHasher, Hash, Hasher},
    marker::PhantomData,
};

use crate::Offset;

#[derive(Copy, Clone, Debug)]
#[repr(transparent)]
/// Backwards offset from the end of the serialized buffer
pub(crate) struct CacheOffset(u32);

pub(crate) trait GetCacheKey {
    /// Gets the cachable byte-slice at the start of the buffer
    fn get_cache_key_impl(serialized: &[u8]) -> Option<&[u8]>;
    fn get_cache_key(serialized: &[u8], offset: CacheOffset) -> Option<&[u8]> {
        serialized
            .len()
            .checked_sub(offset.0.try_into().ok()?)
            .and_then(|offset| Self::get_cache_key_impl(&serialized[offset..]))
    }
}

impl<T: ?Sized> From<CacheOffset> for Offset<T> {
    fn from(offset: CacheOffset) -> Self {
        Self {
            offset: offset.0,
            phantom: PhantomData,
        }
    }
}

impl<T: ?Sized> From<Offset<T>> for CacheOffset {
    fn from(offset: Offset<T>) -> Self {
        Self(offset.offset)
    }
}

impl TryFrom<usize> for CacheOffset {
    type Error = <u32 as TryFrom<usize>>::Error;

    fn try_from(value: usize) -> Result<Self, Self::Error> {
        u32::try_from(value).map(Self)
    }
}

impl From<CacheOffset> for usize {
    fn from(offset: CacheOffset) -> Self {
        offset.0 as usize
    }
}

pub(crate) struct VTable;

impl GetCacheKey for VTable {
    fn get_cache_key_impl(serialized: &[u8]) -> Option<&[u8]> {
        let length = u16::from_le_bytes(serialized.get(..2)?.try_into().ok()?);
        serialized.get(..length as usize)
    }
}

pub(crate) struct ByteVec;

impl GetCacheKey for ByteVec {
    fn get_cache_key_impl(serialized: &[u8]) -> Option<&[u8]> {
        let length = u32::from_le_bytes(serialized.get(..4)?.try_into().ok()?);
        serialized.get(4..4 + length as usize)
    }
}

pub(crate) struct Cache<C> {
    _marker: PhantomData<C>,
    cache: hashbrown::raw::RawTable<CacheOffset>,
    hash_builder: hashbrown::hash_map::DefaultHashBuilder,
}

impl<C> Default for Cache<C> {
    fn default() -> Self {
        Self {
            _marker: Default::default(),
            cache: Default::default(),
            hash_builder: Default::default(),
        }
    }
}

impl<C> core::fmt::Debug for Cache<C> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "Cache<{}>", core::any::type_name::<C>())
    }
}

fn hash_one<H: BuildHasher>(hash_builder: &H, value: &[u8]) -> u64 {
    let mut hasher = hash_builder.build_hasher();
    value.hash(&mut hasher);
    hasher.finish()
}

impl<C: GetCacheKey> Cache<C> {
    pub(crate) fn hash(&self, serialized_data: &[u8]) -> u64 {
        hash_one(&self.hash_builder, serialized_data)
    }

    pub(crate) fn get(
        &mut self,
        serialized_data: &[u8],
        key_hash: u64,
        key: &[u8],
    ) -> Option<CacheOffset> {
        self.cache
            .get(key_hash, |back_offset| {
                C::get_cache_key(serialized_data, *back_offset)
                    .map_or(false, |old_key| old_key == key)
            })
            .copied()
    }

    /// Should only be called if `get` returned `None`
    pub(crate) fn insert(&mut self, key_hash: u64, offset: CacheOffset, serialized_data: &[u8]) {
        self.cache
            .insert(key_hash, CacheOffset(offset.0), |back_offset| {
                C::get_cache_key(serialized_data, *back_offset).map_or_else(
                    || {
                        #[cfg(debug_assertions)]
                        panic!("BUG: It should always be possible to get the cache key");
                        #[cfg(not(debug_assertions))]
                        0
                    },
                    |old_key| hash_one(&self.hash_builder, old_key),
                )
            });
    }

    pub(crate) fn clear(&mut self) {
        self.cache.clear_no_drop();
    }
}