Skip to content

Commit

Permalink
try disable PyString fast-path
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhewitt committed Dec 2, 2024
1 parent 68e6958 commit 76093e3
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions crates/jiter/src/py_string_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,21 +206,21 @@ pub fn pystring_fast_new<'py>(py: Python<'py>, s: &str, ascii_only: bool) -> Bou
}
}

/// Faster creation of PyString from an ASCII string, inspired by
/// https://github.com/ijl/orjson/blob/3.10.0/src/str/create.rs#L41
#[cfg(all(not(PyPy), not(GraalPy)))]
unsafe fn pystring_ascii_new<'py>(py: Python<'py>, s: &str) -> Bound<'py, PyString> {
let ptr = pyo3::ffi::PyUnicode_New(s.len() as isize, 127);
// see https://github.com/pydantic/jiter/pull/72#discussion_r1545485907
debug_assert_eq!(pyo3::ffi::PyUnicode_KIND(ptr), pyo3::ffi::PyUnicode_1BYTE_KIND);
let data_ptr = pyo3::ffi::PyUnicode_DATA(ptr).cast();
core::ptr::copy_nonoverlapping(s.as_ptr(), data_ptr, s.len());
core::ptr::write(data_ptr.add(s.len()), 0);
Bound::from_owned_ptr(py, ptr).downcast_into_unchecked()
}
// /// Faster creation of PyString from an ASCII string, inspired by
// /// https://github.com/ijl/orjson/blob/3.10.0/src/str/create.rs#L41
// #[cfg(all(not(PyPy), not(GraalPy)))]
// unsafe fn pystring_ascii_new<'py>(py: Python<'py>, s: &str) -> Bound<'py, PyString> {
// let ptr = pyo3::ffi::PyUnicode_New(s.len() as isize, 127);
// // see https://github.com/pydantic/jiter/pull/72#discussion_r1545485907
// debug_assert_eq!(pyo3::ffi::PyUnicode_KIND(ptr), pyo3::ffi::PyUnicode_1BYTE_KIND);
// let data_ptr = pyo3::ffi::PyUnicode_DATA(ptr).cast();
// core::ptr::copy_nonoverlapping(s.as_ptr(), data_ptr, s.len());
// core::ptr::write(data_ptr.add(s.len()), 0);
// Bound::from_owned_ptr(py, ptr).downcast_into_unchecked()
// }

// ffi::PyUnicode_DATA seems to be broken for pypy, hence this, marked as unsafe to avoid warnings
#[cfg(any(PyPy, GraalPy))]
// #[cfg(any(PyPy, GraalPy))]
unsafe fn pystring_ascii_new<'py>(py: Python<'py>, s: &str) -> Bound<'py, PyString> {
PyString::new_bound(py, s)
PyString::new(py, s)
}

0 comments on commit 76093e3

Please sign in to comment.