Merge pull request #1296 from davidhewitt/map-with-capacity

dict: use with_capacity in HashMap extraction
This commit is contained in:
David Hewitt 2020-11-26 18:15:36 +00:00 committed by GitHub
commit 6669493502
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 60 additions and 2 deletions

View file

@ -3,6 +3,7 @@
extern crate test;
use pyo3::prelude::*;
use pyo3::types::IntoPyDict;
use std::collections::{BTreeMap, HashMap};
use test::Bencher;
#[bench]
@ -33,3 +34,31 @@ fn dict_get_item(b: &mut Bencher) {
}
});
}
#[bench]
fn extract_hashmap(b: &mut Bencher) {
let gil = Python::acquire_gil();
let py = gil.python();
const LEN: usize = 100_000;
let dict = (0..LEN as u64).map(|i| (i, i * 2)).into_py_dict(py);
b.iter(|| HashMap::<u64, u64>::extract(dict));
}
#[bench]
fn extract_btreemap(b: &mut Bencher) {
let gil = Python::acquire_gil();
let py = gil.python();
const LEN: usize = 100_000;
let dict = (0..LEN as u64).map(|i| (i, i * 2)).into_py_dict(py);
b.iter(|| BTreeMap::<u64, u64>::extract(dict));
}
#[bench]
#[cfg(feature = "hashbrown")]
fn extract_hashbrown_map(b: &mut Bencher) {
let gil = Python::acquire_gil();
let py = gil.python();
const LEN: usize = 100_000;
let dict = (0..LEN as u64).map(|i| (i, i * 2)).into_py_dict(py);
b.iter(|| hashbrown::HashMap::<u64, u64>::extract(dict));
}

View file

@ -3,6 +3,7 @@
extern crate test;
use pyo3::prelude::*;
use pyo3::types::PySet;
use std::collections::{BTreeSet, HashSet};
use test::Bencher;
#[bench]
@ -19,3 +20,31 @@ fn iter_set(b: &mut Bencher) {
}
});
}
#[bench]
fn extract_hashset(b: &mut Bencher) {
let gil = Python::acquire_gil();
let py = gil.python();
const LEN: usize = 100_000;
let set = PySet::new(py, &(0..LEN).collect::<Vec<_>>()).unwrap();
b.iter(|| HashSet::<u64>::extract(set));
}
#[bench]
fn extract_btreeset(b: &mut Bencher) {
let gil = Python::acquire_gil();
let py = gil.python();
const LEN: usize = 100_000;
let set = PySet::new(py, &(0..LEN).collect::<Vec<_>>()).unwrap();
b.iter(|| BTreeSet::<u64>::extract(set));
}
#[bench]
#[cfg(feature = "hashbrown")]
fn extract_hashbrown_set(b: &mut Bencher) {
let gil = Python::acquire_gil();
let py = gil.python();
const LEN: usize = 100_000;
let set = PySet::new(py, &(0..LEN).collect::<Vec<_>>()).unwrap();
b.iter(|| hashbrown::HashSet::<u64>::extract(set));
}

View file

@ -331,7 +331,7 @@ where
{
fn extract(ob: &'source PyAny) -> Result<Self, PyErr> {
let dict = <PyDict as PyTryFrom>::try_from(ob)?;
let mut ret = HashMap::default();
let mut ret = HashMap::with_capacity_and_hasher(dict.len(), S::default());
for (k, v) in dict.iter() {
ret.insert(K::extract(k)?, V::extract(v)?);
}
@ -392,7 +392,7 @@ mod hashbrown_hashmap_conversion {
{
fn extract(ob: &'source PyAny) -> Result<Self, PyErr> {
let dict = <PyDict as PyTryFrom>::try_from(ob)?;
let mut ret = hashbrown::HashMap::default();
let mut ret = hashbrown::HashMap::with_capacity_and_hasher(dict.len(), S::default());
for (k, v) in dict.iter() {
ret.insert(K::extract(k)?, V::extract(v)?);
}