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
refactor: Optimize IsSpace function for common non-whitespace characters #29602
base: master
Are you sure you want to change the base?
refactor: Optimize IsSpace function for common non-whitespace characters #29602
Conversation
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. Code CoverageFor detailed information about the code coverage, see the test coverage report. ReviewsSee the guideline for information on the review process.
If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update. |
c15b5e2
to
210a280
Compare
Is this used in a hot path that is relevant, to warrant the changes? |
It's used in base 58 and 16 conversions while iterating over each character of the input. |
Yes, but base58 isn't used in a hot path, where optimizing it would result in a noticeable difference. (When sending an address over RPC, the remaining overhead is too large to notice a speedup of a few nanoseconds) |
It's a small improvement, indeed, but it eliminates useless work, done frequently. |
Is this really a bottleneck? A benchmark would be useful to see if this isn't something the compiler already does. ACK on the test. |
Thanks for the review @laanwj. The gain is small, but measurable:
which results in a speedup of ~2% for 130 non-whitespace hexadecimal characters:
|
210a280
to
3a47a61
Compare
The IsSpace function has been optimized for the more common case where a character is not whitespace. Previously, the function checked each whitespace character individually, which was not efficient for non-whitespace inputs as it required multiple comparisons. The updated IsSpace function now first checks if the character is less than or equal to ' ' (the space character, which has the highest ASCII value among the whitespace characters). This single condition can quickly determine that most characters (those with ASCII values greater than ' ') are not whitespace, thus short-circuiting the evaluation for the most common case. If the character is less than or equal to ' ', the function performs additional checks to see if it is either a space character or within the range of horizontal tab to carriage return ('\t' to '\r'), which are the remaining whitespace characters in the ASCII table. This change assumes that most calls to IsSpace are for non-whitespace characters, as can be inferred from the usage patterns where IsSpace is often used in loops that parse strings until a non-whitespace character is encountered.
3a47a61
to
e4e4c36
Compare
The
IsSpace
function has been optimized for the more common case where a character is not whitespace. Previously, the function checked each whitespace character individually, which was not efficient for non-whitespace inputs as it required multiple comparisons.This method is often used for parsing various inputs more flexibly, see usages.
This results in an additional ~2% performance improvement for base conversions (see related hexadecimal and base58 optimizations).
The updated
IsSpace
function now first checks if the character is less than or equal to ' ' (the space character, which has the highest ASCII value among the whitespace characters). This single condition can quickly determine that most characters (i.e. letters, numbers) are not whitespace, thus short-circuiting the evaluation for the most common case.Otherwise the function performs additional checks to see if it is either a space character or within the range of horizontal tab to carriage return ('\t' to '\r'), which are the remaining whitespace characters in the ASCII table.
This change assumes that most calls to IsSpace are for non-whitespace characters, as can be inferred from the usage patterns where
IsSpace
is often used in loops that parse strings until a non-whitespace character is encountered.To make sure the changes keep the previous functionality, I've added an exhaustive unit test for it.