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

Speed up TokenStream concatenation #65198

Merged
merged 2 commits into from
Oct 9, 2019
Merged

Conversation

nnethercote
Copy link
Contributor

This PR fixes the quadratic behaviour identified in #65080.

r? @Mark-Simulacrum

Currently, this function creates a new empty stream, and then appends
the elements from each given stream onto that stream. This can cause
quadratic behaviour.

This commit changes the function so that it modifies the first stream
(which can be long) by extending it with the elements from the
subsequent streams (which are almost always short), which avoids the
quadratic behaviour.
Currently, when two tokens must be glued together, this function duplicates
large chunks of the existing streams. This can cause quadratic behaviour.

This commit changes the function so that it overwrites the last token with a
glued token, which avoids the quadratic behaviour. This removes the need for
`TokenStreamBuilder::push_all_but_{first,last}_tree`.

The commit also restructures `push` somewhat, by removing
`TokenStream::{first_tree_and_joint,last_tree_if_joint}` in favour of more
pattern matching and some comments. This makes the code shorter, and in my
opinion, more readable.
@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 8, 2019
@nnethercote
Copy link
Contributor Author

In my local measurements, this provided a very small (<1%) improvement on some of the standard benchmarks.

@bors try @rust-timer queue

@rust-timer
Copy link
Collaborator

Awaiting bors try build completion

@bors
Copy link
Contributor

bors commented Oct 8, 2019

⌛ Trying commit 75e0078 with merge c255427...

bors added a commit that referenced this pull request Oct 8, 2019
Speed up `TokenStream` concatenation

This PR fixes the quadratic behaviour identified in #65080.

r? @Mark-Simulacrum
// Get the first stream. If it's `None`, create an empty
// stream.
let mut iter = streams.drain();
let mut first_stream_lrc = match iter.next().unwrap().0 {
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
let mut first_stream_lrc = match iter.next().unwrap().0 {
let mut first_stream_lrc = match iter.next().unwrap().0.unwrap_or_default();

// space for them.
let first_vec_mut = Lrc::make_mut(&mut first_stream_lrc);
first_vec_mut.reserve(num_appends);
for stream in iter {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this would be cleaner using .filter_map(|x| x) and .flat_map(|s| s.iter().cloned()).

Copy link
Member

Choose a reason for hiding this comment

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

Current version is easy to read, and the alternatives seem to spend more Rust functions to do the same thing, IMO one can prefer the current.

@bors
Copy link
Contributor

bors commented Oct 8, 2019

☀️ Try build successful - checks-azure
Build commit: c255427 (c25542724f393647fc93a8a0319edaa827e701d9)

@rust-timer
Copy link
Collaborator

Queued c255427 with parent d304f5c, future comparison URL.

Copy link
Member

@Mark-Simulacrum Mark-Simulacrum left a comment

Choose a reason for hiding this comment

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

Code changes look good to me. I'd like to get @petrochenkov or perhaps @matklad to sign off too though since I'm not too familiar with this code.

@matklad
Copy link
Member

matklad commented Oct 8, 2019

LGTM

@Mark-Simulacrum
Copy link
Member

@bors r+ rollup=never

@bors
Copy link
Contributor

bors commented Oct 8, 2019

📌 Commit 75e0078 has been approved by Mark-Simulacrum

@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 Oct 8, 2019
@petrochenkov
Copy link
Contributor

r? @Mark-Simulacrum

@nnethercote
Copy link
Contributor Author

The perf run didn't work. Let's try it the old-fashioned way:

@rust-timer build c255427

@rust-timer
Copy link
Collaborator

Queued c255427 with parent d304f5c, future comparison URL.

@mati865
Copy link
Contributor

mati865 commented Oct 8, 2019

@nnethercote it worked but still haven't finished, you can see it here https://perf.rust-lang.org/status.html

That 700000% wall time regression made queue a bit long...

@rust-timer
Copy link
Collaborator

Finished benchmarking try commit c255427, comparison URL.

@nnethercote
Copy link
Contributor Author

Lots of small (<1%) improvements, mostly in the short-running benchmarks.

@bors
Copy link
Contributor

bors commented Oct 9, 2019

⌛ Testing commit 75e0078 with merge e59dab5...

bors added a commit that referenced this pull request Oct 9, 2019
Speed up `TokenStream` concatenation

This PR fixes the quadratic behaviour identified in #65080.

r? @Mark-Simulacrum
@bors
Copy link
Contributor

bors commented Oct 9, 2019

☀️ Test successful - checks-azure
Approved by: Mark-Simulacrum
Pushing e59dab5 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Oct 9, 2019
@bors bors merged commit 75e0078 into rust-lang:master Oct 9, 2019
@nnethercote nnethercote deleted the fix-65080 branch October 9, 2019 22:38
@jyn514 jyn514 added I-compiletime Issue: Problems and improvements with respect to compile times. A-proc-macros Area: Procedural macros T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-proc-macros Area: Procedural macros I-compiletime Issue: Problems and improvements with respect to compile times. 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.