-
Notifications
You must be signed in to change notification settings - Fork 57
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 performance of encoding #64
base: main
Are you sure you want to change the base?
Conversation
let data = data.as_ref(); | ||
let mut out = vec![0; data.len() * 2]; | ||
encode_to_slice(data, &mut out).unwrap(); | ||
String::from_utf8(out).unwrap() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When using from_utf8_unckecked here, it can improve performance by about 8%. (It is safe because we emit only hex characters.)
However, I didn't apply that change because I don't know the policy regarding the unsafe code in this crate.
If It is okay with using unsafe code, I'll add that change.
hex_encode time: [9.4555 us 9.4828 us 9.5106 us]
change: [-9.2447% -8.3200% -7.4582%] (p = 0.00 < 0.05)
Performance has improved.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some overlap with #66 here I think. In particular I suspect the combination of ExactSizeIterator
will allow us to collect into a String
efficiently without indirecting through Vec
.
Nice PR! But the owner of the repo seems to be inactive. |
This change makes encoding about 7x faster than the current implementation, on my machine.
Before:
After:
Benchmarks per commit
Current main branch (aa8f300)
First commit of this PR: Adjust #[inline] on encoding functions (97abf03)
Second commit of this PR: Use encode_to_slice in encode (0129182)
Third commit of this PR: Use chunks_exact_mut instead of generate_iter (19b333d)
This also adds encode_to_slice_upper, which is needed to improve the performance of encode_upper. (fixes #45).