1
0
Fork 0
mirror of https://github.com/azalea-rs/simdnbt.git synced 2025-08-02 07:26:04 +00:00

minor optimization by using push_unchecked for the first tape push

This commit is contained in:
mat 2025-01-18 18:30:59 +00:00
parent d4f50f2420
commit 576a1cc1bf
2 changed files with 27 additions and 12 deletions

View file

@ -53,12 +53,17 @@ pub fn read<'a>(data: &mut Cursor<&'a [u8]>) -> Result<Nbt<'a>, 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<BaseNbtCompound<
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)?;

View file

@ -53,6 +53,11 @@ impl<A: Allocator> MainTape<A> {
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) };
}