atoi-rs icon indicating copy to clipboard operation
atoi-rs copied to clipboard

Optimize ASCII digit and hexdigit conversion by using subtraction

Open okaneco opened this issue 1 year ago • 4 comments

Subtract digits by their "base" digit to simplify the number of match arms when converting from ASCII digit or hexdigit to u8 Use FromPrimitive::from_u8 in ascii_to_digit after checking for valid ASCII digit range Condense ascii_to_hexdigit match arms and convert with from_u8 Add FromPrimitive trait bound to required trait impls Add tests to verify correct parsing of digits and hex digits


I saw some significant wins across the board with this on the benchmarks, hopefully it wasn't just my CPU. ascii_to_hexdigit showed the biggest improvements likely due to the simplification of match arm statements.

Unfortunately, the only way I could figure out how to do this involved adding the FromPrimitive trait bound from num-traits in several places. I think adding another trait bound to a public facing trait makes it a breaking change but I'm not sure. If there's another way to do this, I'd be glad to change it.

okaneco avatar Dec 08 '22 04:12 okaneco