lambdust 0.1.1

A Scheme dialect with gradual typing and effect systems
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
//! String interning for performance optimization.
//!
//! This module provides a string interning system to reduce memory usage
//! and improve performance by storing only one copy of each unique string.

use std::collections::{HashMap, VecDeque};
use std::sync::{Arc, RwLock, Mutex};

/// A thread-safe string interner that stores unique strings.
#[derive(Debug)]
pub struct StringInterner {
    /// Map from string content to interned ID
    string_to_id: RwLock<HashMap<String, InternedId>>,
    /// Map from interned ID to string content
    id_to_string: RwLock<Vec<Arc<str>>>,
}

/// An interned string identifier.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct InternedId(usize);

/// An interned string that can be cheaply cloned and compared.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct InternedString {
    id: InternedId,
    content: Arc<str>,
}

impl StringInterner {
    /// Creates a new string interner.
    pub fn new() -> Self {
        Self {
            string_to_id: RwLock::new(HashMap::new()),
            id_to_string: RwLock::new(Vec::new()),
        }
    }

    /// Interns a string, returning an interned string handle.
    /// If the string is already interned, returns the existing handle.
    pub fn intern(&self, s: &str) -> InternedString {
        // Fast path: check if already interned with read lock
        if let Ok(string_to_id) = self.string_to_id.read() {
            if let Some(&id) = string_to_id.get(s) {
                if let Ok(id_to_string) = self.id_to_string.read() {
                    if let Some(content) = id_to_string.get(id.0) {
                        return InternedString {
                            id,
                            content: content.clone(),
                        };
                    }
                }
            }
        }

        // Slow path: need to intern the string
        let mut string_to_id = self.string_to_id.write().unwrap();
        let mut id_to_string = self.id_to_string.write().unwrap();

        // Double-check after acquiring write lock
        if let Some(&id) = string_to_id.get(s) {
            if let Some(content) = id_to_string.get(id.0) {
                return InternedString {
                    id,
                    content: content.clone(),
                };
            }
        }

        // Create new interned string
        let id = InternedId(id_to_string.len());
        let content: Arc<str> = Arc::from(s);
        
        string_to_id.insert(s.to_string(), id);
        id_to_string.push(content.clone());

        InternedString { id, content }
    }

    /// Gets the string content for an interned ID.
    pub fn resolve(&self, id: InternedId) -> Option<Arc<str>> {
        if let Ok(id_to_string) = self.id_to_string.read() {
            id_to_string.get(id.0).cloned()
        } else {
            None
        }
    }

    /// Returns the number of unique strings interned.
    pub fn len(&self) -> usize {
        if let Ok(id_to_string) = self.id_to_string.read() {
            id_to_string.len()
        } else {
            0
        }
    }

    /// Returns true if no strings are interned.
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Clears all interned strings.
    pub fn clear(&self) {
        if let (Ok(mut string_to_id), Ok(mut id_to_string)) = 
            (self.string_to_id.write(), self.id_to_string.write()) {
            string_to_id.clear();
            id_to_string.clear();
        }
    }
}

impl InternedString {
    /// Gets the interned ID for this string.
    pub fn id(&self) -> InternedId {
        self.id
    }

    /// Gets the string content as a reference.
    pub fn as_str(&self) -> &str {
        &self.content
    }

    /// Gets the string content as an Arc<str>.
    pub fn as_arc_str(&self) -> &Arc<str> {
        &self.content
    }
}

impl std::fmt::Display for InternedString {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.content)
    }
}

impl std::ops::Deref for InternedString {
    type Target = str;

    fn deref(&self) -> &Self::Target {
        &self.content
    }
}

impl AsRef<str> for InternedString {
    fn as_ref(&self) -> &str {
        &self.content
    }
}

impl Default for StringInterner {
    fn default() -> Self {
        Self::new()
    }
}

/// Specialized string interner for Scheme symbols with pre-interned common keywords.
#[derive(Debug)]
pub struct SymbolInterner {
    interner: StringInterner,
    // Pre-allocated symbol IDs for common Scheme keywords
    common_symbols: HashMap<&'static str, InternedId>,
}

impl Default for SymbolInterner {
    fn default() -> Self {
        Self::new()
    }
}

