6

Add fast path to `is_descendant_of` by camsteffen · Pull Request #91043 · rust-l...

 2 years ago
source link: https://github.com/rust-lang/rust/pull/91043
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

New issue

Add fast path to is_descendant_of #91043

Conversation

Copy link

Contributor

camsteffen commented 24 days ago

No description provided.

Copy link

Collaborator

rust-highfive commented 24 days ago

r? @jackh726

(rust-highfive has picked a reviewer for you, use r? to override)

Copy link

Contributor

cjgillot commented 24 days ago

edited

I think we have an invariant that either expn_id == ExpnId::root() or expn_id.krate == self.expn_data(expn_id).parent.krate? I'm not sure that it always hold and that we enforce it. This would allow another fast path if expn_id.krate != ancestor.krate.

Copy link

Contributor

Author

camsteffen commented 24 days ago

I don't think that is true because a macro can have a nested macro call to an external crate?

Copy link

Contributor

cjgillot commented 24 days ago

Macros are expanded from the outer in. ExpnData::parent tracks where the code ends up once expanded, def_site tracks where it was written (possibly in an upstream crate) and call_site where the macro call was written (possibly in a downstream crate).
Hence, even if the nested macro call is to a macro defined in an external crate, this nested macro call is expanded into the crate its parent was expanded into.
Actually, we check that expn_data.parent.krate == LOCAL_CRATE when we assign a LocalExpnId.

Copy link

Contributor

Author

camsteffen commented 24 days ago

Okay I think I get it, tell me if this sounds right. A stack of nested macro calls are all triggered by one outermost macro call in a particular crate. All the expansions in the stack belong to that crate.

Should I modify HygieneData::is_descendant_of too?

Copy link

Contributor

cjgillot commented 24 days ago

Indeed, we should probably make every caller benefit from the optimization.

camsteffen

changed the title Add fast path to ExpnId::is_descendant

Add fast path to is_descendant_of

24 days ago

Copy link

Contributor

jackh726 commented 23 days ago

Whoops.

@petrochenkov let me know if your self-assignment was a mistake and I can review

Copy link

Contributor

petrochenkov commented 22 days ago

expn_id.krate == self.expn_data(expn_id).parent.krate?

That's not necessarily true if the parent is a root because we don't yet preserve krate IDs for root expansions, but otherwise this should be true.
I'd add an assert for this to the ExpnData-creating function, if possible.

Copy link

Contributor

petrochenkov commented 22 days ago

@jackh726
I usually add myself as a second reviewer if I want to be aware of the changes, feel free to review.
(Please don't forget to run perf.)

Copy link

Contributor

Author

camsteffen commented 22 days ago

Copy link

Collaborator

rust-timer commented 22 days ago

Awaiting bors try build completion.

@rustbot label: +S-waiting-on-perf

Copy link

Contributor

bors commented 22 days ago

hourglass Trying commit b708abf with merge 8cf5997...

Copy link

Contributor

bors commented 22 days ago

sunny Try build successful - checks-actions
Build commit: 8cf5997 (8cf5997459fd458031256a1986c32a922e0d6c0a)

Copy link

Collaborator

rust-timer commented 22 days ago

Copy link

Collaborator

rust-timer commented 22 days ago

Finished benchmarking commit (8cf5997): comparison url.

Summary: This change led to small relevant mixed results shrug in compiler performance.

  • Small improvement in instruction counts (up to -1.1% on incr-unchanged builds of wg-grammar)
  • Small regression in instruction counts (up to 0.5% on incr-patched: println builds of style-servo)

If you disagree with this performance assessment, please file an issue in rust-lang/rustc-perf.

Benchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. While you can manually mark this PR as fit for rollup, we strongly recommend not doing so since this PR led to changes in compiler perf.

Next Steps: If you can justify the regressions found in this try perf run, please indicate this with @rustbot label: +perf-regression-triaged along with sufficient written justification. If you cannot justify the regressions please fix the regressions and do another perf run. If the next run shows neutral or positive results, the label will be automatically removed.

@bors rollup=never
@rustbot label: +S-waiting-on-review -S-waiting-on-perf +perf-regression

Copy link

Contributor

petrochenkov commented 18 days ago

Looks like a minor improvement.

Copy link

Contributor

petrochenkov commented 18 days ago

Could you add

  • an assert checking parent crate equality to fn register_expn_id
  • comments to both versions of fn is_descendant_of telling that the start code is a fast path that allows to avoid taking a lock / going in to the loop below.

Copy link

Contributor

Author

camsteffen commented 15 days ago

I also changed a while to loop since I think it's more readable.

@rustbot ready

Copy link

Contributor

@jackh726 jackh726 left a comment

I don't think I know this code enough to actually r+ this, so I'll defer to @petrochenkov.

With that being said, the changes here seem okay to me.

@@ -1223,6 +1240,7 @@ pub fn register_expn_id(

data: ExpnData,

hash: ExpnHash,

) -> ExpnId {

debug_assert!(data.parent == ExpnId::root() || krate == data.parent.krate);

Copy link

Contributor

@jackh726 jackh726 15 days ago

Should this be a regular assert?

Seems fine as is, the assert will be removed anyway when root expansions start keeping their crate IDs.

Copy link

Contributor

petrochenkov commented 14 days ago

@bors r+

Copy link

Contributor

bors commented 14 days ago

pushpin Commit ac8d514 has been approved by petrochenkov

Copy link

Contributor

bors commented 14 days ago

hourglass Testing commit ac8d514 with merge 44723c5...

Copy link

Contributor

bors commented 14 days ago

sunny Test successful - checks-actions
Approved by: petrochenkov
Pushing 44723c5 to master...

bors

merged commit 44723c5 into

rust-lang:master 14 days ago

11 checks passed

rustbot

added this to the 1.59.0 milestone

14 days ago

Copy link

Collaborator

rust-timer commented 14 days ago

Finished benchmarking commit (44723c5): comparison url.

Summary: This benchmark run did not return any relevant changes.

If you disagree with this performance assessment, please file an issue in rust-lang/rustc-perf.

@rustbot label: -perf-regression

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Milestone

1.59.0

Linked issues

Successfully merging this pull request may close these issues.

None yet

9 participants

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK