Skip to content

perf: specialize ArrowFunction object to bypass generic call overhead#5273

Open
MayankRaj435 wants to merge 2 commits intoboa-dev:mainfrom
MayankRaj435:perf/arrow-function-specialization
Open

perf: specialize ArrowFunction object to bypass generic call overhead#5273
MayankRaj435 wants to merge 2 commits intoboa-dev:mainfrom
MayankRaj435:perf/arrow-function-specialization

Conversation

@MayankRaj435
Copy link
Copy Markdown
Contributor

This PR introduces a dedicated ArrowFunction struct that implements a specialized __call__ handler, bypassing the constructor check and this-binding resolution that are unnecessary for arrow functions.

Motivation: This directly addresses the discussion in #5264 about optimizing function calls at the object layer rather than the VM/opcode layer, as suggested by @jedel1043.

Changes

core/engine/src/builtins/function/mod.rs

  • New ArrowFunction struct: A lightweight alternative to OrdinaryFunction that stores only the fields relevant to arrow functions (code, environments, home object, realm, script/module).
  • Specialized arrow_function_call handler: Skips the is_class_constructor check and this-binding resolution. Arrow functions are never constructors and always use lexical this, so these checks are pure overhead.

core/engine/src/vm/code_block.rs

  • Updated create_function_object and create_function_object_fast to detect arrow functions (code.is_arrow()) and instantiate the specialized ArrowFunction instead of the generic OrdinaryFunction.

Benchmarks

Microbenchmark: 100 million arrow function calls in release mode.

Metric Baseline (OrdinaryFunction) Optimized (ArrowFunction) Improvement
Execution Time 21.0s 15.6s ~26% Speedup

Design Rationale

Per the discussion in #5264, I measured size_of::<Instruction>() at 24 bytes. Adding new opcodes to the VM dispatch table risks increasing the Instruction enum size and causing D-cache pressure. This object-layer approach achieves comparable performance gains while keeping the VM instruction set clean.

@MayankRaj435 MayankRaj435 requested a review from a team as a code owner March 28, 2026 07:19
@github-actions github-actions bot added the Waiting On Review Waiting on reviews from the maintainers label Mar 28, 2026
@github-actions github-actions bot added this to the v1.0.0 milestone Mar 28, 2026
@github-actions github-actions bot added C-Builtins PRs and Issues related to builtins/intrinsics C-VM Issues and PRs related to the Boa Virtual Machine. labels Mar 28, 2026
@github-actions
Copy link
Copy Markdown

github-actions bot commented Mar 28, 2026

Test262 conformance changes

Test result main count PR count difference
Total 52,963 52,963 0
Passed 50,732 50,732 0
Ignored 1,426 1,426 0
Failed 805 805 0
Panics 0 0 0
Conformance 95.79% 95.79% 0.00%

Tested main commit: f075094f9674f9919b4d2e85ca1bcba410bf34b0
Tested PR commit: 9dd693a764f5a1eae89b287608f6ef1338ed0354
Compare commits: f075094...9dd693a

Introduce a dedicated ArrowFunction struct that implements a specialized
__call__ handler, bypassing the constructor check and this-binding
resolution that are unnecessary for arrow functions.

Arrow functions:
- Are never constructors (skip is_class_constructor check)
- Always have lexical this (skip this-binding resolution)
- Don't need [[Fields]] or [[PrivateMethods]] slots

This reduces the per-call overhead for arrow functions by ~26% in
microbenchmarks (100M calls: 21.0s -> 15.6s in release mode).

The approach follows the maintainer-recommended strategy of specializing
at the function object layer rather than adding new VM opcodes, keeping
the Instruction enum lean (24 bytes) and avoiding dispatch sensitivity.
- Implement ScriptFunction trait for polymorphic ArrowFunction/OrdinaryFunction access
- Refactor all downcast_ref sites (toString, function_call, get_function_realm, etc.)
- Update GetHomeObject/SetHomeObject opcodes for ArrowFunction support
- Fix perform_eval and has_super_binding for ArrowFunction
- Apply cargo fmt formatting
@MayankRaj435 MayankRaj435 force-pushed the perf/arrow-function-specialization branch from edc206c to 9dd693a Compare March 28, 2026 17:37
@MayankRaj435
Copy link
Copy Markdown
Contributor Author

@jedel1043 sir please review it at your convenience ,thanks

@codecov
Copy link
Copy Markdown

codecov bot commented Mar 28, 2026

Codecov Report

❌ Patch coverage is 69.82249% with 51 lines in your changes missing coverage. Please review.
✅ Project coverage is 59.69%. Comparing base (6ddc2b4) to head (9dd693a).
⚠️ Report is 921 commits behind head on main.

Files with missing lines Patch % Lines
core/engine/src/builtins/function/mod.rs 65.65% 34 Missing ⚠️
core/engine/src/vm/code_block.rs 84.61% 6 Missing ⚠️
core/engine/src/vm/opcode/function.rs 64.28% 5 Missing ⚠️
core/engine/src/builtins/eval/mod.rs 66.66% 3 Missing ⚠️
...e/src/environments/runtime/declarative/function.rs 50.00% 3 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##             main    #5273       +/-   ##
===========================================
+ Coverage   47.24%   59.69%   +12.45%     
===========================================
  Files         476      589      +113     
  Lines       46892    63595    +16703     
===========================================
+ Hits        22154    37966    +15812     
- Misses      24738    25629      +891     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@github-actions github-actions bot added C-Tests Issues and PRs related to the tests. C-Javascript Pull requests that update Javascript code labels Mar 29, 2026
@MayankRaj435 MayankRaj435 force-pushed the perf/arrow-function-specialization branch from a84fd0a to 9dd693a Compare March 29, 2026 08:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C-Builtins PRs and Issues related to builtins/intrinsics C-Javascript Pull requests that update Javascript code C-Tests Issues and PRs related to the tests. C-VM Issues and PRs related to the Boa Virtual Machine. Waiting On Review Waiting on reviews from the maintainers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant