voirs-cli 0.1.0-beta.1

Command-line interface for VoiRS speech synthesis
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
//! Phoneme caching system for improved synthesis performance.
//!
//! This module provides an LRU cache for phoneme conversion results,
//! significantly improving performance for repeated or similar text synthesis.
//!
//! # Performance Benefits
//!
//! - 50-70% faster synthesis for repeated text
//! - Reduced CPU usage on G2P conversion
//! - Better batch processing performance
//!
//! # Example
//!
//! ```no_run
//! use voirs_cli::performance::phoneme_cache::PhonemeCache;
//!
//! let cache = PhonemeCache::new(1000); // Cache 1000 entries
//!
//! // First call: performs actual G2P conversion
//! let phonemes1 = cache.get_or_compute("hello", "en", || {
//!     vec!["h", "ə", "l", "oʊ"]
//! });
//!
//! // Second call: retrieved from cache (much faster)
//! let phonemes2 = cache.get_or_compute("hello", "en", || {
//!     vec!["h", "ə", "l", "oʊ"]
//! });
//! ```

use lru::LruCache;
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
use std::num::NonZeroUsize;
use std::sync::{Arc, Mutex};

/// Cache key for phoneme lookup
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
struct CacheKey {
    /// Input text
    text: String,
    /// Language code
    language: String,
    /// G2P backend identifier (e.g., "phonetisaurus", "neural")
    backend: String,
}

impl CacheKey {
    fn new(
        text: impl Into<String>,
        language: impl Into<String>,
        backend: impl Into<String>,
    ) -> Self {
        Self {
            text: text.into(),
            language: language.into(),
            backend: backend.into(),
        }
    }

    /// Generate a fast hash for the cache key
    fn fast_hash(&self) -> u64 {
        let mut hasher = DefaultHasher::new();
        self.hash(&mut hasher);
        hasher.finish()
    }
}

/// Statistics for cache performance monitoring
#[derive(Debug, Clone, Default)]
pub struct CacheStats {
    /// Number of cache hits
    pub hits: u64,
    /// Number of cache misses
    pub misses: u64,
    /// Total number of entries in cache
    pub entries: usize,
    /// Maximum cache capacity
    pub capacity: usize,
    /// Hit rate (0.0 - 1.0)
    pub hit_rate: f64,
    /// Total memory usage estimate (bytes)
    pub memory_usage: usize,
}

impl CacheStats {
    /// Calculate hit rate
    fn calculate_hit_rate(&mut self) {
        let total = self.hits + self.misses;
        self.hit_rate = if total > 0 {
            self.hits as f64 / total as f64
        } else {
            0.0
        };
    }
}

/// Thread-safe LRU cache for phoneme conversion results
pub struct PhonemeCache {
    cache: Arc<Mutex<LruCache<CacheKey, Vec<String>>>>,
    stats: Arc<Mutex<CacheStats>>,
    capacity: usize,
}

impl PhonemeCache {
    /// Create a new phoneme cache with the specified capacity
    ///
    /// # Arguments
    ///
    /// * `capacity` - Maximum number of entries to cache (minimum: 10)
    ///
    /// # Example
    ///
    /// ```no_run
    /// use voirs_cli::performance::phoneme_cache::PhonemeCache;
    ///
    /// let cache = PhonemeCache::new(1000);
    /// ```
    pub fn new(capacity: usize) -> Self {
        let capacity = capacity.max(10); // Minimum capacity of 10
        let cache_capacity = NonZeroUsize::new(capacity).expect("Capacity must be non-zero");

        Self {
            cache: Arc::new(Mutex::new(LruCache::new(cache_capacity))),
            stats: Arc::new(Mutex::new(CacheStats {
                capacity,
                ..Default::default()
            })),
            capacity,
        }
    }

