From 1f781153715362cb6397d6c9506fe34ff6585877 Mon Sep 17 00:00:00 2001 From: ijl Date: Wed, 4 Aug 2021 13:45:24 +0000 Subject: [PATCH] unlikely misc --- src/deserialize/deserializer.rs | 15 ++++----------- src/serialize/dict.rs | 2 +- src/serialize/int.rs | 10 +++++----- src/serialize/serializer.rs | 20 ++++++++++---------- src/unicode.rs | 2 +- src/util.rs | 8 +------- 6 files changed, 22 insertions(+), 35 deletions(-) diff --git a/src/deserialize/deserializer.rs b/src/deserialize/deserializer.rs index aca027af..9a35a2af 100644 --- a/src/deserialize/deserializer.rs +++ b/src/deserialize/deserializer.rs @@ -140,13 +140,6 @@ impl<'de> Visitor<'de> for JsonValue { Ok(nonnull!(ffi!(PyFloat_FromDouble(value)))) } - fn visit_string(self, value: String) -> Result - where - E: de::Error, - { - Ok(nonnull!(unicode_from_str(value.as_str()))) - } - fn visit_borrowed_str(self, value: &str) -> Result where E: de::Error, @@ -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 { @@ -212,9 +208,6 @@ impl<'de> Visitor<'de> for JsonValue { pykey = entry.get(); pyhash = unsafe { (*pykey.cast::()).hash } } - } else { - pykey = unicode_from_str(&key); - pyhash = hash_str(pykey); } let _ = ffi!(_PyDict_SetItem_KnownHash( dict_ptr, diff --git a/src/serialize/dict.rs b/src/serialize/dict.rs index 4027e419..542414ad 100644 --- a/src/serialize/dict.rs +++ b/src/serialize/dict.rs @@ -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))) diff --git a/src/serialize/int.rs b/src/serialize/int.rs index 05a056f9..8cdc06ae 100644 --- a/src/serialize/int.rs +++ b/src/serialize/int.rs @@ -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) @@ -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) diff --git a/src/serialize/serializer.rs b/src/serialize/serializer.rs index cfbf4dad..59fdfc7d 100644 --- a/src/serialize/serializer.rs +++ b/src/serialize/serializer.rs @@ -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, @@ -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( @@ -257,10 +257,10 @@ 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, @@ -268,9 +268,9 @@ impl<'p> Serialize for PyObjectSerializer { ) .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, diff --git a/src/unicode.rs b/src/unicode.rs index 2b230c18..d3134b1e 100644 --- a/src/unicode.rs +++ b/src/unicode.rs @@ -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) { diff --git a/src/util.rs b/src/util.rs index 396c9ff9..736bd842 100644 --- a/src/util.rs +++ b/src/util.rs @@ -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) }; }