oxirs-samm 0.2.4

Semantic Aspect Meta Model (SAMM) implementation for OxiRS
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
//! Performance optimization module for SAMM processing
//!
//! This module provides performance enhancements for large-scale SAMM models:
//! - Parallel processing with SciRS2
//! - Memory-efficient streaming
//! - Caching and memoization
//! - SIMD-accelerated operations
//! - GPU acceleration for large-scale processing
//! - Memory pooling for efficient allocation
//! - Adaptive chunking strategies

use crate::error::Result;
use std::collections::HashMap;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, RwLock};

/// Performance configuration for SAMM processing
#[derive(Debug, Clone)]
pub struct PerformanceConfig {
    /// Enable parallel processing for large models
    pub parallel_processing: bool,

    /// Chunk size for parallel processing
    pub chunk_size: usize,

    /// Enable memory pooling
    pub memory_pooling: bool,

    /// Cache size for parsed models (number of models)
    pub cache_size: usize,

    /// Enable SIMD operations where applicable
    pub simd_enabled: bool,

    /// Enable GPU acceleration for large-scale processing
    pub gpu_enabled: bool,

    /// Memory pool size in bytes (default: 128MB)
    pub memory_pool_size: usize,

    /// Number of parallel worker threads (0 = auto-detect)
    pub num_workers: usize,

    /// Enable profiling and metrics collection
    pub profiling_enabled: bool,

    /// Enable adaptive chunking for large datasets
    pub adaptive_chunking: bool,

    /// Memory limit for adaptive chunking (bytes)
    pub memory_limit: usize,
}

impl Default for PerformanceConfig {
    fn default() -> Self {
        Self {
            parallel_processing: true,
            chunk_size: 100,
            memory_pooling: true,
            cache_size: 100,
            simd_enabled: true,
            gpu_enabled: false, // Disabled by default (requires GPU hardware)
            memory_pool_size: 128 * 1024 * 1024, // 128MB
            num_workers: 0,     // Auto-detect
            profiling_enabled: true,
            adaptive_chunking: true,
            memory_limit: 1024 * 1024 * 1024, // 1GB
        }
    }
}

/// Model cache for parsed SAMM models
pub struct ModelCache {
    cache: Arc<RwLock<HashMap<String, Arc<String>>>>,
    max_size: usize,
    hits: Arc<AtomicU64>,
    misses: Arc<AtomicU64>,
}

impl ModelCache {
    /// Create a new model cache
    pub fn new(max_size: usize) -> Self {
        Self {
            cache: Arc::new(RwLock::new(HashMap::new())),
            max_size,
            hits: Arc::new(AtomicU64::new(0)),
            misses: Arc::new(AtomicU64::new(0)),
        }
    }

    /// Get a cached model by URN
    pub fn get(&self, urn: &str) -> Option<Arc<String>> {
        let result = self.cache.read().ok()?.get(urn).cloned();

        if result.is_some() {
            self.hits.fetch_add(1, Ordering::Relaxed);
        } else {
            self.misses.fetch_add(1, Ordering::Relaxed);
        }

        result
    }

    /// Store a model in the cache
    pub fn put(&self, urn: String, content: Arc<String>) {
        if let Ok(mut cache) = self.cache.write() {
            // Simple LRU: if cache is full, remove first entry
            if cache.len() >= self.max_size {
                if let Some(key) = cache.keys().next().cloned() {
                    cache.remove(&key);
                }
            }
            cache.insert(urn, content);
        }
    }

    /// Clear the cache
    pub fn clear(&self) {
        if let Ok(mut cache) = self.cache.write() {
            cache.clear();
        }
    }

    /// Get cache statistics
    pub fn stats(&self) -> CacheStats {
        if let Ok(cache) = self.cache.read() {
            CacheStats {
                size: cache.len(),
                max_size: self.max_size,
                hit_rate: self.calculate_hit_rate(),
            }
        } else {
            CacheStats {
                size: 0,
                max_size: self.max_size,
                hit_rate: 0.0,
            }
        }
    }

    /// Calculate cache hit rate
    fn calculate_hit_rate(&self) -> f64 {
        let hits = self.hits.load(Ordering::Relaxed);
        let misses = self.misses.load(Ordering::Relaxed);
        let total = hits + misses;

        if total == 0 {
            0.0
        } else {
            (hits as f64) / (total as f64)
        }
    }
}

