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

slightly more efficient Reader functions

This commit is contained in:
mat 2025-01-19 08:35:05 +00:00
parent 47131f4d9e
commit 4806d0413b
2 changed files with 18 additions and 5 deletions

View file

@ -427,7 +427,7 @@ pub(crate) fn read_tag_in_compound<'a>(
let tag_name_ptr = data.cur;
debug_assert_eq!(tag_name_ptr as u64 >> 56, 0);
// read the string in a more efficient way than just calling read_string
// read the tag name in a more efficient way than just calling read_string
let mut cur_addr = tag_name_ptr as usize;
let end_addr = data.end_addr();
@ -445,7 +445,7 @@ pub(crate) fn read_tag_in_compound<'a>(
}
data.cur = cur_addr as *const u8;
// finished reading the string
// finished reading the tag name
tapes.main.push(TapeElement::new(tag_name_ptr as u64));

View file

@ -47,8 +47,18 @@ impl<'a> Reader<'a> {
}
pub fn read_type<T: Copy>(&mut self) -> Result<T, UnexpectedEofError> {
self.ensure_can_read(mem::size_of::<T>())?;
Ok(unsafe { self.read_type_unchecked() })
let addr = self.cur;
// it's faster to add and then check for eof. it does leave our pointer in a bad
// state if it fails, but we aren't going to dereference it in that case
// anyways.
self.cur = unsafe { self.cur.add(mem::size_of::<T>()) };
if self.cur > self.end {
return Err(UnexpectedEofError);
}
let value = unsafe { addr.cast::<T>().read_unaligned() };
Ok(value)
}
#[inline]
@ -99,8 +109,11 @@ impl<'a> Reader<'a> {
#[inline]
pub fn skip(&mut self, size: usize) -> Result<(), UnexpectedEofError> {
self.ensure_can_read(size)?;
self.cur = unsafe { self.cur.add(size) };
if self.cur > self.end {
return Err(UnexpectedEofError);
}
Ok(())
}