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

[Merged by Bors] - Implement host hooks and job queues APIs #2529

Closed
wants to merge 7 commits into from

Conversation

jedel1043
Copy link
Member

@jedel1043 jedel1043 commented Jan 11, 2023

Follows from #2528, and should complement #2411 to implement the module import hooks.

Similarly to the Intl/ICU4X PR (#2478), this has a lot of trivial changes caused by the new lifetimes. I thought about passing the queue and the hooks by value, but it was very painful having to wrap everything with Rc in order to be accessible by the host.
In contrast, &dyn can be easily provided by the host and has the advantage of not requiring additional allocations, with the downside of adding two more lifetimes to our Context, but I think it's worth.
I was able to unify all lifetimes into the shortest one of the three, making our API just like before!

Changes:

  • Added a new HostHooks trait and a &dyn HostHooks field to Context. This allows hosts to implement the trait for their custom type, then pass it to the context.
  • Added a new JobQueue trait and a &dyn JobQueue field to our Context, allowing custom event loops and other fun things.
  • Added two simple implementations of JobQueue: IdleJobQueue which does nothing and SimpleJobQueue which runs all jobs until all successfully complete or until any of them throws an error.
  • Modified boa_cli to run all jobs until the queue is empty, even if a job returns Err. This also prints all errors to the user.

@jedel1043 jedel1043 added enhancement New feature or request execution Issues or PRs related to code execution API labels Jan 11, 2023
@jedel1043 jedel1043 added this to the v0.17.0 milestone Jan 11, 2023
@jedel1043 jedel1043 added the run-benchmark Label used to run banchmarks on PRs label Jan 11, 2023
@github-actions
Copy link

github-actions bot commented Jan 11, 2023

Test262 conformance changes

Test result main count PR count difference
Total 94,205 94,205 0
Passed 70,616 70,616 0
Ignored 18,622 18,622 0
Failed 4,967 4,967 0
Panics 0 0 0
Conformance 74.96% 74.96% 0.00%

@github-actions
Copy link

Benchmark for 754d5b9

Click to view benchmark
Test Base PR %
Arithmetic operations (Compiler) 521.1±1.62ns 525.6±0.81ns +0.86%
Arithmetic operations (Execution) 404.7±0.41ns 413.5±0.33ns +2.17%
Arithmetic operations (Parser) 7.8±0.03µs 7.7±0.03µs -1.28%
Array access (Compiler) 1499.2±1.31ns 1496.5±1.44ns -0.18%
Array access (Execution) 9.8±0.02µs 9.9±0.05µs +1.02%
Array access (Parser) 15.9±0.05µs 16.0±0.06µs +0.63%
Array creation (Compiler) 2.4±0.01µs 2.4±0.01µs 0.00%
Array creation (Execution) 1387.6±2.74µs 1362.5±2.05µs -1.81%
Array creation (Parser) 18.8±0.03µs 18.8±0.06µs 0.00%
Array pop (Compiler) 4.3±0.01µs 4.3±0.01µs 0.00%
Array pop (Execution) 831.0±8.29µs 849.7±8.66µs +2.25%
Array pop (Parser) 171.3±0.12µs 175.3±0.14µs +2.34%
Boolean Object Access (Compiler) 1263.9±17.25ns 1259.6±9.11ns -0.34%
Boolean Object Access (Execution) 5.5±0.02µs 5.5±0.01µs 0.00%
Boolean Object Access (Parser) 19.4±0.05µs 19.5±0.03µs +0.52%
Clean js (Compiler) 5.1±0.01µs 5.1±0.01µs 0.00%
Clean js (Execution) 780.1±6.64µs 785.8±4.73µs +0.73%
Clean js (Parser) 38.9±0.05µs 39.3±0.05µs +1.03%
Create Realm 287.4±0.28ns 293.5±0.22ns +2.12%
Dynamic Object Property Access (Compiler) 1746.4±3.62ns 1761.6±5.03ns +0.87%
Dynamic Object Property Access (Execution) 5.4±0.01µs 5.3±0.04µs -1.85%
Dynamic Object Property Access (Parser) 14.3±0.03µs 14.3±0.03µs 0.00%
Fibonacci (Compiler) 2.8±0.00µs 2.8±0.00µs 0.00%
Fibonacci (Execution) 1179.3±1.91µs 1178.3±1.02µs -0.08%
Fibonacci (Parser) 22.2±0.03µs 22.2±0.03µs 0.00%
For loop (Compiler) 2.7±0.01µs 2.7±0.00µs 0.00%
For loop (Execution) 16.4±0.09µs 16.3±0.04µs -0.61%
For loop (Parser) 19.3±0.03µs 19.2±0.06µs -0.52%
Mini js (Compiler) 4.4±0.01µs 4.5±0.01µs +2.27%
Mini js (Execution) 730.1±5.77µs 737.5±4.90µs +1.01%
Mini js (Parser) 34.1±0.08µs 34.1±0.04µs 0.00%
Number Object Access (Compiler) 1117.8±1.35ns 1119.8±1.48ns +0.18%
Number Object Access (Execution) 4.2±0.01µs 4.1±0.01µs -2.38%
Number Object Access (Parser) 15.0±0.08µs 15.2±0.03µs +1.33%
Object Creation (Compiler) 1499.7±1.32ns 1497.6±1.88ns -0.14%
Object Creation (Execution) 5.1±0.02µs 5.0±0.02µs -1.96%
Object Creation (Parser) 12.5±0.02µs 12.5±0.02µs 0.00%
RegExp (Compiler) 1765.9±3.59ns 1764.2±6.77ns -0.10%
RegExp (Execution) 14.2±0.04µs 14.4±0.04µs +1.41%
RegExp (Parser) 13.7±0.08µs 13.6±0.04µs -0.73%
RegExp Creation (Compiler) 1571.0±4.53ns 1552.7±4.18ns -1.16%
RegExp Creation (Execution) 9.9±0.02µs 9.9±0.02µs 0.00%
RegExp Creation (Parser) 11.5±0.03µs 11.4±0.04µs -0.87%
RegExp Literal (Compiler) 1762.8±5.09ns 1764.3±5.97ns +0.09%
RegExp Literal (Execution) 14.1±0.03µs 14.4±0.05µs +2.13%
RegExp Literal (Parser) 11.1±0.04µs 11.0±0.04µs -0.90%
RegExp Literal Creation (Compiler) 1571.3±4.04ns 1561.6±4.60ns -0.62%
RegExp Literal Creation (Execution) 9.9±0.04µs 9.9±0.03µs 0.00%
RegExp Literal Creation (Parser) 8.8±0.02µs 8.6±0.03µs -2.27%
Static Object Property Access (Compiler) 1537.4±3.48ns 1540.7±5.22ns +0.21%
Static Object Property Access (Execution) 5.3±0.02µs 5.2±0.02µs -1.89%
Static Object Property Access (Parser) 13.5±0.03µs 13.4±0.02µs -0.74%
String Object Access (Compiler) 1522.4±3.32ns 1515.7±4.73ns -0.44%
String Object Access (Execution) 8.1±0.06µs 7.8±0.02µs -3.70%
String Object Access (Parser) 19.0±0.03µs 19.2±0.04µs +1.05%
String comparison (Compiler) 2.4±0.01µs 2.4±0.01µs 0.00%
String comparison (Execution) 4.7±0.01µs 4.5±0.01µs -4.26%
String comparison (Parser) 15.2±0.04µs 15.1±0.02µs -0.66%
String concatenation (Compiler) 1790.6±3.65ns 1788.8±8.07ns -0.10%
String concatenation (Execution) 4.4±0.03µs 4.3±0.01µs -2.27%
String concatenation (Parser) 10.6±0.03µs 10.5±0.01µs -0.94%
String copy (Compiler) 1440.0±1.64ns 1432.4±3.12ns -0.53%
String copy (Execution) 4.1±0.02µs 4.1±0.01µs 0.00%
String copy (Parser) 8.0±0.03µs 8.0±0.02µs 0.00%
Symbols (Compiler) 1113.6±1.64ns 1113.5±4.20ns -0.01%
Symbols (Execution) 4.2±0.05µs 4.1±0.01µs -2.38%
Symbols (Parser) 6.2±0.02µs 6.2±0.04µs 0.00%

@jedel1043 jedel1043 removed the run-benchmark Label used to run banchmarks on PRs label Jan 11, 2023
@jedel1043
Copy link
Member Author

I thought a bit more about the design of this and I think I can unify the three lifetimes into only one with some lifetime trickery, making this PR a lot more lightweight! I'll make the fix today.

@codecov
Copy link

codecov bot commented Jan 11, 2023

Codecov Report

Merging #2529 (0f76a12) into main (20af6a7) will increase coverage by 1.14%.
The diff coverage is 53.67%.

@@            Coverage Diff             @@
##             main    #2529      +/-   ##
==========================================
+ Coverage   49.92%   51.07%   +1.14%     
==========================================
  Files         377      374       -3     
  Lines       37557    36796     -761     
==========================================
+ Hits        18751    18793      +42     
+ Misses      18806    18003     -803     
Impacted Files Coverage Δ
boa_cli/src/main.rs 0.00% <ø> (-1.08%) ⬇️
boa_engine/src/builtins/async_generator/mod.rs 8.00% <0.00%> (ø)
boa_engine/src/builtins/eval/mod.rs 4.21% <0.00%> (-0.05%) ⬇️
boa_engine/src/bytecompiler/mod.rs 58.10% <ø> (ø)
boa_engine/src/class.rs 0.00% <0.00%> (ø)
boa_engine/src/error.rs 37.50% <ø> (ø)
boa_engine/src/native_function.rs 70.00% <ø> (+31.11%) ⬆️
boa_engine/src/object/operations.rs 49.14% <0.00%> (+0.14%) ⬆️
boa_engine/src/context/mod.rs 59.64% <45.45%> (-3.55%) ⬇️
boa_engine/src/context/hooks.rs 50.00% <50.00%> (ø)
... and 21 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

boa_engine/src/job.rs Outdated Show resolved Hide resolved
@jedel1043 jedel1043 requested a review from nekevss January 12, 2023 05:05
@jasonwilliams
Copy link
Member

👀

Copy link
Member

@raskad raskad left a comment

Choose a reason for hiding this comment

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

Nice work. A great step in the direction of making boa more configurable for users and allowing the host implementations that the spec lists.

Copy link
Member

@HalidOdat HalidOdat left a comment

Choose a reason for hiding this comment

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

Great job @jedel1043 , This looks perfect to me! :)