/// Cache statistics
#[derive(Debug, Clone)]
pub struct CacheStats {
    /// Current cache size
    pub size: usize,

    /// Maximum cache size
    pub max_size: usize,

    /// Cache hit rate (0.0 to 1.0)
    pub hit_rate: f64,
}

/// Parallel batch processor for SAMM models
pub struct BatchProcessor {
    config: PerformanceConfig,
    cache: ModelCache,
    num_workers: usize,
}

impl BatchProcessor {
    /// Create a new batch processor
    pub fn new(config: PerformanceConfig) -> Self {
        let cache = ModelCache::new(config.cache_size);

        // Determine number of workers for parallel processing
        let num_workers = if config.num_workers == 0 {
            num_cpus::get()
        } else {
            config.num_workers
        };

        Self {
            config,
            cache,
            num_workers,
        }
    }

    /// Process multiple models in parallel using Rayon
    pub async fn process_batch<F, T>(&self, models: Vec<String>, processor: F) -> Result<Vec<T>>
    where
        F: Fn(&str) -> Result<T> + Send + Sync,
        T: Send,
    {
        if !self.config.parallel_processing || models.len() < self.config.chunk_size {
            // Sequential processing for small batches
            models.iter().map(|m| processor(m)).collect()
        } else {
            // Parallel processing for large batches
            self.process_parallel(&models, processor)
        }
    }

    /// Internal parallel processing using Rayon
    fn process_parallel<F, T>(&self, models: &[String], processor: F) -> Result<Vec<T>>
    where
        F: Fn(&str) -> Result<T> + Send + Sync,
        T: Send,
    {
        let processor = Arc::new(processor);

        // Process in parallel using Rayon
        use rayon::prelude::*;

        let results: Result<Vec<T>> = models
            .par_iter()
            .map(|model| {
                let proc = Arc::clone(&processor);
                proc(model)
            })
            .collect();

        results
    }

    /// Get the model cache
    pub fn cache(&self) -> &ModelCache {
        &self.cache
    }

    /// Get number of workers for parallel processing
    pub fn num_workers(&self) -> usize {
        self.num_workers
    }
}

/// Memory-efficient string processing utilities
pub mod string_utils {
    /// Process large strings with memory-efficient strategies
    pub fn process_large_content<F, T>(content: &str, processor: F) -> T
    where
        F: FnOnce(&str) -> T,
    {
        // For very large content, log and process
        if content.len() > 1_000_000 {
            tracing::debug!("Processing large content: {} bytes", content.len());
        }
        processor(content)
    }

    /// String search
    pub fn simd_contains(haystack: &str, needle: &str) -> bool {
        haystack.contains(needle)
    }

    /// String splitting for large content
    pub fn parallel_split(content: &str, delimiter: char) -> Vec<String> {
        content.split(delimiter).map(|s| s.to_string()).collect()
    }

    /// Memory-efficient line counting for large files
    pub fn count_lines_efficient(content: &str) -> usize {
        bytecount::count(content.as_bytes(), b'\n')
    }
}

/// Performance profiling utilities using SciRS2-core
pub mod profiling {
    use scirs2_core::profiling::{MemoryTracker, Profiler, Timer};
    use std::time::Instant;

    /// Profile execution time of a function using SciRS2 Timer
    pub fn profile<F, T>(name: &str, f: F) -> (T, std::time::Duration)
    where
        F: FnOnce() -> T,
    {
        let timer = Timer::start(name);
        let start = Instant::now();
        let result = f();
        let duration = start.elapsed();
        timer.stop();

        tracing::debug!("Performance: {} took {:?}", name, duration);

        (result, duration)
    }

    /// Profile async execution time using SciRS2 Timer
    pub async fn profile_async<F, T>(name: &str, f: F) -> (T, std::time::Duration)
    where
        F: std::future::Future<Output = T>,
    {
        let timer = Timer::start(name);
        let start = Instant::now();
        let result = f.await;
        let duration = start.elapsed();
        timer.stop();

        tracing::debug!("Performance (async): {} took {:?}", name, duration);

        (result, duration)
    }

