babbel_yaml 0.1.0

Fast, modular YAML 1.2 parser and emitter with anchors, aliases, and tags
Documentation
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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
//! String Interning Utilities for YAML Library
//!
//! This module provides string interning capabilities to optimize memory usage when
//! identical strings appear repeatedly in YAML documents (such as common keys).
//! String interning can reduce memory usage by 20-40% for typical configuration files.
//!
//! # Features
//! - Efficient string interning for deduplication
//! - Reduces memory footprint for repeated strings
//! - Thread-safe and no_std compatible
//!
//! # Usage
//! Use the string interner to store and reuse common strings in YAML processing.

#[cfg(feature = "std")]
use std::collections::HashMap;
#[cfg(feature = "std")]
use std::sync::{Arc, RwLock};

#[cfg(not(feature = "std"))]
use alloc::collections::BTreeMap as HashMap;
#[cfg(not(feature = "std"))]
use alloc::string::String;
#[cfg(not(feature = "std"))]
use alloc::sync::Arc;

#[cfg(feature = "alloc")]
use alloc::rc::Rc;

/// A reference-counted interned string
///
/// This is a lightweight handle to a string stored in the interner.
/// Cloning is cheap (just increments a reference count).
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct InternedString(Arc<String>);

impl InternedString {
    /// Create a new interned string (typically done by StringInterner)
    pub(crate) fn new(s: String) -> Self {
        Self(Arc::new(s))
    }

    /// Get a reference to the string
    pub fn as_str(&self) -> &str {
        &self.0
    }

    /// Get the length of the string
    pub fn len(&self) -> usize {
        self.0.len()
    }

    /// Check if the string is empty
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    /// Get the reference count (for debugging/profiling)
    pub fn ref_count(&self) -> usize {
        Arc::strong_count(&self.0)
    }
}

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

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

impl From<&str> for InternedString {
    fn from(s: &str) -> Self {
        Self::new(String::from(s))
    }
}

/// String interner for deduplicating strings
///
/// Maintains a cache of strings and returns interned references.
/// Multiple requests for the same string return the same interned reference.
///
/// # Example
/// ```
/// use babbel_yaml::StringInterner;
///
/// let mut interner = StringInterner::new();
/// let s1 = interner.intern("name");
/// let s2 = interner.intern("name");
///
/// // Both references point to the same underlying string
/// assert_eq!(s1.ref_count(), s2.ref_count());
/// ```
#[cfg(feature = "std")]
#[derive(Debug)]
pub struct StringInterner {
    cache: RwLock<HashMap<String, Arc<String>>>,
    stats: RwLock<InternerStats>,
}

#[cfg(feature = "std")]
impl StringInterner {
    /// Create a new string interner
    pub fn new() -> Self {
        Self {
            cache: RwLock::new(HashMap::new()),
            stats: RwLock::new(InternerStats::default()),
        }
    }

    /// Create a new string interner with a specified capacity
    pub fn with_capacity(capacity: usize) -> Self {
        Self {
            cache: RwLock::new(HashMap::with_capacity(capacity)),
            stats: RwLock::new(InternerStats::default()),
        }
    }

    /// Intern a string, returning an interned reference
    ///
    /// If the string is already interned, returns the existing reference.
    /// Otherwise, creates a new interned string.
    pub fn intern(&self, s: &str) -> InternedString {
        // Try read lock first (common case: string already exists)
        {
            let cache = self.cache.read().unwrap();
            if let Some(existing) = cache.get(s) {
                let mut stats = self.stats.write().unwrap();
                stats.hits += 1;
                return InternedString(Arc::clone(existing));
            }
        }

        // Need to insert - use write lock
        let mut cache = self.cache.write().unwrap();

        // Double-check in case another thread inserted while we waited
        if let Some(existing) = cache.get(s) {
            let mut stats = self.stats.write().unwrap();
            stats.hits += 1;
            return InternedString(Arc::clone(existing));
        }

        // Insert new string
        let arc = Arc::new(String::from(s));
        cache.insert(s.to_string(), Arc::clone(&arc));

        let mut stats = self.stats.write().unwrap();
        stats.misses += 1;
        stats.unique_strings = cache.len();

        InternedString(arc)
    }

