feat: use hashbrown

This commit is contained in:
2025-05-17 19:45:41 +08:00
parent fb14027845
commit 7b55a15281
12 changed files with 89 additions and 34 deletions

32
Cargo.lock generated
View File

@@ -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",

View File

@@ -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"] }

View File

@@ -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);

View File

@@ -1,4 +1,4 @@
use std::collections::HashMap;
use hashbrown::HashMap;
use ecow::EcoString;

View File

@@ -1,4 +1,4 @@
use std::collections::HashMap;
use hashbrown::HashMap;
use ecow::EcoString;
use rnix::ast::{self, Expr};

View File

@@ -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<usize, Value<'vm>>,
data: HashMap<usize, Value<'vm>>,
}
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<Value<'vm>> {
@@ -57,11 +56,11 @@ impl<'vm> AttrSet<'vm> {
}
}
pub fn as_inner(&self) -> &HashTrieMap<usize, Value<'vm>> {
pub fn as_inner(&self) -> &HashMap<usize, Value<'vm>> {
&self.data
}
pub fn from_inner(data: HashTrieMap<usize, Value<'vm>>) -> Self {
pub fn from_inner(data: HashMap<usize, Value<'vm>>) -> Self {
Self { data }
}

View File

@@ -1,4 +1,4 @@
use std::collections::HashSet;
use hashbrown::HashSet;
use derive_more::Constructor;
use rpds::Vector;

View File

@@ -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))),
}
}

View File

@@ -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<Symbol, Value>,
data: HashMap<Symbol, Value>,
}
impl Debug for AttrSet {

View File

@@ -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<Rc<Env<'vm>>>,
map: HashTrieMap<usize, Value<'vm>>,
map: Rc<HashMap<usize, Value<'vm>>>,
}
impl<'vm> Env<'vm> {
@@ -16,18 +15,22 @@ impl<'vm> Env<'vm> {
}
pub fn lookup(&self, symbol: usize) -> Option<Value<'vm>> {
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<Item = (usize, Value<'vm>)>) -> 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<AttrSet<'vm>>) -> 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(),

View File

@@ -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),

View File

@@ -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)*}))
)
}