Skip to content

Give host functions a hidden class with length and name - #2328

Open
tsapeta wants to merge 1 commit into
meta:static_hfrom
tsapeta:tsapeta/faster-host-function-creation
Open

tsapeta wants to merge 1 commit into
meta:static_hfrom
tsapeta:tsapeta/faster-host-function-creation

Conversation

@tsapeta

@tsapeta tsapeta commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Summary

FinalizableNativeFunction::createWithoutPrototype, which creates every JSI host function, added length and name with two defineNewOwnProperty calls. Each call did a hidden class transition and a slot allocation. In a profile of a loop that creates host functions, this was about a third of the time.

The runtime now builds this class once at init, with the same property flags and order as Callable::defineNameLengthAndPrototype, so host functions get the same class as before. A new host function starts with this class, and creation only writes the two slots.

  • With compressed pointers, the overlap slots use all the direct property slots, so length and name are in indirect storage. The function allocates it up front, with the default capacity. As in JSObject::allocateNewSlotStorage, an allocation failure is fatal, so createWithoutPrototype still never fails after it creates a cell that owns the context.
  • Building the class adds about 83 ns to runtime init. A warm makeHermesRuntime() takes about 265,000 ns.

Time to create one host function (Release build, Apple M2 Pro, median of 12 interleaved runs, each the median of 30 batches of 10,000 creations):

Case Before After Change
Small lambda 45.2 ns 27.8 ns -38%
Lambda with a 40-byte capture 98.5 ns 85.8 ns -13%
New PropNameID for each function 67.3 ns 48.5 ns -28%

The time to collect the functions stays within noise.

Test Plan

  • New HermesRuntimeTest.HostFunctionNameAndLengthTest checks the values, attributes, and order of length and name, properties added later, deleting and redefining both properties, a length of 0xffffffff, a non-ASCII name, and the values after a GC.
  • New HermesRuntimeSanitizeHandlesTest.HostFunctionNameAndLength creates host functions with a sanitize rate of 1.0, so a HERMESVM_SANITIZE_HANDLES build moves the young generation at every allocation. I checked that it fails when the function holds a raw pointer across the storage allocation (with HEAP_HV_PREFER32) or across the length encoding (with HEAP_HV_BOXED).
  • APITests pass in three ASan Debug builds: the default, HERMESVM_HEAP_HV_MODE=HEAP_HV_PREFER32 with HERMESVM_SANITIZE_HANDLES=ON, and HERMESVM_HEAP_HV_MODE=HEAP_HV_BOXED with HERMESVM_SANITIZE_HANDLES=ON.
  • check-hermes passes in the ASan Debug build.

@meta-cla meta-cla Bot added the CLA Signed Do not delete this pull request or issue due to inactivity. label Nov 20, 2026
@tsapeta
tsapeta force-pushed the tsapeta/faster-host-function-creation branch 2 times, most recently from 8e33162 to c71fa67 Compare November 20, 2026 15:40
@tsapeta
tsapeta marked this pull request as ready for review November 20, 2026 17:25
`FinalizableNativeFunction::createWithoutPrototype` added `length` and
`name` to every new host function with two `defineNewOwnProperty` calls.
Each call did a hidden class transition and a slot allocation, which was
about a third of the time to create a host function.

The runtime now builds this class once at init, with the same flags and
property order, and every new host function starts with it. Creation only
writes the two slots. With compressed pointers, the overlap slots use all
the direct slots, so the function allocates indirect storage for the two
properties up front.

Creating a host function with a small lambda goes from 42.5 ns to 21.8 ns
(Release build, Apple M2 Pro). Runtime init gets about 83 ns slower.
@tsapeta
tsapeta force-pushed the tsapeta/faster-host-function-creation branch from c71fa67 to 88342a2 Compare November 29, 2026 19:03

This branch was previously deployed

1 inactive deployment
npm-publish — 88342a2d Deployed Nov 29, 2026 by tsapeta via publish #1823
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed Do not delete this pull request or issue due to inactivity.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant