X Tutup
The Wayback Machine - https://web.archive.org/web/20201013133117/https://github.com/mre/hyperjson/issues/68
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

Speed up boolean encoding/decoding #68

Open
mre opened this issue Sep 21, 2019 · 1 comment
Open

Speed up boolean encoding/decoding #68

mre opened this issue Sep 21, 2019 · 1 comment

Comments

@mre
Copy link
Owner

@mre mre commented Sep 21, 2019

From our benchmarks we can see that we are consistently slower than everyone else when serializing/deserializing boolean values. We should fix that.

orjson is using an unsafe block to create a reference to a boolean:
https://github.com/ijl/orjson/blob/03d55e99a953ce93cedc05f03e4b63b0bcbbcc7a/src/decode.rs#L81-L96

This avoids additional allocations.
For comparison, this is our code at the moment:

hyperjson/src/lib.rs

Lines 475 to 480 in ded13b4

fn visit_bool<E>(self, value: bool) -> Result<Self::Value, E>
where
E: de::Error,
{
Ok(value.to_object(self.py))
}

I wonder if we could achieve comparable performance without using unsafe.
@konstin, any idea? Maybe there was a recent development in pyo3 that we could leverage here?

@konstin
Copy link
Collaborator

@konstin konstin commented Sep 22, 2019

It's weird that this slower than what orjson does. That's the implementation of ToObject:

unsafe {
    PyObject::from_borrowed_ptr(
        py,
        if *self {
            ffi::Py_True()
        } else {
            ffi::Py_False()
        },
    )
}

And that's the implementation of from_borrowed_ptr:

debug_assert!(
    !ptr.is_null() && ffi::Py_REFCNT(ptr) > 0,
    format!("REFCNT: {:?} - {:?}", ptr, ffi::Py_REFCNT(ptr))
);
ffi::Py_INCREF(ptr);
PyObject(NonNull::new_unchecked(ptr))

So in theory this should compile down to the same as orjson. You could try using PyBool::new and see if that makes a difference, but otherwise I don't know enough about inspecting assembly to debug that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.
X Tutup