-
-
Notifications
You must be signed in to change notification settings - Fork 9
decompress.rs: faster input reading
#85
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
eb01ae8
12c1e76
df1b941
97f5d92
8efb89a
1ca350f
0a58925
c78469e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -244,6 +244,73 @@ mod stream { | |
| unsafe { Allocator::from_bz_stream(self) } | ||
| } | ||
|
|
||
| /// Read up to 7 bytes into the bit buffer. | ||
| /// | ||
| /// The caller is responsible for updating `self.total_in`! | ||
| #[must_use] | ||
| #[inline(always)] | ||
| pub(crate) fn pull_u64( | ||
| &mut self, | ||
| mut bit_buffer: u64, | ||
| bits_used: i32, | ||
| ) -> Option<(u64, i32)> { | ||
| // we should only ask for more input if there are at least 8 free bits | ||
| debug_assert!(bits_used <= 56); | ||
|
|
||
| if self.avail_in < 8 { | ||
| return None; | ||
| } | ||
|
|
||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe add a debug assertion that no more than 7 bytes are requested? And no more than 1 byte in the case of pull_u8.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. well I added an assert that we can at least consume 1 additional byte, in other words that we don't ask for more input if we don't have space in the bit buffer. The check you comment on is a bounds check, so we just skip this function if there are not 8 additional bytes in the input, there is nothing to assert there right? |
||
| // of course this uses big endian values | ||
| let read = u64::from_be_bytes(unsafe { self.next_in.cast::<[u8; 8]>().read() }); | ||
|
|
||
| // because of the endianness, we can only shift in whole bytes. | ||
| // this calculates the number of available bits, rounded down to the nearest multiple | ||
| // of 8. | ||
| let increment_bits = (63 - bits_used) & !7; | ||
|
|
||
| // shift existing bits to the end, and or new bits in | ||
| bit_buffer = (bit_buffer << increment_bits) | (read >> (64 - increment_bits)); | ||
|
|
||
| // we read 8 bytes above, but can only process `increment_bytes` worth of bits | ||
| let increment_bytes = increment_bits / 8; | ||
| self.next_in = unsafe { (self.next_in).add(increment_bytes as usize) }; | ||
| self.avail_in -= increment_bytes as u32; | ||
|
|
||
| // skips updating `self.total_in`: the caller is responsible for keeping it updated | ||
folkertdev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| Some((bit_buffer, bits_used + increment_bits)) | ||
| } | ||
|
|
||
| /// Read exactly 1 byte into the buffer | ||
| /// | ||
| /// The caller is responsible for updating `self.total_in`! | ||
| #[must_use] | ||
| #[inline(always)] | ||
| pub(crate) fn pull_u8( | ||
| &mut self, | ||
| mut bit_buffer: u64, | ||
| bits_used: i32, | ||
| ) -> Option<(u64, i32)> { | ||
| // we should only ask for more input if there are at least 8 free bits | ||
| debug_assert!(bits_used <= 56); | ||
|
|
||
| if self.avail_in == 0 || bits_used > 56 { | ||
| return None; | ||
| } | ||
|
|
||
| let read = unsafe { *(self.next_in as *mut u8) }; | ||
| bit_buffer <<= 8; | ||
| bit_buffer |= u64::from(read); | ||
|
|
||
| self.next_in = unsafe { (self.next_in).offset(1) }; | ||
| self.avail_in -= 1; | ||
|
|
||
| // skips updating `self.total_in`: the caller is responsible for keeping it updated | ||
|
|
||
| Some((bit_buffer, bits_used + 8)) | ||
| } | ||
|
|
||
| #[must_use] | ||
| pub(crate) fn read_byte(&mut self) -> Option<u8> { | ||
| if self.avail_in == 0 { | ||
|
|
@@ -533,7 +600,7 @@ pub(crate) struct DState { | |
| pub k0: u8, | ||
| pub rNToGo: i32, | ||
| pub rTPos: i32, | ||
| pub bsBuff: u32, | ||
| pub bsBuff: u64, | ||
| pub bsLive: i32, | ||
| pub smallDecompress: DecompressMode, | ||
| pub currBlockNo: i32, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.