Skip to content

Commit dd32acf

Browse files
authored
use ahash for hashsets, seems to be significantly quicker (#132)
1 parent 1919da1 commit dd32acf

File tree

4 files changed

+44
-13
lines changed

4 files changed

+44
-13
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ serde = "1.0.137"
1818
indexmap = "1.8.1"
1919
mimalloc = { version = "0.1.29", default-features = false, optional = true }
2020
speedate = "0.4.1"
21+
ahash = "0.7.6"
2122

2223
[lib]
2324
name = "_pydantic_core"

src/validators/literal.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
use std::collections::HashSet;
2-
31
use pyo3::prelude::*;
42
use pyo3::types::{PyDict, PyList};
53

4+
use ahash::AHashSet;
5+
66
use crate::build_tools::{py_error, SchemaDict};
77
use crate::errors::{as_internal, context, err_val_error, ErrorKind, ValResult};
88
use crate::input::Input;
@@ -119,13 +119,13 @@ impl Validator for LiteralSingleIntValidator {
119119

120120
#[derive(Debug, Clone)]
121121
pub struct LiteralMultipleStringsValidator {
122-
expected: HashSet<String>,
122+
expected: AHashSet<String>,
123123
repr: String,
124124
}
125125

126126
impl LiteralMultipleStringsValidator {
127127
fn new(expected_list: &PyList) -> Option<Self> {
128-
let mut expected: HashSet<String> = HashSet::new();
128+
let mut expected: AHashSet<String> = AHashSet::new();
129129
let mut repr_args = Vec::new();
130130
for item in expected_list.iter() {
131131
if let Ok(str) = item.extract() {
@@ -170,13 +170,13 @@ impl Validator for LiteralMultipleStringsValidator {
170170

171171
#[derive(Debug, Clone)]
172172
pub struct LiteralMultipleIntsValidator {
173-
expected: HashSet<i64>,
173+
expected: AHashSet<i64>,
174174
repr: String,
175175
}
176176

177177
impl LiteralMultipleIntsValidator {
178178
fn new(expected_list: &PyList) -> Option<Self> {
179-
let mut expected: HashSet<i64> = HashSet::new();
179+
let mut expected: AHashSet<i64> = AHashSet::new();
180180
let mut repr_args = Vec::new();
181181
for item in expected_list.iter() {
182182
if let Ok(str) = item.extract() {
@@ -221,16 +221,16 @@ impl Validator for LiteralMultipleIntsValidator {
221221

222222
#[derive(Debug, Clone)]
223223
pub struct LiteralGeneralValidator {
224-
expected_int: HashSet<i64>,
225-
expected_str: HashSet<String>,
224+
expected_int: AHashSet<i64>,
225+
expected_str: AHashSet<String>,
226226
expected_py: Py<PyList>,
227227
repr: String,
228228
}
229229

230230
impl LiteralGeneralValidator {
231231
fn new(expected: &PyList) -> PyResult<Self> {
232-
let mut expected_int = HashSet::new();
233-
let mut expected_str = HashSet::new();
232+
let mut expected_int = AHashSet::new();
233+
let mut expected_str = AHashSet::new();
234234
let py = expected.py();
235235
let expected_py = PyList::empty(py);
236236
let mut repr_args: Vec<String> = Vec::new();

src/validators/typed_dict.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ use pyo3::exceptions::{PyAttributeError, PyTypeError};
22
use pyo3::intern;
33
use pyo3::prelude::*;
44
use pyo3::types::{PyDict, PyFunction, PyList, PySet, PyString};
5-
use std::collections::HashSet;
5+
6+
use ahash::AHashSet;
67

78
use crate::build_tools::{py_error, SchemaDict};
89
use crate::errors::{
@@ -143,8 +144,8 @@ impl Validator for TypedDictValidator {
143144

144145
// we only care about which keys have been used if we're iterating over the object for extra after
145146
// the first pass
146-
let mut used_keys: Option<HashSet<&str>> = match self.check_extra {
147-
true => Some(HashSet::with_capacity(self.fields.len())),
147+
let mut used_keys: Option<AHashSet<&str>> = match self.check_extra {
148+
true => Some(AHashSet::with_capacity(self.fields.len())),
148149
false => None,
149150
};
150151

0 commit comments

Comments
 (0)