async-attributes icon indicating copy to clipboard operation
async-attributes copied to clipboard

#[bench] attribute dose not work

Open taiki-e opened this issue 6 years ago • 7 comments

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 avatar Oct 02 '19 06:10 taiki-e

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 avatar Oct 02 '19 06:10 taiki-e

So the way described in https://github.com/async-rs/async-std/issues/70#issuecomment-528042987 may not work until scoped spawn API is implemented.

taiki-e avatar Oct 02 '19 06:10 taiki-e

cc @yoshuawuyts @stjepang

taiki-e avatar Oct 02 '19 06:10 taiki-e

@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.

yoshuawuyts avatar Oct 06 '19 14:10 yoshuawuyts

@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 https://github.com/async-rs/async-std/issues/70#issuecomment-528042987 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

taiki-e avatar Oct 06 '19 15:10 taiki-e

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

0x8f701 avatar May 03 '20 18:05 0x8f701

Totally agree with @taiki-e

0x8f701 avatar May 03 '20 18:05 0x8f701