Repository navigation
Conversation
tsapeta
force-pushed
the
tsapeta/faster-host-function-creation
branch
2 times, most recently
from
November 20, 2026 15:40
8e33162 to
c71fa67
Compare
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
force-pushed
the
tsapeta/faster-host-function-creation
branch
from
November 29, 2026 19:03
c71fa67 to
88342a2
Compare
This branch was previously deployed
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
FinalizableNativeFunction::createWithoutPrototype, which creates every JSI host function, addedlengthandnamewith twodefineNewOwnPropertycalls. 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.lengthandnameare in indirect storage. The function allocates it up front, with the default capacity. As inJSObject::allocateNewSlotStorage, an allocation failure is fatal, socreateWithoutPrototypestill never fails after it creates a cell that owns the context.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):
PropNameIDfor each functionThe time to collect the functions stays within noise.
Test Plan
HermesRuntimeTest.HostFunctionNameAndLengthTestchecks the values, attributes, and order oflengthandname, properties added later, deleting and redefining both properties, alengthof0xffffffff, a non-ASCII name, and the values after a GC.HermesRuntimeSanitizeHandlesTest.HostFunctionNameAndLengthcreates host functions with a sanitize rate of 1.0, so aHERMESVM_SANITIZE_HANDLESbuild moves the young generation at every allocation. I checked that it fails when the function holds a raw pointer across the storage allocation (withHEAP_HV_PREFER32) or across the length encoding (withHEAP_HV_BOXED).APITestspass in three ASan Debug builds: the default,HERMESVM_HEAP_HV_MODE=HEAP_HV_PREFER32withHERMESVM_SANITIZE_HANDLES=ON, andHERMESVM_HEAP_HV_MODE=HEAP_HV_BOXEDwithHERMESVM_SANITIZE_HANDLES=ON.check-hermespasses in the ASan Debug build.