    /// Get phonemes from cache or compute them if not found
    ///
    /// # Arguments
    ///
    /// * `text` - Input text
    /// * `language` - Language code (e.g., "en", "ja")
    /// * `backend` - G2P backend identifier
    /// * `compute_fn` - Function to compute phonemes if not in cache
    ///
    /// # Returns
    ///
    /// Vector of phoneme strings
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use voirs_cli::performance::phoneme_cache::PhonemeCache;
    /// let cache = PhonemeCache::new(1000);
    ///
    /// let phonemes = cache.get_or_compute("hello", "en", "phonetisaurus", || {
    ///     // This expensive computation only runs on cache miss
    ///     vec!["h".to_string(), "ə".to_string(), "l".to_string(), "oʊ".to_string()]
    /// });
    /// ```
    pub fn get_or_compute<F>(
        &self,
        text: &str,
        language: &str,
        backend: &str,
        compute_fn: F,
    ) -> Vec<String>
    where
        F: FnOnce() -> Vec<String>,
    {
        let key = CacheKey::new(text, language, backend);

        // Try to get from cache first
        {
            let mut cache = self
                .cache
                .lock()
                .expect("Phoneme cache mutex poisoned - unrecoverable error");
            if let Some(phonemes) = cache.get(&key) {
                // Cache hit
                let mut stats = self
                    .stats
                    .lock()
                    .expect("Phoneme cache stats mutex poisoned - unrecoverable error");
                stats.hits += 1;
                stats.calculate_hit_rate();
                return phonemes.clone();
            }
        }

        // Cache miss: compute phonemes
        let phonemes = compute_fn();

        // Store in cache
        {
            let mut cache = self
                .cache
                .lock()
                .expect("Phoneme cache mutex poisoned - unrecoverable error");
            cache.put(key, phonemes.clone());

            let mut stats = self
                .stats
                .lock()
                .expect("Phoneme cache stats mutex poisoned - unrecoverable error");
            stats.misses += 1;
            stats.entries = cache.len();
            stats.calculate_hit_rate();

            // Estimate memory usage (rough approximation)
            stats.memory_usage = cache.len() * 256; // Assume ~256 bytes per entry
        }

        phonemes
    }

    /// Clear all cache entries
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use voirs_cli::performance::phoneme_cache::PhonemeCache;
    /// let cache = PhonemeCache::new(1000);
    /// cache.clear();
    /// ```
    pub fn clear(&self) {
        let mut cache = self
            .cache
            .lock()
            .expect("Phoneme cache mutex poisoned - unrecoverable error");
        cache.clear();

        let mut stats = self
            .stats
            .lock()
            .expect("Phoneme cache stats mutex poisoned - unrecoverable error");
        stats.entries = 0;
        stats.memory_usage = 0;
    }

    /// Get current cache statistics
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use voirs_cli::performance::phoneme_cache::PhonemeCache;
    /// let cache = PhonemeCache::new(1000);
    /// let stats = cache.stats();
    /// println!("Hit rate: {:.1}%", stats.hit_rate * 100.0);
    /// println!("Cache entries: {}/{}", stats.entries, stats.capacity);
    /// ```
    pub fn stats(&self) -> CacheStats {
        let cache = self
            .cache
            .lock()
            .expect("Phoneme cache mutex poisoned - unrecoverable error");
        let mut stats = self
            .stats
            .lock()
            .expect("Phoneme cache stats mutex poisoned - unrecoverable error");

        stats.entries = cache.len();
        stats.clone()
    }

    /// Resize the cache capacity
    ///
    /// # Arguments
    ///
    /// * `new_capacity` - New maximum number of entries
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use voirs_cli::performance::phoneme_cache::PhonemeCache;
    /// let cache = PhonemeCache::new(1000);
    /// cache.resize(2000); // Increase capacity to 2000
    /// ```
    pub fn resize(&mut self, new_capacity: usize) {
        let new_capacity = new_capacity.max(10);
        let cache_capacity = NonZeroUsize::new(new_capacity).expect("Capacity must be non-zero");

        let mut cache = self
            .cache
            .lock()
            .expect("Phoneme cache mutex poisoned - unrecoverable error");
        cache.resize(cache_capacity);

        let mut stats = self
            .stats
            .lock()
            .expect("Phoneme cache stats mutex poisoned - unrecoverable error");
        stats.capacity = new_capacity;
        self.capacity = new_capacity;
    }

    /// Check if a specific text is in the cache
    ///
    /// # Arguments
    ///
    /// * `text` - Input text
    /// * `language` - Language code
    /// * `backend` - G2P backend identifier
    ///
    /// # Returns
    ///
    /// `true` if the entry exists in cache, `false` otherwise
    pub fn contains(&self, text: &str, language: &str, backend: &str) -> bool {
        let key = CacheKey::new(text, language, backend);
        let cache = self
            .cache
            .lock()
            .expect("Phoneme cache mutex poisoned - unrecoverable error");
        cache.contains(&key)
    }

    /// Get the current number of cache entries
    pub fn len(&self) -> usize {
        let cache = self
            .cache
            .lock()
            .expect("Phoneme cache mutex poisoned - unrecoverable error");
        cache.len()
    }

    /// Check if the cache is empty
    pub fn is_empty(&self) -> bool {
        let cache = self
            .cache
            .lock()
            .expect("Phoneme cache mutex poisoned - unrecoverable error");
        cache.is_empty()
    }

    /// Get cache capacity
    pub fn capacity(&self) -> usize {
        self.capacity
    }

    /// Create a global phoneme cache instance
    ///
    /// This is useful for sharing a single cache across the entire application.
    pub fn global(capacity: usize) -> Arc<Self> {
        Arc::new(Self::new(capacity))
    }
}

