-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Remove allocation in impl Display for path::Display #38879 #39023
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @aturon (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
I’d much rather this do 1 allocation for a cold case (invalid unicode) rather than have write_char called for every single codepoint every single time. |
Do you mean performance wise? I think it would be interesting to see some benchmarks here. |
Ok, so I hacked together a quick bench of the two options. On my system, with a path length of Now, allocation time is of course system dependent so the exact numbers here aren't written in stone, but seeing what kind of numbers we are talking about here, I'm not sure this is worth pursuing. I guess the results are expected, so unless @jethrogb have some input here I'll close this PR. |
You can test whether |
That is certainly possible, but seeing how small the gains seems to be, and that checking for illegal utf8 before deciding on the path will slow down the function even further, I'm struggling to imagine a scenario where we'll end up on the plus side here. |
If it's valid, you can just push the resulting |
The However, that is only economical if the nitty gritty part is already implemented, i.e., if there's existing code that validates the bytes and tells you where (if at all) the first non-UTF-8 byte is. I don't know if we have something like that. |
@rkruppe that's pretty much the |
Great! Can the relevant parts be accessed from the outside, or can the code easily be refactored to allow that? |
IMHO this should be a double-check for invalid unicode: check if the slice is valid unicode and If so, write_str, otherwise, do the write_char loop. This way, you get the proper optimisation on the average case (valid UTF-8), and the allocation in the uncommon case is replaced by two validity checks. Still better than before, but doesn't incur a performance penalty in the average case. Perhaps in the long-term if we want to optimise this better on the Side note: It'd be nice if we had a no-allocate version of |
I'm going to close out this PR; as it currently stands, it's a net loss to performance and introduces a bit more implementation complexity. We could consider exposing the Thanks for the PR! |
Fixes #38879
I wasn't too sure about testing this, but I threw in a test for checking that the non-unicode sequences were converted to
'\u{fffd}'