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
use std::{fmt::Debug, ops::Deref};
use storage::{IStringKey, ThreadLocalReader, SHARED_STORAGE, THREAD_LOCAL_READER};

mod storage;

/// An immutable and interned string.
/// 
/// Reading an `IString`'s contents is very fast, lock free and wait free (thanks to `left_right`).
/// Can be shared and read from any number of threads.
/// Scales linearly with the number of reading threads.
/// 
/// The tradeoff is that creating a new `IString` is comparatively slower :
/// - Creating a new `IString` with a string that is already interned is generally fast.
///   It acquires a global lock.
/// - Creating a new `IString` with a string that isn't already interned is much slower.
///   It acquired a global lock and waits for all readers to finish reading.
#[derive(Eq, PartialEq, Ord, Hash)]
pub struct IString {
    pub(crate) key: IStringKey
}

// Indispensable traits impl : From, Drop, Deref

impl From<String> for IString {
    #[inline]
    fn from(string: String) -> Self {
        Self {
            key: SHARED_STORAGE.insert_or_retain(string)
        }
    }
}

impl From<&str> for IString {
    #[inline]
    fn from(string: &str) -> Self {
        Self {
            key: SHARED_STORAGE.insert_or_retain(String::from(string))
        }
    }
}

impl Drop for IString {
    #[inline]
    fn drop(&mut self) {
        SHARED_STORAGE.release(self)
    }
}

impl Deref for IString {
    type Target = str;
    
    #[inline]
    fn deref(&self) -> &Self::Target {
        THREAD_LOCAL_READER.with(|reader: &ThreadLocalReader| {
            reader.read(self)
        })
    }
}

impl AsRef<str> for IString {
    #[inline]
    fn as_ref(&self) -> &str {
        THREAD_LOCAL_READER.with(|reader: &ThreadLocalReader| {
            reader.read(self)
        })
    }
}

// Common traits impl that can't be derived : Clone, PartialOrd, Debug, Display, Default

impl Clone for IString {
    fn clone(&self) -> Self {
        SHARED_STORAGE.retain(self.key);

        Self { key: self.key }
    }
}

impl PartialOrd for IString {
    fn lt(&self, other: &Self) -> bool {
        self.deref().lt(other.deref())
    }

    fn le(&self, other: &Self) -> bool {
        self.deref().le(other.deref())
    }

    fn gt(&self, other: &Self) -> bool {
        self.deref().gt(other.deref())
    }

    fn ge(&self, other: &Self) -> bool {
        self.deref().ge(other.deref())
    }
    
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        self.deref().partial_cmp(other.deref())
    }
}

impl Debug for IString {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_tuple("IString")
         .field(&self.deref())
         .finish()
    }
}

impl std::fmt::Display for IString {
    #[inline]
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self)
    }
}

impl Default for IString {
    fn default() -> Self {
        Self::from(String::default())
    }
}

// Convenience trait Intern

pub trait Intern {
    fn intern(self) -> IString where Self: Sized;
}

impl Intern for String {
    #[inline]
    fn intern(self) -> IString {
        IString::from(self)
    }
}

impl Intern for &str {
    #[inline]
    fn intern(self) -> IString {
        IString::from(self)
    }
}

#[cfg(feature = "serde")]
mod feature_serde {
    use serde::{de::Visitor, Deserialize, Serialize};
    use crate::IString;

    impl Serialize for IString {
        fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
            serializer.serialize_str(std::ops::Deref::deref(&self))
        }
    }
    
    impl<'de> Deserialize<'de> for IString {
        fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
            deserializer.deserialize_string(IStringVisitor)
        }
    }
    
    struct IStringVisitor;
    
    impl<'de> Visitor<'de> for IStringVisitor {
        type Value = IString;
    
        fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
            formatter.write_str("a string")
        }
    
        fn visit_string<E: serde::de::Error>(self, string: String) -> Result<Self::Value, E> {
            // does not need to allocate a new string
            Ok(IString::from(string))
        }
    
        fn visit_str<E: serde::de::Error>(self, slice: &str) -> Result<Self::Value, E> {
            // less performant, will allocate
            Ok(IString::from(slice))
        }
    }
}

// tests

#[cfg(test)]
mod tests {
    use std::{ops::Deref, sync::Mutex};
    use radix_trie::TrieCommon;

    use super::*;
    use crate::storage::SHARED_STORAGE;

    #[test]
    fn it_creates_and_removes_1_string() {
        with_exclusive_use_of_shared_storage(|| {
            let my_istring1 = "hello".intern();
            assert!(my_istring1.deref() == "hello");

            assert_string_count_in_storage(1);
            assert_string_is_stored_with_key("hello", my_istring1.key);

            drop(my_istring1);

            assert_string_count_in_storage(1);
            assert_string_is_still_stored("hello");

            let my_istring2 = "another".to_string().intern();
            assert!(my_istring2.deref() == "another");

            assert_string_count_in_storage(1);
            assert_string_is_stored_with_key("another", my_istring2.key);
            assert_string_is_not_stored("hello")
        });
    }