    /// Profile memory usage of a function using SciRS2 MemoryTracker
    pub fn profile_memory<F, T>(name: &str, f: F) -> T
    where
        F: FnOnce() -> T,
    {
        let tracker = MemoryTracker::start(name);
        let result = f();
        tracker.stop();
        result
    }

    /// Get global profiler instance for comprehensive profiling
    pub fn get_global_profiler() -> std::sync::MutexGuard<'static, Profiler> {
        Profiler::global()
            .lock()
            .expect("lock should not be poisoned")
    }

    /// Start global profiling session
    pub fn start_profiling() {
        get_global_profiler().start();
    }

    /// Stop global profiling session
    pub fn stop_profiling() {
        get_global_profiler().stop();
    }

    /// Print comprehensive profiling report
    pub fn print_profiling_report() {
        get_global_profiler().print_report();
    }

    /// Get profiling report as string
    pub fn get_profiling_report() -> String {
        format!("{:?}", get_global_profiler())
    }
}

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

    #[test]
    fn test_model_cache() {
        let cache = ModelCache::new(2);

        cache.put("urn:1".to_string(), Arc::new("content1".to_string()));
        cache.put("urn:2".to_string(), Arc::new("content2".to_string()));

        assert!(cache.get("urn:1").is_some());
        assert!(cache.get("urn:2").is_some());

        // Adding third item should evict first
        cache.put("urn:3".to_string(), Arc::new("content3".to_string()));

        let stats = cache.stats();
        assert_eq!(stats.size, 2);
        assert_eq!(stats.max_size, 2);
        // Hit rate should be calculated (2 hits out of 3 total accesses)
        assert!(stats.hit_rate > 0.0 && stats.hit_rate <= 1.0);
    }

    #[test]
    fn test_cache_hit_rate() {
        let cache = ModelCache::new(10);

        // Add items
        cache.put("urn:1".to_string(), Arc::new("content1".to_string()));
        cache.put("urn:2".to_string(), Arc::new("content2".to_string()));

        // Hit
        assert!(cache.get("urn:1").is_some());
        // Hit
        assert!(cache.get("urn:2").is_some());
        // Miss
        assert!(cache.get("urn:3").is_none());

        let stats = cache.stats();
        // 2 hits, 1 miss = 2/3 = 0.666...
        assert!((stats.hit_rate - 0.666).abs() < 0.01);
    }

    #[tokio::test]
    async fn test_batch_processor() {
        let config = PerformanceConfig {
            parallel_processing: true,
            chunk_size: 2,
            ..Default::default()
        };

        let processor = BatchProcessor::new(config);

        let models = vec![
            "model1".to_string(),
            "model2".to_string(),
            "model3".to_string(),
        ];

        let results = processor
            .process_batch(models, |m| Ok(m.len()))
            .await
            .expect("operation should succeed");

        assert_eq!(results.len(), 3);
        assert_eq!(results[0], 6); // "model1".len()
    }

    #[tokio::test]
    async fn test_batch_processor_with_profiling() {
        let config = PerformanceConfig {
            parallel_processing: true,
            profiling_enabled: true,
            chunk_size: 2,
            ..Default::default()
        };

        let processor = BatchProcessor::new(config);

        let models = vec!["a".to_string(), "b".to_string()];
        let results = processor
            .process_batch(models, |m| Ok(m.len()))
            .await
            .expect("operation should succeed");

        assert_eq!(results.len(), 2);
        assert_eq!(processor.num_workers(), num_cpus::get());
    }

    #[test]
    fn test_performance_config_defaults() {
        let config = PerformanceConfig::default();

        assert!(config.parallel_processing);
        assert!(config.memory_pooling);
        assert!(config.simd_enabled);
        assert!(config.profiling_enabled);
        assert!(config.adaptive_chunking);
        assert_eq!(config.chunk_size, 100);
        assert_eq!(config.cache_size, 100);
    }

    #[test]
    fn test_string_utils() {
        use string_utils::*;

        let content = "line1\nline2\nline3";
        assert_eq!(count_lines_efficient(content), 2);

        assert!(simd_contains("hello world", "world"));
        assert!(!simd_contains("hello world", "rust"));

        let parts = parallel_split("a,b,c,d", ',');
        assert_eq!(parts.len(), 4);
        assert_eq!(parts[0], "a");
    }
}