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

chore: a few small improvements to code quality #9279

Merged
merged 2 commits into from
Aug 2, 2022

Conversation

tabokie
Copy link
Contributor

@tabokie tabokie commented Aug 2, 2022

Some improvements:

  • Simplify implementation of is_unit_type
  • Use slice matching to destruct Call or MethodCall whenever possible

changelog: none

r? @flip1995

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Aug 2, 2022
Copy link
Member

@flip1995 flip1995 left a comment

Choose a reason for hiding this comment

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

Thanks for addressing for going above and beyond with improving the code! The destructuring of arguments is a really nice change.

The symbol comparison changes however are not a code improvement. as_str on symbols is not a no-op, but rather an indexing of a vector requiring to acquire a lock first. See here. Interning an already interned string is pretty much the same, just that it does a HashMap lookup. But then the string comparison is more expensive than the symbol comparison, because the symbol comparison is just an integer comparison. Because those check_* functions are ran on every expr/stmt/item/..., it is in the end cheaper to intern the string once and then do a symbol comparison rather than a string comparison.

(The as_str function additionally goes through a with_session_globals, but I don't know how expensive that is).

TL;DR: Please revert the symbol changes, the rest is great.

// only work if the method name is `map_err` and there are only 2 arguments (e.g. x.map_err(|_|[1]
// Enum::Variant[2]))
if method.ident.as_str() == "map_err" && args.len() == 2 {
if method.ident.as_str() == "map_err" {
Copy link
Member

Choose a reason for hiding this comment

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

This should then also be changed:

Suggested change
if method.ident.as_str() == "map_err" {
if method.ident == sym!(map_err) {

@tabokie
Copy link
Contributor Author

tabokie commented Aug 2, 2022

@flip1995 Okay. Having looked closer, it's basically lock+strcmp(as_str) versus lock+strhash+intcmp(intern). I would argue that strcmp might be faster than strhash because it can exit early. But I agree whatever the perf difference is, it doesn't matter that much to be fixed this late.

@flip1995
Copy link
Member

flip1995 commented Aug 2, 2022

Another non-perf related advantage is, that if a symbol should get pre-inserted by rustc, we have a lint that will tell us to remove the sym! macro call and just compare to sym::. (I don't think we get this with as_str()).

Yeah, your right, it might have equal performance. It probably doesn't make much of a difference. But I'd rather keep the sym! approach.

tabokie added 2 commits August 2, 2022 17:56
Signed-off-by: tabokie <xy.tao@outlook.com>
Signed-off-by: tabokie <xy.tao@outlook.com>
@flip1995
Copy link
Member

flip1995 commented Aug 2, 2022

Perfect, thank you!

@bors r+

@bors
Copy link
Contributor

bors commented Aug 2, 2022

📌 Commit ac7a91e has been approved by flip1995

It is now in the queue for this repository.

@bors
Copy link
Contributor

bors commented Aug 2, 2022

⌛ Testing commit ac7a91e with merge 4914833...

@bors
Copy link
Contributor

bors commented Aug 2, 2022

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: flip1995
Pushing 4914833 to master...

@bors bors merged commit 4914833 into rust-lang:master Aug 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants