Skip to content
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

Avoid 64-bit arithmetic in LEB encoding #345

Merged
merged 3 commits into from
Jan 18, 2021
Merged

Avoid 64-bit arithmetic in LEB encoding #345

merged 3 commits into from
Jan 18, 2021

Conversation

jonas-schievink
Copy link
Contributor

Fixes #75

This uses usize instead of u64 in leb64, which is now possible since timestamps aren't hard-coded to LEB64-encoded-u64 anymore.

Before:

   text	   data	    bss	    dec	    hex	filename
  32084	      0	      8	  32092	   7d5c	../../target/thumbv7m-none-eabi/release/log

After:

   text	   data	    bss	    dec	    hex	filename
  31876	      0	      8	  31884	   7c8c	../../target/thumbv7m-none-eabi/release/log

So, ~200 bytes of code saved.

@jonas-schievink jonas-schievink added the pr waits on: review Pull Request is waiting on review label Jan 15, 2021
Copy link
Contributor

@Lotterleben Lotterleben left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

heh, nice! nitpicks are non-blocking.

let mut low = x as u32;
let mut high = (x >> 32) as u32;
let mut high = ((x >> 16) >> 16) as u32;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why the double-shift instead of going for >>32 right away?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, right, should've added a comment for that. Pushed a commit that explains this.

@@ -58,8 +61,9 @@ pub(crate) fn leb64(x: u64, buf: &mut [u8; 10]) -> usize {
}
}

pub fn zigzag_encode(v: i64) -> u64 {
((v << 1) ^ (v >> 63)) as u64
pub fn zigzag_encode(v: isize) -> usize {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: a 1 sentence doc on what's happening here for those could be helpful who are not familiar with zigzag encoding (not blocking though)

src/leb.rs Outdated
let i = leb64((1 << 32) - 1, &mut buf);
assert_eq!(buf[..i], [0xff, 0xff, 0xff, 0xff, 0xf]);
buf.iter_mut().for_each(|b| *b = 0x55);
if cfg!(target_pointer_width = "64") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

another nitpick: it's not very intuitive to figure out what's being tested here... Personally I'd put each check in a dedicated test case anyway (this way they're independent and their purpose is documented on the fly) but in this constellation comments would be helpful and time-saving

@jonas-schievink
Copy link
Contributor Author

bors r=Lotterleben

@bors
Copy link
Contributor

bors bot commented Jan 18, 2021

Build succeeded:

@bors bors bot merged commit 072cb2e into main Jan 18, 2021
@bors bors bot deleted the leb32 branch January 18, 2021 13:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr waits on: review Pull Request is waiting on review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Investigate efficiency of using leb64 for 32-bit values
2 participants