impl SymbolInterner {
    /// Creates a new symbol interner with pre-interned common symbols.
    pub fn new() -> Self {
        let interner = StringInterner::new();
        let mut common_symbols = HashMap::new();
        
        // Pre-intern common Scheme keywords and built-in symbols
        let common_keywords = [
            // Special forms
            "lambda", "define", "set!", "if", "cond", "case", "and", "or", "let", "let*", "letrec",
            "begin", "do", "quote", "quasiquote", "unquote", "unquote-splicing", "syntax-rules",
            "call/cc", "call-with-current-continuation", "delay", "force",
            
            // Built-in procedures  
            "+", "-", "*", "/", "=", "<", ">", "<=", ">=", "eq?", "eqv?", "equal?",
            "null?", "pair?", "list?", "number?", "string?", "symbol?", "boolean?", "procedure?",
            "cons", "car", "cdr", "list", "length", "append", "reverse", "map", "for-each",
            "apply", "values", "call-with-values", "dynamic-wind",
            
            // Common symbols
            "else", "#t", "#f", "...", "_",
            
            // R7RS library names
            "scheme", "base", "case-lambda", "char", "complex", "cxr", "eval", "file", "inexact",
            "lazy", "load", "process-context", "read", "repl", "time", "write",
            
            // SRFI identifiers  
            "srfi-1", "srfi-13", "srfi-14", "srfi-16", "srfi-23", "srfi-26", "srfi-39", "srfi-9",
        ];
        
        for &keyword in &common_keywords {
            let interned = interner.intern(keyword);
            common_symbols.insert(keyword, interned.id());
        }
        
        Self {
            interner,
            common_symbols,
        }
    }
    
    /// Interns a symbol, with fast path for common keywords.
    pub fn intern_symbol(&self, s: &str) -> InternedString {
        // Fast path: check if it's a pre-interned common symbol
        if let Some(&id) = self.common_symbols.get(s) {
            if let Some(content) = self.interner.resolve(id) {
                return InternedString { id, content };
            }
        }
        
        // Fallback to normal interning
        self.interner.intern(s)
    }
    
    /// Gets all pre-interned common symbols.
    pub fn common_symbol_names(&self) -> Vec<&'static str> {
        self.common_symbols.keys().cloned().collect()
    }
    
    /// Gets statistics about symbol interning.
    pub fn stats(&self) -> SymbolInternerStats {
        let total_symbols = self.interner.len();
        let common_symbols = self.common_symbols.len();
        let dynamic_symbols = total_symbols.saturating_sub(common_symbols);
        
        SymbolInternerStats {
            total_symbols,
            common_symbols,
            dynamic_symbols,
            estimated_memory: total_symbols * 48 + common_symbols * 16, // Rough estimate
        }
    }
}

/// Statistics about symbol interning performance.
#[derive(Debug, Clone)]
pub struct SymbolInternerStats {
    /// Total number of unique symbols
    pub total_symbols: usize,
    /// Number of pre-interned common symbols
    pub common_symbols: usize,
    /// Number of dynamically interned symbols
    pub dynamic_symbols: usize,
    /// Estimated memory usage in bytes
    pub estimated_memory: usize,
}

/// String pool for frequently allocated strings.
#[derive(Debug)]
pub struct StringPool {
    pool: Arc<Mutex<VecDeque<String>>>,
    max_size: usize,
    initial_capacity: usize,
}

impl StringPool {
    /// Creates a new string pool.
    pub fn new(initial_capacity: usize, max_size: usize) -> Self {
        Self {
            pool: Arc::new(Mutex::new(VecDeque::new())),
            max_size,
            initial_capacity,
        }
    }
    
    /// Gets a string from the pool or creates a new one.
    pub fn get(&self) -> PooledString {
        let string = if let Ok(mut pool) = self.pool.lock() {
            pool.pop_front().unwrap_or_else(|| String::with_capacity(self.initial_capacity))
        } else {
            String::with_capacity(self.initial_capacity)
        };
        
        PooledString {
            string: Some(string),
            pool: self.pool.clone(),
            max_size: self.max_size,
        }
    }
    
    /// Returns the current pool size.
    pub fn size(&self) -> usize {
        if let Ok(pool) = self.pool.lock() {
            pool.len()
        } else {
            0
        }
    }
}

/// A string borrowed from a string pool.
pub struct PooledString {
    string: Option<String>,
    pool: Arc<Mutex<VecDeque<String>>>,
    max_size: usize,
}

impl PooledString {
    /// Takes the string out of the pool wrapper.
    pub fn take(mut self) -> String {
        self.string.take().expect("String already taken")
    }
}

impl std::ops::Deref for PooledString {
    type Target = String;
    
    fn deref(&self) -> &Self::Target {
        self.string.as_ref().expect("String already taken")
    }
}

impl std::ops::DerefMut for PooledString {
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.string.as_mut().expect("String already taken")
    }
}

impl Drop for PooledString {
    fn drop(&mut self) {
        if let Some(mut string) = self.string.take() {
            string.clear(); // Clear content but retain capacity
            if let Ok(mut pool) = self.pool.lock() {
                if pool.len() < self.max_size {
                    pool.push_back(string);
                }
            }
        }
    }
}

