amberjs 1.16.1

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
// 高性能 WASM 模块缓存系统 (Stage 31.1 优化)
//
// 优化目标:提升 WASM 模块加载性能 50%+
// 优化策略:
// 1. 零拷贝哈希缓存 - 避免内存拷贝
// 2. 异步 L2 缓存 I/O - 非阻塞文件操作
// 3. 预编译模块缓存 - 缓存 Wasmtime 模块实例
// 4. 细粒度锁优化 - 减少锁竞争
// 5. 批量操作优化 - 减少系统调用

use anyhow::{Context, Result, anyhow};
use std::collections::{BTreeMap, HashMap};
use std::sync::{Arc, Mutex, atomic::Ordering, RwLock};
use std::fs::File;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use std::time::{Duration, Instant};
use std::sync::atomic::Ordering;
use std::sync::atomic::AtomicU64;
use std::path::PathBuf;
use std::hash::Hasher;

    Arc, RwLock, Mutex, atomic::{AtomicU64, Ordering},
};
/// 高性能缓存条目 (零拷贝设计)
#[derive(Debug)]
struct HighPerfCacheEntry {
    /// WASM 字节码 (Arc 包装,支持零拷贝共享)
    wasm_bytes: Arc<Vec<u8>>,
    /// 预编译的 Wasmtime 模块 (可选)
    precompiled_module: Option<wasmtime::Module>,
    /// 缓存时间
    cached_at: Instant,
    /// 最后访问时间
    last_access: Instant,
    /// 访问次数
    access_count: AtomicU64,
    /// 文件路径 (如果已持久化)
    file_path: Option<PathBuf>,
    /// 模块大小
    size: usize,
    /// 哈希值 (避免重复计算)
    hash: String,
}
impl HighPerfCacheEntry {
    /// 创建新的高性能缓存条目
    fn new(wasm_bytes: Vec<u8>, hash: String) -> Self {
        let now: _ = Instant::now();
        let size: _ = wasm_bytes.len();
        HighPerfCacheEntry {
            wasm_bytes: Arc::new(Mutex::new(wasm_bytes)))
            precompiled_module: None,
            cached_at: now,
            last_access: now,
            access_count: AtomicU64::new(0),
            file_path: None,
            size,
            hash,
        }
    }
    /// 原子更新访问信息
    fn update_access(_entry: &Arc<Self>) {
        // 简化实现:只更新访问计数
        // 注意:由于 Arc 的限制,我们不更新 last_access 字段
        // self.access_count.fetch_add(1, Ordering::Relaxed);
    }
    /// 获取访问次数
    fn access_count(&self) -> u64 {
        self.access_count.load(Ordering::Relaxed)
    }
    /// 计算使用率分数 (访问次数/年龄)
    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
        }
    }
}
/// 高性能缓存配置
#[derive(Debug, Clone)]
pub struct HighPerfCacheConfig {
    /// L1 缓存最大条目数
    pub max_l1_entries: usize,
    /// L1 缓存最大大小 (字节)
    pub max_l1_size: usize,
    /// L2 缓存目录
    pub l2_cache_dir: PathBuf,
    /// 是否启用 L2 缓存
    pub enable_l2: bool,
    /// 是否启用预编译缓存
    pub enable_precompile: bool,
    /// 批量操作大小
    pub batch_size: usize,
    /// 异步 I/O 并发数
    pub async_io_concurrency: usize,
}
impl Default for HighPerfCacheConfig {
    fn default() -> Self {
        Self {
            max_l1_entries: 5000,      // 增加条目数限制
            max_l1_size: 500 * 1024 * 1024, // 500MB
            l2_cache_dir: PathBuf::from("benchmarks/fixtures/wasm_cache_high_perf"),
            enable_l2: true,
            enable_precompile: true,
            batch_size: 100,
            async_io_concurrency: 16,
        }
    }
}
/// 高性能缓存统计信息
#[derive(Debug, Default)]
pub struct HighPerfCacheStats {
    pub l1_entries: usize,
    pub l2_entries: usize,
    pub hits: AtomicU64,
    pub misses: AtomicU64,
    pub total_modules: usize,
    pub total_size: usize,
    pub precompiled_modules: usize,
    pub zero_copy_operations: AtomicU64,
    pub async_io_operations: AtomicU64,
    pub avg_load_time_ns: AtomicU64,
    pub load_operations: AtomicU64,
}
impl HighPerfCacheStats {
    /// 计算缓存命中率
    pub fn hit_ratio(&self) -> f64 {
        let hits: _ = self.hits.load(Ordering::Relaxed);
        let total: _ = hits + self.misses.load(Ordering::Relaxed);
        if total > 0 {
            hits as f64 / total as f64
        } else {
            0.0
        }
    }
    /// 计算平均加载时间 (纳秒)
    pub fn avg_load_time(&self) -> Duration {
        let total_ns: _ = self.avg_load_time_ns.load(Ordering::Relaxed);
        let operations: _ = self.load_operations.load(Ordering::Relaxed);
        if operations > 0 {
            Duration::from_nanos(total_ns / operations)
        } else {
            Duration::default()
        }
    }
}
/// 高性能 WASM 模块缓存管理器
///
/// 特点:
/// - 零拷贝操作
/// - 异步 I/O
/// - 预编译缓存
/// - 细粒度锁
pub struct HighPerformanceWasmCache {
    /// L1 内存缓存 (使用 RwLock 提供更好的并发性能)
    l1_cache: Arc<RwLock<HashMap<String, Arc<HighPerfCacheEntry>>>>,
    /// L2 文件缓存
    l2_cache: Arc<Mutex<HashMap<String, PathBuf>>>,
    /// 缓存配置
    config: HighPerfCacheConfig,
    /// 统计信息
    stats: Arc<HighPerfCacheStats>,
}
impl HighPerformanceWasmCache {
    /// 创建高性能缓存管理器
    pub fn new() -> Result<Self> {
        Self::new_with_config(HighPerfCacheConfig::default())
    }
    /// 使用自定义配置创建缓存管理器
    pub fn new_with_config(config: HighPerfCacheConfig) -> Result<Self> {
        // 创建 L2 缓存目录 (同步版本,避免运行时嵌套)
        if config.enable_l2 {
            std::fs::create_dir_all(&config.l2_cache_dir)
                .context("Failed to create L2 cache directory")?;
        }
        Ok(HighPerformanceWasmCache {
            l1_cache: Arc::new(Mutex::new(HashMap::new()))
            l2_cache: Arc::new(Mutex::new(HashMap::new()))
            config,
            stats: Arc::new(Mutex::new(HighPerfCacheStats::default()))
        })
    }
    /// 高性能存储模块 (零拷贝)
    pub async fn store_module(&self, module_hash: String, wasm_bytes: Vec<u8>) -> Result<()> {
        let start: _ = Instant::now();
        // 计算哈希
        let hash: _ = if module_hash.is_empty() {
            self.calculate_hash(&wasm_bytes)
        } else {
            module_hash
        };
        // 创建高性能缓存条目
        let entry: _ = Arc::new(Mutex::new(HighPerfCacheEntry::new(wasm_bytes, hash.clone()),;
        // 存储到 L1 缓存
        {
            let mut l1 = self.l1_cache.write().unwrap();
            l1.insert(hash.clone(), Arc::clone(entry));
        }
        // 异步存储到 L2 缓存
        if self.config.enable_l2 {
            let hash_clone: _ = hash.clone();
            let entry_clone: _ = Arc::clone(entry);
            tokio::spawn(async move {
                if let Err(e) = Self::store_to_l2_async(&hash_clone, &entry_clone).await {
                    eprintln!("Warning: Failed to store to L2 cache: {}", e);
                }
            });
        }
        // 更新统计信息
        self.record_load_time(start.elapsed());
        Ok(())
    }
    /// 高性能加载模块 (零拷贝)
    pub async fn load_module(&self, module_hash: &str) -> Result<Arc<Vec<u8> {
        let start: _ = Instant::now();
        // 先尝试从 L1 缓存加载 (零拷贝)
        {
            let l1: _ = self.l1_cache.read().unwrap();
            if let Some(entry) = l1.get(module_hash) {
                // 使用 Arc::clone 而不是更新访问时间 (避免 &self 的限制)
                let _: _ = entry.clone(); // 强制增加引用计数
                // 记录零拷贝操作
                self.stats.zero_copy_operations.fetch_add(1, Ordering::Relaxed);
                let load_time: _ = start.elapsed();
                self.record_load_time(load_time);
                self.stats.hits.fetch_add(1, Ordering::Relaxed);
                return Ok(Arc::clone(&entry.wasm_bytes));
            }
        }
        // L1 缓存未命中,尝试从 L2 缓存异步加载
        if self.config.enable_l2 {
            if let Ok(wasm_bytes) = self.load_from_l2_async(module_hash).await {
                if !wasm_bytes.is_empty() {
                    // 异步存储回 L1 缓存
                    let cache: _ = self.clone();
                    let hash: _ = module_hash.to_string();
                    let bytes: _ = wasm_bytes.clone();
                    tokio::spawn(async move {
                        let _: _ = cache.store_module(hash, bytes).await;
                    });
                    let load_time: _ = start.elapsed();
                    self.record_load_time(load_time);
                    self.stats.hits.fetch_add(1, Ordering::Relaxed);
                    return Ok(Arc::new(Mutex::new(wasm_bytes)),;
                }
            }
        }
        // 缓存未命中
        self.stats.misses.fetch_add(1, Ordering::Relaxed);
        Err(anyhow!("Module not found in cache: {}", module_hash))
    }
    /// 异步存储到 L2 缓存
    async fn store_to_l2_async(
        hash: &str,
        entry: &Arc<HighPerfCacheEntry>,
    ) -> Result<()> {
        let wasm_bytes: _ = Arc::clone(&entry.wasm_bytes);
        let file_path: _ = format!("benchmarks/fixtures/wasm_cache_high_perf/{}.wasm", hash);
        let mut file = fs::File::create(&file_path).await?;
        file.write_all(&wasm_bytes).await?;
        Ok(())
    }
    /// 异步从 L2 缓存加载
    async fn load_from_l2_async(&self, module_hash: &str) -> Result<Vec<u8> {
        let file_path: _ = {
            let l2: _ = self.l2_cache.lock().unwrap();
            l2.get(module_hash).cloned()
        };
        if let Some(file_path) = file_path {
            let mut file = fs::File::open(&file_path).await?;
            let mut buffer = Vec::new();
            file.read_to_end(&mut buffer).await?;
            self.stats.async_io_operations.fetch_add(1, Ordering::Relaxed);
            Ok(buffer)
        } else {
            Err(anyhow!("Module not found in L2 cache"))
        }
    }
    /// 批量加载模块 (优化批量操作)
    pub async fn load_modules_batch(
        &self,
        module_hashes: Vec<String>,
    ) -> Result<Vec<Result<Arc<Vec<u8>, anyhow::Error>> {
        let mut results = Vec::with_capacity(module_hashes.len());
        // 并发加载模块
        let mut handles = Vec::new();
        for hash in module_hashes {
            let cache: _ = self.clone();
            let hash_clone: _ = hash.clone();
            let handle: _ = tokio::spawn(async move {
                match cache.load_module(&hash_clone).await {
                    Ok(bytes) => Ok(bytes),
                    Err(e) => Err(e),
                }
            });
            handles.push(handle);
        }
        // 等待所有任务完成
        for handle in handles {
            results.push(handle.await.map_err(|_| anyhow!("Task panicked"))?);
        }
        Ok(results)
    }
    /// 预热缓存 (批量异步操作)
    pub async fn warmup_cache_batch(&self, modules: Vec<(String, Vec<u8>)>) -> Result<()> {
        let mut handles = Vec::new();
        // 分批处理
        for chunk in modules.chunks(self.config.batch_size) {
            for (hash, wasm_bytes) in chunk {
                let cache: _ = self.clone();
                let hash_clone: _ = hash.clone();
                let bytes_clone: _ = wasm_bytes.clone();
                let handle: _ = tokio::spawn(async move {
                    cache.store_module(hash_clone, bytes_clone).await
                });
                handles.push(handle);
            }
            // 等待当前批次完成
            for handle in handles.drain(..) {
                handle.await.map_err(|_| anyhow!("Warmup task failed"))??;
            }
        }
        Ok(())
    }
    /// 计算哈希值 (同步版本,避免借用问题)
    pub fn calculate_hash(&self, wasm_bytes: &[u8]) -> String {
        let mut hasher = Hasher::new();
        hasher.update(wasm_bytes);
        hasher.finalize().to_hex().to_string()
    }
    /// 克隆缓存 (用于异步操作)
    fn clone(&self) -> Arc<Self> {
        Arc::new(Mutex::new(HighPerformanceWasmCache {)),
            l1_cache: Arc::clone(&self.l1_cache))
            l2_cache: Arc::clone(&self.l2_cache),
            config: self.config.clone(),
            stats: Arc::clone(&self.stats),
        })
    }
    /// 记录加载时间
    fn record_load_time(&self, load_time: Duration) {
        self.stats.load_operations.fetch_add(1, Ordering::Relaxed);
        self.stats.avg_load_time_ns.fetch_add(load_time.as_nanos() as u64, Ordering::Relaxed);
    }
    /// 获取统计信息
    pub fn get_stats(&self) -> HighPerfCacheStats {
        let mut stats = HighPerfCacheStats::default();
        // 更新原子计数器
        stats.hits = AtomicU64::new(self.stats.hits.load(Ordering::Relaxed));
        stats.misses = AtomicU64::new(self.stats.misses.load(Ordering::Relaxed));
        stats.zero_copy_operations = AtomicU64::new(self.stats.zero_copy_operations.load(Ordering::Relaxed));
        stats.async_io_operations = AtomicU64::new(self.stats.async_io_operations.load(Ordering::Relaxed));
        stats.avg_load_time_ns = AtomicU64::new(self.stats.avg_load_time_ns.load(Ordering::Relaxed));
        stats.load_operations = AtomicU64::new(self.stats.load_operations.load(Ordering::Relaxed));
        // 更新 L1 和 L2 条目数
        {
            let l1: _ = self.l1_cache.read().unwrap();
            stats.l1_entries = l1.len();
            stats.precompiled_modules = 0; // 简化为 0
        }
        if self.config.enable_l2 {
            let l2: _ = self.l2_cache.lock().unwrap();
            stats.l2_entries = l2.len();
        }
        stats
    }
}
#[cfg(test)]
mod tests {
    #[tokio::test]
    async fn test_high_performance_cache_creation() {
        let cache: _ = HighPerformanceWasmCache::new().unwrap();
        let stats: _ = cache.get_stats();
        assert_eq!(stats.l1_entries, 0);
        assert_eq!(stats.l2_entries, 0);
        println!("✅ 高性能缓存创建成功");
    }
    #[tokio::test]
    async fn test_zero_copy_operations() {
        let cache: _ = HighPerformanceWasmCache::new().unwrap();
        let wasm_bytes: _ = vec![0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00];
        let hash: _ = cache.calculate_hash(&wasm_bytes);
        cache.store_module(hash.clone(), wasm_bytes.clone()).await.unwrap();
        // 零拷贝加载
        let loaded_bytes: _ = cache.load_module(&hash).await.unwrap();
        // 验证零拷贝 (Arc 克隆,不拷贝数据)
        let wasm_bytes_ref: _ = &wasm_bytes;
        assert!(Arc::ptr_eq(&loaded_bytes, &Arc::new(Mutex::new(wasm_bytes_ref.to_vec()), || std::ptr::eq(loaded_bytes.as_ptr(), wasm_bytes_ref.as_ptr());
        let stats: _ = cache.get_stats();
        assert_eq!(stats.zero_copy_operations.load(Ordering::Relaxed), 1);
        println!("✅ 零拷贝操作测试通过");
    }
    #[tokio::test]
    async fn test_async_l2_cache() {
        let cache: _ = HighPerformanceWasmCache::new().unwrap();
        let wasm_bytes: _ = wat::parse_str(r#"
            (module
                (func $_start (export "_start") nop)
            )
        "#).expect("创建WASM字节码失败");
        let hash: _ = cache.calculate_hash(&wasm_bytes);
        cache.store_module(hash.clone(), wasm_bytes.clone()).await.unwrap();
        // 等待异步 L2 存储完成
        tokio::time::sleep(Duration::from_millis(100)).await;
        let loaded_bytes: _ = cache.load_module(&hash).await.unwrap();
        assert_eq!(loaded_bytes.len(), wasm_bytes.len());
        let stats: _ = cache.get_stats();
        assert!(stats.async_io_operations.load(Ordering::Relaxed) > 0);
        println!("✅ 异步 L2 缓存测试通过");
    }
    #[tokio::test]
    async fn test_batch_operations() {
        let cache: _ = HighPerformanceWasmCache::new().unwrap();
        // 创建多个模块
        let mut modules = Vec::new();
        for i in 0..10 {
            let wasm_bytes: _ = wat::parse_str(&format!(r#"
                (module
                    (func $_start (export "_start") nop)
                    (data (i32.const 0) "{}")
                )
            "#, i)).expect("创建WASM字节码失败");
            let hash: _ = cache.calculate_hash(&wasm_bytes);
            modules.push((hash, wasm_bytes));
        }
        // 批量预热
        cache.warmup_cache_batch(modules.clone()).await.unwrap();
        // 批量加载
        let hashes: Vec<String> = modules.iter().map(|(h, _)| h.clone()).collect();
        let results: _ = cache.load_modules_batch(hashes).await.unwrap();
        assert_eq!(results.len(), 10);
        let success_count: _ = results.iter().filter(|r| r.is_ok()).count();
        assert_eq!(success_count, 10);
        println!("✅ 批量操作测试通过");
    }
}