Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tidy up some uses of Bound<'_, PyString>::to_str #1544

Merged
merged 1 commit into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions src/serializers/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ pub(crate) fn infer_to_python_known(
v.into_py(py)
}
ObType::Decimal => value.to_string().into_py(py),
ObType::StrSubclass => value.downcast::<PyString>()?.to_str()?.into_py(py),
ObType::StrSubclass => PyString::new_bound(py, value.downcast::<PyString>()?.to_str()?).into(),
ObType::Bytes => extra
.config
.bytes_mode
Expand Down Expand Up @@ -609,16 +609,11 @@ pub(crate) fn infer_json_key_known<'a>(
}
ObType::Decimal => Ok(Cow::Owned(key.to_string())),
ObType::Bool => super::type_serializers::simple::bool_json_key(key),
ObType::Str | ObType::StrSubclass => {
let py_str = key.downcast::<PyString>()?;
Ok(Cow::Owned(py_str.to_str()?.to_string()))
}
Comment on lines -612 to -615
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While most of this diff is within noise of allocations elsewhere in the system, removing this allocation does show a teeny tiny improvement here:

image

ObType::Str | ObType::StrSubclass => key.downcast::<PyString>()?.to_cow(),
ObType::Bytes => extra
.config
.bytes_mode
.bytes_to_string(key.py(), key.downcast::<PyBytes>()?.as_bytes())
// FIXME it would be nice to have a "PyCow" which carries ownership of the Python type too
.map(|s| Cow::Owned(s.into_owned())),
.bytes_to_string(key.py(), key.downcast::<PyBytes>()?.as_bytes()),
ObType::Bytearray => {
let py_byte_array = key.downcast::<PyByteArray>()?;
// Safety: the GIL is held while serialize_bytes is running; it doesn't run
Expand Down
5 changes: 2 additions & 3 deletions src/serializers/type_serializers/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,15 @@ impl BuildSerializer for FormatSerializer {
) -> PyResult<CombinedSerializer> {
let py = schema.py();
let formatting_string: Bound<'_, PyString> = schema.get_as_req(intern!(py, "formatting_string"))?;
let formatting_string = formatting_string.to_str()?;
if formatting_string.is_empty() {
if formatting_string.is_empty()? {
ToStringSerializer::build(schema, config, definitions)
} else {
Ok(Self {
format_func: py
.import_bound(intern!(py, "builtins"))?
.getattr(intern!(py, "format"))?
.into_py(py),
formatting_string: PyString::new_bound(py, formatting_string).into(),
formatting_string: formatting_string.unbind(),
when_used: WhenUsed::new(schema, WhenUsed::JsonUnlessNone)?,
}
.into())
Expand Down
Loading