Skip to content

Commit

Permalink
fix fast string creation for pypy (#75)
Browse files Browse the repository at this point in the history
Co-authored-by: David Hewitt <mail@davidhewitt.dev>
  • Loading branch information
samuelcolvin and davidhewitt authored Apr 2, 2024
1 parent 496e36a commit a5f7dac
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pyo3 = { version = "0.21.0", default-features=false, features = ["num-bigint"],
lexical-parse-float = { version = "0.8.5", features = ["format"] }

[features]
python = ["dep:pyo3"]
python = ["dep:pyo3", "dep:pyo3-build-config"]

[dev-dependencies]
bencher = "0.1.5"
Expand All @@ -29,6 +29,9 @@ serde = "1.0.147"
pyo3 = { version = "0.21.0", default-features=false, features = ["num-bigint", "auto-initialize"] }
codspeed-bencher-compat = "2.3.1"

[build-dependencies]
pyo3-build-config = { version = "0.21.0", optional = true }

[profile.bench]
debug = true
lto = true
Expand Down
4 changes: 4 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main() {
#[cfg(feature = "python")]
pyo3_build_config::use_pyo3_cfgs();
}
7 changes: 7 additions & 0 deletions src/py_string_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ 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(not(PyPy))]
unsafe fn pystring_ascii_new<'py>(py: Python<'py>, s: &str) -> Bound<'py, PyString> {
let ptr = ffi::PyUnicode_New(s.len() as isize, 127);
// see https://github.com/pydantic/jiter/pull/72#discussion_r1545485907
Expand All @@ -204,3 +205,9 @@ unsafe fn pystring_ascii_new<'py>(py: Python<'py>, s: &str) -> Bound<'py, PyStri
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
#[cfg(PyPy)]
fn pystring_ascii_new<'py>(py: Python<'py>, s: &str) -> Bound<'py, PyString> {
PyString::new_bound(py, s)
}

0 comments on commit a5f7dac

Please sign in to comment.