Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

gather body owners #98203

Merged
merged 7 commits into from
Jul 15, 2022

Conversation

kckeiks
Copy link
Contributor

@kckeiks kckeiks commented Jun 17, 2022

Issue #96341

@rustbot rustbot added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Jun 17, 2022
@rust-highfive
Copy link
Contributor

r? @nagisa

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

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 17, 2022
@kckeiks
Copy link
Contributor Author

kckeiks commented Jun 17, 2022

r? @cjgillot

@rust-highfive rust-highfive assigned cjgillot and unassigned nagisa Jun 17, 2022
@rust-log-analyzer

This comment has been minimized.

@@ -1337,6 +1368,10 @@ pub(crate) fn hir_crate_items(tcx: TyCtxt<'_>, _: ()) -> ModuleItems {
}

fn visit_item(&mut self, item: &'hir Item<'hir>) {
if self.tcx.hir().is_body_owner(item.def_id) {
Copy link
Contributor

Choose a reason for hiding this comment

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

What about this?

Suggested change
if self.tcx.hir().is_body_owner(item.def_id) {
if associated_body(Node::Item(item)).is_some() {

@kckeiks kckeiks force-pushed the gather-body-owners-in-hir-item-queries branch 2 times, most recently from 0366bfc to 327a469 Compare June 17, 2022 21:58
@rust-log-analyzer

This comment has been minimized.

@kckeiks kckeiks force-pushed the gather-body-owners-in-hir-item-queries branch 2 times, most recently from 8343258 to b535db3 Compare June 17, 2022 23:02
@rust-log-analyzer

This comment has been minimized.

@kckeiks kckeiks force-pushed the gather-body-owners-in-hir-item-queries branch from b535db3 to 1160496 Compare June 17, 2022 23:29
@rust-log-analyzer

This comment has been minimized.

if matches!(ex.kind, ExprKind::Closure { .. })
&& associated_body(Node::Expr(ex)).is_some()
{
self.body_owners.push(ex.hir_id.owner);
Copy link
Contributor

Choose a reason for hiding this comment

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

We want to record the LocalDefId of the closure. hir_id.owner is the one of the closure's surrounding item-like (that we have already recorded in visit_*item).

Suggested change
self.body_owners.push(ex.hir_id.owner);
self.body_owners.push(self.tcx.hir().local_def_id(ex.hir_id));

Likewise for anon consts.

self.items.push(item.item_id());

if self.tcx.hir().is_body_owner(item.def_id) {
self.body_owners.push(item.def_id);
Copy link
Contributor

Choose a reason for hiding this comment

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

This probably pushes the same LocalDefId twice.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, that was the problem.

@cjgillot cjgillot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 21, 2022
@kckeiks kckeiks force-pushed the gather-body-owners-in-hir-item-queries branch 3 times, most recently from bbc27a9 to 17734a6 Compare June 24, 2022 16:39
@kckeiks kckeiks marked this pull request as ready for review June 24, 2022 17:19
Copy link
Contributor

@cjgillot cjgillot left a comment

Choose a reason for hiding this comment

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

Sorry for the delay @kckeiks, I had not realized you were waiting for me.

}

fn visit_anon_const(&mut self, c: &'hir AnonConst) {
if associated_body(Node::AnonConst(c)).is_some() {
Copy link
Contributor

Choose a reason for hiding this comment

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

This is always true.


fn visit_expr(&mut self, ex: &'hir Expr<'hir>) {
if matches!(ex.kind, ExprKind::Closure { .. })
&& associated_body(Node::Expr(ex)).is_some()
Copy link
Contributor

Choose a reason for hiding this comment

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

associated_body(...).is_some() is always true on closures.

self.impl_items.push(item.impl_item_id());
intravisit::walk_impl_item(self, item)
}

fn visit_foreign_item(&mut self, item: &'hir ForeignItem<'hir>) {
if associated_body(Node::ForeignItem(item)).is_some() {
Copy link
Contributor

Choose a reason for hiding this comment

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

This is always false.

self.trait_items.push(item.trait_item_id());
intravisit::walk_trait_item(self, item)
}

fn visit_impl_item(&mut self, item: &'hir ImplItem<'hir>) {
if associated_body(Node::ImplItem(item)).is_some() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you inline the definition of associated_body here?

@@ -1281,19 +1291,47 @@ pub(super) fn hir_module_items(tcx: TyCtxt<'_>, module_id: LocalDefId) -> Module
}

fn visit_trait_item(&mut self, item: &'hir TraitItem<'hir>) {
if associated_body(Node::TraitItem(item)).is_some() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you inline the definition of associated_body here?

@@ -1271,7 +1276,12 @@ pub(super) fn hir_module_items(tcx: TyCtxt<'_>, module_id: LocalDefId) -> Module
}

fn visit_item(&mut self, item: &'hir Item<'hir>) {
if associated_body(Node::Item(item)).is_some() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you inline the definition of associated_body here?

@@ -1327,6 +1374,7 @@ pub(crate) fn hir_crate_items(tcx: TyCtxt<'_>, _: ()) -> ModuleItems {
trait_items: Vec<TraitItemId>,
impl_items: Vec<ImplItemId>,
foreign_items: Vec<ForeignItemId>,
body_owners: Vec<LocalDefId>,
}

impl<'hir> Visitor<'hir> for CrateCollector<'hir> {
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if we can merge this visitor and the one for hir_module_items using some flag to switch behaviour.

@@ -464,6 +464,23 @@ impl<'hir> Map<'hir> {
}
}

/// Returns true if the `LocalDefId` corresponds to a body owner and false otherwise.
pub fn is_body_owner(self, def_id: LocalDefId) -> bool {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this used anywhere?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, I forgot to remove this.

| DefKind::AnonConst
| DefKind::Ctor(..)
| DefKind::Fn
| DefKind::AssocFn
Copy link
Contributor

Choose a reason for hiding this comment

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

This case is possibly wrong.

| DefKind::AssocFn
| DefKind::Closure
| DefKind::Generator
| DefKind::Static(..)
Copy link
Contributor

Choose a reason for hiding this comment

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

This one too.

kckeiks added 3 commits July 6, 2022 19:17
Signed-off-by: Miguel Guarniz <mi9uel9@gmail.com>
Signed-off-by: Miguel Guarniz <mi9uel9@gmail.com>
Signed-off-by: Miguel Guarniz <mi9uel9@gmail.com>
@kckeiks kckeiks force-pushed the gather-body-owners-in-hir-item-queries branch from 4d38f6e to 75ffbb0 Compare July 6, 2022 23:17
@rust-log-analyzer

This comment has been minimized.

@cjgillot
Copy link
Contributor

@bors retry

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 13, 2022
@bors
Copy link
Collaborator

bors commented Jul 13, 2022

⌛ Testing commit 75ffbb021cc96d6032fe5cad46153e401dc51802 with merge adc213b9b01134a4bcfc3efdac1e293946e6ab2d...

@bors
Copy link
Collaborator

bors commented Jul 13, 2022

💔 Test failed - checks-actions

@bors bors added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Jul 13, 2022
@rust-log-analyzer

This comment has been minimized.

}
});

par_iter(self.tcx.hir_crate_items(()).body_owners.to_vec()).for_each(|def_id| f(def_id));
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
par_iter(self.tcx.hir_crate_items(()).body_owners.to_vec()).for_each(|def_id| f(def_id));
par_iter(&self.tcx.hir_crate_items(()).body_owners[..]).for_each(|&def_id| f(def_id));

?

self.submodules.push(n.owner);
intravisit::walk_mod(self, m, n);
}
fn visit_mod(&mut self, m: &'hir Mod<'hir>, _s: Span, n: HirId) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This will insert a module as its own submodule. We should probably handle all ItemKind::Mod in visit_item.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think the if-else in visit_item would prevent that from happening? visit_mod gets called only by walk_item.

        if !self.crate_collector && let ItemKind::Mod(..) = item.kind {
            // If this declares another module, do not recurse inside it.
            self.submodules.push(item.def_id);
        } else {
            intravisit::walk_item(self, item)
        }

Copy link
Contributor

Choose a reason for hiding this comment

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

visit_mod is the entry point called by hir_module_items.

kckeiks added 3 commits July 13, 2022 12:22
Signed-off-by: Miguel Guarniz <mi9uel9@gmail.com>
Signed-off-by: Miguel Guarniz <mi9uel9@gmail.com>
Signed-off-by: Miguel Guarniz <mi9uel9@gmail.com>
@kckeiks kckeiks force-pushed the gather-body-owners-in-hir-item-queries branch from 75ffbb0 to ad76362 Compare July 13, 2022 17:26
Signed-off-by: Miguel Guarniz <mi9uel9@gmail.com>
@kckeiks kckeiks force-pushed the gather-body-owners-in-hir-item-queries branch from ad76362 to 2d265b6 Compare July 13, 2022 17:55
@cjgillot
Copy link
Contributor

@bors r+

@bors
Copy link
Collaborator

bors commented Jul 14, 2022

📌 Commit 2d265b6 has been approved by cjgillot

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 14, 2022
@bors
Copy link
Collaborator

bors commented Jul 15, 2022

⌛ Testing commit 2d265b6 with merge 30243dd...

@bors
Copy link
Collaborator

bors commented Jul 15, 2022

☀️ Test successful - checks-actions
Approved by: cjgillot
Pushing 30243dd to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Jul 15, 2022
@bors bors merged commit 30243dd into rust-lang:master Jul 15, 2022
@rustbot rustbot added this to the 1.64.0 milestone Jul 15, 2022
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (30243dd): comparison url.

Instruction count

  • Primary benchmarks: 🎉 relevant improvements found
  • Secondary benchmarks: 🎉 relevant improvements found
mean1 max count2
Regressions 😿
(primary)
N/A N/A 0
Regressions 😿
(secondary)
0.6% 0.6% 1
Improvements 🎉
(primary)
-0.4% -0.7% 30
Improvements 🎉
(secondary)
-0.5% -0.8% 18
All 😿🎉 (primary) -0.4% -0.7% 30

Max RSS (memory usage)

Results
  • Primary benchmarks: 🎉 relevant improvement found
  • Secondary benchmarks: 🎉 relevant improvements found
mean1 max count2
Regressions 😿
(primary)
N/A N/A 0
Regressions 😿
(secondary)
N/A N/A 0
Improvements 🎉
(primary)
-2.7% -2.7% 1
Improvements 🎉
(secondary)
-3.2% -4.3% 4
All 😿🎉 (primary) -2.7% -2.7% 1

Cycles

Results
  • Primary benchmarks: 🎉 relevant improvement found
  • Secondary benchmarks: mixed results
mean1 max count2
Regressions 😿
(primary)
N/A N/A 0
Regressions 😿
(secondary)
2.0% 2.0% 1
Improvements 🎉
(primary)
-1.2% -1.2% 1
Improvements 🎉
(secondary)
-2.9% -2.9% 1
All 😿🎉 (primary) -1.2% -1.2% 1

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

@rustbot label: -perf-regression

Footnotes

  1. the arithmetic mean of the percent change 2 3

  2. number of relevant changes 2 3

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants