Hacker News new | ask | show | jobs
by brabel 1708 days ago
> it’s using Unicode-aware string stuff

Rust uses UTF-8 internally for Strings, so it's very efficient to parse a file into a String, then using slices to go through it... this is probably the best you can get as parsing ASCII input as UTF-8 is very efficient (the 0-bit is always zero in ASCII, the unicode decoder only needs to check that's the case for every byte, so it's not some kind of complicated computation it's doing to decode)...

If you use bytes for everything, you will make the whole code much harder to follow and it still won't run faster.

Check for yourself: https://github.com/renatoathaydes/prechelt-phone-number-enco...

1 comments

The code will be somewhat faster (I don’t care to predict how much) from removing the variable-width character encoding in favour of bytewise access. Yes, pure ASCII stuff has some fast paths in string access, but they’re still decidedly slower than the fixed-width encoding that is [u8]. Using strings also gives the incorrect impression that it can cope with non-ASCII.

The code will be easier to follow if you use bytes throughout, because currently it’s a mixture of bytewise and charwise operations, so that you need to think a little about whether you’re dealing with char or u8 in each place (and half of them are even mislabelled); and there are suitable alternative ASCII methods for every place that uses charwise methods (e.g. char.is_digit(10) → u8.is_ascii_digit()) so that no extra burden is added. In the end the only place slightly complicated by it is printing the solutions, but more code will have been decomplicated—hotter path code, too—so that it’s easily worth it.

I don’t know where the code you’re citing came from, it’s newer than what’s on the master branch but in its changes includes some pretty bad stuff like DIGITS, using &str for something that is always a single-character ASCII digit, accessed by already having had the digit as a u8 and turning it back into a string prematurely. Admittedly the optimiser is going to fix a fair bit of that badness, but not all.

There are a few pull requests that claim to make the code faster, but you can run the benchmarks and see none of them actually did. Why not try to improve the code I posted above and make the apparently small changes you want to make and check if it's faster or not.

I've tried a few myself and I am almost sure your hints will not work.

> some pretty bad stuff like DIGITS, using &str for something that is always a single-character ASCII digit, accessed by already having had the digit as a u8 and turning it back into a string prematurely.

The end result needs to be printing the strings so I don't see how you can work around that. Can you at least post your code doing that in a way that won't totally destroy the performance gains you may have obtained elsewhere?