Skip to content

Commit

Permalink
get_unicode_key() returns only the key
Browse files Browse the repository at this point in the history
  • Loading branch information
ijl committed Jul 7, 2023
1 parent 1953ecf commit c3766d7
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 16 deletions.
16 changes: 9 additions & 7 deletions src/deserialize/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,16 @@ impl<'de> Visitor<'de> for JsonValue {
{
let dict_ptr = ffi!(PyDict_New());
while let Some(key) = map.next_key::<beef::lean::Cow<str>>()? {
let (pykey, pyhash) = get_unicode_key(&key);
let pykey = get_unicode_key(&key);
let pyval = map.next_value_seed(self)?;
let _ = ffi!(_PyDict_SetItem_KnownHash(
dict_ptr,
pykey,
pyval.as_ptr(),
pyhash
));
let _ = unsafe {
pyo3_ffi::_PyDict_SetItem_KnownHash(
dict_ptr,
pykey,
pyval.as_ptr(),
str_hash!(pykey),
)
};
py_decref_without_destroy!(pykey);
py_decref_without_destroy!(pyval.as_ptr());
}
Expand Down
8 changes: 3 additions & 5 deletions src/deserialize/pyobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ use crate::str::*;
use crate::typeref::*;
use std::ptr::NonNull;

pub fn get_unicode_key(key_str: &str) -> (*mut pyo3_ffi::PyObject, pyo3_ffi::Py_hash_t) {
pub fn get_unicode_key(key_str: &str) -> *mut pyo3_ffi::PyObject {
let pykey: *mut pyo3_ffi::PyObject;
let pyhash: pyo3_ffi::Py_hash_t;
if unlikely!(key_str.len() > 64) {
pykey = unicode_from_str(key_str);
pyhash = hash_str(pykey);
hash_str(pykey);
} else {
let hash = cache_hash(key_str.as_bytes());
let map = unsafe {
Expand All @@ -27,9 +26,8 @@ pub fn get_unicode_key(key_str: &str) -> (*mut pyo3_ffi::PyObject, pyo3_ffi::Py_
},
);
pykey = entry.get();
pyhash = unsafe { (*pykey.cast::<pyo3_ffi::PyASCIIObject>()).hash }
}
(pykey, pyhash)
pykey
}

#[allow(dead_code)]
Expand Down
11 changes: 7 additions & 4 deletions src/deserialize/yyjson.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,16 @@ fn parse_yy_object(elem: *mut yyjson_val) -> NonNull<pyo3_ffi::PyObject> {
let mut key = unsafe_yyjson_get_first(elem);
let dict = ffi!(_PyDict_NewPresized(len as isize));
for _ in 0..=len - 1 {
let val = key.add(1);
let key_str = str_from_slice!((*key).uni.str_ as *const u8, unsafe_yyjson_get_len(key));
let (pykey, pyhash) = get_unicode_key(key_str);
let pyval = parse_node(key.add(1)).as_ptr();
let _ = ffi!(_PyDict_SetItem_KnownHash(dict, pykey, pyval, pyhash));
let pykey = get_unicode_key(key_str);
let pyval = parse_node(val).as_ptr();
key = unsafe_yyjson_get_next(val);
let _ = unsafe {
pyo3_ffi::_PyDict_SetItem_KnownHash(dict, pykey, pyval, str_hash!(pykey))
};
py_decref_without_destroy!(pykey);
py_decref_without_destroy!(pyval);
key = unsafe_yyjson_get_next(key.add(1));
}
nonnull!(dict)
}
Expand Down
6 changes: 6 additions & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ macro_rules! call_method {
};
}

macro_rules! str_hash {
($op:expr) => {
unsafe { (*$op.cast::<pyo3_ffi::PyASCIIObject>()).hash }
};
}

#[cfg(Py_3_10)]
macro_rules! pydict_contains {
($obj1:expr, $obj2:expr) => {
Expand Down

0 comments on commit c3766d7

Please sign in to comment.