/// Global string interner for commonly used strings.
static GLOBAL_INTERNER: once_cell::sync::Lazy<StringInterner> = 
    once_cell::sync::Lazy::new(StringInterner::new);

/// Global symbol interner with pre-interned common symbols.
static GLOBAL_SYMBOL_INTERNER: once_cell::sync::Lazy<SymbolInterner> = 
    once_cell::sync::Lazy::new(SymbolInterner::new);

/// Global string pool for temporary string allocations.
static GLOBAL_STRING_POOL: once_cell::sync::Lazy<StringPool> = 
    once_cell::sync::Lazy::new(|| StringPool::new(64, 20));

/// Interns a string using the global interner.
pub fn intern(s: &str) -> InternedString {
    GLOBAL_INTERNER.intern(s)
}

/// Interns a symbol using the global symbol interner (with common symbol optimization).
pub fn intern_symbol(s: &str) -> InternedString {
    GLOBAL_SYMBOL_INTERNER.intern_symbol(s)
}

/// Gets a pooled string from the global string pool.
pub fn get_pooled_string() -> PooledString {
    GLOBAL_STRING_POOL.get()
}

/// Gets statistics about the global interner.
pub fn global_interner_stats() -> (usize, usize) {
    let len = GLOBAL_INTERNER.len();
    // Estimate memory usage (rough approximation)
    let estimated_memory = len * 32; // 32 bytes per entry on average
    (len, estimated_memory)
}

/// Gets statistics about the global symbol interner.
pub fn global_symbol_interner_stats() -> SymbolInternerStats {
    GLOBAL_SYMBOL_INTERNER.stats()
}

/// Gets statistics about the global string pool.
pub fn global_string_pool_stats() -> (usize, usize) {
    let size = GLOBAL_STRING_POOL.size();
    let estimated_memory = size * 64; // Rough estimate based on initial capacity
    (size, estimated_memory)
}

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

    #[test]
    fn test_basic_interning() {
        let interner = StringInterner::new();
        
        let s1 = interner.intern("hello");
        let s2 = interner.intern("world");
        let s3 = interner.intern("hello"); // Same string
        
        assert_eq!(s1.as_str(), "hello");
        assert_eq!(s2.as_str(), "world");
        assert_eq!(s3.as_str(), "hello");
        
        // Same string should have same ID
        assert_eq!(s1.id(), s3.id());
        assert_ne!(s1.id(), s2.id());
        
        assert_eq!(interner.len(), 2);
    }

    #[test]
    fn test_global_interner() {
        let s1 = intern("test");
        let s2 = intern("test");
        let s3 = intern("other");
        
        assert_eq!(s1.id(), s2.id());
        assert_ne!(s1.id(), s3.id());
        
        assert_eq!(s1.as_str(), "test");
        assert_eq!(s3.as_str(), "other");
    }

    #[test]
    fn test_thread_safety() {
        let interner = Arc::new(StringInterner::new());
        let mut handles = Vec::new();
        
        for i in 0..10 {
            let interner_clone = interner.clone();
            let handle = thread::spawn(move || {
                let s = format!("string_{}", i % 3);
                interner_clone.intern(&s)
            });
            handles.push(handle);
        }
        
        let results: Vec<_> = handles.into_iter().map(|h| h.join().unwrap()).collect();
        
        // Should have at most 3 unique strings
        assert!(interner.len() <= 3);
        
        // Strings with same content should have same ID
        for i in 0..results.len() {
            for j in (i+1)..results.len() {
                if results[i].as_str() == results[j].as_str() {
                    assert_eq!(results[i].id(), results[j].id());
                }
            }
        }
    }

    #[test]
    fn test_resolve() {
        let interner = StringInterner::new();
        let s = interner.intern("resolve_test");
        
        let resolved = interner.resolve(s.id()).unwrap();
        assert_eq!(resolved.as_ref(), "resolve_test");
    }

    #[test]
    fn test_clear() {
        let interner = StringInterner::new();
        interner.intern("test1");
        interner.intern("test2");
        
        assert_eq!(interner.len(), 2);
        
        interner.clear();
        assert_eq!(interner.len(), 0);
        assert!(interner.is_empty());
    }

    #[test]
    fn test_string_operations() {
        let s = intern("test_operations");
        
        assert_eq!(s.to_string(), "test_operations");
        assert_eq!(format!("{}", s), "test_operations");
        assert_eq!(s.as_ref(), "test_operations");
        
        // Test deref
        assert_eq!(s.len(), "test_operations".len());
        assert!(s.starts_with("test"));
    }
}