Skip to content

Commit

Permalink
update python benchmarks to use new string cache
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelcolvin committed Jan 21, 2024
1 parent 3598994 commit 06480b2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
12 changes: 9 additions & 3 deletions jiter-python/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
use pyo3::prelude::*;

use jiter::{python_parse, map_json_error};
use jiter::{map_json_error, python_parse};

#[pyfunction(signature = (data, *, allow_inf_nan=true, cache_strings=true))]
pub fn from_json(py: Python, data: &[u8], allow_inf_nan: bool, cache_strings: bool) -> PyResult<PyObject> {
pub fn from_json(
py: Python,
data: &[u8],
allow_inf_nan: bool,
cache_strings: bool,
) -> PyResult<PyObject> {
let cache_mode = cache_strings.into();
let json_bytes = data;
python_parse(py, json_bytes, allow_inf_nan, cache_strings).map_err(|e| map_json_error(json_bytes, &e))
python_parse(py, json_bytes, allow_inf_nan, cache_mode).map_err(|e| map_json_error(json_bytes, &e))
}

#[pymodule]
Expand Down
24 changes: 16 additions & 8 deletions src/py_string_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,31 @@ pub enum StringCacheMode {
None,
}

impl TryFrom<&PyAny> for StringCacheMode {
type Error = PyErr;

fn try_from(mode: &PyAny) -> PyResult<Self> {
if let Ok(bool_mode) = mode.downcast::<PyBool>() {
Ok(if bool_mode.is_true() { Self::All } else { Self::None })
impl<'py> FromPyObject<'py> for StringCacheMode {
fn extract(ob: &'py PyAny) -> PyResult<StringCacheMode> {
if let Ok(bool_mode) = ob.downcast::<PyBool>() {
Ok(bool_mode.is_true().into())
} else {
match mode.extract()? {
match ob.extract()? {
"all" => Ok(Self::All),
"keys" => Ok(Self::Keys),
"none" => Ok(Self::None),
_ => Err(PyTypeError::new_err(format!("Invalid string cache mode: {}", mode))),
_ => Err(PyTypeError::new_err(format!("Invalid string cache mode: {}", ob))),
}
}
}
}

impl From<bool> for StringCacheMode {
fn from(mode: bool) -> Self {
if mode {
Self::All
} else {
Self::None
}
}
}

pub trait StringMaybeCache {
fn get_key(py: Python, json_str: &str) -> PyObject;

Expand Down

0 comments on commit 06480b2

Please sign in to comment.