-
Notifications
You must be signed in to change notification settings - Fork 4.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
Streamline bool.TryParse/Format #64782
Conversation
stephentoub
commented
Feb 4, 2022
•
edited
Loading
edited
Method | Toolchain | Mean | Error | StdDev | Ratio |
---|---|---|---|---|---|
ParseTrue | \main\corerun.exe | 7.311 ns | 0.0171 ns | 0.0142 ns | 1.00 |
ParseTrue | \pr\corerun.exe | 1.149 ns | 0.0243 ns | 0.0203 ns | 0.16 |
ParseFalse | \main\corerun.exe | 8.749 ns | 0.0569 ns | 0.0504 ns | 1.00 |
ParseFalse | \pr\corerun.exe | 2.309 ns | 0.0572 ns | 0.0477 ns | 0.26 |
ParseSomethingElse | \main\corerun.exe | 17.638 ns | 0.1478 ns | 0.1382 ns | 1.00 |
ParseSomethingElse | \pr\corerun.exe | 8.219 ns | 0.0511 ns | 0.0453 ns | 0.47 |
FormatTrue | \main\corerun.exe | 3.117 ns | 0.0245 ns | 0.0204 ns | 1.00 |
FormatTrue | \pr\corerun.exe | 2.000 ns | 0.0287 ns | 0.0269 ns | 0.64 |
FormatFalse | \main\corerun.exe | 2.101 ns | 0.0363 ns | 0.0322 ns | 1.00 |
FormatFalse | \pr\corerun.exe | 2.065 ns | 0.0416 ns | 0.0389 ns | 0.98 |
I couldn't figure out the best area label to add to this PR. If you have write-permissions please help me learn by adding exactly one area label. |
Tagging subscribers to this area: @dotnet/area-system-runtime Issue Details private char[] _true = new char[] { 'T', 'r', 'u', 'e' };
private char[] _false = new char[] { 'F', 'a', 'l', 's', 'e' };
private char[] _somethingElse = new char[] { 'O', 't', 'h', 'e', 'r' };
[Benchmark] public bool ParseTrue() => bool.TryParse(_true, out _);
[Benchmark] public bool ParseFalse() => bool.TryParse(_false, out _);
[Benchmark] public bool ParseSomethingElse() => bool.TryParse(_somethingElse, out _);
[Benchmark] public bool FormatTrue() => true.TryFormat(_true, out _);
[Benchmark] public bool FormatFalse() => false.TryFormat(_false, out _);
|
45ffc11
to
c7a32ea
Compare
Do you think that TrimWhiteSpaceAndNull can be improved also? It's on the "not a boolean" path. |
c7a32ea
to
1fd41cf
Compare
I don't see anything about the implementation that can be meaningfully improved without restoring to unsafe code, at least not until the JIT recognizes some sort of backward iteration pattern for bounds-check elimination. We can, however, push that whole trimming path off into a separate function, making it a bit cheaper to invoke the main parsing routine, along with being able to add a few more checks to more quickly weed out bad inputs.
|
Assuming that once we get to trimming the result will usually be a failure, I wonder whether IndexOfAny(char[] { 'e', 'E' } might allow bailing out quickly if the input is large. |
Have you seen any real-world cases of large inputs filled with whitespace and nulls being passed to bool.{Try}Parse? |
This commit introduced serious regressions on s390x, about 40 library test cases now fail. The problem seems to be that this (and similar lines):
is incorrect on big-endian systems. The destination memory will be set up as the following bytes:
while on a big-endian system, the string should actually be:
I'm currently testing a fix. |
This is now #65078 |
Improvements: dotnet/perf-autofiling-issues#3529 |
Always nice when what you saw locally is mirrored by the lab subsequently :) |