mirror of
https://github.com/pjh456/OxideJS.git
synced 2026-10-23 18:21:13 +08:00
perf: 类私有名作用域容器改哈希表,点查由线性扫描改 O(1)
This commit is contained in:
@@ -167,10 +167,8 @@ impl Emitter {
|
||||
let mut stacked_names = saved_private_names.clone();
|
||||
let mut stacked_kinds = saved_private_kinds.clone();
|
||||
for (n, id, kind, is_static) in &private_names {
|
||||
stacked_names.retain(|(en, _)| en != n);
|
||||
stacked_kinds.retain(|(en, _, _, _)| en != n);
|
||||
stacked_names.push((n.clone(), *id));
|
||||
stacked_kinds.push((n.clone(), *kind, *is_static, private_brand_id.unwrap_or(0)));
|
||||
stacked_names.insert(n.clone(), *id);
|
||||
stacked_kinds.insert(n.clone(), (*kind, *is_static, private_brand_id.unwrap_or(0)));
|
||||
}
|
||||
ctx.scopes.private_name_map = stacked_names;
|
||||
ctx.scopes.private_element_kinds = stacked_kinds;
|
||||
|
||||
@@ -266,8 +266,8 @@ impl CompileCtx {
|
||||
scopes: ScopeCtx {
|
||||
symbols: SymbolTable::new(),
|
||||
builtin_reg_map: Vec::new(),
|
||||
private_name_map: Vec::new(),
|
||||
private_element_kinds: Vec::new(),
|
||||
private_name_map: HashMap::new(),
|
||||
private_element_kinds: HashMap::new(),
|
||||
private_brand_id: None,
|
||||
next_private_name_id: 1,
|
||||
},
|
||||
|
||||
@@ -5,6 +5,8 @@
|
||||
//! `PatternCtx` 为解构工作预留。执行流字段(insts/registers/pc 等)仍
|
||||
//! 平铺在 `CompileCtx` 上。
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::symbol_table::SymbolTable;
|
||||
use oxide_ir::operand::LabelId;
|
||||
use oxide_parser::MethodDefinitionKind;
|
||||
@@ -147,12 +149,13 @@ impl LabelCtx {
|
||||
pub(crate) struct ScopeCtx {
|
||||
pub(crate) symbols: SymbolTable,
|
||||
pub(crate) builtin_reg_map: Vec<(String, u32)>,
|
||||
pub(crate) private_name_map: Vec<(String, u32)>,
|
||||
/// 私有元素类型(name, kind,static 标志,声明类 brand id)。kind=None 表示
|
||||
/// 私有名 → 私有名 id。
|
||||
pub(crate) private_name_map: HashMap<String, u32>,
|
||||
/// 私有元素类型(kind,static 标志,声明类 brand id)。kind=None 表示
|
||||
/// 字段;instance 字段的私有访问走 PrivateFieldFind 原型链查找,不加 brand
|
||||
/// 检查。brand_id 是声明该私有名的类的 brand id:跨类访问时 brand 检查须
|
||||
/// 定位声明类(而非访问发生处类)的 brand 对象。
|
||||
pub(crate) private_element_kinds: Vec<(String, Option<MethodDefinitionKind>, bool, u32)>,
|
||||
pub(crate) private_element_kinds: HashMap<String, (Option<MethodDefinitionKind>, bool, u32)>,
|
||||
/// 当前类的私有 brand 私有名 id:私有方法/访问器访问时对实例做 brand 检查。
|
||||
pub(crate) private_brand_id: Option<u32>,
|
||||
pub(crate) next_private_name_id: u32,
|
||||
|
||||
@@ -12,8 +12,8 @@ impl Emitter {
|
||||
pub(crate) fn private_name_id(&self, name: &str, ctx: &CompileCtx) -> Result<u32, String> {
|
||||
ctx.scopes
|
||||
.private_name_map
|
||||
.iter()
|
||||
.find_map(|(n, id)| (n == name).then_some(*id))
|
||||
.get(name)
|
||||
.copied()
|
||||
.ok_or_else(|| format!("private name #{name} is not defined"))
|
||||
}
|
||||
|
||||
@@ -36,8 +36,7 @@ impl Emitter {
|
||||
pub(crate) fn private_access_brand(
|
||||
&self, _obj_reg: u32, name: &str, ctx: &mut CompileCtx,
|
||||
) -> Result<(u32, u32), String> {
|
||||
let entry = ctx.scopes.private_element_kinds.iter().find(|(n, _, _, _)| n == name);
|
||||
let Some((_, kind, is_static, brand_id)) = entry else {
|
||||
let Some((kind, is_static, brand_id)) = ctx.scopes.private_element_kinds.get(name) else {
|
||||
return Ok((0, 0));
|
||||
};
|
||||
if kind.is_none() && !is_static {
|
||||
|
||||
Reference in New Issue
Block a user