Skip to content

Commit

Permalink
optimize(size) init and error paths
Browse files Browse the repository at this point in the history
  • Loading branch information
ijl committed Dec 5, 2021
1 parent 6a8ac61 commit 4514980
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/deserialize/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ impl<'a> DeserializeError<'a> {

/// Return position of the error in the deserialized data
#[cold]
#[cfg_attr(feature = "unstable-simd", optimize(size))]
pub fn pos(&self) -> usize {
if self.line == 0 {
return 1;
Expand Down
1 change: 1 addition & 0 deletions src/exc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub enum SerializeError {

impl std::fmt::Display for SerializeError {
#[cold]
#[cfg_attr(feature = "unstable-simd", optimize(size))]
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match *self {
SerializeError::DatetimeLibraryUnsupported => write!(f, "datetime's timezone library is not supported: use datetime.timezone.utc, pendulum, pytz, or dateutil"),
Expand Down
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// SPDX-License-Identifier: (Apache-2.0 OR MIT)

#![cfg_attr(feature = "unstable-simd", feature(core_intrinsics))]
#![cfg_attr(feature = "unstable-simd", feature(optimize_attribute))]
#![allow(unused_unsafe)]
#![allow(clippy::missing_safety_doc)]
#![allow(clippy::redundant_field_names)]
Expand Down Expand Up @@ -43,6 +44,7 @@ macro_rules! opt {
#[allow(non_snake_case)]
#[no_mangle]
#[cold]
#[cfg_attr(feature = "unstable-simd", optimize(size))]
pub unsafe extern "C" fn PyInit_orjson() -> *mut PyObject {
let init = PyModuleDef {
m_base: PyModuleDef_HEAD_INIT,
Expand Down Expand Up @@ -205,6 +207,7 @@ pub unsafe extern "C" fn PyInit_orjson() -> *mut PyObject {

#[cold]
#[inline(never)]
#[cfg_attr(feature = "unstable-simd", optimize(size))]
fn raise_loads_exception(err: deserialize::DeserializeError) -> *mut PyObject {
let pos = err.pos() as i64;
let msg = err.message;
Expand All @@ -226,6 +229,7 @@ fn raise_loads_exception(err: deserialize::DeserializeError) -> *mut PyObject {

#[cold]
#[inline(never)]
#[cfg_attr(feature = "unstable-simd", optimize(size))]
fn raise_dumps_exception(msg: Cow<str>) -> *mut PyObject {
unsafe {
let err_msg =
Expand Down
4 changes: 4 additions & 0 deletions src/serialize/numpy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,8 @@ pub enum NumpyDatetimeUnit {
}

impl fmt::Display for NumpyDatetimeUnit {
#[cold]
#[cfg_attr(feature = "unstable-simd", optimize(size))]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let unit = match self {
Self::Years => "years",
Expand Down Expand Up @@ -688,6 +690,8 @@ enum NumpyDateTimeError {
}

impl NumpyDateTimeError {
#[cold]
#[cfg_attr(feature = "unstable-simd", optimize(size))]
fn into_serde_err<T: ser::Error>(self) -> T {
let err = match self {
Self::UnsupportedUnit(unit) => format!("unsupported numpy.datetime64 unit: {}", unit),
Expand Down
10 changes: 10 additions & 0 deletions src/typeref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ pub static mut JsonDecodeError: *mut PyObject = 0 as *mut PyObject;
static INIT: Once = Once::new();

#[cold]
#[cfg_attr(feature = "unstable-simd", optimize(size))]
pub fn init_typerefs() {
INIT.call_once(|| unsafe {
assert!(crate::deserialize::KEY_MAP
Expand Down Expand Up @@ -142,6 +143,7 @@ pub fn init_typerefs() {
}

#[cold]
#[cfg_attr(feature = "unstable-simd", optimize(size))]
unsafe fn look_up_json_exc() -> *mut PyObject {
let module = PyImport_ImportModule("json\0".as_ptr() as *const c_char);
let module_dict = PyObject_GenericGetDict(module, std::ptr::null_mut());
Expand All @@ -159,6 +161,7 @@ unsafe fn look_up_json_exc() -> *mut PyObject {
}

#[cold]
#[cfg_attr(feature = "unstable-simd", optimize(size))]
unsafe fn look_up_numpy_type(numpy_module: *mut PyObject, np_type: &str) -> *mut PyTypeObject {
let mod_dict = PyObject_GenericGetDict(numpy_module, std::ptr::null_mut());
let ptr = PyMapping_GetItemString(mod_dict, np_type.as_ptr() as *const c_char);
Expand All @@ -168,6 +171,7 @@ unsafe fn look_up_numpy_type(numpy_module: *mut PyObject, np_type: &str) -> *mut
}

#[cold]
#[cfg_attr(feature = "unstable-simd", optimize(size))]
unsafe fn load_numpy_types() -> Option<NumpyTypes> {
let numpy = PyImport_ImportModule("numpy\0".as_ptr() as *const c_char);
if numpy.is_null() {
Expand All @@ -193,6 +197,7 @@ unsafe fn load_numpy_types() -> Option<NumpyTypes> {
}

#[cold]
#[cfg_attr(feature = "unstable-simd", optimize(size))]
unsafe fn look_up_field_type() -> NonNull<PyObject> {
let module = PyImport_ImportModule("dataclasses\0".as_ptr() as *const c_char);
let module_dict = PyObject_GenericGetDict(module, std::ptr::null_mut());
Expand All @@ -204,6 +209,7 @@ unsafe fn look_up_field_type() -> NonNull<PyObject> {
}

#[cold]
#[cfg_attr(feature = "unstable-simd", optimize(size))]
unsafe fn look_up_enum_type() -> *mut PyTypeObject {
let module = PyImport_ImportModule("enum\0".as_ptr() as *const c_char);
let module_dict = PyObject_GenericGetDict(module, std::ptr::null_mut());
Expand All @@ -215,6 +221,7 @@ unsafe fn look_up_enum_type() -> *mut PyTypeObject {
}

#[cold]
#[cfg_attr(feature = "unstable-simd", optimize(size))]
unsafe fn look_up_uuid_type() -> *mut PyTypeObject {
let uuid_mod = PyImport_ImportModule("uuid\0".as_ptr() as *const c_char);
let uuid_mod_dict = PyObject_GenericGetDict(uuid_mod, std::ptr::null_mut());
Expand All @@ -227,6 +234,7 @@ unsafe fn look_up_uuid_type() -> *mut PyTypeObject {
}

#[cold]
#[cfg_attr(feature = "unstable-simd", optimize(size))]
unsafe fn look_up_datetime_type() -> *mut PyTypeObject {
let datetime = (PyDateTimeAPI.DateTime_FromDateAndTime)(
1970,
Expand All @@ -245,6 +253,7 @@ unsafe fn look_up_datetime_type() -> *mut PyTypeObject {
}

#[cold]
#[cfg_attr(feature = "unstable-simd", optimize(size))]
unsafe fn look_up_date_type() -> *mut PyTypeObject {
let date = (PyDateTimeAPI.Date_FromDate)(1970, 1, 1, PyDateTimeAPI.DateType);
let ptr = (*date).ob_type;
Expand All @@ -253,6 +262,7 @@ unsafe fn look_up_date_type() -> *mut PyTypeObject {
}

#[cold]
#[cfg_attr(feature = "unstable-simd", optimize(size))]
unsafe fn look_up_time_type() -> *mut PyTypeObject {
let time = (PyDateTimeAPI.Time_FromTime)(0, 0, 0, 0, NONE, PyDateTimeAPI.TimeType);
let ptr = (*time).ob_type;
Expand Down

0 comments on commit 4514980

Please sign in to comment.