Skip to content

refactor: push_lifetime* goes through an array instead of a manual iterator #293

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 2 commits into from
Mar 11, 2025

Conversation

aatifsyed
Copy link
Contributor

This should be faster:

$ cargo bench --bench token-iteration --quiet -- --min-time 10
Timer precision: 25 ns
token_iteration  fastest       │ slowest       │ median        │ mean          │ samples │ iters
╰─ bench                       │               │               │               │         │
   ├─ array      143.8 ns      │ 11.52 ms      │ 149.8 ns      │ 169.7 ns      │ 29596510 │ 29596510
   ╰─ manual     162.8 ns      │ 19.56 ms      │ 168.8 ns      │ 190.3 ns      │ 28223255 │ 28223255
Benchmark source
use core::fmt;

use proc_macro2::{Ident, Punct, Spacing, Span, TokenStream, TokenTree};

fn main() {
    divan::main();
}

#[derive(Clone, Copy)]
enum Method {
    Array,
    Manual,
}

impl fmt::Display for Method {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(match self {
            Method::Array => "array",
            Method::Manual => "manual",
        })
    }
}

#[divan::bench(args = [Method::Array, Method::Manual])]
fn bench(method: Method) {
    let mut tokens = TokenStream::new();
    let lifetime = "'lifetime";

    match method {
        Method::Array => {
            tokens.extend([
                TokenTree::Punct(Punct::new('\'', Spacing::Joint)),
                TokenTree::Ident(Ident::new(&lifetime[1..], Span::call_site())),
            ]);
        }
        Method::Manual => {
            struct Lifetime<'a> {
                name: &'a str,
                state: u8,
            }

            impl Iterator for Lifetime<'_> {
                type Item = TokenTree;

                fn next(&mut self) -> Option<Self::Item> {
                    match self.state {
                        0 => {
                            self.state = 1;
                            Some(TokenTree::Punct(Punct::new('\'', Spacing::Joint)))
                        }
                        1 => {
                            self.state = 2;
                            Some(TokenTree::Ident(Ident::new(self.name, Span::call_site())))
                        }
                        _ => None,
                    }
                }
            }

            tokens.extend(Lifetime {
                name: &lifetime[1..],
                state: 0,
            });
        }
    }
}

Shouldn't affect MSRV:

$ rm Cargo.lock; cargo +1.56.1 test --quiet

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s


running 1 test
i
test result: ok. 0 passed; 0 failed; 1 ignored; 0 measured; 0 filtered out; finished in 0.00s


running 40 tests
........................................
test result: ok. 40 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.02s


running 22 tests
......................
test result: ok. 22 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 1.99s

@aatifsyed aatifsyed requested a review from dtolnay March 11, 2025 23:31
Copy link
Owner

@dtolnay dtolnay left a comment

Choose a reason for hiding this comment

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

Thanks!

@dtolnay dtolnay merged commit 3ff6882 into dtolnay:master Mar 11, 2025
10 checks passed
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.

2 participants