Skip to content

Commit

Permalink
perf(python): Reduce ObjectType size
Browse files Browse the repository at this point in the history
Signed-off-by: Dmitry Dygalo <dmitry@dygalo.dev>
  • Loading branch information
Stranger6667 committed Oct 26, 2024
1 parent b0913ad commit c373c9f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
2 changes: 2 additions & 0 deletions crates/jsonschema-py/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
### Performance

- Optimize error formatting in some cases.
- Speedup Python -> Rust data serialization
- Significant improvement for the `validate` function.

## [0.25.1] - 2024-10-25

Expand Down
17 changes: 9 additions & 8 deletions crates/jsonschema-py/src/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ use serde::{
};

use crate::{ffi, types};
use std::ffi::CStr;
use std::ffi::{c_char, CStr};

pub const RECURSION_LIMIT: u8 = 255;

#[derive(Clone)]
#[derive(Clone, Copy)]
pub enum ObjectType {
Str,
Int,
Expand All @@ -29,7 +29,7 @@ pub enum ObjectType {
Dict,
Tuple,
Enum,
Unknown(String),
Unknown(*const c_char),
}

pub(crate) struct SerializePyObject {
Expand Down Expand Up @@ -126,7 +126,8 @@ pub fn get_object_type(object_type: *mut pyo3::ffi::PyTypeObject) -> ObjectType
} else if is_dict_subclass(object_type) {
ObjectType::Dict
} else {
ObjectType::Unknown(get_type_name(object_type).to_string())
let type_name_ptr = unsafe { (*object_type).tp_name };
ObjectType::Unknown(type_name_ptr)
}
}

Expand Down Expand Up @@ -252,7 +253,7 @@ impl Serialize for SerializePyObject {
#[allow(clippy::arithmetic_side_effects)]
sequence.serialize_element(&SerializePyObject::with_obtype(
elem,
ob_type.clone(),
ob_type,
self.recursion_depth + 1,
))?;
}
Expand Down Expand Up @@ -280,7 +281,7 @@ impl Serialize for SerializePyObject {
#[allow(clippy::arithmetic_side_effects)]
sequence.serialize_element(&SerializePyObject::with_obtype(
elem,
ob_type.clone(),
ob_type,
self.recursion_depth + 1,
))?;
}
Expand All @@ -292,9 +293,9 @@ impl Serialize for SerializePyObject {
#[allow(clippy::arithmetic_side_effects)]
SerializePyObject::new(value, self.recursion_depth + 1).serialize(serializer)
}
ObjectType::Unknown(ref type_name) => Err(ser::Error::custom(format!(
ObjectType::Unknown(type_name_ptr) => Err(ser::Error::custom(format!(
"Unsupported type: '{}'",
type_name
unsafe { CStr::from_ptr(type_name_ptr).to_string_lossy() }
))),
}
}
Expand Down

0 comments on commit c373c9f

Please sign in to comment.