feat: hashmap with_capacity

This commit is contained in:
2025-05-17 20:04:12 +08:00
parent 7b55a15281
commit ec61eaa140
4 changed files with 16 additions and 10 deletions

View File

@@ -38,7 +38,7 @@ pub enum OpCode {
JmpIfFalse { step: usize }, JmpIfFalse { step: usize },
/// push an empty attribute set onto stack /// push an empty attribute set onto stack
AttrSet, AttrSet { cap: usize },
/// finalize the recursive attribute set at TOS /// finalize the recursive attribute set at TOS
FinalizeRec, FinalizeRec,
/// [ .. set value ] consume 1 element, push a static kv pair (`name`, `value`) into `set` /// [ .. set value ] consume 1 element, push a static kv pair (`name`, `value`) into `set`

View File

@@ -97,7 +97,7 @@ impl Compile for ir::Thunk {
impl Compile for ir::Attrs { impl Compile for ir::Attrs {
fn compile(self, comp: &mut Compiler) { fn compile(self, comp: &mut Compiler) {
comp.push(OpCode::AttrSet); comp.push(OpCode::AttrSet { cap: self.stcs.len() + self.dyns.len() });
for stc in self.stcs { for stc in self.stcs {
stc.1.compile(comp); stc.1.compile(comp);
if !self.rec { if !self.rec {
@@ -250,17 +250,17 @@ impl Compile for ir::HasAttr {
for attr in self.rhs { for attr in self.rhs {
match attr { match attr {
ir::Attr::Str(sym) => { ir::Attr::Str(sym) => {
comp.push(OpCode::AttrSet); comp.push(OpCode::AttrSet { cap: 0 });
comp.push(OpCode::SelectOrDefault { sym }) comp.push(OpCode::SelectOrDefault { sym })
} }
ir::Attr::Dynamic(dynamic) => { ir::Attr::Dynamic(dynamic) => {
dynamic.compile(comp); dynamic.compile(comp);
comp.push(OpCode::AttrSet); comp.push(OpCode::AttrSet { cap: 0 });
comp.push(OpCode::SelectDynamicOrDefault); comp.push(OpCode::SelectDynamicOrDefault);
} }
ir::Attr::Strs(string) => { ir::Attr::Strs(string) => {
string.compile(comp); string.compile(comp);
comp.push(OpCode::AttrSet); comp.push(OpCode::AttrSet { cap: 0 });
comp.push(OpCode::SelectDynamicOrDefault); comp.push(OpCode::SelectDynamicOrDefault);
} }
} }
@@ -281,17 +281,17 @@ impl Compile for ir::Select {
for attr in self.attrpath { for attr in self.attrpath {
match attr { match attr {
ir::Attr::Str(sym) => { ir::Attr::Str(sym) => {
comp.push(OpCode::AttrSet); comp.push(OpCode::AttrSet { cap: 0 });
comp.push(OpCode::SelectOrDefault { sym }) comp.push(OpCode::SelectOrDefault { sym })
} }
ir::Attr::Dynamic(dynamic) => { ir::Attr::Dynamic(dynamic) => {
dynamic.compile(comp); dynamic.compile(comp);
comp.push(OpCode::AttrSet); comp.push(OpCode::AttrSet { cap: 0 });
comp.push(OpCode::SelectDynamicOrDefault); comp.push(OpCode::SelectDynamicOrDefault);
} }
ir::Attr::Strs(string) => { ir::Attr::Strs(string) => {
string.compile(comp); string.compile(comp);
comp.push(OpCode::AttrSet); comp.push(OpCode::AttrSet { cap: 0 });
comp.push(OpCode::SelectDynamicOrDefault); comp.push(OpCode::SelectDynamicOrDefault);
} }
} }

View File

@@ -1,3 +1,5 @@
use std::hash::Hash;
use hashbrown::{HashMap, HashSet}; use hashbrown::{HashMap, HashSet};
use derive_more::Constructor; use derive_more::Constructor;
@@ -22,6 +24,10 @@ impl<'vm> AttrSet<'vm> {
} }
} }
pub fn with_capacity(cap: usize) -> Self {
AttrSet { data: HashMap::with_capacity(cap) }
}
pub fn push_attr_force(&mut self, sym: usize, val: Value<'vm>) { pub fn push_attr_force(&mut self, sym: usize, val: Value<'vm>) {
self.data.insert(sym, val); self.data.insert(sym, val);
} }

View File

@@ -182,8 +182,8 @@ impl<'vm, 'jit: 'vm> VM<'jit> {
let elem = stack.pop(); let elem = stack.pop();
stack.tos_mut()?.push(elem); stack.tos_mut()?.push(elem);
} }
OpCode::AttrSet => { OpCode::AttrSet { cap } => {
stack.push(Value::AttrSet(AttrSet::empty().into()))?; stack.push(Value::AttrSet(AttrSet::with_capacity(cap).into()))?;
} }
OpCode::FinalizeRec => { OpCode::FinalizeRec => {
let env = env.clone().enter( let env = env.clone().enter(