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

#[bench] attribute dose not work #9

Open
taiki-e opened this issue Oct 2, 2019 · 7 comments
Open

#[bench] attribute dose not work #9

taiki-e opened this issue Oct 2, 2019 · 7 comments

Comments

@taiki-e
Copy link
Contributor

taiki-e commented Oct 2, 2019

The current #[bench] attribute is broken because #7 was merged without testing.

code:

#![feature(test)]
extern crate test;

use async_std::task;

#[async_attributes::bench]
async fn bench(b: &mut test::Bencher) {
    b.iter(|| {
        println!("hello world");
    })
}

error:

error: macros that expand to items must be delimited with braces or followed by a semicolon
 --> benches/bench.rs:7:16
  |
7 | async fn bench(b: &mut test::Bencher) {
  |                ^^^^^^^^^^^^^^^^^^^^^
help: change the delimiters to curly braces
  |
7 | async fn bench( {: &mut test::Benche}) {
  |                 ^                   ^
help: add a semicolon
  |
7 | async fn bench(b: &mut test::Bencher;) {
  |                                     ^

error: macro expansion ignores token `,` and any following
 --> benches/bench.rs:7:16
  |
6 | #[async_attributes::bench]
  | -------------------------- caused by the macro expansion here
7 | async fn bench(b: &mut test::Bencher) {
  |                ^^^^^^^^^^^^^^^^^^^^^
  |
  = note: the usage of `async_attributes::bench!` is likely invalid in item context

error: async benchmarks don't take any arguments
 --> benches/bench.rs:7:16
  |
7 | async fn bench(b: &mut test::Bencher) {
  |                ^^^^^^^^^^^^^^^^^^^^^

@taiki-e
Copy link
Contributor Author

taiki-e commented Oct 2, 2019

The error here is related to proc-macro, but if we fix it, it still doesn't work.

error[E0621]: explicit lifetime required in the type of `b`
 --> benches/bench.rs:6:1
  |
6 | #[async_attributes::bench]
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^ lifetime `'static` required
7 | async fn bench(b: &mut test::Bencher) {
  |                   ------------------ help: add explicit lifetime `'static` to the type of `b`: `&'static mut test::Bencher`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0621`.
error: could not compile `async-attributes`.

This is because task::spawn requires 'static lifetime.

The code generated at this time is as follows:

#[bench]
fn bench(b: &mut test::Bencher) {
    task::block_on(task::spawn(async {
        b.iter(|| {
            println!("hello world");
        })
    }))
}

(This doesn't seem to work even if &mut test::Bencher is changed to &'static mut test::Bencher or async block is changed to async fn or async move block.)

@taiki-e
Copy link
Contributor Author

taiki-e commented Oct 2, 2019

So the way described in async-rs/async-std#70 (comment) may not work until scoped spawn API is implemented.

@taiki-e
Copy link
Contributor Author

taiki-e commented Oct 2, 2019

cc @yoshuawuyts @stjepang

@yoshuawuyts
Copy link
Collaborator

@taiki-e thanks so much for tracking this down. I think probably the best course of action here would be to revert for now and revisit this later?

Also could you perhaps clarify what you mean by "scoped spawn API"? I feel like I've heard it before but can't quite recall what it means.

@taiki-e
Copy link
Contributor Author

taiki-e commented Oct 6, 2019

@yoshuawuyts

I think probably the best course of action here would be to revert for now and revisit this later?

Yeah, for now, I think this needs to be reverted.

Also, since Bencher::iter takes a closure as an argument, I think the way mentioned in async-rs/async-std#70 (comment) may not work. cc @stjepang

#[bench]
fn benchmark(b: &mut Bencher) {
    task::block_on(task::spawn(async {
        b.iter(|| {
            // `await` is only allowed inside `async` functions and blocks.
            // So, we need to make a future in `b.iter(..)`.
        })
    }))
}

Also could you perhaps clarify what you mean by "scoped spawn API"? I feel like I've heard it before but can't quite recall what it means.

stjepang/async-std-old#39

@GopherJ
Copy link

GopherJ commented May 3, 2020

I still got async benchmarks don't take any arguments even I have an argument

@GopherJ
Copy link

GopherJ commented May 3, 2020

Totally agree with @taiki-e

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

No branches or pull requests

3 participants