Skip to content

Commit 3c76b61

Browse files
committed
update python benchmarks to use new string cache
1 parent 9561ae6 commit 3c76b61

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

jiter-python/src/lib.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
use pyo3::prelude::*;
22

3-
use jiter::{python_parse, map_json_error};
3+
use jiter::{map_json_error, python_parse};
44

55
#[pyfunction(signature = (data, *, allow_inf_nan=true, cache_strings=true))]
6-
pub fn from_json(py: Python, data: &[u8], allow_inf_nan: bool, cache_strings: bool) -> PyResult<PyObject> {
6+
pub fn from_json(
7+
py: Python,
8+
data: &[u8],
9+
allow_inf_nan: bool,
10+
cache_strings: bool,
11+
) -> PyResult<PyObject> {
12+
let cache_mode = cache_strings.into();
713
let json_bytes = data;
8-
python_parse(py, json_bytes, allow_inf_nan, cache_strings).map_err(|e| map_json_error(json_bytes, &e))
14+
python_parse(py, json_bytes, allow_inf_nan, cache_mode).map_err(|e| map_json_error(json_bytes, &e))
915
}
1016

1117
#[pymodule]

src/py_string_cache.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,31 @@ pub enum StringCacheMode {
1515
None,
1616
}
1717

18-
impl TryFrom<&PyAny> for StringCacheMode {
19-
type Error = PyErr;
20-
21-
fn try_from(mode: &PyAny) -> PyResult<Self> {
22-
if let Ok(bool_mode) = mode.downcast::<PyBool>() {
23-
Ok(if bool_mode.is_true() { Self::All } else { Self::None })
18+
impl<'py> FromPyObject<'py> for StringCacheMode {
19+
fn extract(ob: &'py PyAny) -> PyResult<StringCacheMode> {
20+
if let Ok(bool_mode) = ob.downcast::<PyBool>() {
21+
Ok(bool_mode.is_true().into())
2422
} else {
25-
match mode.extract()? {
23+
match ob.extract()? {
2624
"all" => Ok(Self::All),
2725
"keys" => Ok(Self::Keys),
2826
"none" => Ok(Self::None),
29-
_ => Err(PyTypeError::new_err(format!("Invalid string cache mode: {}", mode))),
27+
_ => Err(PyTypeError::new_err(format!("Invalid string cache mode: {}", ob))),
3028
}
3129
}
3230
}
3331
}
3432

33+
impl From<bool> for StringCacheMode {
34+
fn from(mode: bool) -> Self {
35+
if mode {
36+
Self::All
37+
} else {
38+
Self::None
39+
}
40+
}
41+
}
42+
3543
pub trait StringMaybeCache {
3644
fn get_key(py: Python, json_str: &str) -> PyObject;
3745

0 commit comments

Comments
 (0)