Skip to content
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

Fix bad case-insensitive ASCII equality check #45928

Merged
merged 3 commits into from
Dec 15, 2020

Conversation

GrabYourPitchforks
Copy link
Member

Fixes #45889. There was an edge case where the characters '`' and '@' would inadvertently compare as equal under OrdinalIgnoreCase. This PR also introduces a unit test to run through all combinations of ASCII chars to look for other edge cases, and it looks like this was the only edge case.

My AMD-based dev box is reporting a tiny perf improvement: maybe 1 cycle or so. I'm not able to measure this with much accuracy, so I'd depend on the perf lab to let us know if something has gone horribly wrong. Since we're reducing the overall code size I wouldn't expect throughput to regress.

This will need to be backported to 5.x and 3.x.

@GrabYourPitchforks
Copy link
Member Author

FYI to reviewers - the diff is a few dozen lines, but that's mostly because I rewrote the comments in the code to clarify exactly what the bit twiddling accomplishes. The fix itself is only a half dozen lines or so, duplicated across 4 methods.

@GrabYourPitchforks
Copy link
Member Author

Latest iteration only changes the unit test. Easiest way to review the single-commit delta is to ignore whitespace changes: 7380c45?diff=unified&w=1

@EgorBo
Copy link
Member

EgorBo commented Dec 11, 2020

@GrabYourPitchforks a general question: have you tried to vectorize Ordinal.EqualsIgnoreCase ? I see it's currently "vectorized" via GP registers or it doesn't make sense?

@EgorBo
Copy link
Member

EgorBo commented Dec 11, 2020

NIT: would be nice to address Ben's feedback dotnet/coreclr#20734 (comment)
(UInt32OrdinalIgnoreCaseAscii -> UInt32OrdinalIgnoreCaseAsciiEqauls) but I guess it's better to keep as is for less painful backport to 3.1 and 5.0.

@GrabYourPitchforks
Copy link
Member Author

@EgorBo We measured it a while back. Vectorization really only provides a benefit when we're talking about long-ish inputs (over a few dozen chars). Since typical inputs to this method are on the order of ~10 chars max, using non-vectorized code incurred less overhead than using vectorized code and had better perf characteristics.

@GrabYourPitchforks
Copy link
Member Author

  • @adamsitnik, because you like weird bit twiddling tricks :)

@GrabYourPitchforks GrabYourPitchforks merged commit 1bfdab4 into dotnet:master Dec 15, 2020
@GrabYourPitchforks
Copy link
Member Author

/backport to release/5.0

@GrabYourPitchforks GrabYourPitchforks deleted the ascii_cmp branch December 15, 2020 01:02
@github-actions
Copy link
Contributor

Started backporting to release/5.0: https://github.com/dotnet/runtime/actions/runs/422106031

@github-actions
Copy link
Contributor

@GrabYourPitchforks backporting to release/5.0 failed, the patch most likely resulted in conflicts:

$ git am --3way --ignore-whitespace --keep-non-patch changes.patch

Applying: Fix bad case-insensitive ASCII equality check
error: sha1 information is lacking or useless (src/libraries/System.Runtime/tests/System/StringTests.cs).
error: could not build fake ancestor
hint: Use 'git am --show-current-patch=diff' to see the failed patch
Patch failed at 0001 Fix bad case-insensitive ASCII equality check
When you have resolved this problem, run "git am --continue".
If you prefer to skip this patch, run "git am --skip" instead.
To restore the original branch and stop patching, run "git am --abort".
Error: The process '/usr/bin/git' failed with exit code 128

Please backport manually!

@GrabYourPitchforks
Copy link
Member Author

Ah, yeah, looks like the recent source tree rename may have killed the poor backport bot.

Copy link
Member

@adamsitnik adamsitnik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@GrabYourPitchforks I must admit you have a very unusual way of spending your winter holidays. Is Cyberpunk that buggy? ;)

Jokes aside, big thanks for not just fixing the bug but also providing great test coverage and ensuring that perf is not worse!

LGTM!

@ghost ghost locked as resolved and limited conversation to collaborators Jan 14, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

string Equals StringComparison.OrdinalIgnoreCase bug with @ and ` characters
5 participants