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

Avoid some allocations in the macro parser #37318

Merged
merged 3 commits into from
Oct 22, 2016

Conversation

nnethercote
Copy link
Contributor

These three commits reduce the number of heap allocations done when compiling rustc-benchmarks/html5ever-2016-08-25 by 20%, from 16.5M to 13.3M. This speeds up (debug) compilation of it with a stage1 compiler by about 7%.

This avoids ~800,000 allocations in html5ever.
This avoids ~800,000 allocations in html5ever.
@nnethercote
Copy link
Contributor Author

r? @nrc

@rust-highfive rust-highfive assigned nrc and unassigned pnkfelix Oct 21, 2016
@rust-highfive
Copy link
Collaborator

r? @pnkfelix

(rust_highfive has picked a reviewer for you, use r? to override)

@nnethercote
Copy link
Contributor Author

@eddyb tells me the 3rd commit is bogus, because those failure messages are not just used in panics.

@nrc
Copy link
Member

nrc commented Oct 21, 2016

r = me for the first two commits, but I agree with @eddyb that the third is a non-starter. Not sure how much he explained, but the parser used to use panic to give errors. That behaviour is mostly gone, but there are a few remnants left such as the macro parser, so this is an error message that is user facing whenever a macro doesn't apply.

@nnethercote
Copy link
Contributor Author

@nrc: Thank you for the fast response. I've updated to fix the third commit. I had to use the turbofish in multiple calls like this ParseResult::<i32>::msg(tok) -- not sure if there's a better way to do that.

/// Fatal error (malformed macro?). Abort compilation.
Error(syntax_pos::Span, String)
}

impl<T> ParseResult<T> {
pub fn msg(tok: Option<Token>) -> String {
Copy link
Member

Choose a reason for hiding this comment

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

can you make this a method on &self rather than a static method? That should avoid the turbo fish.

Copy link
Member

Choose a reason for hiding this comment

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

Can't because it needs to be used in several places, without a ParseResult around.

Copy link
Member

Choose a reason for hiding this comment

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

Then I would just make it a top-level function (with a more descriptive name), rather than a method

Copy link
Member

Choose a reason for hiding this comment

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

Indeed, I suggested failure_message on IRC (with that same descriptivity goal).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's difficult, because the self is gone by the time the method is called in some cases. I'll make it a top-level fn instead.

@@ -227,7 +231,11 @@ pub fn compile(sess: &ParseSess, def: &ast::MacroDef) -> SyntaxExtension {

let argument_map = match parse(sess, &Vec::new(), arg_reader, &argument_gram) {
Success(m) => m,
Failure(sp, str) | Error(sp, str) => {
Failure(sp, tok) => {
let str = ParseResult::<i32>::msg(tok);
Copy link
Member

Choose a reason for hiding this comment

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

nit: str is a bad name because it is a very common type, so the code is a little jarring.

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'll use s.

@nnethercote
Copy link
Contributor Author

All comments addressed!

@eddyb
Copy link
Member

eddyb commented Oct 21, 2016

@nnethercote There's still the matter of using Token::Eof instead of None. Right now Some(Token::Eof) is an impossible case so the Option is wasteful.

This lets us delay creation of failure messages until they are needed,
which avoids ~1.6M allocations in html5ever.
@nnethercote
Copy link
Contributor Author

There's still the matter of using Token::Eof instead of None

Oh, that was a comment on IRC and I missed it. Thanks for the suggestion, I've updated again.

@eddyb
Copy link
Member

eddyb commented Oct 21, 2016

@bors r=nrc,eddyb

@bors
Copy link
Contributor

bors commented Oct 21, 2016

📌 Commit b817cf8 has been approved by nrc,eddyb

Manishearth added a commit to Manishearth/rust that referenced this pull request Oct 22, 2016
…ddyb

Avoid some allocations in the macro parser

These three commits reduce the number of heap allocations done when compiling rustc-benchmarks/html5ever-2016-08-25 by 20%, from 16.5M to 13.3M. This speeds up (debug) compilation of it with a stage1 compiler by about 7%.
@bors
Copy link
Contributor

bors commented Oct 22, 2016

⌛ Testing commit b817cf8 with merge a117bba...

bors added a commit that referenced this pull request Oct 22, 2016
Avoid some allocations in the macro parser

These three commits reduce the number of heap allocations done when compiling rustc-benchmarks/html5ever-2016-08-25 by 20%, from 16.5M to 13.3M. This speeds up (debug) compilation of it with a stage1 compiler by about 7%.
@bors bors merged commit b817cf8 into rust-lang:master Oct 22, 2016
@nnethercote nnethercote deleted the html5ever-more branch October 23, 2016 22:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants