Skip to content

Commit 1f78115

Browse files
committed
unlikely misc
1 parent d630136 commit 1f78115

File tree

6 files changed

+22
-35
lines changed

6 files changed

+22
-35
lines changed

src/deserialize/deserializer.rs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,6 @@ impl<'de> Visitor<'de> for JsonValue {
140140
Ok(nonnull!(ffi!(PyFloat_FromDouble(value))))
141141
}
142142

143-
fn visit_string<E>(self, value: String) -> Result<Self::Value, E>
144-
where
145-
E: de::Error,
146-
{
147-
Ok(nonnull!(unicode_from_str(value.as_str())))
148-
}
149-
150143
fn visit_borrowed_str<E>(self, value: &str) -> Result<Self::Value, E>
151144
where
152145
E: de::Error,
@@ -193,7 +186,10 @@ impl<'de> Visitor<'de> for JsonValue {
193186
let value = map.next_value_seed(self)?;
194187
let pykey: *mut pyo3::ffi::PyObject;
195188
let pyhash: pyo3::ffi::Py_hash_t;
196-
if likely!(key.len() <= 64) {
189+
if unlikely!(key.len() > 64) {
190+
pykey = unicode_from_str(&key);
191+
pyhash = hash_str(pykey);
192+
} else {
197193
let hash = cache_hash(key.as_bytes());
198194
{
199195
let map = unsafe {
@@ -212,9 +208,6 @@ impl<'de> Visitor<'de> for JsonValue {
212208
pykey = entry.get();
213209
pyhash = unsafe { (*pykey.cast::<PyASCIIObject>()).hash }
214210
}
215-
} else {
216-
pykey = unicode_from_str(&key);
217-
pyhash = hash_str(pykey);
218211
}
219212
let _ = ffi!(_PyDict_SetItem_KnownHash(
220213
dict_ptr,

src/serialize/dict.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ impl DictNonStrKey {
218218
if unlikely!(ival == -1 && !ffi!(PyErr_Occurred()).is_null()) {
219219
ffi!(PyErr_Clear());
220220
let uval = ffi!(PyLong_AsUnsignedLongLong(key));
221-
if unlikely!(uval == u64::MAX) && !ffi!(PyErr_Occurred()).is_null() {
221+
if unlikely!(uval == u64::MAX && !ffi!(PyErr_Occurred()).is_null()) {
222222
return Err(NonStrError::IntegerRange);
223223
}
224224
Ok(InlinableString::from(itoa::Buffer::new().format(uval)))

src/serialize/int.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ impl<'p> Serialize for IntSerializer {
2929
S: Serializer,
3030
{
3131
let val = ffi!(PyLong_AsLongLong(self.ptr));
32-
if unlikely!(val == -1) && !ffi!(PyErr_Occurred()).is_null() {
32+
if unlikely!(val == -1 && !ffi!(PyErr_Occurred()).is_null()) {
3333
return UIntSerializer::new(self.ptr).serialize(serializer);
34-
} else if unlikely!(self.opts & STRICT_INTEGER != 0)
35-
&& (val > STRICT_INT_MAX || val < STRICT_INT_MIN)
36-
{
34+
} else if unlikely!(
35+
self.opts & STRICT_INTEGER != 0 && (val > STRICT_INT_MAX || val < STRICT_INT_MIN)
36+
) {
3737
err!("Integer exceeds 53-bit range")
3838
}
3939
serializer.serialize_i64(val)
@@ -59,7 +59,7 @@ impl<'p> Serialize for UIntSerializer {
5959
{
6060
ffi!(PyErr_Clear());
6161
let val = ffi!(PyLong_AsUnsignedLongLong(self.ptr));
62-
if unlikely!(val == u64::MAX) && !ffi!(PyErr_Occurred()).is_null() {
62+
if unlikely!(val == u64::MAX && !ffi!(PyErr_Occurred()).is_null()) {
6363
err!("Integer exceeds 64-bit range")
6464
}
6565
serializer.serialize_u64(val)

src/serialize/serializer.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,9 @@ impl<'p> Serialize for PyObjectSerializer {
196196
if unlikely!(self.recursion == RECURSION_LIMIT) {
197197
err!(RECURSION_LIMIT_REACHED)
198198
}
199-
if unlikely!(unsafe { PyDict_GET_SIZE(self.ptr) as usize } == 0) {
199+
if unlikely!(unsafe { PyDict_GET_SIZE(self.ptr) } == 0) {
200200
serializer.serialize_map(Some(0)).unwrap().end()
201-
} else if likely!(self.opts & SORT_OR_NON_STR_KEYS == 0) {
201+
} else if self.opts & SORT_OR_NON_STR_KEYS == 0 {
202202
Dict::new(
203203
self.ptr,
204204
self.opts,
@@ -231,7 +231,7 @@ impl<'p> Serialize for PyObjectSerializer {
231231
if unlikely!(self.recursion == RECURSION_LIMIT) {
232232
err!(RECURSION_LIMIT_REACHED)
233233
}
234-
if unlikely!(ffi!(PyList_GET_SIZE(self.ptr)) as usize == 0) {
234+
if unlikely!(ffi!(PyList_GET_SIZE(self.ptr)) == 0) {
235235
serializer.serialize_seq(Some(0)).unwrap().end()
236236
} else {
237237
ListSerializer::new(
@@ -257,20 +257,20 @@ impl<'p> Serialize for PyObjectSerializer {
257257
err!(RECURSION_LIMIT_REACHED)
258258
}
259259
let dict = ffi!(PyObject_GetAttr(self.ptr, DICT_STR));
260-
if likely!(!dict.is_null()) {
261-
ffi!(Py_DECREF(dict));
262-
DataclassFastSerializer::new(
263-
dict,
260+
if unlikely!(dict.is_null()) {
261+
unsafe { pyo3::ffi::PyErr_Clear() };
262+
DataclassFallbackSerializer::new(
263+
self.ptr,
264264
self.opts,
265265
self.default_calls,
266266
self.recursion,
267267
self.default,
268268
)
269269
.serialize(serializer)
270270
} else {
271-
unsafe { pyo3::ffi::PyErr_Clear() };
272-
DataclassFallbackSerializer::new(
273-
self.ptr,
271+
ffi!(Py_DECREF(dict));
272+
DataclassFastSerializer::new(
273+
dict,
274274
self.opts,
275275
self.default_calls,
276276
self.recursion,

src/unicode.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ enum PyUnicodeKind {
5151
fn find_str_kind(buf: &str, num_chars: usize) -> PyUnicodeKind {
5252
if buf.len() == num_chars {
5353
PyUnicodeKind::Ascii
54-
} else if unlikely!(encoding_rs::mem::is_str_latin1(buf)) {
54+
} else if encoding_rs::mem::is_str_latin1(buf) {
5555
// fails fast, no obvious effect on CJK
5656
PyUnicodeKind::OneByte
5757
} else if is_four_byte(buf) {

src/util.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,7 @@ macro_rules! err {
2020

2121
macro_rules! unlikely {
2222
($exp:expr) => {
23-
std::intrinsics::unlikely($exp)
24-
};
25-
}
26-
27-
macro_rules! likely {
28-
($exp:expr) => {
29-
std::intrinsics::likely($exp)
23+
core::intrinsics::unlikely($exp)
3024
};
3125
}
3226

0 commit comments

Comments
 (0)