-
Notifications
You must be signed in to change notification settings - Fork 14k
Refactor is_case_difference function
#115258
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 is_case_difference function
#115258
Conversation
|
(rustbot has picked a reviewer for you, use r? to override) |
|
The job Click to see the possible cause of the failure (guessed by this bot) |
WaffleLapkin
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this function needs optimization, but your code is generally nicer and more documented here, so let's go with that.
I've left a few nitpicks, don't mind them much, it's always easy to find things to nitpick about on small PRs like there 😅
Thank you for the pull request!
| pub fn is_case_difference(sm: &SourceMap, suggested: &str, sp: Span) -> bool { | ||
| // FIXME: this should probably be extended to also account for `FO0` → `FOO` and unicode. | ||
| // Retrieve code | ||
| let found = match sm.span_to_snippet(sp) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could you turn this into a let-else?
| && found != suggested | ||
|
|
||
| // ASCII confusable characters when capitalization. | ||
| const ASCII_CONFUSABLES: [char; 12] = ['c', 'f', 'i', 'k', 'o', 's', 'u', 'v', 'w', 'x', 'y', 'z']; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using a const does not make a difference, actually. Consts are copied on access, so this is actually worse, if anything (as we now potentially spill two copies on the attach, instead of one).
Not that it actually matters, given than this is the error path, but still. You should probably change the type to &[char; 12] if you want to make it const (do that only the reference is copied, not the array).
| // ASCII confusable characters when capitalization. | ||
| const ASCII_CONFUSABLES: [char; 12] = ['c', 'f', 'i', 'k', 'o', 's', 'u', 'v', 'w', 'x', 'y', 'z']; | ||
|
|
||
| // # CHECK 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, do you think those titles are useful? To me they seem somewhat pointless, they don't really add information or make it easier to scan the code, I think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the original code, the comment sounds like // All the chars that differ in capitalization are confusable (above):
Should I make it more descriptive?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure what you mean, I was only talking about the # CHECK titles, the comments themself look fine.
| // 'zip' function is used to iterate over two sequences in parallel | ||
| found.chars() | ||
| .zip(suggested.chars()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is no need to explain zip, it's a well known function :)
Additionally I'd suggest using iter::zip, in my opinion it's nicer, as it highlights the symmetry.
| // 'zip' function is used to iterate over two sequences in parallel | |
| found.chars() | |
| .zip(suggested.chars()) | |
| iter::zip(found.chars(), suggested.chars()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the remainder :)
Actually, I'm still new to Rust, and there's a lot I'm still catching on to.
About the iter::zip, I completely agree with you. I'll modify it later.
| // - Check character `f` is present in the `ASCII_CONFUSABLES` array, OR | ||
| // - Check character `s` is present in the `ASCII_CONFUSABLES` array | ||
| // | ||
| // This line equivalent to the: .filter(|(f, s)| f != s).all(|(f, s)| (ascii_confusables.contains(&f) || ascii_confusables.contains(&s))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is redundant, the filter representation is more confusing if anything
|
@rustbot author |
|
@rustbot review |
|
@allaboutevemirolive You have not materially responded to the reviewer's comments. Please do not return this to the reviewer's queue without making some meaningful change or at least requesting distinctive feedback. @rustbot author |
Motivation
Previous code was refactored to remove unnecessary memory allocation and to include comments for clarity.
Some of the current code's optimizations include:
eq_ignore_ascii_case()instead ofto_lowercase()to avoid memory allocations and performance overhead.all()and logical conditions, reducing the overall number of operations.ascii_confusablesby usingconst.constvalues are evaluated and resolved at compile time, which means they don't result in runtime allocations.Testing
This improvement was tested with command
./x test --stage 2 tests/uion Debian.