    #[test]
    fn it_creates_and_removes_1_shared_string() {
        with_exclusive_use_of_shared_storage(|| {
            let my_istring1 = IString::from("hello");
            let my_istring2 = IString::from("hello");
            assert!(my_istring1.deref() == "hello");
            assert!(my_istring2.deref() == "hello");
            assert!(my_istring1.key == my_istring2.key);

            assert_string_count_in_storage(1);
            assert_string_is_stored_with_key("hello", my_istring1.key);

            drop(my_istring1);

            assert_string_count_in_storage(1);
            assert_string_is_stored_with_key("hello", my_istring2.key);

            drop(my_istring2);

            assert_string_count_in_storage(1);
            assert_string_is_still_stored("hello");
        });
    }

    #[test]
    fn it_creates_and_removes_3_strings() {
        with_exclusive_use_of_shared_storage(|| {
            let my_istring1 = IString::from("hello");
            let my_istring2 = IString::from("world");
            let my_istring3 = IString::from("howdy");
            assert!(my_istring1.deref() == "hello");
            assert!(my_istring2.deref() == "world");
            assert!(my_istring3.deref() == "howdy");
            assert!(my_istring1.key != my_istring2.key);
            assert!(my_istring2.key != my_istring3.key);

            assert_string_count_in_storage(3);
            assert_string_is_stored_with_key("hello", my_istring1.key);
            assert_string_is_stored_with_key("world", my_istring2.key);
            assert_string_is_stored_with_key("howdy", my_istring3.key);
            assert_string_is_not_stored("hola");

            drop(my_istring1);
            drop(my_istring2);

            assert_string_count_in_storage(3);
            assert_string_is_still_stored("hello");
            assert_string_is_still_stored("world");
            assert_string_is_stored_with_key("howdy", my_istring3.key);
            assert_string_is_not_stored("hola");

            // it should reuse the storage
            let my_istring1bis = IString::from("hello");
            assert!(my_istring1bis.deref() == "hello");

            // and not clean up the storage of "world" yet
            assert_string_count_in_storage(3);
            assert_string_is_stored_with_key("hello", my_istring1bis.key);
            assert_string_is_stored_with_key("howdy", my_istring3.key);
            assert_string_is_still_stored("world");

            let my_istring4 = IString::from("another");
            assert!(my_istring4.deref() == "another");

            // creating a new string should cause the storage of unused strings to be cleaned up
            assert_string_is_stored_with_key("hello", my_istring1bis.key);
            assert_string_is_stored_with_key("howdy", my_istring3.key);
            assert_string_is_stored_with_key("another", my_istring4.key);
            assert_string_is_not_stored("world");
            assert_string_count_in_storage(3);
        });
    }

    #[test]
    fn test_send() {
        fn assert_send<T: Send>() {}
        assert_send::<IString>();
    }

    #[test]
    fn test_sync() {
        fn assert_sync<T: Sync>() {}
        assert_sync::<IString>();
    }

    fn assert_string_count_in_storage(count: usize) {
        let guard = SHARED_STORAGE.read_handle.lock().unwrap();
        let read_handle = guard.enter().unwrap();
        assert_eq!(read_handle.map.len(), count);
        assert_eq!(read_handle.trie.len(), count);
    }

    fn assert_string_is_still_stored(string: &str) {
        let guard = SHARED_STORAGE.read_handle.lock().unwrap();
        let read_handle = guard.enter().unwrap();
        let key = read_handle.trie.get(&string.into());
        if let Some(key) = key {
            assert!(read_handle.map.get(&key).unwrap().inner.deref() == string);
        } else {
            assert!(false, "the string is not in the trie");
        }
    }

    fn assert_string_is_stored_with_key(string: &str, key: u32) {
        let guard = SHARED_STORAGE.read_handle.lock().unwrap();
        let read_handle = guard.enter().unwrap();
        assert!(read_handle.map.get(&key).unwrap().inner.deref() == string);
        assert_eq!(read_handle.trie.get(&string.into()), Some(&key));
    }

    fn assert_string_is_not_stored(string: &str) {
        let guard = SHARED_STORAGE.read_handle.lock().unwrap();
        let read_handle = guard.enter().unwrap();
        assert_eq!(read_handle.trie.get(&string.into()), None);
    }

    static SHARED_STORAGE_MUTEX: Mutex<()> = Mutex::new(());

    fn with_exclusive_use_of_shared_storage(closure: fn()) {
        let guard = SHARED_STORAGE_MUTEX.lock().expect("test lock is not poisoned");
        closure();

        // reset the writer for the next test
        let mut writer = SHARED_STORAGE.writer.lock().unwrap();
        writer.write_handle.append(storage::StringStorageOp::DropUnusedStrings);
        writer.write_handle.publish();
        drop(writer);
        drop(guard);
    }
}