Skip to content

Commit

Permalink
Optimize array creation (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarshalX authored Mar 19, 2024
1 parent 9fb1353 commit 1c0df5e
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use anyhow::{anyhow, Result};
use byteorder::{BigEndian, ByteOrder};
use multihash::Multihash;
use pyo3::conversion::ToPyObject;
use pyo3::prelude::*;
use pyo3::ffi::Py_ssize_t;
use pyo3::{ffi, prelude::*};
use pyo3::pybacked::PyBackedStr;
use pyo3::types::*;
use pyo3::{PyObject, Python};
Expand Down Expand Up @@ -108,14 +109,18 @@ fn decode_dag_cbor_to_pyobject<R: Read + Seek>(
PyString::new_bound(py, &decode::read_str(r, len)?).to_object(py)
}
MajorKind::Array => {
let len = decode_len(decode::read_uint(r, major)?)?;
let list = PyList::empty_bound(py);
let len: Py_ssize_t = decode_len(decode::read_uint(r, major)?)?.try_into()?;

for _ in 0..len {
list.append(decode_dag_cbor_to_pyobject(py, r, deep + 1)?)?;
}
unsafe {
let ptr = ffi::PyList_New(len);

list.to_object(py)
for i in 0..len {
ffi::PyList_SET_ITEM(ptr, i, decode_dag_cbor_to_pyobject(py, r, deep + 1)?.into_ptr());
}

let list: Bound<'_, PyList> = Bound::from_owned_ptr(py, ptr).downcast_into_unchecked();
list.to_object(py)
}
}
MajorKind::Map => {
let len = decode_len(decode::read_uint(r, major)?)?;
Expand Down

0 comments on commit 1c0df5e

Please sign in to comment.