Skip to content

Commit 180be42

Browse files
authored
Merge branch 'main' into test-mimalloc
2 parents db1a32a + f1f19a9 commit 180be42

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

src/lib.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use mimalloc::MiMalloc;
33
#[global_allocator]
44
static GLOBAL: MiMalloc = MiMalloc;
55

6-
use std::io::{self, Write};
6+
use std::io::{self, Cursor, Write};
77

88
use anyhow::{anyhow, Result};
99
use cbor4ii::core::{
@@ -504,7 +504,10 @@ pub fn decode_car<'py>(py: Python<'py>, data: &[u8]) -> PyResult<(Py<PyAny>, Bou
504504
break;
505505
}
506506

507-
let cid_result = Cid::read_bytes(&mut buf.buf);
507+
let cid_bytes_before = buf.buf;
508+
let mut cursor = Cursor::new(cid_bytes_before);
509+
510+
let cid_result = Cid::read_bytes(&mut cursor);
508511
let Ok(cid) = cid_result else {
509512
return Err(get_err(
510513
"Failed to read CID of block",
@@ -519,6 +522,10 @@ pub fn decode_car<'py>(py: Python<'py>, data: &[u8]) -> PyResult<(Py<PyAny>, Bou
519522
));
520523
}
521524

525+
let consumed = cursor.position() as usize;
526+
buf.advance(consumed);
527+
let cid_raw = &cid_bytes_before[..consumed];
528+
522529
let block_result = decode_dag_cbor_to_pyobject(py, buf, 0);
523530
let Ok(block) = block_result else {
524531
return Err(get_err(
@@ -527,8 +534,7 @@ pub fn decode_car<'py>(py: Python<'py>, data: &[u8]) -> PyResult<(Py<PyAny>, Bou
527534
));
528535
};
529536

530-
// FIXME(MarshalX): to_bytes allocates
531-
let key = PyBytes::new(py, &cid.to_bytes()).into_pyobject(py)?;
537+
let key = PyBytes::new(py, cid_raw).into_pyobject(py)?;
532538
parsed_blocks.set_item(key, block)?;
533539
}
534540

0 commit comments

Comments
 (0)