Skip to content

Implement async setTimeout #844

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

Closed
wants to merge 1 commit into from
Closed

Conversation

rgerd
Copy link
Member

@rgerd rgerd commented Jul 23, 2021

Note this PR is a work in progress as I learn more about async programming

While taking CPU profiles of BabylonNative I noticed that Window::RecursiveWaitOrCall (used by the setTimeout polyfill) was taking up a bunch of cycles:
recursivewaitorcall-desktop

This seemed to be due to the high number of Dispatch calls coming from the recursive loop.

Now we're using a single worker thread with a condition variable and a priority queue, which allows us to both queue up delayed functions and preempt them, reducing the CPU load significantly.

fixes #843

@rgerd rgerd force-pushed the async-settimeout branch from a75c61f to 8e1c142 Compare July 29, 2021 16:01

void Dispatch(std::shared_ptr<Napi::FunctionReference> func, std::chrono::system_clock::time_point time)
{
if (time <= std::chrono::system_clock::now())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, system_clock can roll backwards if the user adjusts their system clock. steady_clock is guaranteed to always progress forward. I'd recommend using steady_clock for everything here.

@docEdub
Copy link
Contributor

docEdub commented Oct 17, 2022

Now implemented in #1146.

@docEdub docEdub closed this Oct 17, 2022
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

Successfully merging this pull request may close these issues.

Window::RecursiveWaitOrCall is performance-heavy
3 participants