@jedel1043
Copy link
Member Author

bors r+

bors bot pushed a commit that referenced this pull request Jan 19, 2023
Follows from #2528, and should complement #2411 to implement the module import hooks.

~~Similarly to the Intl/ICU4X PR (#2478), this has a lot of trivial changes caused by the new lifetimes. I thought about passing the queue and the hooks by value, but it was very painful having to wrap everything with `Rc` in order to be accessible by the host.
In contrast, `&dyn` can be easily provided by the host and has the advantage of not requiring additional allocations, with the downside of adding two more lifetimes to our `Context`, but I think it's worth.~~ I was able to unify all lifetimes into the shortest one of the three, making our API just like before!

Changes:
- Added a new `HostHooks` trait and a `&dyn HostHooks` field to `Context`. This allows hosts to implement the trait for their custom type, then pass it to the context.
- Added a new `JobQueue` trait and a `&dyn JobQueue` field to our `Context`, allowing custom event loops and other fun things.
- Added two simple implementations of `JobQueue`: `IdleJobQueue` which does nothing and `SimpleJobQueue` which runs all jobs until all successfully complete or until any of them throws an error.
- Modified `boa_cli` to run all jobs until the queue is empty, even if a job returns `Err`. This also prints all errors to the user.
@bors
Copy link

bors bot commented Jan 19, 2023

Pull request successfully merged into main.

Build succeeded:

@bors bors bot changed the title Implement host hooks and job queues APIs [Merged by Bors] - Implement host hooks and job queues APIs Jan 19, 2023
@bors bors bot closed this Jan 19, 2023
@bors bors bot deleted the host-callbacks branch January 19, 2023 10:57
bors bot pushed a commit that referenced this pull request Feb 24, 2023
~~Builds off of #2529.~~ Merged.

This Pull Request allows passing any function returning `impl Future<Output = JsResult<JsValue>>` to the `NativeFunction` constructor, allowing native concurrency hooks into the engine.

It changes the following:

- Adds a `NativeFunction::from_async_fn` function.
- Adds a new `JobQueue::enqueue_future_job` method.
- Adds an example usage on `boa_examples`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API enhancement New feature or request execution Issues or PRs related to code execution
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants