From 576a1cc1bfa539e23343028700c812492bc1508a Mon Sep 17 00:00:00 2001 From: mat Date: Sat, 18 Jan 2025 18:30:59 +0000 Subject: [PATCH] minor optimization by using push_unchecked for the first tape push --- simdnbt/src/borrow/mod.rs | 34 ++++++++++++++++++++++------------ simdnbt/src/borrow/tape.rs | 5 +++++ 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/simdnbt/src/borrow/mod.rs b/simdnbt/src/borrow/mod.rs index a49d1bc..950b1c2 100644 --- a/simdnbt/src/borrow/mod.rs +++ b/simdnbt/src/borrow/mod.rs @@ -53,12 +53,17 @@ pub fn read<'a>(data: &mut Cursor<&'a [u8]>) -> Result, Error> { stack.push(ParsingStackElement::Compound { index_of_compound_element: 0, })?; - tapes.main.push(TapeElement::new_with_approx_len_and_offset( - TapeTagKind::Compound, - // these get overwritten later - 0, - 0, - )); + unsafe { + // SAFETY: we just created the MainTape so there's definitely space for an item + tapes + .main + .push_unchecked(TapeElement::new_with_approx_len_and_offset( + TapeTagKind::Compound, + // these get overwritten later + 0, + 0, + )) + }; read_with_stack(&mut data, &mut tapes, &mut stack)?; @@ -91,12 +96,17 @@ pub fn read_compound<'a>(data: &mut Cursor<&'a [u8]>) -> Result MainTape { self.end = unsafe { self.cur.add(extending_by) }; } + unsafe { self.push_unchecked(element) }; + } + + #[inline] + pub unsafe fn push_unchecked(&mut self, element: TapeElement) { unsafe { self.cur.write(element) }; self.cur = unsafe { self.cur.add(1) }; }