From 7b55a152810df843258989e492a9ea71f520ce6d Mon Sep 17 00:00:00 2001 From: imxyy_soope_ Date: Sat, 17 May 2025 19:45:41 +0800 Subject: [PATCH] feat: use hashbrown --- Cargo.lock | 32 +++++++++++++++++++++++++++++++- Cargo.toml | 1 + src/builtins/mod.rs | 7 +++---- src/bytecode.rs | 2 +- src/ir.rs | 2 +- src/ty/internal/attrset.rs | 15 +++++++-------- src/ty/internal/list.rs | 2 +- src/ty/internal/mod.rs | 6 +++--- src/ty/public/mod.rs | 5 +++-- src/vm/env.rs | 28 +++++++++++++++++++--------- src/vm/mod.rs | 4 ++-- src/vm/test.rs | 19 +++++++++++++++++-- 12 files changed, 89 insertions(+), 34 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4549078..091d8b8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -11,6 +11,12 @@ dependencies = [ "memchr", ] +[[package]] +name = "allocator-api2" +version = "0.2.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "683d7910e743518b0e34f1186f92494becacb047c7b6bf616c96772180fef923" + [[package]] name = "anyhow" version = "1.0.98" @@ -124,6 +130,12 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c34f04666d835ff5d62e058c3995147c06f42fe86ff053337632bca83e42702d" +[[package]] +name = "equivalent" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f" + [[package]] name = "errno" version = "0.3.11" @@ -151,12 +163,29 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "foldhash" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9c4f5dac5e15c24eb999c26181a6ca40b39fe946cbe4c263c7209467bc83af2" + [[package]] name = "hashbrown" version = "0.14.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604" +[[package]] +name = "hashbrown" +version = "0.15.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "84b26c544d002229e640969970a2e74021aadf6e2f96372b9c58eff97de08eb3" +dependencies = [ + "allocator-api2", + "equivalent", + "foldhash", +] + [[package]] name = "home" version = "0.5.11" @@ -280,6 +309,7 @@ version = "0.0.0" dependencies = [ "derive_more", "ecow", + "hashbrown 0.15.3", "inkwell", "itertools", "regex", @@ -374,7 +404,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32a58fa8a7ccff2aec4f39cc45bf5f985cec7125ab271cf681c279fd00192b49" dependencies = [ "countme", - "hashbrown", + "hashbrown 0.14.3", "memoffset", "rustc-hash", "text-size", diff --git a/Cargo.toml b/Cargo.toml index 54c6be2..c2e0441 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,6 +22,7 @@ rpds = "1.1" derive_more = { version = "2.0", features = [ "full" ] } ecow = "0.2" regex = "1.11" +hashbrown = "0.15" inkwell = { version = "0.6.0", features = ["llvm18-1"] } diff --git a/src/builtins/mod.rs b/src/builtins/mod.rs index 850e57a..d94a46c 100644 --- a/src/builtins/mod.rs +++ b/src/builtins/mod.rs @@ -1,7 +1,6 @@ +use hashbrown::HashMap; use std::rc::Rc; -use rpds::HashTrieMap; - use crate::ty::internal::{AttrSet, Const, PrimOp, Value}; use crate::vm::{Env, VM}; @@ -43,14 +42,14 @@ pub fn env<'vm>(vm: &'vm VM) -> Env<'vm> { }), ]; - let mut map = HashTrieMap::new(); + let mut map = HashMap::new(); for primop in primops { let primop = Rc::new(primop); env.insert( vm.new_sym(format!("__{}", primop.name)), Value::PrimOp(primop.clone()), ); - map.insert_mut(vm.new_sym(primop.name), Value::PrimOp(primop)); + map.insert(vm.new_sym(primop.name), Value::PrimOp(primop)); } let attrs: Rc<_> = AttrSet::from_inner(map).into(); let mut builtins = Value::AttrSet(attrs); diff --git a/src/bytecode.rs b/src/bytecode.rs index 5dd06be..6ee9227 100644 --- a/src/bytecode.rs +++ b/src/bytecode.rs @@ -1,4 +1,4 @@ -use std::collections::HashMap; +use hashbrown::HashMap; use ecow::EcoString; diff --git a/src/ir.rs b/src/ir.rs index 39727f3..66b9995 100644 --- a/src/ir.rs +++ b/src/ir.rs @@ -1,4 +1,4 @@ -use std::collections::HashMap; +use hashbrown::HashMap; use ecow::EcoString; use rnix::ast::{self, Expr}; diff --git a/src/ty/internal/attrset.rs b/src/ty/internal/attrset.rs index 3ec082c..498f546 100644 --- a/src/ty/internal/attrset.rs +++ b/src/ty/internal/attrset.rs @@ -1,8 +1,7 @@ -use std::collections::HashSet; +use hashbrown::{HashMap, HashSet}; use derive_more::Constructor; use itertools::Itertools; -use rpds::HashTrieMap; use crate::error::Result; use crate::vm::{Env, VM}; @@ -13,25 +12,25 @@ use super::Value; #[repr(C)] #[derive(Debug, Constructor, Clone, PartialEq)] pub struct AttrSet<'vm> { - data: HashTrieMap>, + data: HashMap>, } impl<'vm> AttrSet<'vm> { pub fn empty() -> Self { AttrSet { - data: HashTrieMap::new(), + data: HashMap::new(), } } pub fn push_attr_force(&mut self, sym: usize, val: Value<'vm>) { - self.data.insert_mut(sym, val); + self.data.insert(sym, val); } pub fn push_attr(&mut self, sym: usize, val: Value<'vm>) { if self.data.get(&sym).is_some() { todo!() } - self.data.insert_mut(sym, val); + self.data.insert(sym, val); } pub fn select(&self, sym: usize) -> Option> { @@ -57,11 +56,11 @@ impl<'vm> AttrSet<'vm> { } } - pub fn as_inner(&self) -> &HashTrieMap> { + pub fn as_inner(&self) -> &HashMap> { &self.data } - pub fn from_inner(data: HashTrieMap>) -> Self { + pub fn from_inner(data: HashMap>) -> Self { Self { data } } diff --git a/src/ty/internal/list.rs b/src/ty/internal/list.rs index 9dff84b..ae19421 100644 --- a/src/ty/internal/list.rs +++ b/src/ty/internal/list.rs @@ -1,4 +1,4 @@ -use std::collections::HashSet; +use hashbrown::HashSet; use derive_more::Constructor; use rpds::Vector; diff --git a/src/ty/internal/mod.rs b/src/ty/internal/mod.rs index 784eb58..a0efbfc 100644 --- a/src/ty/internal/mod.rs +++ b/src/ty/internal/mod.rs @@ -1,6 +1,6 @@ use std::cell::OnceCell; use std::cell::RefCell; -use std::collections::HashSet; +use hashbrown::HashSet; use std::hash::Hash; use std::rc::Rc; @@ -219,11 +219,11 @@ impl<'vm> Value<'vm> { } } - pub fn eq(self, other: Self) -> Self { + pub fn eq(self, other: Self, vm: &'vm VM<'_>) -> Self { use Const::Bool; match (self, other) { (x @ Value::Catchable(_), _) | (_, x @ Value::Catchable(_)) => x, - (s, other) => VmConst(Bool(s == other)), + (s, other) => VmConst(Bool(s.eq_impl(&other, vm))), } } diff --git a/src/ty/public/mod.rs b/src/ty/public/mod.rs index 83c6b27..5f31f08 100644 --- a/src/ty/public/mod.rs +++ b/src/ty/public/mod.rs @@ -1,3 +1,4 @@ +use hashbrown::HashMap; use std::fmt::{Debug, Display, Formatter, Result as FmtResult}; use std::ops::Deref; use std::sync::LazyLock; @@ -5,7 +6,7 @@ use std::sync::LazyLock; use derive_more::{Constructor, IsVariant, Unwrap}; use ecow::EcoString; use regex::Regex; -use rpds::{HashTrieMap, VectorSync}; +use rpds::VectorSync; use super::common::*; @@ -59,7 +60,7 @@ impl Symbol { #[derive(Constructor, Clone, PartialEq)] pub struct AttrSet { - data: HashTrieMap, + data: HashMap, } impl Debug for AttrSet { diff --git a/src/vm/env.rs b/src/vm/env.rs index 5f241c6..62a390a 100644 --- a/src/vm/env.rs +++ b/src/vm/env.rs @@ -1,13 +1,12 @@ use std::rc::Rc; - -use rpds::HashTrieMap; +use hashbrown::HashMap; use crate::ty::internal::{AttrSet, Value}; #[derive(Debug, Default, Clone)] pub struct Env<'vm> { last: Option>>, - map: HashTrieMap>, + map: Rc>>, } impl<'vm> Env<'vm> { @@ -16,18 +15,22 @@ impl<'vm> Env<'vm> { } pub fn lookup(&self, symbol: usize) -> Option> { - self.map.get(&symbol).cloned() + if let Some(val) = self.map.get(&symbol).cloned() { + return Some(val) + } + self.last.as_ref().map(|env| env.lookup(symbol)).flatten() } pub fn insert(&mut self, symbol: usize, value: Value<'vm>) { - self.map.insert_mut(symbol, value); + Rc::make_mut(&mut self.map).insert(symbol, value); } pub fn enter(self, new: impl Iterator)>) -> Self { - let mut map = self.map.clone(); + /* let mut map = self.map.clone(); for (k, v) in new { map.insert_mut(k, v); - } + } */ + let map = Rc::new(new.collect()); let last = Some( Env { last: self.last, @@ -39,7 +42,7 @@ impl<'vm> Env<'vm> { } pub fn enter_with(self, new: Rc>) -> Self { - let mut map = self.map.clone(); + /* let mut map = self.map.clone(); for (k, v) in new.as_inner().iter() { let v = if let Value::Builtins = v { Value::AttrSet(new.clone()) @@ -47,7 +50,14 @@ impl<'vm> Env<'vm> { v.clone() }; map.insert_mut(k.clone(), v); - } + } */ + let map = Rc::new(new.as_inner().iter().map(|(&k, v)| { + (k, if let Value::Builtins = v { + Value::AttrSet(new.clone()) + } else { + v.clone() + }) + }).collect()); let last = Some( Env { last: self.last.clone(), diff --git a/src/vm/mod.rs b/src/vm/mod.rs index ca718a4..a18e2c8 100644 --- a/src/vm/mod.rs +++ b/src/vm/mod.rs @@ -1,5 +1,5 @@ use std::cell::RefCell; -use std::collections::{HashMap, HashSet}; +use hashbrown::{HashMap, HashSet}; use std::pin::Pin; use crate::builtins::env; @@ -161,7 +161,7 @@ impl<'vm, 'jit: 'vm> VM<'jit> { Div => lhs.div(rhs)?, And => lhs.and(rhs), Or => lhs.or(rhs), - Eq => lhs.eq(rhs), + Eq => lhs.eq(rhs, self), Lt => lhs.lt(rhs), Con => lhs.concat(rhs), Upd => lhs.update(rhs), diff --git a/src/vm/test.rs b/src/vm/test.rs index 0407971..847714a 100644 --- a/src/vm/test.rs +++ b/src/vm/test.rs @@ -1,10 +1,12 @@ extern crate test; +use hashbrown::HashMap; + use inkwell::context::Context; use test::{Bencher, black_box}; use ecow::EcoString; -use rpds::{ht_map, vector_sync}; +use rpds::vector_sync; use crate::compile::compile; use crate::ir::downgrade; @@ -24,6 +26,19 @@ fn test_expr(expr: &str, expected: Value) { assert_eq!(run(prog, jit).unwrap(), expected); } +macro_rules! map { + ($($k:expr => $v:expr),*) => { + { + #[allow(unused_mut)] + let mut m = HashMap::new(); + $( + m.insert($k, $v); + )* + m + } + }; +} + macro_rules! thunk { () => { Value::Thunk @@ -68,7 +83,7 @@ macro_rules! list { macro_rules! attrs { ($($x:tt)*) => ( - Value::AttrSet(AttrSet::new(ht_map!{$($x)*})) + Value::AttrSet(AttrSet::new(map!{$($x)*})) ) }