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

84304 - rustdoc: shrink Item::Attributes #84494

Merged
merged 2 commits into from
Apr 27, 2021
Merged

Conversation

tdelabro
Copy link
Contributor

@tdelabro tdelabro commented Apr 23, 2021

Helps with #84304

@rust-highfive
Copy link
Collaborator

Some changes occurred in intra-doc-links.

cc @jyn514

@rust-highfive
Copy link
Collaborator

r? @GuillaumeGomez

(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 Apr 23, 2021
@tdelabro
Copy link
Contributor Author

r? @jyn514

@jyn514 jyn514 added the T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. label Apr 23, 2021
@rust-log-analyzer

This comment has been minimized.

Copy link
Member

@jyn514 jyn514 left a 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 Show resolved Hide resolved
src/librustdoc/clean/types.rs Show resolved Hide resolved
src/librustdoc/clean/types.rs Show resolved Hide resolved
Comment on lines 676 to 742
fn other_attrs(&self) -> Vec<ast::Attribute> {
self.iter().filter_map(|attr| match attr.doc_str() {
Some(_) => None,
None => Some(attr.clone()),
}).collect()
}
Copy link
Member

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?

Copy link
Contributor Author

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.

Comment on lines 676 to 680
fn other_attrs(&self) -> Vec<ast::Attribute> {
self.iter().filter_map(|attr| match attr.doc_str() {
Some(_) => None,
None => Some(attr.clone()),
}).collect()
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
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())

Copy link
Member

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.

Copy link
Member

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?

Copy link
Member

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.

Copy link
Contributor Author

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 !

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 tried, didn't go well ;)

.iter()
.find(|a| a.doc_str().is_some())
.map_or(true, |a| a.style == AttrStyle::Inner);

Attributes {
doc_strings,
Copy link
Member

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.

Copy link
Contributor Author

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

src/librustdoc/clean/types.rs Outdated Show resolved Hide resolved
Comment on lines -53 to +55
e.attrs
ast_attrs
Copy link
Member

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?

Copy link
Contributor Author

@tdelabro tdelabro Apr 25, 2021

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 ?

Copy link
Member

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.

src/librustdoc/passes/collect_intra_doc_links.rs Outdated Show resolved Hide resolved
src/librustdoc/passes/collect_intra_doc_links.rs Outdated Show resolved Hide resolved
@jyn514 jyn514 added C-cleanup Category: PRs that clean code up or issues documenting cleanup. 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 Apr 23, 2021
@tdelabro tdelabro changed the title 84304 - rustdoc: Get rid of Item::Attributes WIP 84304 - rustdoc: shrink Item::Attributes Apr 24, 2021
@tdelabro tdelabro marked this pull request as draft April 24, 2021 18:58
@bors

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@jyn514
Copy link
Member

jyn514 commented Apr 24, 2021

@tdelabro do you mind using rebasing instead of merging? There are instructions in https://rustc-dev-guide.rust-lang.org/git.html#conflicts.

@bors

This comment has been minimized.

@tdelabro
Copy link
Contributor Author

@tdelabro do you mind using rebasing instead of merging? There are instructions in https://rustc-dev-guide.rust-lang.org/git.html#conflicts.

Sorry about this. I fixed it with a push force.

@tdelabro
Copy link
Contributor Author

tdelabro commented Apr 25, 2021

At this point Attributes is only two fields large:

#[derive(Clone, Debug, Default)]
crate struct Attributes {
    crate doc_strings: Vec<DocFragment>,
    crate other_attrs: Vec<ast::Attribute>,
}

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 (that we can query at any time using txc.get_attrs(def_id)) and these other_attrs. Once this will be done, I think we will be able to remove Attributes form the codebase.

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.
What do you think ?

Copy link
Member

@jyn514 jyn514 left a 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.

src/librustdoc/clean/types.rs Outdated Show resolved Hide resolved
Comment on lines +168 to +171
self.extern_locations.insert(
n,
(name, src_root, extern_location(e, extern_url, tcx.get_attrs(did), &dst, tcx)),
);
Copy link
Member

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.

Copy link
Contributor Author

@tdelabro tdelabro Apr 25, 2021

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 ?

Copy link
Member

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.

Copy link
Member

@jyn514 jyn514 Apr 26, 2021

Choose a reason for hiding this comment

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

Opened #84588.

@tdelabro tdelabro changed the title WIP 84304 - rustdoc: shrink Item::Attributes 84304 - rustdoc: shrink Item::Attributes Apr 25, 2021
@tdelabro tdelabro marked this pull request as ready for review April 25, 2021 19:58
@tdelabro tdelabro marked this pull request as draft April 25, 2021 19:59
@tdelabro tdelabro marked this pull request as ready for review April 25, 2021 20:54
@tdelabro
Copy link
Contributor Author

@rustbot modify labels to -S-waiting-on-author and +S-waiting-on-review

@jyn514 jyn514 added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Apr 26, 2021
@bors
Copy link
Contributor

bors commented Apr 26, 2021

☀️ Try build successful - checks-actions
Build commit: 762707b31816d73aa7eb2aadf5961e9bc3d8e434 (762707b31816d73aa7eb2aadf5961e9bc3d8e434)

@rust-timer
Copy link
Collaborator

Queued 762707b31816d73aa7eb2aadf5961e9bc3d8e434 with parent 8212de8, future comparison URL.

@rust-timer
Copy link
Collaborator

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 rollup- to bors.

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
@rustbot label: +S-waiting-on-review -S-waiting-on-perf

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Apr 26, 2021
@GuillaumeGomez
Copy link
Member

Nice improvement on both the RSS usage and on the number of instructions (max -1.0% in both cases).

@jyn514 jyn514 removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 26, 2021
Copy link
Member

@jyn514 jyn514 left a 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 :)

Comment on lines 306 to 310
if let Some(new_id) = parent_module {
Attributes::from_ast(old_attrs, Some((inner, new_id)))
} else {
both.clean(cx)
},
Copy link
Member

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:

Suggested change
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)
},

Copy link
Contributor Author

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

@jyn514
Copy link
Member

jyn514 commented Apr 26, 2021

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.
@tdelabro
Copy link
Contributor Author

Oh, and if you could squash the commits a little that would be nice too, but I won't block on it.

Here we are. I just kept two of them

@rust-log-analyzer

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
@jyn514
Copy link
Member

jyn514 commented Apr 27, 2021

@bors r+

@bors
Copy link
Contributor

bors commented Apr 27, 2021

📌 Commit 727f904 has been approved by jyn514

@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-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Apr 27, 2021
@bors
Copy link
Contributor

bors commented Apr 27, 2021

⌛ Testing commit 727f904 with merge 727d101...

@bors
Copy link
Contributor

bors commented Apr 27, 2021

☀️ Test successful - checks-actions
Approved by: jyn514
Pushing 727d101 to master...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-cleanup Category: PRs that clean code up or issues documenting cleanup. 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-rustdoc Relevant to the rustdoc 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