Skip to content

Refactor item attributes into a SortedIndexMultiMap<u32, Symbol, &Attribute> #94905

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

Closed
cjgillot opened this issue Mar 13, 2022 · 12 comments
Closed
Assignees
Labels
E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-help-wanted Call for participation: Help is requested to fix this issue. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.

Comments

@cjgillot
Copy link
Contributor

cjgillot commented Mar 13, 2022

Now, when code needs to look up attributes on an item, it fetches the complete list of attributes and sequentially looks for the interesting one. This could be made more efficient by making the attribute list a SortedIndexMultiMap.

Steps:

  • replace &[ast::Attribute] by SortedIndexMultiMap<u32, Symbol, &ast::Attribute> in rustc_hir::hir, rustc_middle::query and rustc_middle::ty;
  • build the SortedIndexMultiMap in rustc_ast_lowering and rustc_metadata::decoder using Attribute::name_or_empty as key;
  • adapt the use sites to fetch the attributes using attrs.get_by_key(my_name) instead of attrs.iter().filter(|attr| attr.has_name(my_name);
  • evaluate performance impact.

Extra:

  • implement item_attrs query for the local crate, and replace calls to tcx.get_attrs by calls to tcx.item_attrs.

I am available on zulip for more detailed information.

@cjgillot cjgillot added E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. E-help-wanted Call for participation: Help is requested to fix this issue. labels Mar 13, 2022
@camelid
Copy link
Member

camelid commented Mar 13, 2022

I would like to attempt this. @rustbot claim

@raiyansayeed
Copy link
Contributor

Hey @camelid , just wondering if you're still working on the task?

@NEUDitao
Copy link

Since @camelid hasn't reported on this in a while, can I take a stab at it?

@camelid
Copy link
Member

camelid commented Jul 31, 2022

Hi, sorry, I didn't see these previous messages. I did attempt this but got stuck and didn't have time to figure out the right approach. Feel free to try it!

@camelid camelid removed their assignment Jul 31, 2022
@NEUDitao
Copy link

@rustbot claim

@hottea773
Copy link

hottea773 commented Sep 22, 2022

@NEUDitao are you actively working on this?

I had a little look at this, but hit an issue in replacing &[ast::Attribute] with SortedIndexMultiMap<u32, Symbol, &ast::Attribute> in rustc_middle::query; specifically, https://github.com/rust-lang/rust/blob/1.63.0/compiler/rustc_middle/src/query/mod.rs#L1105.

error[E0277]: the trait bound `rustc_data_structures::sorted_map::SortedIndexMultiMap<u32, rustc_span::Symbol, &Attribute>: std::marker::Copy` is not satisfied
    --> compiler/rustc_middle/src/ty/query.rs:255:91
     |
178  | /  macro_rules! define_callbacks {
179  | |      (
180  | |       $($(#[$attr:meta])*
181  | |          [$($modifiers:tt)*] fn $name:ident($($K:tt)*) -> $V:ty,)*) => {
...    |
255  | |                  let cached = try_get_cached(self.tcx, &self.tcx.query_caches.$name, &key, copy);
     | |                                                                                            ^^^^ the trait `std::marker::Copy` is not implemented for `rustc_data_structures::sorted_map::SortedIndexMultiMap<u32, rustc_span::Symbol, &Attribute>`
...    |
324  | |      };
325  | |  }
     | |__- in this expansion of `define_callbacks!` (#2)
...
339  |    rustc_query_append! { define_callbacks! }
     |    ----------------------------------------- in this macro invocation (#1)
     |
    ::: compiler/rustc_middle/src/query/mod.rs:18:1
     |
18   |  / rustc_queries! {
19   |  |     query trigger_delay_span_bug(key: DefId) -> () {
20   |  |         desc { "trigger a delay span bug" }
21   |  |     }
...     |
2101 |  |     }
2102 |  | }
     |  | -
     |  | |
     |  |_in this expansion of `rustc_query_append!` (#1)
     |    in this macro invocation (#2)
     |
note: required by a bound in `ty::query::copy`
    --> compiler/rustc_middle/src/ty/query.rs:114:12
     |
114  | fn copy<T: Copy>(x: &T) -> T {
     |            ^^^^ required by this bound in `ty::query::copy`

Which is deliberate; as in https://github.com/rust-lang/rust/blob/1.63.0/compiler/rustc_middle/src/ty/query.rs#L108-L112.
I think this means this approach doesn't work; but I'm a total newbie to rustc development, so am not sure whether there are larger things we should be doing to get this working.

@cjgillot
Copy link
Contributor Author

@hottea773 Query results indeed require to be Copy. If the information you are constructing is not Copy, you can use arena_cache in the query description. The computed value will be put on an arena, and the query will just return a reference to it, which is Copy.

@czzrr
Copy link
Contributor

czzrr commented Jan 29, 2023

@rustbot claim

@rustbot rustbot assigned czzrr and unassigned NEUDitao Jan 29, 2023
@Jesse-Bakker
Copy link
Contributor

Hey @czzrr, are you actively working on this? Otherwise I'll take a stab at it

@czzrr
Copy link
Contributor

czzrr commented Mar 17, 2023

@Jesse-Bakker Go ahead - I don't have the time right now :)

@Jesse-Bakker
Copy link
Contributor

@rustbot claim

@cjgillot
Copy link
Contributor Author

cjgillot commented Jul 1, 2023

Closing as this did not yield the wanted improvement in #109532.

@cjgillot cjgillot closed this as completed Jul 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-help-wanted Call for participation: Help is requested to fix this issue. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.
Projects
None yet
Development

No branches or pull requests

7 participants