Skip to content

ffi: shrink trampoline placement probe window - #64969

Open
trivikr wants to merge 1 commit into
nodejs:mainfrom
trivikr:ffi-allocate-code-near-probe
Open

ffi: shrink trampoline placement probe window#64969
trivikr wants to merge 1 commit into
nodejs:mainfrom
trivikr:ffi-allocate-code-near-probe

Conversation

@trivikr

@trivikr trivikr commented Aug 2, 2026

Copy link
Copy Markdown
Member

Fixes: #64968

AllocateCodeNear() searched 1024 pages in each direction for a free page next to the native target, one MAP_FIXED_NOREPLACE mmap per candidate. Every probe is expected to fail, so an exhausted window cost up to 2048 failing syscalls before taking the plain-mmap fallback that was already there, all to enable a jmp rel32 instead of movabs+jmp. Resolving a fast-eligible signature plateaued at ~790us, against ~28us for one that allocates no trampoline.

Reduce the window to 16 pages per direction, bounding the exhausted case to 32 probes while still covering typical per-library symbol counts. Placement stays opportunistic: the caller already emits the absolute form when EmitJmpRel32() reports the target is out of range.

Add benchmark/ffi/get-function.js, which measures resolution rather than call throughput, so this path has coverage. Its fast arm reports 1,852 ops/sec at the old window and 37,836 at the new one, with the fast-ineligible control arm flat at ~80,000.

                                            confidence improvement accuracy (*)    (**)   (***)
ffi/get-function.js n=1000 signature='fast'        ***   2172.59 %      ±47.69% ±64.14% ±84.87%
ffi/get-function.js n=1000 signature='slow'                 3.85 %       ±4.73%  ±6.32%  ±8.27%

Assisted-by: claude:opus-5

AllocateCodeNear() searched 1024 pages in each direction for a free
page next to the native target, one MAP_FIXED_NOREPLACE mmap per
candidate. Every probe is expected to fail, so an exhausted window cost
up to 2048 failing syscalls before taking the plain-mmap fallback that
was already there, all to enable a jmp rel32 instead of movabs+jmp.
Resolving a fast-eligible signature plateaued at ~790us, against ~28us
for one that allocates no trampoline.

Reduce the window to 16 pages per direction, bounding the exhausted
case to 32 probes while still covering typical per-library symbol
counts. Placement stays opportunistic: the caller already emits the
absolute form when EmitJmpRel32() reports the target is out of range.

Signed-off-by: Trivikram Kamat <16024985+trivikr@users.noreply.github.com>
Assisted-by: claude:opus-5
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/ffi
  • @nodejs/performance

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run. labels Aug 2, 2026
@trivikr trivikr added ffi Issues and PRs related to experimental Foreign Function Interface support. request-ci Add this label to start a Jenkins CI on a PR. labels Aug 2, 2026
@codecov

codecov Bot commented Aug 2, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.27%. Comparing base (f43086d) to head (aa9a075).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #64969   +/-   ##
=======================================
  Coverage   90.27%   90.27%           
=======================================
  Files         762      762           
  Lines      247515   247515           
  Branches    46683    46681    -2     
=======================================
  Hits       223447   223447           
- Misses      15485    15491    +6     
+ Partials     8583     8577    -6     

see 30 files with indirect coverage changes

🚀 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.

@trivikr trivikr added the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label Aug 3, 2026
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Aug 3, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

author ready PRs that have at least one approval, no pending requests for changes, and a CI started. c++ Issues and PRs that require attention from people who are familiar with C++. ffi Issues and PRs related to experimental Foreign Function Interface support. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ffi: AllocateCodeNear() probes up to 2048 pages with a failing mmap each, costing ~790 µs per trampoline

3 participants