From 8ca65c3e2dbede7bcf03d5680f6d4f9bef4bcb01 Mon Sep 17 00:00:00 2001 From: pjh456 <147148383@qq.com> Date: Fri, 5 Jun 2026 21:46:23 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=9E=E7=8E=B0=E5=85=A8=E5=B1=80?= =?UTF-8?q?=E5=AD=97=E8=8A=82=E7=A0=81=E7=BC=96=E8=AF=91=E7=BC=93=E5=AD=98?= =?UTF-8?q?=E6=B1=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.lock | 1 + crates/oxide_kernel/Cargo.toml | 1 + crates/oxide_kernel/src/code_forge.rs | 195 ++++++++++++++++++++++++++ crates/oxide_kernel/src/lib.rs | 1 + 4 files changed, 198 insertions(+) create mode 100644 crates/oxide_kernel/src/code_forge.rs diff --git a/Cargo.lock b/Cargo.lock index 196ac33a..5f1998df 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -784,6 +784,7 @@ dependencies = [ "dashmap", "hashbrown 0.14.5", "oxide_compiler", + "oxide_parser", "rustc-hash", ] diff --git a/crates/oxide_kernel/Cargo.toml b/crates/oxide_kernel/Cargo.toml index 37de18ce..4879d9cf 100644 --- a/crates/oxide_kernel/Cargo.toml +++ b/crates/oxide_kernel/Cargo.toml @@ -8,6 +8,7 @@ description = "OxideJS - Shared kernel (CodeForge, ShapeForge, IcForge, StringFo [dependencies] oxide_compiler = { path = "../oxide_compiler" } +oxide_parser = { path = "../oxide_parser" } dashmap.workspace = true hashbrown.workspace = true rustc-hash.workspace = true diff --git a/crates/oxide_kernel/src/code_forge.rs b/crates/oxide_kernel/src/code_forge.rs new file mode 100644 index 00000000..a9fe35d3 --- /dev/null +++ b/crates/oxide_kernel/src/code_forge.rs @@ -0,0 +1,195 @@ +use std::hash::{Hash, Hasher}; +use std::sync::Arc; + +use dashmap::DashMap; + +use oxide_compiler::compiler::Compiler; +use oxide_compiler::module::CompiledModule; +use oxide_parser::{Expression, Statement}; + +pub fn structural_hash(program: &oxide_parser::Program) -> u64 { + let mut h = rustc_hash::FxHasher::default(); + + for stmt in &program.body { + hash_statement(stmt, &mut h); + } + + h.finish() +} + +fn hash_statement(stmt: &Statement, h: &mut rustc_hash::FxHasher) { + match stmt { + Statement::ExpressionStatement(es) => { + 0u8.hash(h); + hash_expression(&es.expression, h); + } + Statement::VariableDeclaration(decl) => { + 1u8.hash(h); + (decl.declarations.len() as u32).hash(h); + } + Statement::ReturnStatement(ret) => { + 2u8.hash(h); + if let Some(arg) = &ret.argument { + hash_expression(arg, h); + } + } + Statement::IfStatement(ifs) => { + 3u8.hash(h); + hash_expression(&ifs.test, h); + hash_statement(&ifs.consequent, h); + if let Some(alt) = &ifs.alternate { + hash_statement(alt, h); + } + } + Statement::WhileStatement(wh) => { + 4u8.hash(h); + hash_expression(&wh.test, h); + hash_statement(&wh.body, h); + } + Statement::ForStatement(fr) => { + 5u8.hash(h); + hash_statement(&fr.body, h); + } + Statement::BlockStatement(block) => { + 6u8.hash(h); + for s in &block.body { + hash_statement(s, h); + } + } + _ => { + std::mem::discriminant(stmt).hash(h); + } + } +} + +fn hash_expression(expr: &Expression, h: &mut rustc_hash::FxHasher) { + match expr { + Expression::BinaryExpression(bin) => { + 0u8.hash(h); + std::mem::discriminant(&bin.operator).hash(h); + hash_expression(&bin.left, h); + hash_expression(&bin.right, h); + } + Expression::UnaryExpression(un) => { + 1u8.hash(h); + std::mem::discriminant(&un.operator).hash(h); + hash_expression(&un.argument, h); + } + Expression::CallExpression(_) => { + 2u8.hash(h); + } + Expression::LogicalExpression(log) => { + 3u8.hash(h); + std::mem::discriminant(&log.operator).hash(h); + hash_expression(&log.left, h); + hash_expression(&log.right, h); + } + _ => { + std::mem::discriminant(expr).hash(h); + } + } +} + +pub struct CodeForge { + map: DashMap>, +} + +impl CodeForge { + pub fn new() -> Self { + Self { + map: DashMap::new(), + } + } + + pub fn get_or_compile( + &self, + program: &oxide_parser::Program, + compiler: &Compiler, + ) -> Result, String> { + let hash = structural_hash(program); + + if let Some(module) = self.map.get(&hash) { + return Ok(Arc::clone(&module)); + } + + let module = compiler.compile(program)?; + let module = Arc::new(module); + + 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 = 3; var b = 4;").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); + } +} diff --git a/crates/oxide_kernel/src/lib.rs b/crates/oxide_kernel/src/lib.rs index 21252791..84c016ca 100644 --- a/crates/oxide_kernel/src/lib.rs +++ b/crates/oxide_kernel/src/lib.rs @@ -1,4 +1,5 @@ #![doc = "OxideJS - Shared kernel (CodeForge, ShapeForge, IcForge, StringForge, BuiltinWorld)"] +pub mod code_forge; pub mod shape_forge; pub mod string_forge;