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

improve encode_varint performance by bounding its loop #940

Merged
merged 1 commit into from
Mar 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ pub fn encode_varint<B>(mut value: u64, buf: &mut B)
where
B: BufMut,
{
loop {
// Varints are never more than 10 bytes
for _ in 0..10 {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you explain the constant 10? Maybe a static assert, formula or documentation?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

is it not sufficiently covered by the comment already on this function? i can add it, just wondering

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah, you are probably right. I saw that comment and started looking into why 10 bytes is correct. I took me a moment to understand.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

protobuf's varints are fairly thoroughly documented to be 10 bytes maximum, yes. i mean they could be 9 but they decided not to do that. i believe every minimal length protobuf varint that is 10 bytes always has 0x01 as its last byte value, which is rather a waste considering how many situations cause them to become maximum length this way. the burden of legacy i guess

if value < 0x80 {
buf.put_u8(value as u8);
break;
Expand Down