-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
84304 - rustdoc: shrink Item::Attributes #84494
Conversation
Some changes occurred in intra-doc-links. cc @jyn514 |
(rust-highfive has picked a reviewer for you, use r? to override) |
r? @jyn514 |
This comment has been minimized.
This comment has been minimized.
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.
This is a great change :) but I think the title is not quite accurate? It should say something like "Shrink Item::Attributes", Attributes still calculates some info ahead of time.
To fix the CI error you can run x.py fmt
.
src/librustdoc/clean/types.rs
Outdated
fn other_attrs(&self) -> Vec<ast::Attribute> { | ||
self.iter().filter_map(|attr| match attr.doc_str() { | ||
Some(_) => None, | ||
None => Some(attr.clone()), | ||
}).collect() | ||
} |
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.
Hmm, you added this function, but didn't remove the other_attrs
field - was that intentional?
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.
This implementation was required to remove cfg
from Attributes
but to be able to entirely remove the other_attrs
field from Attributes
we will have to move it in Item
. It will be done in a future MR.
src/librustdoc/clean/types.rs
Outdated
fn other_attrs(&self) -> Vec<ast::Attribute> { | ||
self.iter().filter_map(|attr| match attr.doc_str() { | ||
Some(_) => None, | ||
None => Some(attr.clone()), | ||
}).collect() |
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.
fn other_attrs(&self) -> Vec<ast::Attribute> { | |
self.iter().filter_map(|attr| match attr.doc_str() { | |
Some(_) => None, | |
None => Some(attr.clone()), | |
}).collect() | |
fn other_attrs(&self) -> impl Iterator<Item = &ast::Attribute> { | |
self.iter().filter(|attr| attr.doc_str().is_none()) |
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.
Hmm, I realized while reviewing that attr.doc_str().is_some()
and attr.is_doc_comment()
behave differently :/ I'll look into fixing that.
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 noticed you change this to use .filter()
but didn't change the return type - was that intentional? Did you run into trouble?
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.
The reason I suggested removing .collect()
is it avoids an allocation for the common case where you just want to check each attribute one at a time.
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.
Ho ok. That's why !
I didn't notice the change on the return type, so yes I got some trouble and had to clone and collect the attributes.
I will try your way instead !
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 tried, didn't go well ;)
src/librustdoc/clean/types.rs
Outdated
.iter() | ||
.find(|a| a.doc_str().is_some()) | ||
.map_or(true, |a| a.style == AttrStyle::Inner); | ||
|
||
Attributes { | ||
doc_strings, |
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.
Would it possible to also calculate this on demand? I think extract_include
reads from the filesystem, so you probably want to add a cache - I'm fine with putting that off for a later PR.
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.
It had been moved as an AttributeExt
method
e.attrs | ||
ast_attrs |
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.
It looks like this was the only place the attributes for ExternalCrate was used, so you could turn it into a parameter instead - that's a great change, but it makes it a little harder to see what changes are related to removing files from Attributes itself. Do you mind splitting it into a separate commit?
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.
Should I revert the changes on ExternalCrate
and put them in a separate MR ?
Also I didn't really get the "turn it into a parameter instead" part. Can you give me a little bit more explanation ?
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.
Should I revert the changes on ExternalCrate and put them in a separate MR ?
Nah, don't worry about it, this is fine :) It's a good change.
Also I didn't really get the "turn it into a parameter instead" part. Can you give me a little bit more explanation ?
This is something you're already doing: instead of storing the attributes on ExternalCrate, you pass them in to extern_location
, which is the only place that uses them.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
@tdelabro do you mind using rebasing instead of merging? There are instructions in https://rustc-dev-guide.rust-lang.org/git.html#conflicts. |
This comment has been minimized.
This comment has been minimized.
Sorry about this. I fixed it with a push force. |
At this point #[derive(Clone, Debug, Default)]
crate struct Attributes {
crate doc_strings: Vec<DocFragment>,
crate other_attrs: Vec<ast::Attribute>,
} Next I will try to store However, I don't know what we should do with all the functions defined in |
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.
Next I will try to store other_attrs in Item so they will be available at any moment to build doc_strings, which is based on the combination of the Item attributes and these other_attrs. Once this will be done, I think we will be able to remove Attributes form the codebase.
I think we should save this for a follow-up PR, it will be easier to catch regressions.
However, I don't know what we should do with all the functions defined in impl Attributes (eg: extract_include or has_doc_flags). Probably move them to impl Item.
I would move them to AttributesExt where possible.
self.extern_locations.insert( | ||
n, | ||
(name, src_root, extern_location(e, extern_url, tcx.get_attrs(did), &dst, tcx)), | ||
); |
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.
Doesn't have to be here, but it would be nice to store the CrateNum here instead of the name and attributes. Both of those are available from the tcx.
Actually all of this looks like it could be calculated on-demand with only the CrateNum, tcx, and extern_html_root_urls.
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 I understood correctly:
Yo would like to replace crate external_paths: FxHashMap<DefId, (Vec<String>, ItemType)>
by crate extern_html_urls: FxHashMap<DefId, &BTreeMap<String, String>>
, get name
and attributes
from tcx
, combine them with cache.extern_html_root_url[crate_num]
and then compute extern_locations
with all this ?
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 would like to get rid of extern_locations
altogether, and calculate name
, src_root
, and external_location
on demand. external_paths
can stay the same.
I'll open an issue for it, it doesn't belong here.
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.
Opened #84588.
@rustbot modify labels to -S-waiting-on-author and +S-waiting-on-review |
☀️ Try build successful - checks-actions |
Queued 762707b31816d73aa7eb2aadf5961e9bc3d8e434 with parent 8212de8, future comparison URL. |
Finished benchmarking try commit (762707b31816d73aa7eb2aadf5961e9bc3d8e434): comparison url. Benchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. Please note that if the perf results are neutral, you should likely undo the rollup=never given below by specifying Importantly, though, if the results of this run are non-neutral do not roll this PR up -- it will mask other regressions or improvements in the roll up. @bors rollup=never |
Nice improvement on both the RSS usage and on the number of instructions (max -1.0% in both cases). |
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.
r=me with the nit addressed :)
src/librustdoc/clean/inline.rs
Outdated
if let Some(new_id) = parent_module { | ||
Attributes::from_ast(old_attrs, Some((inner, new_id))) | ||
} else { | ||
both.clean(cx) | ||
}, |
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.
It was like this before, but I think it would be more clear to use from_ast
consistently:
if let Some(new_id) = parent_module { | |
Attributes::from_ast(old_attrs, Some((inner, new_id))) | |
} else { | |
both.clean(cx) | |
}, | |
if let Some(new_id) = parent_module { | |
Attributes::from_ast(old_attrs, Some((inner, new_id))) | |
} else { | |
Attributes::from_ast(both, None) | |
}, |
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.
Indeed it's better this way
Oh, and if you could squash the commits a little that would be nice too, but I won't block on it. |
…t of the fields in Attributes, as functions in AttributesExt. refacto use from_def_id_and_attrs_and_parts instead of an old trick most of josha suggestions + check if def_id is not fake before using it in a query Removed usage of Attributes in FnDecl and ExternalCrate. Relocate part of the Attributes fields as functions in AttributesExt.
Here we are. I just kept two of them |
This comment has been minimized.
This comment has been minimized.
check item.is_fake() instead of self_id.is_some() Remove empty branching in Attributes::from_ast diverse small refacto after Josha review cfg computation moved in merge_attrs refacto use from_ast twice for coherence take cfg out of Attributes and move it to Item
@bors r+ |
📌 Commit 727f904 has been approved by |
☀️ Test successful - checks-actions |
Helps with #84304