Skip to content

#[bench] attribute dose not work #9

@taiki-e

Description

@taiki-e
Contributor

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) {
  |                ^^^^^^^^^^^^^^^^^^^^^

Activity

taiki-e

taiki-e commented on Oct 2, 2019

@taiki-e
ContributorAuthor

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

taiki-e commented on Oct 2, 2019

@taiki-e
ContributorAuthor

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

taiki-e

taiki-e commented on Oct 2, 2019

@taiki-e
ContributorAuthor
yoshuawuyts

yoshuawuyts commented on Oct 6, 2019

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

taiki-e commented on Oct 6, 2019

@taiki-e
ContributorAuthor

@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

0x8f701

0x8f701 commented on May 3, 2020

@0x8f701

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

0x8f701

0x8f701 commented on May 3, 2020

@0x8f701

Totally agree with @taiki-e

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @yoshuawuyts@0x8f701@taiki-e

        Issue actions

          #[bench] attribute dose not work · Issue #9 · async-rs/async-attributes