amberjs 1.17.0

JavaScript/TypeScript runtime built with Rust and V8
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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
// WASM 模块缓存系统
//
// 提供高效的 WebAssembly 模块缓存功能,包括多级缓存(L1 内存 + L2 文件)、
// 智能缓存策略、缓存预热和更新机制等

use anyhow::{Context, Result, anyhow};
use std::collections::{BTreeMap, HashMap};
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex, RwLock};
use std::sync::atomic::Ordering;
use std::time::{Duration, Instant};
use std::time::SystemTime;
use std::hash::Hasher;

/// 缓存条目结构体
#[derive(Debug, Clone)]
struct CacheEntry {
    /// 模块字节码
    wasm_bytes: Vec<u8>,
    /// 缓存时间
    cached_at: Instant,
    /// 最后访问时间
    last_access: Instant,
    /// 访问次数
    access_count: usize,
    /// 文件路径(如果已持久化)
    file_path: Option<PathBuf>,
    /// 模块大小
    size: usize,
}
impl CacheEntry {
    /// 创建新的缓存条目
    fn new(wasm_bytes: Vec<u8>) -> Self {
        let now: _ = Instant::now();
        CacheEntry {
            wasm_bytes,
            cached_at: now,
            last_access: now,
            access_count: 0,
            file_path: None,
            size: 0,
        }
    }
    /// 更新访问信息
    fn update_access(&mut self) {
        self.last_access = Instant::now();
        self.access_count += 1;
    }
    /// 计算缓存年龄
    fn age(&self) -> Duration {
        self.cached_at.elapsed()
    }
    /// 计算缓存使用率(基于访问频率)
    fn usage_score(&self) -> f64 {
        let age_secs: _ = self.cached_at.elapsed().as_secs_f64();
        if age_secs == 0.0 {
            self.access_count as f64
        } else {
            self.access_count as f64 / age_secs
        }
    }
}
/// L1 内存缓存
type L1Cache = HashMap<String, Arc<RwLock<CacheEntry>>>;
/// L2 文件缓存
type L2Cache = HashMap<String, PathBuf>;
/// 缓存统计信息
#[derive(Debug, Clone, Default)]
pub struct CacheStats {
    /// L1 缓存条目数
    pub l1_entries: usize,
    /// L2 缓存条目数
    pub l2_entries: usize,
    /// 缓存命中次数
    pub hits: usize,
    /// 缓存未命中次数
    pub misses: usize,
    /// 总模块数
    pub total_modules: usize,
    /// 总缓存大小(字节)
    pub total_size: usize,
    /// 缓存命中率
    pub hit_ratio: f64,
    /// 平均缓存加载时间
    pub avg_load_time: Duration,
}
/// 缓存策略配置
#[derive(Debug, Clone)]
struct CacheConfig {
    /// L1 缓存最大条目数
    max_l1_entries: usize,
    /// L1 缓存最大大小(字节)
    max_l1_size: usize,
    /// L2 缓存目录
    l2_cache_dir: PathBuf,
    /// 缓存淘汰阈值(使用率)
    eviction_threshold: f64,
    /// 缓存过期时间
    expiration_time: Duration,
    /// 是否启用 L2 缓存
    enable_l2: bool,
}
impl Default for CacheConfig {
    fn default() -> Self {
        Self {
            max_l1_entries: 1000,
            max_l1_size: 100 * 1024 * 1024, // 100MB
            l2_cache_dir: PathBuf::from("./wasm_cache"),
            eviction_threshold: 0.1,
            expiration_time: Duration::from_secs(3600), // 1小时
            enable_l2: true,
        }
    }
}
/// WASM 模块缓存管理器
///
/// 提供多级缓存、智能缓存策略和高性能的模块缓存功能
pub struct WasmModuleCache {
    /// L1 内存缓存
    l1_cache: Arc<Mutex<L1Cache>>,
    /// L2 文件缓存
    l2_cache: Arc<Mutex<L2Cache>>,
    /// 缓存配置
    config: CacheConfig,
    /// 统计信息
    stats: Arc<Mutex<CacheStats>>,
    /// 缓存加载时间追踪
    load_times: Arc<Mutex<Vec<Duration>>>,
}
impl WasmModuleCache {
    /// 创建新的模块缓存管理器
    ///
    /// # 返回值
    /// * `Result<WasmModuleCache>` - 缓存管理器实例
    ///
    /// # 示例
    /// ```
    /// let cache: _ = WasmModuleCache::new()?;
    /// ```
    pub fn new() -> Result<Self> {
        Self::new_with_config(CacheConfig::default())
    }
    /// 使用自定义配置创建缓存管理器
    ///
    /// # 参数
    /// * `config` - 缓存配置
    ///
    /// # 返回值
    /// * `Result<WasmModuleCache>` - 缓存管理器实例
    pub fn new_with_config(config: CacheConfig) -> Result<Self> {
        // 创建 L2 缓存目录
        if config.enable_l2 {
            std::fs::create_dir_all(&config.l2_cache_dir)
                .context("Failed to create L2 cache directory")?;
        }
        Ok(WasmModuleCache {
            l1_cache: Arc::new(Mutex::new(HashMap::new())),
            l2_cache: Arc::new(Mutex::new(HashMap::new())),
            config,
            stats: Arc::new(Mutex::new(CacheStats::default())),
            load_times: Arc::new(Mutex::new(Vec::new())),
        })
    }
    /// 存储模块到缓存
    ///
    /// # 参数
    /// * `module_hash` - 模块哈希值
    /// * `wasm_bytes` - WASM 字节码
    ///
    /// # 返回值
    /// * `Result<()>` - 成功返回空,失败返回错误
    ///
    /// # 示例
    /// ```
    /// let hash: _ = cache.calculate_hash(&wasm_bytes);
    /// cache.store_module(hash, wasm_bytes)?;
    /// ```
    pub fn store_module(&self, module_hash: String, wasm_bytes: Vec<u8>) -> Result<()> {
        let entry: _ = Arc::new(Mutex::new(CacheEntry::new(wasm_bytes)));
        // 尝试存储到 L1 缓存
        {
            let mut l1 = self.l1_cache.lock().unwrap();
            // 检查 L1 缓存是否已满
            if l1.len() >= self.config.max_l1_entries {
                self.evict_l1_cache()?;
            }
            l1.insert(module_hash.clone(), Arc::clone(entry));
        }
        // 如果启用 L2 缓存,同时存储到文件
        if self.config.enable_l2 {
            self.store_to_l2(&module_hash, &entry)?;
        }
        // 更新统计信息
        self.update_stats_after_store(&entry.read().unwrap());
        Ok(())
    }
    /// 从缓存加载模块
    ///
    /// # 参数
    /// * `module_hash` - 模块哈希值
    ///
    /// # 返回值
    /// * `Result<Vec<u8>` - 成功返回 WASM 字节,失败返回错误
    ///
    /// # 示例
    /// ```
    /// let wasm_bytes: _ = cache.load_module(module_hash)?;
    /// ```
    pub fn load_module(&self, module_hash: &str) -> Result<Vec<u8>> {
        let start: _ = Instant::now();
        // 先尝试从 L1 缓存加载
        {
            let l1: _ = self.l1_cache.lock().unwrap();
            if let Some(entry) = l1.get(module_hash) {
                let mut entry = entry.write().unwrap();
                entry.update_access();
                let load_time: _ = start.elapsed();
                self.record_load_time(load_time);
                self.update_stats_after_hit(true);
                return Ok(entry.wasm_bytes.clone());
            }
        }
        // L1 缓存未命中,尝试从 L2 缓存加载
        if self.config.enable_l2 {
            let wasm_bytes: _ = self.load_from_l2(module_hash)?;
            if !wasm_bytes.is_empty() {
                // 加载到 L1 缓存
                self.store_module(module_hash.to_string(), wasm_bytes.clone())?;
                let load_time: _ = start.elapsed();
                self.record_load_time(load_time);
                self.update_stats_after_hit(true);
                return Ok(wasm_bytes);
            }
        }
        // 缓存未命中
        self.update_stats_after_hit(false);
        Err(anyhow!("Module not found in cache: {}", module_hash))
    }
    /// 检查模块是否在缓存中
    ///
    /// # 参数
    /// * `module_hash` - 模块哈希值
    ///
    /// # 返回值
    /// * `bool` - 如果在缓存中返回 true,否则返回 false
    pub fn contains(&self, module_hash: &str) -> bool {
        let l1: _ = self.l1_cache.lock().unwrap();
        if l1.contains_key(module_hash) {
            return true;
        }
        if self.config.enable_l2 {
            let l2: _ = self.l2_cache.lock().unwrap();
            return l2.contains_key(module_hash);
        }
        false
    }
    /// 预热缓存
    ///
    /// # 参数
    /// * `modules` - 模块列表 (哈希值, 字节码)
    ///
    /// # 返回值
    /// * `Result<()>` - 成功返回空,失败返回错误
    pub fn warmup_cache(&self, modules: Vec<(String, Vec<u8>)>) -> Result<()> {
        for (hash, wasm_bytes) in modules {
            self.store_module(hash, wasm_bytes)?;
        }
        Ok(())
    }
    /// 清空缓存
    ///
    /// # 返回值
    /// * `Result<()>` - 成功返回空,失败返回错误
    pub fn clear_cache(&self) -> Result<()> {
        // 清空 L1 缓存
        {
            let mut l1 = self.l1_cache.lock().unwrap();
            l1.clear();
        }
        // 清空 L2 缓存
        if self.config.enable_l2 {
            let l2: _ = self.l2_cache.lock().unwrap();
            for (_, file_path) in l2.iter() {
                let _: _ = std::fs::remove_file(file_path);
            }
        }
        // 重置统计信息
        {
            let mut stats = self.stats.lock().unwrap();
            *stats = CacheStats::default();
        }
        Ok(())
    }
    /// 获取缓存统计信息
    ///
    /// # 返回值
    /// * `CacheStats` - 统计信息
    pub fn get_stats(&self) -> CacheStats {
        let mut stats = self.stats.lock().unwrap();
        // 更新 L1 和 L2 缓存条目数
        {
            let l1: _ = self.l1_cache.lock().unwrap();
            stats.l1_entries = l1.len();
        }
        if self.config.enable_l2 {
            let l2: _ = self.l2_cache.lock().unwrap();
            stats.l2_entries = l2.len();
        }
        // 计算总模块数和大小
        let mut total_modules = 0;
        let mut total_size = 0;
        {
            let l1: _ = self.l1_cache.lock().unwrap();
            for entry in l1.values() {
                let entry = entry.read().unwrap();
                total_modules += 1;
                total_size += entry.size;
            }
        }
        stats.total_modules = total_modules;
        stats.total_size = total_size;
        // 计算缓存命中率
        let total_accesses: _ = stats.hits + stats.misses;
        stats.hit_ratio = if total_accesses > 0 {
            stats.hits as f64 / total_accesses as f64
        } else {
            0.0
        };
        // 计算平均加载时间
        {
            let load_times: _ = self.load_times.lock().unwrap();
            if !load_times.is_empty() {
                let total: Duration = load_times.iter().sum();
                stats.avg_load_time = Duration::from_nanos(total.as_nanos() as u64 / load_times.len() as u64);
            }
        }
        stats.clone()
    }
    /// 计算模块哈希值
    ///
    /// # 参数
    /// * `wasm_bytes` - WASM 字节码
    ///
    /// # 返回值
    /// * `String` - 哈希值
    pub fn calculate_hash(&self, wasm_bytes: &[u8]) -> String {
        let mut hasher = Hasher::new();
        hasher.update(wasm_bytes);
        hasher.finalize().to_hex().to_string()
    }
    /// 淘汰 L1 缓存
    fn evict_l1_cache(&self) -> Result<()> {
        // 按使用率排序,淘汰使用率最低的条目
        let hashes_to_evict: Vec<String> = {
            let l1: _ = self.l1_cache.lock().unwrap();
            let mut entries: Vec<(String, f64)> = l1.iter()
                .map(|(hash, entry)| {
                    let score: _ = entry.read().unwrap().usage_score();
                    (hash.clone(), score)
                })
                .collect();
            entries.sort_by(|a, b| {
                a.1.partial_cmp(&b.1).unwrap_or(std::cmp::Ordering::Equal)
            });
            // 提取要淘汰的哈希
            let to_evict: _ = entries.len() / 2;
            entries.into_iter().take(to_evict).map(|(hash, _)| hash).collect()
        };
        // 移除条目
        {
            let mut l1 = self.l1_cache.lock().unwrap();
            for hash in hashes_to_evict {
                l1.remove(hash.as_str());
            }
        }
        Ok(())
    }
    /// 存储到 L2 缓存
    fn store_to_l2(&self, module_hash: &str, entry: &Arc<RwLock<CacheEntry>>) -> Result<()> {
        let file_path: _ = self.config.l2_cache_dir.join(format!("{}.wasm", module_hash));
        let wasm_bytes: _ = {
            let entry = entry.read().unwrap();
            entry.wasm_bytes.clone()
        };
        std::fs::write(&file_path, wasm_bytes)
            .context("Failed to write to L2 cache")?;
        // 更新缓存映射
        {
            let mut l2 = self.l2_cache.lock().unwrap();
            l2.insert(module_hash.to_string(), file_path.clone());
        }
        // 更新条目信息
        {
            let mut entry = entry.write().unwrap();
            entry.file_path = Some(file_path);
        }
        Ok(())
    }
    /// 从 L2 缓存加载
    fn load_from_l2(&self, module_hash: &str) -> Result<Vec<u8>> {
        let l2: _ = self.l2_cache.lock().unwrap();
        if let Some(file_path) = l2.get(module_hash) {
            let wasm_bytes: _ = std::fs::read(file_path)
                .context("Failed to read from L2 cache")?;
            // 更新文件访问时间
            let _: _ = filetime::set_file_atime(
                file_path,
                filetime::FileTime::now()
            );
            Ok(wasm_bytes)
        } else {
            Ok(Vec::new())
        }
    }
    /// 记录加载时间
    fn record_load_time(&self, load_time: Duration) {
        let mut times = self.load_times.lock().unwrap();
        times.push(load_time);
        // 保持最近 1000 次的记录
        if times.len() > 1000 {
            times.remove(0);
        }
    }
    /// 更新命中统计
    fn update_stats_after_hit(&self, hit: bool) {
        let mut stats = self.stats.lock().unwrap();
        if hit {
            stats.hits += 1;
        } else {
            stats.misses += 1;
        }
    }
    /// 更新存储统计
    fn update_stats_after_store(&self, entry: &CacheEntry) {
        let mut stats = self.stats.lock().unwrap();
        stats.total_modules += 1;
        stats.total_size += entry.size;
    }
    /// 清理过期缓存
    pub fn cleanup_expired(&self) -> Result<usize> {
        let mut cleaned = 0;
        let now: _ = Instant::now();
        // 清理 L1 缓存
        {
            let mut l1 = self.l1_cache.lock().unwrap();
            let expired_keys: Vec<String> = l1.iter()
                .filter_map(|(hash, entry)| {
                    let entry = entry.read().unwrap();
                    if entry.age() > self.config.expiration_time {
                        Some(hash.clone())
                    } else {
                        None
                    }
                })
                .collect();
            for key in expired_keys {
                l1.remove(&key);
                cleaned += 1;
            }
        }
        // 清理 L2 缓存文件
        if self.config.enable_l2 {
            let mut l2 = self.l2_cache.lock().unwrap();
            let sys_now: _ = std::time::SystemTime::now();
            let expired_keys: Vec<String> = l2.iter()
                .filter_map(|(hash, path)| {
                    if let Ok(metadata) = std::fs::metadata(path) {
                        if let Ok(modified) = metadata.modified() {
                            if let Ok(elapsed) = sys_now.duration_since(modified) {
                                if elapsed > self.config.expiration_time {
                                    return Some(hash.clone());
                                }
                            }
                        }
                    }
                    None
                })
                .collect();
            for key in expired_keys {
                if let Some(path) = l2.remove(&key) {
                    let _: _ = std::fs::remove_file(path);
                    cleaned += 1;
                }
            }
        }
        Ok(cleaned)
    }
}
impl Drop for WasmModuleCache {
    fn drop(&mut self) {
        // 确保缓存目录清理
        if self.config.enable_l2 {
            let l2: _ = self.l2_cache.lock().unwrap();
            if l2.is_empty() {
                let _: _ = std::fs::remove_dir_all(&self.config.l2_cache_dir);
            }
        }
    }
}
#[cfg(test)]
mod tests {
    #[test]
    fn test_cache_creation() {
        let cache: _ = WasmModuleCache::new();
        assert!(cache.is_ok());
    }
    #[test]
    fn test_cache_store_and_load() {
        let cache: _ = WasmModuleCache::new().unwrap();
        let wasm_bytes: _ = vec![0, 1, 2, 3, 4];
        let hash: _ = cache.calculate_hash(&wasm_bytes);
        let result: _ = cache.store_module(hash.clone(), wasm_bytes.clone());
        assert!(result.is_ok());
        let loaded: _ = cache.load_module(&hash);
        assert!(loaded.is_ok());
        assert_eq!(loaded.unwrap(), wasm_bytes);
    }
    #[test]
    fn test_cache_contains() {
        let cache: _ = WasmModuleCache::new().unwrap();
        let wasm_bytes: _ = vec![0, 1, 2, 3, 4];
        let hash: _ = cache.calculate_hash(&wasm_bytes);
        assert!(!cache.contains(&hash));
        cache.store_module(hash.clone(), wasm_bytes).unwrap();
        assert!(cache.contains(&hash));
    }
    #[test]
    fn test_cache_miss() {
        let cache: _ = WasmModuleCache::new().unwrap();
        let result: _ = cache.load_module("nonexistent");
        assert!(result.is_err());
    }
    #[test]
    fn test_cache_stats() {
        let cache: _ = WasmModuleCache::new().unwrap();
        let wasm_bytes: _ = vec![0, 1, 2, 3, 4];
        let hash: _ = cache.calculate_hash(&wasm_bytes);
        cache.store_module(hash.clone(), wasm_bytes).unwrap();
        cache.load_module(&hash).unwrap();
        let stats: _ = cache.get_stats();
        assert_eq!(stats.l1_entries, 1);
        assert!(stats.hits > 0);
        assert_eq!(stats.misses, 0);
        assert!(stats.hit_ratio > 0.0);
    }
    #[test]
    fn test_cache_clear() {
        let cache: _ = WasmModuleCache::new().unwrap();
        let wasm_bytes: _ = vec![0, 1, 2, 3, 4];
        let hash: _ = cache.calculate_hash(&wasm_bytes);
        cache.store_module(hash.clone(), wasm_bytes).unwrap();
        cache.clear_cache().unwrap();
        assert!(!cache.contains(&hash));
        let stats: _ = cache.get_stats();
        assert_eq!(stats.l1_entries, 0);
        assert_eq!(stats.total_modules, 0);
    }
    #[test]
    fn test_hash_calculation() {
        let cache: _ = WasmModuleCache::new().unwrap();
        let wasm_bytes: _ = vec![0, 1, 2, 3, 4];
        let hash1: _ = cache.calculate_hash(&wasm_bytes);
        let wasm_bytes2: _ = vec![0, 1, 2, 3, 5];
        let hash2: _ = cache.calculate_hash(&wasm_bytes2);
        assert_ne!(hash1, hash2);
    }
    #[test]
    fn test_cache_warmup() {
        let cache: _ = WasmModuleCache::new().unwrap();
        let modules: _ = vec![
            ("hash1".to_string(), vec![0, 1, 2]),
            ("hash2".to_string(), vec![3, 4, 5]),
        ];
        let result: _ = cache.warmup_cache(modules);
        assert!(result.is_ok());
        let stats: _ = cache.get_stats();
        assert_eq!(stats.l1_entries, 2);
    }
    #[test]
    fn test_batch_operations() {
        let cache: _ = WasmModuleCache::new().unwrap();
        // 批量存储
        for i in 0..10 {
            let wasm_bytes: _ = vec![i as u8; 100];
            let hash: _ = cache.calculate_hash(&wasm_bytes);
            cache.store_module(hash, wasm_bytes).unwrap();
        }
        // 批量加载
        for i in 0..10 {
            let wasm_bytes: _ = vec![i as u8; 100];
            let hash: _ = cache.calculate_hash(&wasm_bytes);
            let result: _ = cache.load_module(&hash);
            assert!(result.is_ok());
        }
        let stats: _ = cache.get_stats();
        assert!(stats.hit_ratio > 0.8);
    }
}