refactor: 拆分 bytecode/cache crate 并模块化 counter

This commit is contained in:
2026-06-23 17:06:39 +08:00
parent ba5e095a83
commit be87e4e21b
38 changed files with 1332 additions and 1306 deletions
+1 -141
View File
@@ -1,141 +1 @@
use std::sync::Arc;
use dashmap::DashMap;
use oxide_compiler::compiler::Compiler;
use oxide_compiler::module::CompiledModule;
use crate::{kernel_debug, kernel_info};
pub use oxide_compiler::compiler::{compiled_module_hash, structural_hash};
/// Shared compiled-module cache.
///
/// Concurrency model:
/// - Read-heavy after warmup: cache hits dominate steady-state execution.
/// - Backed by a fixed-shard `DashMap` so lookup contention stays predictable across
/// developer machines and CI hosts.
pub struct CodeForge {
map: DashMap<u64, Arc<CompiledModule>>,
}
impl CodeForge {
pub fn new() -> Self {
Self {
map: DashMap::with_shard_amount(16),
}
}
pub fn get_or_compile(
&self, program: &oxide_parser::Program, compiler: &Compiler,
) -> Result<Arc<CompiledModule>, String> {
let hash = compiled_module_hash(program);
if let Some(module) = self.map.get(&hash) {
kernel_info!("CodeForge hit compiled_hash={:#x}", hash);
return Ok(Arc::clone(&module));
}
let module = compiler.compile(program)?;
let module = Arc::new(module);
kernel_debug!("CodeForge miss compiled_hash={:#x}", hash);
Ok(Arc::clone(self.map.entry(hash).or_insert(module).value()))
}
}
impl Default for CodeForge {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
use oxide_parser::Allocator;
#[test]
fn test_structural_hash_same_input() {
let a1 = Allocator::default();
let a2 = Allocator::default();
let p1 = oxide_parser::parse(&a1, "1 + 2").expect("parse failed");
let p2 = oxide_parser::parse(&a2, "1 + 2").expect("parse failed");
assert_eq!(structural_hash(&p1), structural_hash(&p2));
}
#[test]
fn test_structural_hash_same_structure() {
let a1 = Allocator::default();
let a2 = Allocator::default();
let p1 = oxide_parser::parse(&a1, "var x = 1; var y = 2;").expect("parse failed");
let p2 = oxide_parser::parse(&a2, "var a = 1; var b = 2;").expect("parse failed");
assert_eq!(structural_hash(&p1), structural_hash(&p2));
}
#[test]
fn test_compiled_module_hash_distinguishes_identifier_loads() {
let a1 = Allocator::default();
let a2 = Allocator::default();
let p1 = oxide_parser::parse(&a1, "var x = 1; var y = 2; x").expect("parse failed");
let p2 = oxide_parser::parse(&a2, "var x = 1; var y = 2; y").expect("parse failed");
assert_eq!(structural_hash(&p1), structural_hash(&p2));
assert_ne!(compiled_module_hash(&p1), compiled_module_hash(&p2));
}
#[test]
fn test_structural_hash_different_ops() {
let a1 = Allocator::default();
let a2 = Allocator::default();
let p1 = oxide_parser::parse(&a1, "1 + 2").expect("parse failed");
let p2 = oxide_parser::parse(&a2, "1 - 2").expect("parse failed");
assert_ne!(structural_hash(&p1), structural_hash(&p2));
}
#[test]
fn test_cache_hit() {
let forge = CodeForge::new();
let compiler = Compiler::new();
let allocator = Allocator::default();
let program = oxide_parser::parse(&allocator, "1 + 2").expect("parse failed");
let first = forge.get_or_compile(&program, &compiler).expect("first compile");
let second = forge.get_or_compile(&program, &compiler).expect("second compile");
assert_eq!((first.bytecode.len(), first.n_registers), (second.bytecode.len(), second.n_registers));
}
#[test]
fn test_cache_miss_new_program() {
let forge = CodeForge::new();
let compiler = Compiler::new();
let a1 = Allocator::default();
let p1 = oxide_parser::parse(&a1, "1 + 2").expect("parse failed");
let a2 = Allocator::default();
let p2 = oxide_parser::parse(&a2, "3 * 4").expect("parse failed");
let m1 = forge.get_or_compile(&p1, &compiler).expect("compile p1");
let m2 = forge.get_or_compile(&p2, &compiler).expect("compile p2");
assert_ne!(m1.bytecode, m2.bytecode);
}
#[test]
fn test_cache_miss_same_shape_different_binding_program() {
let forge = CodeForge::new();
let compiler = Compiler::new();
let a1 = Allocator::default();
let p1 = oxide_parser::parse(&a1, "var x = 1; var y = 2; x").expect("parse failed");
let a2 = Allocator::default();
let p2 = oxide_parser::parse(&a2, "var x = 1; var y = 2; y").expect("parse failed");
let m1 = forge.get_or_compile(&p1, &compiler).expect("compile p1");
let m2 = forge.get_or_compile(&p2, &compiler).expect("compile p2");
assert!(!Arc::ptr_eq(&m1, &m2));
assert_ne!(m1.bytecode, m2.bytecode);
}
}
pub use oxide_code_cache::CodeForge;