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

Suggestion for small performance improvement #38

Open
gammelalf opened this issue Jul 22, 2023 · 0 comments
Open

Suggestion for small performance improvement #38

gammelalf opened this issue Jul 22, 2023 · 0 comments

Comments

@gammelalf
Copy link

Hi,
nice project!

I just did a small prototype before knowing of this crate, which ended up implementing the same thing as you did.
Although mine is just a prototype, I spotted a simple change you could do to improve your speed a tiny bit:

How

Replace your create function in waker.rs with:

pub fn create() -> impl std::ops::Deref<Target = Waker> {
    // Safety: The waker points to a vtable with functions that do nothing. Doing
    // nothing is memory-safe.
    std::mem::ManuallyDrop::new(unsafe { Waker::from_raw(RAW_WAKER) })
}

Nothing else would have to change because you only ever use the waker as reference and ManuallyDrop implements Deref.

Why

Your waker's drop method is a noop because it has no data to clean up.
However the Waker instance doesn't know this and will always call drop resulting in a virtual function call whose body does nothing.
By wrapping the Waker in ManuallyDrop you avoid this single unnecessary function call which is done in each advance call.

peterhj pushed a commit to peterhj/genawaiter that referenced this issue Aug 31, 2023
(This implements the suggestion of gammelalf in whatisaphone#38.)
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

1 participant