Skip to content

Commit 97538a3

Browse files
simplify Lang internals (#678)
1 parent 2312def commit 97538a3

1 file changed

Lines changed: 20 additions & 26 deletions

File tree

src/eval/lang.rs

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::collections::HashMap;
22
use std::fmt::Debug;
33
use std::marker::PhantomData;
44

5+
use indexmap::IndexMap;
56
use lurk_macros::Coproc;
67
use serde::{Deserialize, Serialize};
78

@@ -14,8 +15,6 @@ use crate::z_ptr::ZExprPtr;
1415

1516
use crate::{self as lurk, lurk_sym_ptr};
1617

17-
type IndexSet<K> = indexmap::IndexSet<K, ahash::RandomState>;
18-
1918
/// `DummyCoprocessor` is a concrete implementation of the [`crate::coprocessor::Coprocessor`] trait.
2019
///
2120
/// It provides specific behavior for a dummy coprocessor.
@@ -80,27 +79,22 @@ pub enum Coproc<F: LurkField> {
8079
// TODO: Define a trait for the Hash and parameterize on that also.
8180
#[derive(Debug, Default, Clone, Deserialize, Serialize)]
8281
pub struct Lang<F: LurkField, C: Coprocessor<F>> {
83-
// A HashMap that stores coprocessors with their associated `Sym` keys.
84-
coprocessors: HashMap<Symbol, (C, ZExprPtr<F>)>,
85-
names: Vec<Symbol>,
86-
index: IndexSet<ZExprPtr<F>>,
82+
/// An IndexMap that stores coprocessors with their associated `Sym` keys.
83+
coprocessors: IndexMap<Symbol, (C, ZExprPtr<F>)>,
84+
index: HashMap<ZExprPtr<F>, usize>,
8785
}
8886

8987
impl<F: LurkField, C: Coprocessor<F>> Lang<F, C> {
88+
#[inline]
9089
pub fn new() -> Self {
9190
Self {
9291
coprocessors: Default::default(),
93-
names: Default::default(),
9492
index: Default::default(),
9593
}
9694
}
9795

9896
pub fn new_with_bindings<B: Into<Binding<F, C>>>(s: &mut Store<F>, bindings: Vec<B>) -> Self {
99-
let mut new = Self {
100-
coprocessors: Default::default(),
101-
names: Default::default(),
102-
index: Default::default(),
103-
};
97+
let mut new = Self::new();
10498
for b in bindings {
10599
new.add_binding(b.into(), s);
106100
}
@@ -133,19 +127,16 @@ impl<F: LurkField, C: Coprocessor<F>> Lang<F, C> {
133127

134128
self.coprocessors
135129
.insert(name.clone(), (cproc.into(), z_ptr));
136-
self.names.push(name.clone());
137-
self.index.insert(z_ptr);
130+
self.index.insert(z_ptr, self.index.len());
138131
}
139132

140133
pub fn add_binding<B: Into<Binding<F, C>>>(&mut self, binding: B, store: &mut Store<F>) {
141134
let Binding { name, coproc, _p } = binding.into();
142-
let ptr = store.intern_symbol(&name);
143-
let _z_ptr = store.hash_expr(&ptr).unwrap();
144-
145135
self.add_coprocessor(name, coproc, store);
146136
}
147137

148-
pub fn coprocessors(&self) -> &HashMap<Symbol, (C, ZExprPtr<F>)> {
138+
#[inline]
139+
pub fn coprocessors(&self) -> &IndexMap<Symbol, (C, ZExprPtr<F>)> {
149140
&self.coprocessors
150141
}
151142

@@ -163,33 +154,36 @@ impl<F: LurkField, C: Coprocessor<F>> Lang<F, C> {
163154
maybe_sym.and_then(|sym| self.coprocessors.get(&sym))
164155
}
165156

157+
#[inline]
166158
pub fn has_coprocessors(&self) -> bool {
167159
!self.coprocessors.is_empty()
168160
}
169161

162+
#[inline]
170163
pub fn is_default(&self) -> bool {
171164
!self.has_coprocessors()
172165
}
173166

167+
#[inline]
174168
pub fn coprocessor_count(&self) -> usize {
175-
self.index.len()
169+
self.coprocessors.len()
176170
}
177171

172+
#[inline]
178173
pub fn get_index(&self, z_ptr: &ZExprPtr<F>) -> Option<usize> {
179-
self.index.get_index_of(z_ptr)
174+
self.index.get(z_ptr).copied()
180175
}
181176

182177
pub fn get_coprocessor(&self, index: usize) -> Option<&C> {
183-
self.names
184-
.get(index)
185-
.map(|name| &self.coprocessors.get(name).unwrap().0)
178+
self.coprocessors.get_index(index).map(|(_, (c, _))| c)
186179
}
187180

188181
pub fn get_coprocessor_z_ptr(&self, index: usize) -> Option<&ZExprPtr<F>> {
189-
self.names
190-
.get(index)
191-
.map(|name| &self.coprocessors.get(name).unwrap().1)
182+
self.coprocessors
183+
.get_index(index)
184+
.map(|(_, (_, z_ptr))| z_ptr)
192185
}
186+
193187
pub fn get_coprocessor_from_zptr(&self, z_ptr: &ZExprPtr<F>) -> Option<&C> {
194188
self.get_index(z_ptr)
195189
.and_then(|index| self.get_coprocessor(index))

0 commit comments

Comments
 (0)