    /// Get the number of unique strings currently interned
    pub fn len(&self) -> usize {
        self.cache.read().unwrap().len()
    }

    /// Check if the interner is empty
    pub fn is_empty(&self) -> bool {
        self.cache.read().unwrap().is_empty()
    }

    /// Clear all interned strings
    pub fn clear(&self) {
        self.cache.write().unwrap().clear();
        let mut stats = self.stats.write().unwrap();
        stats.unique_strings = 0;
    }

    /// Get statistics about the interner
    pub fn stats(&self) -> InternerStats {
        *self.stats.read().unwrap()
    }

    /// Calculate memory savings estimate
    ///
    /// Returns (total_string_bytes, interned_bytes, savings_bytes, savings_percent)
    pub fn memory_savings(&self) -> (usize, usize, usize, f64) {
        let cache = self.cache.read().unwrap();
        let stats = self.stats.read().unwrap();

        let total_hits = stats.hits;
        let unique_count = cache.len();

        if unique_count == 0 || total_hits == 0 {
            return (0, 0, 0, 0.0);
        }

        let mut total_string_bytes = 0;
        let mut interned_bytes = 0;

        for (key, value) in cache.iter() {
            let str_len = key.len();
            let ref_count = Arc::strong_count(value);

            // Total bytes if each reference was a separate string
            // String overhead: capacity + length + pointer
            let string_overhead = core::mem::size_of::<String>();
            total_string_bytes += (str_len + string_overhead) * ref_count;

            // Actual bytes: one copy of the string + Arc pointers for each reference
            interned_bytes +=
                str_len + string_overhead + (ref_count * core::mem::size_of::<*const String>());
        }

        let savings = total_string_bytes.saturating_sub(interned_bytes);
        let savings_percent = if total_string_bytes > 0 {
            (savings as f64 / total_string_bytes as f64) * 100.0
        } else {
            0.0
        };

        (total_string_bytes, interned_bytes, savings, savings_percent)
    }
}

#[cfg(feature = "std")]
impl Default for StringInterner {
    fn default() -> Self {
        Self::new()
    }
}

/// Statistics about string interning
#[derive(Debug, Clone, Copy, Default)]
pub struct InternerStats {
    /// Number of cache hits (string already interned)
    pub hits: usize,
    /// Number of cache misses (new string interned)
    pub misses: usize,
    /// Number of unique strings currently interned
    pub unique_strings: usize,
}

impl InternerStats {
    /// Get the hit rate as a percentage
    pub fn hit_rate(&self) -> f64 {
        let total = self.hits + self.misses;
        if total == 0 {
            0.0
        } else {
            (self.hits as f64 / total as f64) * 100.0
        }
    }

    /// Get the total number of intern requests
    pub fn total_requests(&self) -> usize {
        self.hits + self.misses
    }
}

/// Simple non-thread-safe interner for single-threaded use
///
/// This is lighter weight than the thread-safe version but cannot be
/// shared across threads.
#[cfg(feature = "alloc")]
#[derive(Debug)]
pub struct SimpleInterner {
    cache: HashMap<String, Rc<String>>,
    stats: InternerStats,
}

#[cfg(feature = "alloc")]
impl SimpleInterner {
    /// Create a new simple interner
    pub fn new() -> Self {
        Self {
            cache: HashMap::new(),
            stats: InternerStats::default(),
        }
    }

    /// Create a new simple interner with capacity
    pub fn with_capacity(capacity: usize) -> Self {
        Self {
            #[cfg(feature = "std")]
            cache: HashMap::with_capacity(capacity),
            #[cfg(not(feature = "std"))]
            cache: HashMap::new(),
            stats: InternerStats::default(),
        }
    }

