Skip to content

Commit ed85823

Browse files
committed
perf: switch to ahash, indexmap-allocator-api
1 parent 8e979cf commit ed85823

8 files changed

Lines changed: 105 additions & 23 deletions

File tree

Cargo.lock

Lines changed: 75 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ smallvec = { version = "1.15.1", features = ["union", "const_generics", "const_n
3030
bumpalo = { version = "3.20.2", features = ["boxed", "collections", "std", "allocator-api2"] }
3131
hashbrown = "0.17.0"
3232
allocator-api2 = "0.4.0"
33+
ahash = "0.8.12"
3334

3435
[dependencies]
35-
# lexer.workspace = true # FIXME(parser complete): bypass through parser
3636
interpreter.workspace = true
3737
parser.workspace = true
3838
color-eyre.workspace = true
@@ -49,6 +49,12 @@ clap = { version = "4.6.1", features = ["derive"] }
4949
ctor = "1.0"
5050
uutests = "0.8"
5151

52+
[patch.crates-io]
53+
# I'm not particularly happy about this, but it's crucial for performance, and
54+
# I have audited the two commits. It's pinned to that hash to avoid possible
55+
# supply-chain attacks. Hopefully upstream maintainers will change their mind.
56+
indexmap-allocator-api = { git = 'https://github.com/Hans-Halverson/indexmap-allocator-api.git', rev = "36852f985c01fb9f58f2d9e510655ca1dbb1f4d0" }
57+
5258
[profile.release]
5359
lto = true
5460
panic = "abort"

interpreter/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ parser.workspace = true
1414
bumpalo.workspace = true
1515
allocator-api2.workspace = true
1616
hashbrown.workspace = true
17-
indexmap = "2.14.0"
17+
ahash.workspace = true
18+
indexmap-allocator-api = "2.7.1"
1819

1920
[lints]
2021
workspace = true

interpreter/src/ir/lower.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use crate::{
2020
pub struct Code<'arena> {
2121
pub arena: &'arena Bump,
2222
pub bc: Bytecode<'arena>,
23-
pub consts: Consts,
23+
pub consts: Consts<'arena>,
2424
pub symbols: SymbolTable<'arena>,
2525
free_regs: Vec<'arena, Reg>,
2626
pub reg_pointer: u16,
@@ -240,7 +240,7 @@ pub fn test_interpreter(stmnt: &Body<'_>) -> String {
240240
let mut c = Code {
241241
arena: &bump,
242242
bc: Bytecode::new_in(&bump),
243-
consts: Consts::new(),
243+
consts: Consts::new_in(&bump),
244244
symbols: SymbolTable::new_in(&bump),
245245
reg_pointer: 0,
246246
free_regs: Vec::new_in(&bump),

interpreter/src/vm.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use std::fmt::{self, Display};
22

3+
use ahash::RandomState;
34
use bumpalo::{Bump, collections::Vec};
4-
use hashbrown::{DefaultHashBuilder, HashMap};
5-
use indexmap::{IndexMap, IndexSet};
5+
use hashbrown::HashMap;
6+
use indexmap_allocator_api::{IndexMap, IndexSet};
67
use parser::Identifier;
78

89
use crate::ir::{
@@ -28,7 +29,7 @@ pub struct Interpreter<'a> {
2829
program_counter: usize,
2930
registers: Registers<'a>,
3031
symbols: SymbolTable<'a>,
31-
consts: Consts,
32+
consts: Consts<'a>,
3233
compat: ExecMode,
3334
}
3435

@@ -37,14 +38,14 @@ pub struct Registers<'a>(Vec<'a, Value>);
3738

3839
#[derive(Debug)]
3940
pub struct SymbolTable<'a> {
40-
user: IndexMap<Identifier<'a>, Value>,
41+
user: IndexMap<Identifier<'a>, Value, RandomState, &'a Bump>,
4142
// separate table for cheap invalidation. It's an arena _visibly shrugs_.
42-
records: HashMap<usize, Value, DefaultHashBuilder, &'a Bump>,
43+
records: HashMap<usize, Value, RandomState, &'a Bump>,
4344
// etc
4445
}
4546

4647
#[derive(Debug)]
47-
pub struct Consts(pub IndexSet<Value>);
48+
pub struct Consts<'a>(pub IndexSet<Value, RandomState, &'a Bump>);
4849

4950
impl<'a> Interpreter<'a> {
5051
pub fn new(compat: ExecMode, code: Code<'a>) -> Self {
@@ -63,8 +64,8 @@ impl<'a> Interpreter<'a> {
6364
impl<'a> SymbolTable<'a> {
6465
pub fn new_in(arena: &'a Bump) -> Self {
6566
Self {
66-
user: IndexMap::new(),
67-
records: HashMap::new_in(arena),
67+
user: IndexMap::new_in(arena),
68+
records: HashMap::with_hasher_in(RandomState::new(), arena),
6869
}
6970
}
7071
fn lookup_user_var(&self, var: NonLocal) -> &Value {
@@ -88,9 +89,9 @@ impl<'a> SymbolTable<'a> {
8889
}
8990
}
9091

91-
impl Consts {
92-
pub fn new() -> Self {
93-
Self(IndexSet::with_capacity(4))
92+
impl<'a> Consts<'a> {
93+
pub fn new_in(arena: &'a Bump) -> Self {
94+
Self(IndexSet::with_capacity_in(4, arena))
9495
}
9596
}
9697

@@ -195,7 +196,7 @@ impl Display for SymbolTable<'_> {
195196
}
196197
}
197198

198-
impl Display for Consts {
199+
impl Display for Consts<'_> {
199200
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
200201
write!(f, "Consts:")?;
201202
fmt_list(f, self.0.iter(), |f, i, e| write!(f, "mem[{i}] = {e:?}"))

parser/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ smallvec.workspace = true
1414
bumpalo.workspace = true
1515
hashbrown.workspace = true
1616
allocator-api2.workspace = true
17+
ahash.workspace = true
1718
ariadne = "0.6.0"
1819

1920
[lints]

parser/src/ast.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55

66
use std::fmt::Debug;
77

8+
use ahash::RandomState;
89
use bumpalo::{Bump, boxed::Box, collections::Vec};
910
use either::Either;
10-
use hashbrown::{DefaultHashBuilder, HashMap};
11+
use hashbrown::HashMap;
1112
use lexer::{Slice, Span, Token};
1213

1314
use crate::{ParsingError, Result, lex::TokenExt};
@@ -21,7 +22,7 @@ pub struct Ast<'a> {
2122
pub end_file: Vec<'a, Body<'a>>,
2223
pub rules: Vec<'a, Rule<'a>>,
2324
pub concurrent: Vec<'a, Rule<'a>>,
24-
pub functions: HashMap<Identifier<'a>, Function<'a>, DefaultHashBuilder, &'a Bump>,
25+
pub functions: HashMap<Identifier<'a>, Function<'a>, RandomState, &'a Bump>,
2526
}
2627

2728
#[derive(Debug)]

parser/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ mod tests;
1616

1717
use std::{fmt::Debug, mem::replace};
1818

19+
use ahash::RandomState;
1920
use bumpalo::{Bump, boxed::Box, collections::Vec, vec};
2021
use either::Either::{Left, Right};
2122
use hashbrown::HashMap;
@@ -685,7 +686,7 @@ impl<'a> Ast<'a> {
685686
end_file: Vec::new_in(arena),
686687
rules: Vec::new_in(arena),
687688
concurrent: Vec::new_in(arena),
688-
functions: HashMap::new_in(arena),
689+
functions: HashMap::with_hasher_in(RandomState::new(), arena),
689690
}
690691
}
691692
}

0 commit comments

Comments
 (0)