async-attributes
async-attributes copied to clipboard
#[bench] attribute dose not work
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) {
| ^^^^^^^^^^^^^^^^^^^^^
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.)
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.
cc @yoshuawuyts @stjepang
@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
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
I still got async benchmarks don't take any arguments even I have an argument
Totally agree with @taiki-e