KokaKiwi / rust-hex

A basic crate to encode values to hexadecimal representation. Originally extracted from rustc-serialize.
https://crates.io/crates/hex
Apache License 2.0
201 stars 55 forks source link

Increase Decoder Performance With Minimal Code Change #82

Open ultrabear opened 1 year ago

ultrabear commented 1 year ago

After evaluating what I believe this crates goals are (being simple) I have come up with (along with the help of browsing other hex crates performance boosting methods) a minimal code change that doesn't sacrifice a lot of readability, or dip into unsafe, while still giving a noticeable speedup to the decoder.

Its possible that even simpler changes could speed up the crate, but this is the least intrusive method I have found that still produces a real performance gain (over the past few days of looking into this).

This code passes all tests, fmt, and clippy (stable rust 1.72).

Relevant benchmarks (using benches/hex.rs):

Performed on a Ryzen 2600

Before change
hex_decode              time:   [139.11 µs 139.97 µs 140.95 µs]                       

After change
hex_decode              time:   [107.11 µs 108.24 µs 109.44 µs]                       

This crate is a candidate to include #![forbid(unsafe_code)], and I recommend doing so, but that is outside of the scope of this PR.