Skip to content

Commit 9d5de78

Browse files
committed
Serialization starts with a larger vec
This is a mild improvement over Vec::with_capacity(128) in serde_json.
1 parent a432d15 commit 9d5de78

1 file changed

Lines changed: 13 additions & 8 deletions

File tree

src/encode.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,24 @@ pub fn serialize(
1414
ptr: *mut pyo3::ffi::PyObject,
1515
default: Option<NonNull<pyo3::ffi::PyObject>>,
1616
) -> PyResult<PyObject> {
17-
let buf: Vec<u8> = serde_json::to_vec(&SerializePyObject {
18-
ptr: ptr,
19-
default: default,
20-
recursion: 0,
21-
})
17+
let mut buf: Vec<u8> = Vec::with_capacity(1008);
18+
{
19+
serde_json::to_writer(
20+
&mut buf,
21+
&SerializePyObject {
22+
ptr: ptr,
23+
default: default,
24+
recursion: 0,
25+
},
26+
)
27+
}
2228
.map_err(|error| JSONEncodeError::py_err(error.to_string()))?;
23-
let slice = buf.as_slice();
2429
Ok(unsafe {
2530
PyObject::from_owned_ptr(
2631
py,
2732
pyo3::ffi::PyBytes_FromStringAndSize(
28-
slice.as_ptr() as *const c_char,
29-
slice.len() as pyo3::ffi::Py_ssize_t,
33+
buf.as_ptr() as *const c_char,
34+
buf.len() as pyo3::ffi::Py_ssize_t,
3035
),
3136
)
3237
})

0 commit comments

Comments
 (0)