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

[email protected]: big benchmark regression on block_on #770

Closed
0x8f701 opened this issue May 9, 2020 · 10 comments
Closed

[email protected]: big benchmark regression on block_on #770

0x8f701 opened this issue May 9, 2020 · 10 comments

Comments

@0x8f701
Copy link

0x8f701 commented May 9, 2020

In this PR casbin/casbin-rs#136, I upgraded async-std to 1.6-beta.1 because I'm interested in its wasm support, thanks for @dignifiedquire who made the fix in branch fix/file-block #768

However, after upgrading, I found that there is a big bench regression, you should be able to see it from here:

https://travis-ci.org/github/casbin/casbin-rs/jobs/685015785#L813

@0x8f701
Copy link
Author

0x8f701 commented May 9, 2020

bench code: https://github.com/casbin/casbin-rs/blob/master/benches/benchmark.rs, this bench code isn't that correct because I'm creating runtime in every bench iter. I'm not able to solve it because of: #749 and async-rs/async-attributes#9

However, casbin-rs's previous version uses the same bench code so basically I think the bench can be compared to previous version which uses [email protected]

@0x8f701
Copy link
Author

0x8f701 commented May 9, 2020

@dignifiedquire Any idea on this? Thanks in advance.

@0x8f701 0x8f701 changed the title [email protected]: big benchmark regression [email protected]: big benchmark regression in a project May 9, 2020
@yoshuawuyts
Copy link
Contributor

@GopherJ it may also be useful to open an issue on smol since most of the runtime logic now lives there.

@0x8f701 0x8f701 changed the title [email protected]: big benchmark regression in a project [email protected]: big benchmark regression in block_on May 9, 2020
@0x8f701
Copy link
Author

0x8f701 commented May 11, 2020

@yoshuawuyts From stjepang :

I believe because async-std's block_on currently starts a smol worker rather than just blocking on a future. This is currently a hack necessary to make spawning thread-local tasks within block_on possible.

then it becomes hard to benchmark async code. block_on will influence the result a lot

@0x8f701 0x8f701 changed the title [email protected]: big benchmark regression in block_on [email protected]: big benchmark regression on block_on May 11, 2020
@ghost
Copy link

ghost commented May 22, 2020

I am working on this, the issue will be fixed with smol 0.2 when it gets released. The solution is to create a thread-local executor lazily on demand. If there is no spawn_local() inside block_on(), there is no reason to initialize a thread-local executor.

@algesten
Copy link

@stjepang does that mean smol 0.2 block_on will semantically be closer to old async-std and tokio? i.e. in a single threaded runtime, it uses the local thread?

i would like to use smol, but i'm currently struggling cause my simplest case is a block_on that i don't want to spawn any new threads to drive, just use the current.

@ghost
Copy link

ghost commented May 23, 2020

@algesten In that case, I wonder why not just use smol::run() to run it on the current thread?

Version 0.2 will break smol into 3 subcrates: blocking (already done), async I/O + timers, and executor. There will be more knobs for async I/O and executors, which will allow us to implement block_on() in async-std more efficiently.

@algesten
Copy link

@stjepang ah you're right! thanks! I'm just getting acquainted with smol's API. run is indeed what i'm after.

@ghost
Copy link

ghost commented Aug 27, 2020

Is this still an issue? I believe it has been resolved but can we check?

@0x8f701
Copy link
Author

0x8f701 commented Aug 29, 2020

HI, @stjepang , I confirm it's nearly fixed:

#![feature(test)]

extern crate test;

use test::Bencher;

fn raw_enforce(r: [&str; 3]) -> bool {
    let policies = [["alice", "data1", "read"], ["bob", "data2", "write"]];
    for policy in &policies {
        if policy == &r {
            return true;
        }
    }
    return false;
}

async fn async_enforce(r: [&str; 3]) -> bool {
    raw_enforce(r)
}

#[bench]
fn b_benchmark_raw(b: &mut Bencher) {
    b.iter(|| {
        raw_enforce(["alice", "data1", "read"]);
    });
}

#[bench]
fn b_benchmark_async(b: &mut Bencher) {
    b.iter(|| {
        async_std::task::block_on(async_enforce(["alice", "data1", "read"]));
    });
}

[email protected]

image

[email protected]

image

@0x8f701 0x8f701 closed this as completed Aug 29, 2020
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