Skip to content

Commit

Permalink
no hashing of short strings in python
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelcolvin committed Nov 30, 2023
1 parent 4da99fd commit f6739a8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
10 changes: 10 additions & 0 deletions benches/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ fn python_parse_true_object_not_cached(bench: &mut Bencher) {
_python_parse_file("./benches/true_object.json", bench, false);
}

fn python_parse_string_array_not_cached(bench: &mut Bencher) {
_python_parse_file("./benches/string_array.json", bench, false);
}

fn python_parse_string_array(bench: &mut Bencher) {
_python_parse_file("./benches/string_array.json", bench, true);
}

fn python_parse_true_object(bench: &mut Bencher) {
_python_parse_file("./benches/true_object.json", bench, true);
}
Expand All @@ -83,6 +91,8 @@ benchmark_group!(
python_parse_medium_response_not_cached,
python_parse_medium_response,
python_parse_true_object_not_cached,
python_parse_string_array_not_cached,
python_parse_string_array,
python_parse_true_object,
python_parse_true_array,
);
Expand Down
3 changes: 2 additions & 1 deletion src/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ impl StringMaybeCache for StringCache {
fn get(py: Python, json_str: &str) -> PyObject {
static STRINGS_CACHE: GILOnceCell<GILProtected<RefCell<AHashMap<String, PyObject>>>> = GILOnceCell::new();

if json_str.len() < 64 {
// from tests, 0 and 1 character strings are faster not cached
if (2..64).contains(&json_str.len()) {
let cache = STRINGS_CACHE
.get_or_init(py, || GILProtected::new(RefCell::new(AHashMap::new())))
.get(py);
Expand Down

0 comments on commit f6739a8

Please sign in to comment.