-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor the execution to avoid setTimeout (#2999)
Close #2996 This PR does many things: - Remove the `crate::util::yield_twice` function in favor of a platform-specific function `TPlat::yield_after_cpu_intensive`. This way, the implementation of the yielding, which is a bit hacky due to the requirements of the Wasm node, is specific to the Wasm node. - Refactor the `nextJsonRpcResponse` function and the `jsonRpcResponsesNonEmptyCallback` callback. Right now, when we wait for a JSON-RPC response to come, we wait for smoldot to notify that it has a response available. But because this notification is done through a callback, we can't immediately query the content of the response, so instead we use `setTimeout(..., 0)` to register a task that queries the content of the response and then notify the `Promise` that was waiting. Now we instead immediately notify a `Promise`, and this `Promise` then queries the content of the response as an aftermath. - In the JS code, add a `registerShouldPeriodicallyYield` function whose implementation is "platform-specific" (browsers, NodeJS, Deno). This function controls a new setting called "should periodically yield". In NodeJS and Deno, this is always `false`. In the browser, this is `false` if and only if the page is in the background and `true` if it is in the foreground. Passing `false` reduces the usage of `setTimeout`, while passing `true` promotes the usage of `setTimeout` to avoid blocking the browser. - In the JS <-> Rust bindings, add a `periodically_yield` parameter to `init` and add a new `set_periodically_yield` function. See above point. - Refactor the way the execution is done. Currently we spawn at initialization a task (using `setTimeout(..., 0)`) that runs everything. When something needs to be executed, this task wakes up by calling `setTimeout(..., 0)` again. After this PR, there is no "background task" anymore. Instead, the `Future` that drives everything is now put in `CLIENT` variable, and every single binding function calls a new `advance_execution` function before returning. `advance_execution` polls that `Future` and, depending on the `periodically_yield` setting, either executes everything until there is nothing more to do, or executes until the first yield. All these changes eliminate the usage of `setTimeout` to the strict minimum, which is when we actually need a timer.
- Loading branch information
Showing
18 changed files
with
298 additions
and
210 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.