    /// Intern a string
    pub fn intern(&mut self, s: &str) -> Rc<String> {
        if let Some(existing) = self.cache.get(s) {
            self.stats.hits += 1;
            return Rc::clone(existing);
        }

        let rc = Rc::new(String::from(s));
        self.cache.insert(s.to_string(), Rc::clone(&rc));
        self.stats.misses += 1;
        self.stats.unique_strings = self.cache.len();

        rc
    }

    /// Get the number of unique strings
    pub fn len(&self) -> usize {
        self.cache.len()
    }

    /// Check if empty
    pub fn is_empty(&self) -> bool {
        self.cache.is_empty()
    }

    /// Clear all strings
    pub fn clear(&mut self) {
        self.cache.clear();
        self.stats.unique_strings = 0;
    }

    /// Get statistics
    pub fn stats(&self) -> InternerStats {
        self.stats
    }
}

#[cfg(feature = "alloc")]
impl Default for SimpleInterner {
    fn default() -> Self {
        Self::new()
    }
}

/// Common strings that frequently appear in YAML documents
///
/// Pre-interned for maximum efficiency.
pub struct CommonStrings {
    pub name: InternedString,
    pub type_: InternedString,
    pub id: InternedString,
    pub value: InternedString,
    pub key: InternedString,
    pub data: InternedString,
    pub config: InternedString,
    pub version: InternedString,
    pub description: InternedString,
    pub enabled: InternedString,
    pub disabled: InternedString,
    pub status: InternedString,
    pub message: InternedString,
    pub error: InternedString,
    pub result: InternedString,
}

