Skip to content

Commit 8cdef9e

Browse files
committed
Use ahash in the vm
1 parent 77ab762 commit 8cdef9e

15 files changed

+51
-31
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vm/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ itertools = "0.9"
5757
hex = "0.4.0"
5858
hexf-parse = "0.1.0"
5959
indexmap = "1.0.2"
60+
ahash = "0.6"
6061
crc = "^1.0.0"
61-
maplit = "1.0"
6262
bitflags = "1.2.1"
6363
libc = "0.2"
6464
nix = "0.18"

vm/src/builtins/dict.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ impl PyDictRef {
450450

451451
/// Take a python dictionary and convert it to attributes.
452452
pub fn to_attributes(self) -> PyAttributes {
453-
let mut attrs = PyAttributes::new();
453+
let mut attrs = PyAttributes::default();
454454
for (key, value) in self {
455455
let key = pystr::clone_value(&key);
456456
attrs.insert(key, value);

vm/src/builtins/int.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -908,7 +908,7 @@ pub(crate) fn try_int(obj: &PyObjectRef, vm: &VirtualMachine) -> PyResult<BigInt
908908
vm.to_repr(obj)?,
909909
))),
910910
}
911-
};
911+
}
912912

913913
// test for strings and bytes
914914
if let Some(s) = obj.downcast_ref::<PyStr>() {

vm/src/builtins/pytype.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::common::lock::{
22
PyMappedRwLockReadGuard, PyRwLock, PyRwLockReadGuard, PyRwLockUpgradableReadGuard,
33
PyRwLockWriteGuard,
44
};
5-
use std::collections::{HashMap, HashSet};
5+
use std::collections::HashSet;
66
use std::fmt;
77

88
use super::classmethod::PyClassMethod;
@@ -133,7 +133,7 @@ impl PyType {
133133

134134
pub fn get_attributes(&self) -> PyAttributes {
135135
// Gather all members here:
136-
let mut attributes = PyAttributes::new();
136+
let mut attributes = PyAttributes::default();
137137

138138
for bc in self.iter_mro().rev() {
139139
for (name, value) in bc.attributes.read().iter() {
@@ -791,7 +791,7 @@ pub fn new(
791791
name: &str,
792792
base: PyTypeRef,
793793
bases: Vec<PyTypeRef>,
794-
attrs: HashMap<String, PyObjectRef>,
794+
attrs: PyAttributes,
795795
mut slots: PyTypeSlots,
796796
) -> Result<PyTypeRef, String> {
797797
// Check for duplicates in bases.
@@ -914,8 +914,7 @@ fn best_base(bases: &[PyTypeRef], vm: &VirtualMachine) -> PyResult<PyTypeRef> {
914914

915915
#[cfg(test)]
916916
mod tests {
917-
use super::{linearise_mro, new};
918-
use super::{HashMap, IdProtocol, PyContext, PyTypeRef};
917+
use super::*;
919918

920919
fn map_ids(obj: Result<Vec<PyTypeRef>, String>) -> Result<Vec<usize>, String> {
921920
Ok(obj?.into_iter().map(|x| x.get_id()).collect())
@@ -932,7 +931,7 @@ mod tests {
932931
"A",
933932
object.clone(),
934933
vec![object.clone()],
935-
HashMap::new(),
934+
PyAttributes::default(),
936935
Default::default(),
937936
)
938937
.unwrap();
@@ -941,7 +940,7 @@ mod tests {
941940
"B",
942941
object.clone(),
943942
vec![object.clone()],
944-
HashMap::new(),
943+
PyAttributes::default(),
945944
Default::default(),
946945
)
947946
.unwrap();

vm/src/frozen.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ pub fn map_frozen<'a>(
1919
})
2020
}
2121

22-
pub fn get_module_inits(vm: &VirtualMachine) -> HashMap<String, code::FrozenModule> {
23-
let mut modules = HashMap::new();
22+
pub fn get_module_inits(
23+
vm: &VirtualMachine,
24+
) -> HashMap<String, code::FrozenModule, ahash::RandomState> {
25+
let mut modules = HashMap::default();
2426

2527
macro_rules! ext_modules {
2628
($($t:tt)*) => {

vm/src/function.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use crate::vm::VirtualMachine;
1010
use indexmap::IndexMap;
1111
use itertools::Itertools;
1212
use result_like::impl_option_like;
13-
use std::collections::HashMap;
1413
use std::marker::PhantomData;
1514
use std::ops::RangeInclusive;
1615

@@ -325,9 +324,9 @@ impl<T> KwArgs<T> {
325324
self.0.remove(name)
326325
}
327326
}
328-
impl<T> From<HashMap<String, T>> for KwArgs<T> {
329-
fn from(kwargs: HashMap<String, T>) -> Self {
330-
KwArgs(kwargs.into_iter().collect())
327+
impl<T> std::iter::FromIterator<(String, T)> for KwArgs<T> {
328+
fn from_iter<I: IntoIterator<Item = (String, T)>>(iter: I) -> Self {
329+
KwArgs(iter.into_iter().collect())
331330
}
332331
}
333332
impl<T> Default for KwArgs<T> {

vm/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ extern crate flamer;
2121
extern crate bitflags;
2222
#[macro_use]
2323
extern crate log;
24-
#[macro_use]
25-
extern crate maplit;
2624
// extern crate env_logger;
2725

2826
#[macro_use]

vm/src/macros.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,3 +256,21 @@ cfg_if::cfg_if! {
256256
}
257257
}
258258
}
259+
260+
/// A modified version of the hashmap! macro from the maplit crate
261+
macro_rules! hashmap {
262+
(@single $($x:tt)*) => (());
263+
(@count $($rest:expr),*) => (<[()]>::len(&[$(hashmap!(@single $rest)),*]));
264+
265+
(hasher=$hasher:expr, $($key:expr => $value:expr,)+) => { hashmap!(hasher=$hasher, $($key => $value),+) };
266+
(hasher=$hasher:expr, $($key:expr => $value:expr),*) => {
267+
{
268+
let _cap = hashmap!(@count $($key),*);
269+
let mut _map = ::std::collections::HashMap::with_capacity_and_hasher(_cap, $hasher);
270+
$(
271+
let _ = _map.insert($key, $value);
272+
)*
273+
_map
274+
}
275+
};
276+
}

vm/src/pyobject.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ pub type PyResult<T = PyObjectRef> = Result<T, PyBaseExceptionRef>; // A valid v
6464
/// For attributes we do not use a dict, but a hashmap. This is probably
6565
/// faster, unordered, and only supports strings as keys.
6666
/// TODO: class attributes should maintain insertion order (use IndexMap here)
67-
pub type PyAttributes = HashMap<String, PyObjectRef>;
67+
pub type PyAttributes = HashMap<String, PyObjectRef, ahash::RandomState>;
6868

6969
// TODO: remove this impl
7070
impl fmt::Display for PyObjectRef {

vm/src/pyobjectrc.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ pub(crate) fn init_type_hierarchy() -> (PyTypeRef, PyTypeRef) {
489489
bases: vec![],
490490
mro: vec![],
491491
subclasses: PyRwLock::default(),
492-
attributes: PyRwLock::new(PyAttributes::new()),
492+
attributes: PyRwLock::new(PyAttributes::default()),
493493
slots: PyType::make_slots(),
494494
};
495495
let object_payload = PyType {
@@ -498,7 +498,7 @@ pub(crate) fn init_type_hierarchy() -> (PyTypeRef, PyTypeRef) {
498498
bases: vec![],
499499
mro: vec![],
500500
subclasses: PyRwLock::default(),
501-
attributes: PyRwLock::new(PyAttributes::new()),
501+
attributes: PyRwLock::new(PyAttributes::default()),
502502
slots: object::PyBaseObject::make_slots(),
503503
};
504504
let type_type = PyRc::new(partially_init!(

vm/src/stdlib/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,10 @@ mod zlib;
7171

7272
pub type StdlibInitFunc = Box<py_dyn_fn!(dyn Fn(&VirtualMachine) -> PyObjectRef)>;
7373

74-
pub fn get_module_inits() -> HashMap<String, StdlibInitFunc> {
74+
pub fn get_module_inits() -> HashMap<String, StdlibInitFunc, ahash::RandomState> {
7575
#[allow(unused_mut)]
7676
let mut modules = hashmap! {
77+
hasher = ahash::RandomState::default(),
7778
"array".to_owned() => Box::new(array::make_module) as StdlibInitFunc,
7879
"atexit".to_owned() => Box::new(atexit::make_module),
7980
"binascii".to_owned() => Box::new(binascii::make_module),

vm/src/stdlib/thread.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::builtins::pytype::PyTypeRef;
44
use crate::builtins::tuple::PyTupleRef;
55
/// Implementation of the _thread module
66
use crate::exceptions::{self, IntoPyException};
7-
use crate::function::{FuncArgs, OptionalArg};
7+
use crate::function::{FuncArgs, KwArgs, OptionalArg};
88
use crate::pyobject::{
99
BorrowValue, Either, IdProtocol, ItemProtocol, PyCallable, PyClassImpl, PyObjectRef, PyRef,
1010
PyResult, PyValue, StaticType, TypeProtocol,
@@ -224,7 +224,10 @@ fn _thread_start_new_thread(
224224
) -> PyResult<u64> {
225225
let args = FuncArgs::new(
226226
args.borrow_value().to_owned(),
227-
kwargs.map_or_else(Default::default, |k| k.to_attributes()),
227+
kwargs
228+
.map_or_else(Default::default, |k| k.to_attributes())
229+
.into_iter()
230+
.collect::<KwArgs>(),
228231
);
229232
let mut thread_builder = thread::Builder::new();
230233
let stacksize = vm.state.stacksize.load();

vm/src/types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ pub fn create_type_with_slots(
255255
base: &PyTypeRef,
256256
slots: PyTypeSlots,
257257
) -> PyTypeRef {
258-
let dict = PyAttributes::new();
258+
let dict = PyAttributes::default();
259259
pytype::new(
260260
type_type.clone(),
261261
name,

vm/src/vm.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
//!
66
77
use std::cell::{Cell, Ref, RefCell};
8-
use std::collections::hash_map::HashMap;
9-
use std::collections::hash_set::HashSet;
8+
use std::collections::HashMap;
9+
use std::collections::HashSet;
1010
use std::fmt;
1111

1212
use crossbeam_utils::atomic::AtomicCell;
@@ -110,8 +110,8 @@ pub(crate) mod thread {
110110

111111
pub struct PyGlobalState {
112112
pub settings: PySettings,
113-
pub stdlib_inits: HashMap<String, stdlib::StdlibInitFunc>,
114-
pub frozen: HashMap<String, code::FrozenModule>,
113+
pub stdlib_inits: HashMap<String, stdlib::StdlibInitFunc, ahash::RandomState>,
114+
pub frozen: HashMap<String, code::FrozenModule, ahash::RandomState>,
115115
pub stacksize: AtomicCell<usize>,
116116
pub thread_count: AtomicCell<usize>,
117117
pub hash_secret: HashSecret,
@@ -273,7 +273,7 @@ impl VirtualMachine {
273273
state: PyRc::new(PyGlobalState {
274274
settings,
275275
stdlib_inits,
276-
frozen: HashMap::new(),
276+
frozen: HashMap::default(),
277277
stacksize: AtomicCell::new(0),
278278
thread_count: AtomicCell::new(0),
279279
hash_secret,

0 commit comments

Comments
 (0)