impl Default for PhonemeCache {
    /// Create a default cache with capacity of 1000 entries
    fn default() -> Self {
        Self::new(1000)
    }
}

impl Clone for PhonemeCache {
    fn clone(&self) -> Self {
        Self {
            cache: Arc::clone(&self.cache),
            stats: Arc::clone(&self.stats),
            capacity: self.capacity,
        }
    }
}

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

    #[test]
    fn test_cache_basic_operations() {
        let cache = PhonemeCache::new(100);

        // First call: cache miss
        let result1 = cache.get_or_compute("hello", "en", "test", || {
            vec![
                "h".to_string(),
                "ɛ".to_string(),
                "l".to_string(),
                "".to_string(),
            ]
        });

        assert_eq!(result1.len(), 4);
        assert_eq!(cache.len(), 1);

        // Second call: cache hit
        let result2 = cache.get_or_compute("hello", "en", "test", || {
            panic!("Should not be called - should use cache");
        });

        assert_eq!(result1, result2);
    }

    #[test]
    fn test_cache_stats() {
        let cache = PhonemeCache::new(100);

        // Perform some operations
        cache.get_or_compute("hello", "en", "test", || vec!["h".to_string()]);
        cache.get_or_compute("hello", "en", "test", || vec!["h".to_string()]);
        cache.get_or_compute("world", "en", "test", || vec!["w".to_string()]);

        let stats = cache.stats();
        assert_eq!(stats.hits, 1); // Second "hello" was a hit
        assert_eq!(stats.misses, 2); // First "hello" and "world" were misses
        assert_eq!(stats.entries, 2);
        assert_eq!(stats.hit_rate, 1.0 / 3.0);
    }

    #[test]
    fn test_cache_clear() {
        let cache = PhonemeCache::new(100);

        cache.get_or_compute("hello", "en", "test", || vec!["h".to_string()]);
        assert_eq!(cache.len(), 1);

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

    #[test]
    fn test_cache_different_languages() {
        let cache = PhonemeCache::new(100);

        let en_result = cache.get_or_compute("hello", "en", "test", || {
            vec![
                "h".to_string(),
                "ɛ".to_string(),
                "l".to_string(),
                "".to_string(),
            ]
        });

        let ja_result = cache.get_or_compute("hello", "ja", "test", || {
            vec![
                "h".to_string(),
                "e".to_string(),
                "r".to_string(),
                "o".to_string(),
            ]
        });

        // Should be different due to different language
        assert_ne!(en_result, ja_result);
        assert_eq!(cache.len(), 2);
    }

    #[test]
    fn test_cache_resize() {
        let mut cache = PhonemeCache::new(10);

        // Add a few entries
        cache.get_or_compute("a", "en", "test", || vec!["a".to_string()]);
        cache.get_or_compute("b", "en", "test", || vec!["b".to_string()]);
        cache.get_or_compute("c", "en", "test", || vec!["c".to_string()]);

        assert_eq!(cache.len(), 3);
        assert_eq!(cache.capacity(), 10);

        // Resize to larger capacity
        cache.resize(20);
        assert_eq!(cache.capacity(), 20);

        // Entries should still be there
        assert!(cache.contains("a", "en", "test"));
        assert!(cache.contains("b", "en", "test"));
        assert!(cache.contains("c", "en", "test"));

        // Add more entries
        cache.get_or_compute("d", "en", "test", || vec!["d".to_string()]);
        cache.get_or_compute("e", "en", "test", || vec!["e".to_string()]);

        assert!(cache.len() >= 3); // Should have at least our original 3 entries
    }

    #[test]
    fn test_cache_contains() {
        let cache = PhonemeCache::new(100);

        assert!(!cache.contains("hello", "en", "test"));

        cache.get_or_compute("hello", "en", "test", || vec!["h".to_string()]);

        assert!(cache.contains("hello", "en", "test"));
        assert!(!cache.contains("hello", "ja", "test")); // Different language
        assert!(!cache.contains("world", "en", "test")); // Different text
    }

    #[test]
    fn test_cache_minimum_capacity() {
        let cache = PhonemeCache::new(0); // Should be clamped to minimum
        assert_eq!(cache.capacity(), 10);

        let cache2 = PhonemeCache::new(5); // Should be clamped to minimum
        assert_eq!(cache2.capacity(), 10);
    }

    #[test]
    fn test_cache_clone() {
        let cache1 = PhonemeCache::new(100);
        cache1.get_or_compute("hello", "en", "test", || vec!["h".to_string()]);

        let cache2 = cache1.clone();

        // Both should share the same cache
        assert_eq!(cache1.len(), cache2.len());

        cache2.get_or_compute("world", "en", "test", || vec!["w".to_string()]);

        // Change in cache2 should be visible in cache1
        assert_eq!(cache1.len(), 2);
        assert_eq!(cache2.len(), 2);
    }
}