impl CommonStrings {
    /// Create a new CommonStrings with pre-interned values
    pub fn new() -> Self {
        Self {
            name: InternedString::from("name"),
            type_: InternedString::from("type"),
            id: InternedString::from("id"),
            value: InternedString::from("value"),
            key: InternedString::from("key"),
            data: InternedString::from("data"),
            config: InternedString::from("config"),
            version: InternedString::from("version"),
            description: InternedString::from("description"),
            enabled: InternedString::from("enabled"),
            disabled: InternedString::from("disabled"),
            status: InternedString::from("status"),
            message: InternedString::from("message"),
            error: InternedString::from("error"),
            result: InternedString::from("result"),
        }
    }
}

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

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

    #[test]
    #[cfg(feature = "std")]
    fn test_string_interner_basic() {
        let interner = StringInterner::new();
        let s1 = interner.intern("test");
        let s2 = interner.intern("test");
        let s3 = interner.intern("other");

        assert_eq!(s1.as_str(), "test");
        assert_eq!(s2.as_str(), "test");
        assert_eq!(s3.as_str(), "other");

        // Same string should have same reference
        assert_eq!(s1.ref_count(), s2.ref_count());

        assert_eq!(interner.len(), 2);
    }

    #[test]
    #[cfg(feature = "std")]
    fn test_interner_stats() {
        let interner = StringInterner::new();

        interner.intern("a");
        interner.intern("b");
        interner.intern("a"); // hit
        interner.intern("c");
        interner.intern("b"); // hit
        interner.intern("a"); // hit

        let stats = interner.stats();
        assert_eq!(stats.hits, 3);
        assert_eq!(stats.misses, 3);
        assert_eq!(stats.unique_strings, 3);
        assert_eq!(stats.total_requests(), 6);
        assert_eq!(stats.hit_rate(), 50.0);
    }

    #[test]
    #[cfg(feature = "alloc")]
    fn test_simple_interner() {
        let mut interner = SimpleInterner::new();

        let s1 = interner.intern("test");
        let s2 = interner.intern("test");

        assert_eq!(*s1, *s2);
        assert_eq!(interner.len(), 1);

        let stats = interner.stats();
        assert_eq!(stats.hits, 1);
        assert_eq!(stats.misses, 1);
    }

    #[test]
    fn test_interned_string() {
        let s = InternedString::from("hello");
        assert_eq!(s.as_str(), "hello");
        assert_eq!(s.len(), 5);
        assert!(!s.is_empty());
        assert_eq!(s.to_string(), "hello");
    }

    #[test]
    #[cfg(feature = "std")]
    fn test_memory_savings() {
        let interner = StringInterner::new();

        // Keep references alive to show memory savings
        let mut refs = Vec::new();
        for _ in 0..10 {
            refs.push(interner.intern("name"));
            refs.push(interner.intern("type"));
            refs.push(interner.intern("value"));
        }

        let (total, interned, savings, percent) = interner.memory_savings();

        // Should show significant savings
        assert!(savings > 0, "Expected savings > 0, got {}", savings);
        assert!(percent > 0.0, "Expected percent > 0, got {}", percent);
        assert!(
            interned < total,
            "Expected interned {} < total {}",
            interned,
            total
        );
    }

    #[test]
    fn test_common_strings() {
        let common = CommonStrings::new();
        assert_eq!(common.name.as_str(), "name");
        assert_eq!(common.type_.as_str(), "type");
        assert_eq!(common.id.as_str(), "id");
        assert_eq!(common.version.as_str(), "version");
    }

    #[test]
    #[cfg(feature = "std")]
    fn test_clear() {
        let interner = StringInterner::new();
        interner.intern("test");
        assert_eq!(interner.len(), 1);

        interner.clear();
        assert_eq!(interner.len(), 0);
        assert!(interner.is_empty());
    }

    #[test]
    #[cfg(feature = "std")]
    fn test_interner_with_capacity() {
        let interner = StringInterner::with_capacity(100);
        interner.intern("test");
        assert_eq!(interner.len(), 1);
    }
}

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

    #[test]
    fn test_interned_string_empty() {
        let s = InternedString::from("");
        assert_eq!(s.len(), 0);
        assert!(s.is_empty());
    }

    #[test]
    #[cfg(feature = "std")]
    fn test_string_interner_duplicate_interning() {
        let interner = StringInterner::new();
        let s1 = interner.intern("dup");
        let s2 = interner.intern("dup");
        assert_eq!(s1.as_str(), s2.as_str());
        assert_eq!(s1.ref_count(), s2.ref_count());
        assert_eq!(interner.len(), 1);
    }

    #[test]
    #[cfg(feature = "std")]
    fn test_string_interner_clear_and_reuse() {
        let interner = StringInterner::new();
        interner.intern("foo");
        interner.clear();
        assert_eq!(interner.len(), 0);
        let s = interner.intern("foo");
        assert_eq!(s.as_str(), "foo");
        assert_eq!(interner.len(), 1);
    }

    #[test]
    #[cfg(feature = "alloc")]
    fn test_simple_interner_empty() {
        let interner = SimpleInterner::new();
        assert!(interner.is_empty());
        assert_eq!(interner.len(), 0);
    }

    #[test]
    #[cfg(feature = "alloc")]
    fn test_simple_interner_clear_and_reuse() {
        let mut interner = SimpleInterner::new();
        interner.intern("bar");
        interner.clear();
        assert_eq!(interner.len(), 0);
        let s = interner.intern("bar");
        assert_eq!(&*s, "bar");
        assert_eq!(interner.len(), 1);
    }

    #[test]
    fn test_common_strings_all_fields() {
        let common = CommonStrings::new();
        assert_eq!(common.key.as_str(), "key");
        assert_eq!(common.data.as_str(), "data");
        assert_eq!(common.config.as_str(), "config");
        assert_eq!(common.description.as_str(), "description");
        assert_eq!(common.enabled.as_str(), "enabled");
        assert_eq!(common.disabled.as_str(), "disabled");
        assert_eq!(common.status.as_str(), "status");
        assert_eq!(common.message.as_str(), "message");
        assert_eq!(common.error.as_str(), "error");
        assert_eq!(common.result.as_str(), "result");
    }
}