docs: 修正结构哈希口径注释并展开编译管线黑话

This commit is contained in:
2026-09-17 14:43:54 +00:00
parent 83d536242f
commit 1ec72a832e
4 changed files with 36 additions and 13 deletions
+16 -4
View File
@@ -1,8 +1,10 @@
//! AST 结构哈希:按语法域(statement/expression/class/...)递归计算
//! `Program` 的稳定哈希,用作编译缓存键(code cache)。
//!
//! `structural_hash` 忽略绑定名(结构相等即可命中),
//! `compiled_module_hash` 纳入绑定名(精确匹配避免错误复用)。
//! `structural_hash` 为粗粒度键,忽略变量声明名、标识符读、import 本地名与
//! 函数参数等大部分绑定名;`compiled_module_hash` 为精确键,额外纳入全部绑定名。
//! 函数声明名、类名与 catch 参数标识名是编译产物物化名,两种粒度下恒计入哈希;
//! catch 参数为解构形态时不贡献哈希。
use oxide_parser::{
ArrayExpressionElement, BindingPattern, ChainElement, Class, ClassElement, Declaration,
@@ -11,6 +13,8 @@ use oxide_parser::{
};
use std::hash::Hash;
/// 哈希域标识:各语法域占用独立哈希空间,避免跨类型同形节点
/// (如语句与表达式字段结构相同)发生碰撞。
#[derive(Hash)]
enum HashDomain {
Statement,
@@ -21,6 +25,8 @@ enum HashDomain {
SimpleAssignmentTarget,
}
/// 结构哈希统一入口:先哈希域标识、再哈希变体判别符,最后分派到臂内载荷,
/// 保证跨域同形节点不碰撞。
macro_rules! hash_match {
($domain:expr, $value:expr, $h:expr, { $($arms:tt)* }) => {{
$domain.hash($h);
@@ -37,12 +43,15 @@ mod property;
mod statement;
mod target;
/// 结构哈希:忽略绑定名的 `Program` 哈希(用于粗粒度缓存命中判断)。
/// 粗粒度结构哈希:忽略变量声明名、标识符读、import 本地名与函数参数等大部分
/// 绑定名,仅保留编译产物依赖的函数声明名、类名与 catch 参数标识名,
/// 用于结构相等即命中的缓存判断。
pub fn structural_hash(program: &oxide_parser::Program) -> u64 {
hash_program(program, false)
}
/// 编译模块哈希:纳入绑定名的 `Program` 哈希(用于精确缓存命中判断)。
/// 精确编译模块哈希:在粗粒度键基础上额外纳入全部绑定名,
/// 避免绑定名不同但结构相同的程序错误复用缓存。
pub fn compiled_module_hash(program: &oxide_parser::Program) -> u64 {
hash_program(program, true)
}
@@ -65,6 +74,9 @@ fn hash_program(program: &oxide_parser::Program, include_binding_names: bool) ->
h.finish()
}
/// 哈希 `BindingIdentifier` 的绑定名;只由 `include_binding_names` 为真的调用点
/// 使用(变量声明、函数与箭头参数、方法参数、catch 参数),非标识符模式
/// (数组/对象解构)不贡献哈希。
fn hash_binding_pattern(pattern: &BindingPattern, h: &mut rustc_hash::FxHasher) {
if let BindingPattern::BindingIdentifier(ident) = pattern {
ident.name.as_str().hash(h);