Skip to content

Defer calls to proc_macro::TokenStream::extend #199

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

Merged
merged 1 commit into from
Oct 4, 2019
Merged

Defer calls to proc_macro::TokenStream::extend #199

merged 1 commit into from
Oct 4, 2019

Conversation

dtolnay
Copy link
Owner

@dtolnay dtolnay commented Oct 4, 2019

Fixes #198 by batching invocations of the impl Extend<TokenTree> for TokenStream impl.

For the benchmark in https://github.com/illicitonion/num_enum_regression_13 this reduces compile time from 118 seconds to 2.0 seconds on my machine.

Once the Extend performance is fixed in libproc_macro (rust-lang/rust#65080), this commit should be reverted.

@dtolnay
Copy link
Owner Author

dtolnay commented Oct 4, 2019

N= before PR with PR
2 0.1ms 0.04ms
10 0.3ms 0.1ms
100 8.7ms 0.6ms
1000 610ms 6.2ms
2000 2400ms 11ms
3000 5600ms 18ms
4000 9500ms 23ms
5000 15000ms 28ms
6000 23000ms 34ms
7000 32000ms 37ms
8000 47000ms 41ms
extern crate proc_macro;

use proc_macro::TokenStream;
use quote::quote;
use std::time::Instant;

const N: u32 = 7000;

#[proc_macro]
pub fn bench(_input: TokenStream) -> TokenStream {
    let start = Instant::now();
    let counter = 0..N;
    let _ = quote! {
        #(
            #counter
            core::option::Option::None,
        )*
    };
    eprintln!("ELAPSED: {} millis", start.elapsed().as_millis());

    TokenStream::new()
}

@alexcrichton alexcrichton merged commit 29c19f4 into dtolnay:master Oct 4, 2019
@dtolnay dtolnay deleted the extend branch October 4, 2019 20:04
#[derive(Clone)]
pub struct DeferredTokenStream {
stream: proc_macro::TokenStream,
extra: Vec<proc_macro::TokenTree>,

Choose a reason for hiding this comment

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

Cannot all of this be put into a single vector?

Copy link
Owner Author

Choose a reason for hiding this comment

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

That has different performance characteristics when converting between proc_macro::TokenStream and proc_macro2::TokenStream.

Repository owner locked and limited conversation to collaborators Dec 26, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

libproc_macro calls are extremely expensive
3 participants