slvm/
interner.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
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
use std::collections::HashMap;
use std::hash::{Hash, Hasher};
use std::mem;

#[derive(Clone, Copy, Debug)]
pub struct Interned {
    pub id: u32,
}

impl PartialEq for Interned {
    fn eq(&self, other: &Self) -> bool {
        self.id == other.id
    }
}

impl Eq for Interned {}

impl Hash for Interned {
    fn hash<H: Hasher>(&self, state: &mut H) {
        state.write_u32(self.id);
    }
}

// This interner is initially based on this: https://matklad.github.io/2020/03/22/fast-simple-rust-interner.html
// Inspiration also from: https://github.com/CAD97/simple-interner/blob/master/src/interner.rs
// See https://www.reddit.com/r/rust/comments/fn1jxf/blog_post_fast_and_simple_rust_interner/
// This is a simple string interner.  It hands out &'static str and it WILL leak memory
// to keep them valid.  Intended to live for the programs lifetime.
#[derive(Clone, Debug)]
pub struct Interner {
    map: HashMap<&'static str, Interned>,
    vals: Vec<&'static str>,
    // Leak buffers to keep the static lifetimes we hand out valid.
    buf: mem::ManuallyDrop<String>,
    capacity: usize,
    used: usize,
}

impl Interner {
    /// Create an interner with capacity cap (to the next power of two).
    pub fn with_capacity(cap: usize) -> Interner {
        let cap = cap.next_power_of_two();
        Interner {
            map: HashMap::default(),
            vals: Vec::new(),
            buf: mem::ManuallyDrop::new(String::with_capacity(cap)),
            capacity: cap,
            used: 0,
        }
    }

    /// True if name is an interned symbol.
    pub fn contains(&self, name: &str) -> bool {
        self.map.contains_key(name)
    }

    fn intern_final(&mut self, name: &'static str) -> Interned {
        let id = self.vals.len() as u32;
        self.vals.push(name);
        let interned = Interned { id };
        self.map.insert(name, interned);
        interned
    }

    /// If name is interned then return it, otherwise None.
    pub fn get_if_interned(&self, name: &str) -> Option<Interned> {
        self.map.get(name).copied()
    }

    /// Intern name in this interner.  Will return the existing symbol if it
    /// exists or add it and and return it if not.  Use this if you already have
    /// a static str reference to avoid wasting space on making another.
    pub fn intern_static(&mut self, name: &'static str) -> Interned {
        if let Some(&id) = self.map.get(name) {
            return id;
        }
        self.intern_final(name)
    }

    /// Intern name in this interner.  Will return the existing symbol if it
    /// exists or add it and return it if not.
    pub fn intern(&mut self, name: &str) -> Interned {
        if let Some(&id) = self.map.get(name) {
            return id;
        }
        let name = {
            let cap = self.buf.capacity();
            if cap < self.buf.len() + name.len() {
                let new_cap = (cap.max(name.len()) + 1).next_power_of_two();
                let new_buf = mem::ManuallyDrop::new(String::with_capacity(new_cap));
                self.capacity += new_cap;
                // Leak memory to keep the static lifetimes valid.
                let _old_buf = mem::replace(&mut self.buf, new_buf);
            }

            let start = self.buf.len();
            self.buf.push_str(name);
            self.used += name.len();
            unsafe { &*(&self.buf[start..] as *const str) }
        };
        self.intern_final(name)
    }

    pub fn get_string(&self, interned: Interned) -> Option<&'static str> {
        if let Some(s) = self.vals.get(interned.id as usize) {
            Some(s)
        } else {
            None
        }
    }

    /// Return the amount of memory allocated by the interner.
    pub fn capacity(&self) -> usize {
        self.capacity
    }

    /// Return the amount of memory used to store symbols in the interner.
    pub fn used(&self) -> usize {
        self.used
    }

    /// Return the number of symbols in the interner.
    pub fn len(&self) -> usize {
        self.map.len()
    }

    /// Are there no symbols in this interner?
    pub fn is_empty(&self) -> bool {
        self.map.is_empty()
    }
}

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

    #[test]
    fn test_basic() {
        let mut i = Interner::with_capacity(7);
        assert!(i.capacity() == 8);
        assert!(i.used() == 0);
        assert!(i.is_empty());
        let one = i.intern("one");
        assert!(i.get_string(one).unwrap() == "one");
        assert!(i.used() == 3);
        assert!(i.len() == 1);
        let fives = i.intern("fives");
        assert!(i.get_string(fives).unwrap() == "fives");
        assert!(i.capacity() == 8);
        assert!(i.used() == 8);
        assert!(i.len() == 2);
        let v1 = i.intern("xxx");
        assert!(i.get_string(one).unwrap() == "one");
        assert!(i.get_string(fives).unwrap() == "fives");
        assert!(i.get_string(v1).unwrap() == "xxx");
        assert!(i.capacity() == 24);
        assert!(i.used() == 11);
        assert!(i.len() == 3);

        let one2 = i.intern("one");
        assert!(i.get_string(one).unwrap() == "one");
        assert!(i.get_string(one2).unwrap() == "one");
        assert!(one == one2);
        assert!(std::ptr::eq(
            i.get_string(one).unwrap(),
            i.get_string(one2).unwrap()
        ));
        assert!(i.capacity() == 24);
        assert!(i.used() == 11);
        assert!(i.len() == 3);

        let v2 = i.intern("1234567890");
        assert!(i.get_string(one).unwrap() == "one");
        assert!(i.get_string(fives).unwrap() == "fives");
        assert!(i.get_string(v1).unwrap() == "xxx");
        assert!(i.get_string(v2).unwrap() == "1234567890");
        assert!(i.capacity() == 24);
        assert!(i.used() == 21);
        assert!(i.len() == 4);

        let v3 = i.intern("1234");
        assert!(i.get_string(one).unwrap() == "one");
        assert!(i.get_string(fives).unwrap() == "fives");
        assert!(i.get_string(v1).unwrap() == "xxx");
        assert!(i.get_string(v2).unwrap() == "1234567890");
        assert!(i.get_string(v3).unwrap() == "1234");
        assert!(i.capacity() == 56);
        assert!(i.used() == 25);
        assert!(i.len() == 5);

        let v2_2 = i.intern("1234567890");
        assert!(i.get_string(v2).unwrap() == "1234567890");
        assert!(i.get_string(v2_2).unwrap() == "1234567890");
        assert!(v2 == v2_2);
        assert!(std::ptr::eq(
            i.get_string(v2).unwrap(),
            i.get_string(v2_2).unwrap()
        ));
        assert!(i.capacity() == 56);
        assert!(i.used() == 25);
        assert!(i.len() == 5);
    }
}