This repository has been archived on 2026-09-15. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
OxideJS/crates/oxide_kernel/src/code_forge.rs
T

114 lines
3.5 KiB
Rust

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::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 = structural_hash(program);
if let Some(module) = self.map.get(&hash) {
kernel_info!("CodeForge hit hash={:#x}", hash);
return Ok(Arc::clone(&module));
}
let module = compiler.compile(program)?;
let module = Arc::new(module);
kernel_debug!("CodeForge miss 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_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);
}
}