Skip to content

Commit

Permalink
unlikely misc
Browse files Browse the repository at this point in the history
  • Loading branch information
ijl committed Aug 4, 2021
1 parent d630136 commit 1f78115
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 35 deletions.
15 changes: 4 additions & 11 deletions src/deserialize/deserializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,6 @@ impl<'de> Visitor<'de> for JsonValue {
Ok(nonnull!(ffi!(PyFloat_FromDouble(value))))
}

fn visit_string<E>(self, value: String) -> Result<Self::Value, E>
where
E: de::Error,
{
Ok(nonnull!(unicode_from_str(value.as_str())))
}

fn visit_borrowed_str<E>(self, value: &str) -> Result<Self::Value, E>
where
E: de::Error,
Expand Down Expand Up @@ -193,7 +186,10 @@ impl<'de> Visitor<'de> for JsonValue {
let value = map.next_value_seed(self)?;
let pykey: *mut pyo3::ffi::PyObject;
let pyhash: pyo3::ffi::Py_hash_t;
if likely!(key.len() <= 64) {
if unlikely!(key.len() > 64) {
pykey = unicode_from_str(&key);
pyhash = hash_str(pykey);
} else {
let hash = cache_hash(key.as_bytes());
{
let map = unsafe {
Expand All @@ -212,9 +208,6 @@ impl<'de> Visitor<'de> for JsonValue {
pykey = entry.get();
pyhash = unsafe { (*pykey.cast::<PyASCIIObject>()).hash }
}
} else {
pykey = unicode_from_str(&key);
pyhash = hash_str(pykey);
}
let _ = ffi!(_PyDict_SetItem_KnownHash(
dict_ptr,
Expand Down
2 changes: 1 addition & 1 deletion src/serialize/dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ impl DictNonStrKey {
if unlikely!(ival == -1 && !ffi!(PyErr_Occurred()).is_null()) {
ffi!(PyErr_Clear());
let uval = ffi!(PyLong_AsUnsignedLongLong(key));
if unlikely!(uval == u64::MAX) && !ffi!(PyErr_Occurred()).is_null() {
if unlikely!(uval == u64::MAX && !ffi!(PyErr_Occurred()).is_null()) {
return Err(NonStrError::IntegerRange);
}
Ok(InlinableString::from(itoa::Buffer::new().format(uval)))
Expand Down
10 changes: 5 additions & 5 deletions src/serialize/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ impl<'p> Serialize for IntSerializer {
S: Serializer,
{
let val = ffi!(PyLong_AsLongLong(self.ptr));
if unlikely!(val == -1) && !ffi!(PyErr_Occurred()).is_null() {
if unlikely!(val == -1 && !ffi!(PyErr_Occurred()).is_null()) {
return UIntSerializer::new(self.ptr).serialize(serializer);
} else if unlikely!(self.opts & STRICT_INTEGER != 0)
&& (val > STRICT_INT_MAX || val < STRICT_INT_MIN)
{
} else if unlikely!(
self.opts & STRICT_INTEGER != 0 && (val > STRICT_INT_MAX || val < STRICT_INT_MIN)
) {
err!("Integer exceeds 53-bit range")
}
serializer.serialize_i64(val)
Expand All @@ -59,7 +59,7 @@ impl<'p> Serialize for UIntSerializer {
{
ffi!(PyErr_Clear());
let val = ffi!(PyLong_AsUnsignedLongLong(self.ptr));
if unlikely!(val == u64::MAX) && !ffi!(PyErr_Occurred()).is_null() {
if unlikely!(val == u64::MAX && !ffi!(PyErr_Occurred()).is_null()) {
err!("Integer exceeds 64-bit range")
}
serializer.serialize_u64(val)
Expand Down
20 changes: 10 additions & 10 deletions src/serialize/serializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,9 @@ impl<'p> Serialize for PyObjectSerializer {
if unlikely!(self.recursion == RECURSION_LIMIT) {
err!(RECURSION_LIMIT_REACHED)
}
if unlikely!(unsafe { PyDict_GET_SIZE(self.ptr) as usize } == 0) {
if unlikely!(unsafe { PyDict_GET_SIZE(self.ptr) } == 0) {
serializer.serialize_map(Some(0)).unwrap().end()
} else if likely!(self.opts & SORT_OR_NON_STR_KEYS == 0) {
} else if self.opts & SORT_OR_NON_STR_KEYS == 0 {
Dict::new(
self.ptr,
self.opts,
Expand Down Expand Up @@ -231,7 +231,7 @@ impl<'p> Serialize for PyObjectSerializer {
if unlikely!(self.recursion == RECURSION_LIMIT) {
err!(RECURSION_LIMIT_REACHED)
}
if unlikely!(ffi!(PyList_GET_SIZE(self.ptr)) as usize == 0) {
if unlikely!(ffi!(PyList_GET_SIZE(self.ptr)) == 0) {
serializer.serialize_seq(Some(0)).unwrap().end()
} else {
ListSerializer::new(
Expand All @@ -257,20 +257,20 @@ impl<'p> Serialize for PyObjectSerializer {
err!(RECURSION_LIMIT_REACHED)
}
let dict = ffi!(PyObject_GetAttr(self.ptr, DICT_STR));
if likely!(!dict.is_null()) {
ffi!(Py_DECREF(dict));
DataclassFastSerializer::new(
dict,
if unlikely!(dict.is_null()) {
unsafe { pyo3::ffi::PyErr_Clear() };
DataclassFallbackSerializer::new(
self.ptr,
self.opts,
self.default_calls,
self.recursion,
self.default,
)
.serialize(serializer)
} else {
unsafe { pyo3::ffi::PyErr_Clear() };
DataclassFallbackSerializer::new(
self.ptr,
ffi!(Py_DECREF(dict));
DataclassFastSerializer::new(
dict,
self.opts,
self.default_calls,
self.recursion,
Expand Down
2 changes: 1 addition & 1 deletion src/unicode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ enum PyUnicodeKind {
fn find_str_kind(buf: &str, num_chars: usize) -> PyUnicodeKind {
if buf.len() == num_chars {
PyUnicodeKind::Ascii
} else if unlikely!(encoding_rs::mem::is_str_latin1(buf)) {
} else if encoding_rs::mem::is_str_latin1(buf) {
// fails fast, no obvious effect on CJK
PyUnicodeKind::OneByte
} else if is_four_byte(buf) {
Expand Down
8 changes: 1 addition & 7 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,7 @@ macro_rules! err {

macro_rules! unlikely {
($exp:expr) => {
std::intrinsics::unlikely($exp)
};
}

macro_rules! likely {
($exp:expr) => {
std::intrinsics::likely($exp)
core::intrinsics::unlikely($exp)
};
}

Expand Down

0 comments on commit 1f78115

Please sign in to comment.