Skip to content

perf: cut first-window, ipc, contextBridge, Buffer and asar overhead - #52923

Merged
jkleinsc merged 8 commits into
mainfrom
perf/launch-ipc-asar-wins
Aug 18, 2026
Merged

jkleinsc merged 8 commits into
mainfrom
perf/launch-ipc-asar-wins

Conversation

@codebytere

@codebytere codebytere commented Aug 17, 2026 •

Copy link
Copy Markdown
Member

Description of Change

Eight independent commits, batched to be easier on CI. Together on a Linux test app (main process loading ~1000 modules from app.asar, one window with a sandboxed preload): launch to first contentful paint 633 -> 452 ms, MB-scale IPC a third cheaper in both directions, bridged calls 25-57% cheaper, Buffer.allocUnsafe 6-80x cheaper, large files from the asar to the renderer as fast as plain file://. Each commit stands alone; its message carries the mechanism and the full numbers. Linux x64 release build, medians of interleaved fresh-process runs, all p < 0.01.

commit what gets faster before -> after
preallocate the file descriptor table before threads start (linux) first new BrowserWindow() on Linux; launch to first contentful paint 100 -> 18 ms; 610 -> 525 ms
stop rebuilding the whole path per component in asar::GetAsarArchivePath every fs lookup on a path inside an .asar (asar.splitPath()); launch to ready for the test app 5.3 -> 1.3 us; 393 -> 347 ms
keep context bridge proxy function state in the function's data array + trim per-value work when the bridge copies objects and arrays calls through contextBridge-exposed APIs: empty call / callback round trip / 400-item list 0.30 -> 0.13 us / 2.7 -> 1.9 us / 757 -> 684 us
give V8's in-sandbox allocator a PartitionAlloc backend outside renderers Buffer.allocUnsafe 1 MB; fs.promises.readFile 1 MB in the main process 297 -> 36 us; 1.2 -> 0.9 ms
serialize ipc values straight into the buffer they are sent in 1 MB ipcRenderer.invoke round trip; main -> renderer 1 MB; 4 MB round trip 4.2 -> 2.8 ms; 2.7 -> 1.6 ms; 14.2 -> 9.2 ms
size protocol and asar response pipes to the body 16 MB file from an .asar to the renderer; 16 MB protocol.handle body with a Content-Length 35 -> 22 ms; 49 -> 40 ms
remember module stat results for paths inside an archive repeated internalModuleStat misses during require() from an archive (1040 modules) 250 -> 245 ms

Per commit, briefly:

  • fd table (Linux only). The browser crosses 64 open descriptors while setting up the first renderer, and Linux's expand_fdtable() waits a full RCU grace period before freeing the old table once the process has threads; that wait was 55-116 ms of uninterruptible sleep inside window creation on the test host (10-30 ms is typical on a workstation). content's zygote already grows the table for the children it forks; main() now does the same for the browser and exec'd children with one F_DUPFD_CLOEXEC while still single-threaded (1024 entries, 8 KB).

  • GetAsarArchivePath. Walks component boundaries as offsets instead of DirName()/MatchesExtension() per component plus GetComponents() twice; byte-identical outputs, three new asar-spec cases for the corner cases.

  • contextBridge. Proxy function state moves from three private-property lookups per call to a three-slot array in the function's data; per copied value, the inner trace event goes, the Blink wrapper probes only run for API wrappers, one scope pair per property, bulk v8::Array::New. What is copied or proxied and all error paths are unchanged.

  • in-sandbox allocator. Node allocates unpooled Buffers through V8's default in-sandbox allocator, which V8 documents as a slow placeholder (page-granular, global mutex, zero-fills everything); Blink replaces it for renderers, the browser/utility/run-as-node processes never did. JavascriptEnvironment now installs one backed by gin's array-buffer partition via v8::V8::SetInSandboxAllocator, with allocations PartitionAlloc refuses (~2 GiB and up) served from the sandbox address space so huge Buffer.allocUnsafe calls keep working. Give-back: page zeroing moves to first touch, so Buffer.concat of MB inputs measures a few percent slower, which is upstream Node's behavior.

  • ipc payloads. Electron's own IPC mojom switches from blink.mojom.CloneableMessage to electron.mojom.SerializedValue (BigBuffer + length): the V8 serializer writes large values straight into the shared memory region that is sent, and a renderer parses a received region in place; the browser still parses a private copy because the sender keeps write access to shared memory. New api-ipc spec round-trips payloads across the 64 KB threshold up to 3 MB. Small messages, MessagePort traffic and startup unchanged.

  • response pipes. Electron's URL loaders used mojo's 64 KB default pipe (the asar loader a hard-coded 65536); where the body length is known before the pipe is created it is sized to the body with upstream's caps (2 MB for archive entries like file://, 512 KB for string/buffer and Content-Length responses); unknown-length streams and small responses keep exactly the pipe they had.

  • module stat cache. Node's own statCache only lives for one depth-0 execution, so repeated internalModuleStat misses on archive paths across separate require chains re-probe the archive; a bounded map keyed by the exact path remembers answers derived from an already-open (immutable) header. Small on a single startup graph, useful for lazy-require patterns.

Testing: full main-process suite identical to the baseline before/after with all seven applied; ASAN pass over the C++ commits clean; the asar and ipc commits add specs.

Checklist

Release Notes

Notes:

  • Fixed a stall of tens of milliseconds while creating the first BrowserWindow on Linux.
  • Improved the performance of file lookups inside ASAR archives and of large files served from them to the renderer.
  • Improved the performance of calls through contextBridge-exposed APIs.
  • Improved the performance of Buffer.allocUnsafe() and of large file reads in the main and utility processes.
  • Reduced the main-thread cost of sending large values through ipcRenderer, ipcMain handlers and webContents.send.

@MarshallOfSound MarshallOfSound left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Went through the seven commits with a fine tooth comb, one thing on the fd table commit, the rest looks fine to me.

Comment thread shell/app/electron_main_delegate.cc Resolved
@codebytere
codebytere force-pushed the perf/launch-ipc-asar-wins branch from 7d5b0bf to b80c25a Compare August 18, 2026 05:57
@codebytere
codebytere requested a review from deepak1556 August 18, 2026 07:41

@MarshallOfSound MarshallOfSound left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Three more, all perf-in-a-corner rather than correctness, none blocking.

Comment thread shell/browser/net/node_stream_loader.cc Resolved
Comment thread shell/common/v8_util.cc Resolved
Comment thread shell/browser/javascript_environment.cc Resolved
Comment thread shell/browser/javascript_environment.cc Resolved
Linux grows a process's file descriptor table in powers of two on demand
and, once the table is shared with other threads, expand_fdtable() waits
for a full RCU grace period before freeing the old table. Whichever
open()/socket()/memfd call crosses the 64-descriptor boundary in an
already multi-threaded process eats that wait; in the browser process it
lands inside the first `new BrowserWindow` (the renderer's channel and
shared memory setup) and measured 55-115 ms of uninterruptible sleep in
openat() on a 64-vCPU host, 10-30 ms being typical elsewhere.

content's zygote already grows the table of every child it forks for this
reason (content/zygote/zygote_linux.cc). Do the same in main() for the
browser and the exec'd children: one F_DUPFD_CLOEXEC to descriptor 512
while the process is still single-threaded leaves a 1024-entry table
(8 KB) behind and nothing else. Where RLIMIT_NOFILE is lower the fcntl
fails and nothing changes.
…chivePath

GetAsarArchivePath() located the archive by calling DirName() once per
path component and MatchesExtension() on each intermediate path (a
BaseName() copy every time), then split the result with
AppendRelativePath(), which runs GetComponents() over both the parent
and the child. For a file inside app.asar that is dozens of string
allocations per lookup, and the fs wrapper performs this lookup
(asar.splitPath) for every fs call whose path mentions ".asar" - around
ten per require()'d module.

Scan the component boundaries as offsets into the existing string
instead, hand only the candidate component to MatchesExtension(), and
build the relative part with Append() from the components after the
match. The outputs are byte-identical to before, including the existing
corner cases (a real directory named *.asar is not an archive, the
deepest matching component wins, doubled separators and a leading "."
collapse the way Append() collapses them, a trailing separator on the
archive path itself is kept); new asar-spec cases pin those down.
… array

The proxy that contextBridge installs for a function carried its target,
receiver and dynamic-property flag as three private properties on a state
object, so every call through the bridge did three v8::Private::ForApi
lookups (each allocating the key string) plus three GetPrivate calls
before it could invoke anything. Store the three values in a packed
v8::Array handed to v8::Function::New as the callback data and read them
back by index. The array is only reachable through the function's data
slot, exactly like the old state object, so nothing new becomes visible
to either world; the original-function marker on the proxy itself stays a
private property because that one has to live on a script-visible
object.
…arrays

Copying a plain object or array across the bridge visits every value, and
each visit paid for a trace event, three Blink wrapper probes
(WebElement, WebBlob, VideoFrame) that can only ever match API wrapper
objects, and, for object properties, an extra destination Context::Scope
enter/exit on top of the source one. Arrays were also filled with one
Set() per element.

Skip the Blink probes unless the value is an API wrapper object, drop the
inner trace event (the entry points keep theirs), read each property
under the source scope and define it under the destination scope that is
already active, and build array clones with the bulk v8::Array::New. The
values produced, the object cache and the recursion order are unchanged.
@codebytere
codebytere force-pushed the perf/launch-ipc-asar-wins branch from b80c25a to bc4bb4f Compare August 18, 2026 10:20
… renderers

Node allocates Buffer.allocUnsafe(), Buffer.allocUnsafeSlow() and the
64 KB Buffer pool refill through ArrayBuffer::Allocator::NewDefaultAllocator()
(Electron's sandboxed-pointers patch), which under the V8 sandbox forwards
to V8's built-in in-sandbox allocator. V8 documents that allocator as a slow
placeholder embedders are expected to replace: it hands out page-granular
regions under a global mutex and zero-fills even "uninitialized" requests.
Blink replaces it for renderers (V8Initializer::InitializeInSandboxAllocator);
the browser, utility and ELECTRON_RUN_AS_NODE processes never did, so every
unpooled Buffer there paid a map/unmap round trip: Buffer.allocUnsafe of
1 MB ~300 us against ~12 us in a stock Node build, 64 KB ~30 us against
~2 us, and fs.readFile of a 1 MB file was several times slower than
reading into a preallocated buffer.

JavascriptEnvironment now installs an allocator with
v8::V8::SetInSandboxAllocator() before the isolate is created, backed by
gin's array buffer partition (which already lives inside the sandbox) the
same way Blink's is backed by its ArrayBufferContents partition.
PartitionAlloc refuses single allocations of ~2 GiB and more while the
placeholder had no such cap, so requests above PartitionAlloc's limit go to
the sandbox's address space directly (checked up front, so V8 never sees a
failed allocation to retry) and Buffer.allocUnsafe of 2 GiB and more keeps
working. Measured in the main process: allocUnsafe 64 KB 33 -> 5.6 us,
1 MB 297 -> 36 us, 4 MB 1691 -> 41 us, allocUnsafeSlow 1 MB 218 -> 13 us,
fs.promises.readFile 1 MB 1.2 -> 0.9 ms. Page zeroing moves from the
allocator to first touch, so a Buffer that is immediately written end to
end pays the same faults later instead.
Values passed through ipcRenderer.send/invoke/sendSync/sendToHost,
webContents.send, webFrameMain.send and their service worker equivalents
travel as blink.mojom.CloneableMessage. For payloads above mojo's 64 KB
inline limit that costs three fresh multi-megabyte buffers per hop: the
V8 serializer grows a zero-filled std::vector, mojo copies that into a
newly created shared memory region, and the receiving traits copy the
region back out into another vector before V8 reads a byte. In profiles
of 1-4 MB messages both main threads spent 30-40% of their time in page
faults for those buffers.

Electron owns both ends of these interfaces, so give them their own
payload type: electron.mojom.SerializedValue is a BigBuffer plus the
number of bytes written. The serializer keeps small values on the heap as
before and, once V8 asks for more than the inline limit, continues in the
shared memory region that will be sent, so the sender never holds a
second copy. On the receiving side a renderer parses the region in
place; the browser (and any other process) still parses a private copy,
because the sender keeps write access to shared memory while it is read.

MessagePort/postMessage paths keep blink's TransferableMessage, and the
CloneableMessage overloads stay for in-process users (contextBridge,
app 'second-instance' data).

The serializer moves to shared memory once V8's request shows the value
needs more than an inline message (not merely when the doubled request
crosses 64 KB), and grows regions 4x from there, so a value written in
many small pieces creates two or three regions rather than one per
doubling; each region is a brokered round trip for a sandboxed renderer
on Linux. Object-heavy payloads (arrays of small records) measure
renderer -> main 1 MB 10.2 -> 10.2 ms and 4 MB 41.5 -> 39.9 ms with this,
against +8% / +3% with per-doubling regions.
Electron's URL loaders created their response data pipes with mojo's
64 KB default (the asar loader with a hard-coded 65536 copied from an old
FileURLLoader), so every response larger than that reached the renderer
in 64 KB slices, each one a producer wakeup on the browser main thread
plus a consumer wakeup in the renderer. Chromium since sized these up:
network::GetDataPipeDefaultAllocationSize() is 512 KB for ordinary loads
and 2 MB (kLargerSizeIfPossible) for file://.

A pipe is a shared memory buffer that base preallocates in full, though,
so a uniformly larger pipe makes every small response measurably slower
for the sake of the large ones. Instead, where the body size is known up
front, size the pipe to the body with the same caps upstream uses: archive
entries (file-like) up to the 2 MB file:// size, string/buffer responses
and any protocol response that declares a Content-Length up to the
512 KB default. Streams of unknown length keep mojo's default. Small
responses keep exactly the pipe they had; a 16 MB archive entry reaches
the renderer in 8 slices instead of 256, a 16 MB protocol body with a
known length in 32.
@trop

trop Bot commented Aug 19, 2026

Copy link
Copy Markdown
Contributor

@codebytere has manually backported this PR to "44-x-y", please check out #53017

@trop

trop Bot commented Aug 19, 2026

Copy link
Copy Markdown
Contributor

@codebytere has manually backported this PR to "43-x-y", please check out #53018

@trop

trop Bot commented Aug 19, 2026

Copy link
Copy Markdown
Contributor

@codebytere has manually backported this PR to "42-x-y", please check out #53019

codebytere added a commit that referenced this pull request Aug 19, 2026
…52923)

* perf: preallocate the file descriptor table before threads start (linux)

Linux grows a process's file descriptor table in powers of two on demand
and, once the table is shared with other threads, expand_fdtable() waits
for a full RCU grace period before freeing the old table. Whichever
open()/socket()/memfd call crosses the 64-descriptor boundary in an
already multi-threaded process eats that wait; in the browser process it
lands inside the first `new BrowserWindow` (the renderer's channel and
shared memory setup) and measured 55-115 ms of uninterruptible sleep in
openat() on a 64-vCPU host, 10-30 ms being typical elsewhere.

content's zygote already grows the table of every child it forks for this
reason (content/zygote/zygote_linux.cc). Do the same in main() for the
browser and the exec'd children: one F_DUPFD_CLOEXEC to descriptor 512
while the process is still single-threaded leaves a 1024-entry table
(8 KB) behind and nothing else. Where RLIMIT_NOFILE is lower the fcntl
fails and nothing changes.

* perf: stop rebuilding the whole path per component in asar::GetAsarArchivePath

GetAsarArchivePath() located the archive by calling DirName() once per
path component and MatchesExtension() on each intermediate path (a
BaseName() copy every time), then split the result with
AppendRelativePath(), which runs GetComponents() over both the parent
and the child. For a file inside app.asar that is dozens of string
allocations per lookup, and the fs wrapper performs this lookup
(asar.splitPath) for every fs call whose path mentions ".asar" - around
ten per require()'d module.

Scan the component boundaries as offsets into the existing string
instead, hand only the candidate component to MatchesExtension(), and
build the relative part with Append() from the components after the
match. The outputs are byte-identical to before, including the existing
corner cases (a real directory named *.asar is not an archive, the
deepest matching component wins, doubled separators and a leading "."
collapse the way Append() collapses them, a trailing separator on the
archive path itself is kept); new asar-spec cases pin those down.

* perf: keep context bridge proxy function state in the function's data array

The proxy that contextBridge installs for a function carried its target,
receiver and dynamic-property flag as three private properties on a state
object, so every call through the bridge did three v8::Private::ForApi
lookups (each allocating the key string) plus three GetPrivate calls
before it could invoke anything. Store the three values in a packed
v8::Array handed to v8::Function::New as the callback data and read them
back by index. The array is only reachable through the function's data
slot, exactly like the old state object, so nothing new becomes visible
to either world; the original-function marker on the proxy itself stays a
private property because that one has to live on a script-visible
object.

* perf: trim per-value work when the context bridge copies objects and arrays

Copying a plain object or array across the bridge visits every value, and
each visit paid for a trace event, three Blink wrapper probes
(WebElement, WebBlob, VideoFrame) that can only ever match API wrapper
objects, and, for object properties, an extra destination Context::Scope
enter/exit on top of the source one. Arrays were also filled with one
Set() per element.

Skip the Blink probes unless the value is an API wrapper object, drop the
inner trace event (the entry points keep theirs), read each property
under the source scope and define it under the destination scope that is
already active, and build array clones with the bulk v8::Array::New. The
values produced, the object cache and the recursion order are unchanged.

* perf: give V8's in-sandbox allocator a PartitionAlloc backend outside renderers

Node allocates Buffer.allocUnsafe(), Buffer.allocUnsafeSlow() and the
64 KB Buffer pool refill through ArrayBuffer::Allocator::NewDefaultAllocator()
(Electron's sandboxed-pointers patch), which under the V8 sandbox forwards
to V8's built-in in-sandbox allocator. V8 documents that allocator as a slow
placeholder embedders are expected to replace: it hands out page-granular
regions under a global mutex and zero-fills even "uninitialized" requests.
Blink replaces it for renderers (V8Initializer::InitializeInSandboxAllocator);
the browser, utility and ELECTRON_RUN_AS_NODE processes never did, so every
unpooled Buffer there paid a map/unmap round trip: Buffer.allocUnsafe of
1 MB ~300 us against ~12 us in a stock Node build, 64 KB ~30 us against
~2 us, and fs.readFile of a 1 MB file was several times slower than
reading into a preallocated buffer.

JavascriptEnvironment now installs an allocator with
v8::V8::SetInSandboxAllocator() before the isolate is created, backed by
gin's array buffer partition (which already lives inside the sandbox) the
same way Blink's is backed by its ArrayBufferContents partition.
PartitionAlloc refuses single allocations of ~2 GiB and more while the
placeholder had no such cap, so requests above PartitionAlloc's limit go to
the sandbox's address space directly (checked up front, so V8 never sees a
failed allocation to retry) and Buffer.allocUnsafe of 2 GiB and more keeps
working. Measured in the main process: allocUnsafe 64 KB 33 -> 5.6 us,
1 MB 297 -> 36 us, 4 MB 1691 -> 41 us, allocUnsafeSlow 1 MB 218 -> 13 us,
fs.promises.readFile 1 MB 1.2 -> 0.9 ms. Page zeroing moves from the
allocator to first touch, so a Buffer that is immediately written end to
end pays the same faults later instead.

* perf: serialize ipc values straight into the buffer they are sent in

Values passed through ipcRenderer.send/invoke/sendSync/sendToHost,
webContents.send, webFrameMain.send and their service worker equivalents
travel as blink.mojom.CloneableMessage. For payloads above mojo's 64 KB
inline limit that costs three fresh multi-megabyte buffers per hop: the
V8 serializer grows a zero-filled std::vector, mojo copies that into a
newly created shared memory region, and the receiving traits copy the
region back out into another vector before V8 reads a byte. In profiles
of 1-4 MB messages both main threads spent 30-40% of their time in page
faults for those buffers.

Electron owns both ends of these interfaces, so give them their own
payload type: electron.mojom.SerializedValue is a BigBuffer plus the
number of bytes written. The serializer keeps small values on the heap as
before and, once V8 asks for more than the inline limit, continues in the
shared memory region that will be sent, so the sender never holds a
second copy. On the receiving side a renderer parses the region in
place; the browser (and any other process) still parses a private copy,
because the sender keeps write access to shared memory while it is read.

MessagePort/postMessage paths keep blink's TransferableMessage, and the
CloneableMessage overloads stay for in-process users (contextBridge,
app 'second-instance' data).

The serializer moves to shared memory once V8's request shows the value
needs more than an inline message (not merely when the doubled request
crosses 64 KB), and grows regions 4x from there, so a value written in
many small pieces creates two or three regions rather than one per
doubling; each region is a brokered round trip for a sandboxed renderer
on Linux. Object-heavy payloads (arrays of small records) measure
renderer -> main 1 MB 10.2 -> 10.2 ms and 4 MB 41.5 -> 39.9 ms with this,
against +8% / +3% with per-doubling regions.

* perf: size protocol and asar response pipes to the body

Electron's URL loaders created their response data pipes with mojo's
64 KB default (the asar loader with a hard-coded 65536 copied from an old
FileURLLoader), so every response larger than that reached the renderer
in 64 KB slices, each one a producer wakeup on the browser main thread
plus a consumer wakeup in the renderer. Chromium since sized these up:
network::GetDataPipeDefaultAllocationSize() is 512 KB for ordinary loads
and 2 MB (kLargerSizeIfPossible) for file://.

A pipe is a shared memory buffer that base preallocates in full, though,
so a uniformly larger pipe makes every small response measurably slower
for the sake of the large ones. Instead, where the body size is known up
front, size the pipe to the body with the same caps upstream uses: archive
entries (file-like) up to the 2 MB file:// size, string/buffer responses
and any protocol response that declares a Content-Length up to the
512 KB default. Streams of unknown length keep mojo's default. Small
responses keep exactly the pipe they had; a 16 MB archive entry reaches
the renderer in 8 slices instead of 256, a 16 MB protocol body with a
known length in 32.

* perf: remember module stat results for paths inside an archive

Module resolution calls internalModuleStat() thousands of times while an
app loads from app.asar, and most of those repeat a path it already asked
about (parent node_modules directories, extension-less candidates):
loading a 1040-module tree issues 6357 calls for 2751 distinct paths, 5065
of them misses. Node's own stat cache only remembers hits and only within
one require() chain, so every repeat went back through the archive
lookup.

An archive's header never changes once it is open, so keep the answers
derived from it in a Map keyed by the path (bounded, cleared when it grows
past 16k entries). Answers that came from failing to open the archive are
not kept, so an archive that appears later is still picked up.

(cherry picked from commit 51d485b)
codebytere added a commit that referenced this pull request Aug 19, 2026
…52923)

* perf: preallocate the file descriptor table before threads start (linux)

Linux grows a process's file descriptor table in powers of two on demand
and, once the table is shared with other threads, expand_fdtable() waits
for a full RCU grace period before freeing the old table. Whichever
open()/socket()/memfd call crosses the 64-descriptor boundary in an
already multi-threaded process eats that wait; in the browser process it
lands inside the first `new BrowserWindow` (the renderer's channel and
shared memory setup) and measured 55-115 ms of uninterruptible sleep in
openat() on a 64-vCPU host, 10-30 ms being typical elsewhere.

content's zygote already grows the table of every child it forks for this
reason (content/zygote/zygote_linux.cc). Do the same in main() for the
browser and the exec'd children: one F_DUPFD_CLOEXEC to descriptor 512
while the process is still single-threaded leaves a 1024-entry table
(8 KB) behind and nothing else. Where RLIMIT_NOFILE is lower the fcntl
fails and nothing changes.

* perf: stop rebuilding the whole path per component in asar::GetAsarArchivePath

GetAsarArchivePath() located the archive by calling DirName() once per
path component and MatchesExtension() on each intermediate path (a
BaseName() copy every time), then split the result with
AppendRelativePath(), which runs GetComponents() over both the parent
and the child. For a file inside app.asar that is dozens of string
allocations per lookup, and the fs wrapper performs this lookup
(asar.splitPath) for every fs call whose path mentions ".asar" - around
ten per require()'d module.

Scan the component boundaries as offsets into the existing string
instead, hand only the candidate component to MatchesExtension(), and
build the relative part with Append() from the components after the
match. The outputs are byte-identical to before, including the existing
corner cases (a real directory named *.asar is not an archive, the
deepest matching component wins, doubled separators and a leading "."
collapse the way Append() collapses them, a trailing separator on the
archive path itself is kept); new asar-spec cases pin those down.

* perf: keep context bridge proxy function state in the function's data array

The proxy that contextBridge installs for a function carried its target,
receiver and dynamic-property flag as three private properties on a state
object, so every call through the bridge did three v8::Private::ForApi
lookups (each allocating the key string) plus three GetPrivate calls
before it could invoke anything. Store the three values in a packed
v8::Array handed to v8::Function::New as the callback data and read them
back by index. The array is only reachable through the function's data
slot, exactly like the old state object, so nothing new becomes visible
to either world; the original-function marker on the proxy itself stays a
private property because that one has to live on a script-visible
object.

* perf: trim per-value work when the context bridge copies objects and arrays

Copying a plain object or array across the bridge visits every value, and
each visit paid for a trace event, three Blink wrapper probes
(WebElement, WebBlob, VideoFrame) that can only ever match API wrapper
objects, and, for object properties, an extra destination Context::Scope
enter/exit on top of the source one. Arrays were also filled with one
Set() per element.

Skip the Blink probes unless the value is an API wrapper object, drop the
inner trace event (the entry points keep theirs), read each property
under the source scope and define it under the destination scope that is
already active, and build array clones with the bulk v8::Array::New. The
values produced, the object cache and the recursion order are unchanged.

* perf: give V8's in-sandbox allocator a PartitionAlloc backend outside renderers

Node allocates Buffer.allocUnsafe(), Buffer.allocUnsafeSlow() and the
64 KB Buffer pool refill through ArrayBuffer::Allocator::NewDefaultAllocator()
(Electron's sandboxed-pointers patch), which under the V8 sandbox forwards
to V8's built-in in-sandbox allocator. V8 documents that allocator as a slow
placeholder embedders are expected to replace: it hands out page-granular
regions under a global mutex and zero-fills even "uninitialized" requests.
Blink replaces it for renderers (V8Initializer::InitializeInSandboxAllocator);
the browser, utility and ELECTRON_RUN_AS_NODE processes never did, so every
unpooled Buffer there paid a map/unmap round trip: Buffer.allocUnsafe of
1 MB ~300 us against ~12 us in a stock Node build, 64 KB ~30 us against
~2 us, and fs.readFile of a 1 MB file was several times slower than
reading into a preallocated buffer.

JavascriptEnvironment now installs an allocator with
v8::V8::SetInSandboxAllocator() before the isolate is created, backed by
gin's array buffer partition (which already lives inside the sandbox) the
same way Blink's is backed by its ArrayBufferContents partition.
PartitionAlloc refuses single allocations of ~2 GiB and more while the
placeholder had no such cap, so requests above PartitionAlloc's limit go to
the sandbox's address space directly (checked up front, so V8 never sees a
failed allocation to retry) and Buffer.allocUnsafe of 2 GiB and more keeps
working. Measured in the main process: allocUnsafe 64 KB 33 -> 5.6 us,
1 MB 297 -> 36 us, 4 MB 1691 -> 41 us, allocUnsafeSlow 1 MB 218 -> 13 us,
fs.promises.readFile 1 MB 1.2 -> 0.9 ms. Page zeroing moves from the
allocator to first touch, so a Buffer that is immediately written end to
end pays the same faults later instead.

* perf: serialize ipc values straight into the buffer they are sent in

Values passed through ipcRenderer.send/invoke/sendSync/sendToHost,
webContents.send, webFrameMain.send and their service worker equivalents
travel as blink.mojom.CloneableMessage. For payloads above mojo's 64 KB
inline limit that costs three fresh multi-megabyte buffers per hop: the
V8 serializer grows a zero-filled std::vector, mojo copies that into a
newly created shared memory region, and the receiving traits copy the
region back out into another vector before V8 reads a byte. In profiles
of 1-4 MB messages both main threads spent 30-40% of their time in page
faults for those buffers.

Electron owns both ends of these interfaces, so give them their own
payload type: electron.mojom.SerializedValue is a BigBuffer plus the
number of bytes written. The serializer keeps small values on the heap as
before and, once V8 asks for more than the inline limit, continues in the
shared memory region that will be sent, so the sender never holds a
second copy. On the receiving side a renderer parses the region in
place; the browser (and any other process) still parses a private copy,
because the sender keeps write access to shared memory while it is read.

MessagePort/postMessage paths keep blink's TransferableMessage, and the
CloneableMessage overloads stay for in-process users (contextBridge,
app 'second-instance' data).

The serializer moves to shared memory once V8's request shows the value
needs more than an inline message (not merely when the doubled request
crosses 64 KB), and grows regions 4x from there, so a value written in
many small pieces creates two or three regions rather than one per
doubling; each region is a brokered round trip for a sandboxed renderer
on Linux. Object-heavy payloads (arrays of small records) measure
renderer -> main 1 MB 10.2 -> 10.2 ms and 4 MB 41.5 -> 39.9 ms with this,
against +8% / +3% with per-doubling regions.

* perf: size protocol and asar response pipes to the body

Electron's URL loaders created their response data pipes with mojo's
64 KB default (the asar loader with a hard-coded 65536 copied from an old
FileURLLoader), so every response larger than that reached the renderer
in 64 KB slices, each one a producer wakeup on the browser main thread
plus a consumer wakeup in the renderer. Chromium since sized these up:
network::GetDataPipeDefaultAllocationSize() is 512 KB for ordinary loads
and 2 MB (kLargerSizeIfPossible) for file://.

A pipe is a shared memory buffer that base preallocates in full, though,
so a uniformly larger pipe makes every small response measurably slower
for the sake of the large ones. Instead, where the body size is known up
front, size the pipe to the body with the same caps upstream uses: archive
entries (file-like) up to the 2 MB file:// size, string/buffer responses
and any protocol response that declares a Content-Length up to the
512 KB default. Streams of unknown length keep mojo's default. Small
responses keep exactly the pipe they had; a 16 MB archive entry reaches
the renderer in 8 slices instead of 256, a 16 MB protocol body with a
known length in 32.

* perf: remember module stat results for paths inside an archive

Module resolution calls internalModuleStat() thousands of times while an
app loads from app.asar, and most of those repeat a path it already asked
about (parent node_modules directories, extension-less candidates):
loading a 1040-module tree issues 6357 calls for 2751 distinct paths, 5065
of them misses. Node's own stat cache only remembers hits and only within
one require() chain, so every repeat went back through the archive
lookup.

An archive's header never changes once it is open, so keep the answers
derived from it in a Map keyed by the path (bounded, cleared when it grows
past 16k entries). Answers that came from failing to open the archive are
not kept, so an archive that appears later is still picked up.

(cherry picked from commit 51d485b)
MarshallOfSound pushed a commit that referenced this pull request Aug 19, 2026
…52923) (#53017)

* perf: preallocate the file descriptor table before threads start (linux)

Linux grows a process's file descriptor table in powers of two on demand
and, once the table is shared with other threads, expand_fdtable() waits
for a full RCU grace period before freeing the old table. Whichever
open()/socket()/memfd call crosses the 64-descriptor boundary in an
already multi-threaded process eats that wait; in the browser process it
lands inside the first `new BrowserWindow` (the renderer's channel and
shared memory setup) and measured 55-115 ms of uninterruptible sleep in
openat() on a 64-vCPU host, 10-30 ms being typical elsewhere.

content's zygote already grows the table of every child it forks for this
reason (content/zygote/zygote_linux.cc). Do the same in main() for the
browser and the exec'd children: one F_DUPFD_CLOEXEC to descriptor 512
while the process is still single-threaded leaves a 1024-entry table
(8 KB) behind and nothing else. Where RLIMIT_NOFILE is lower the fcntl
fails and nothing changes.

* perf: stop rebuilding the whole path per component in asar::GetAsarArchivePath

GetAsarArchivePath() located the archive by calling DirName() once per
path component and MatchesExtension() on each intermediate path (a
BaseName() copy every time), then split the result with
AppendRelativePath(), which runs GetComponents() over both the parent
and the child. For a file inside app.asar that is dozens of string
allocations per lookup, and the fs wrapper performs this lookup
(asar.splitPath) for every fs call whose path mentions ".asar" - around
ten per require()'d module.

Scan the component boundaries as offsets into the existing string
instead, hand only the candidate component to MatchesExtension(), and
build the relative part with Append() from the components after the
match. The outputs are byte-identical to before, including the existing
corner cases (a real directory named *.asar is not an archive, the
deepest matching component wins, doubled separators and a leading "."
collapse the way Append() collapses them, a trailing separator on the
archive path itself is kept); new asar-spec cases pin those down.

* perf: keep context bridge proxy function state in the function's data array

The proxy that contextBridge installs for a function carried its target,
receiver and dynamic-property flag as three private properties on a state
object, so every call through the bridge did three v8::Private::ForApi
lookups (each allocating the key string) plus three GetPrivate calls
before it could invoke anything. Store the three values in a packed
v8::Array handed to v8::Function::New as the callback data and read them
back by index. The array is only reachable through the function's data
slot, exactly like the old state object, so nothing new becomes visible
to either world; the original-function marker on the proxy itself stays a
private property because that one has to live on a script-visible
object.

* perf: trim per-value work when the context bridge copies objects and arrays

Copying a plain object or array across the bridge visits every value, and
each visit paid for a trace event, three Blink wrapper probes
(WebElement, WebBlob, VideoFrame) that can only ever match API wrapper
objects, and, for object properties, an extra destination Context::Scope
enter/exit on top of the source one. Arrays were also filled with one
Set() per element.

Skip the Blink probes unless the value is an API wrapper object, drop the
inner trace event (the entry points keep theirs), read each property
under the source scope and define it under the destination scope that is
already active, and build array clones with the bulk v8::Array::New. The
values produced, the object cache and the recursion order are unchanged.

* perf: give V8's in-sandbox allocator a PartitionAlloc backend outside renderers

Node allocates Buffer.allocUnsafe(), Buffer.allocUnsafeSlow() and the
64 KB Buffer pool refill through ArrayBuffer::Allocator::NewDefaultAllocator()
(Electron's sandboxed-pointers patch), which under the V8 sandbox forwards
to V8's built-in in-sandbox allocator. V8 documents that allocator as a slow
placeholder embedders are expected to replace: it hands out page-granular
regions under a global mutex and zero-fills even "uninitialized" requests.
Blink replaces it for renderers (V8Initializer::InitializeInSandboxAllocator);
the browser, utility and ELECTRON_RUN_AS_NODE processes never did, so every
unpooled Buffer there paid a map/unmap round trip: Buffer.allocUnsafe of
1 MB ~300 us against ~12 us in a stock Node build, 64 KB ~30 us against
~2 us, and fs.readFile of a 1 MB file was several times slower than
reading into a preallocated buffer.

JavascriptEnvironment now installs an allocator with
v8::V8::SetInSandboxAllocator() before the isolate is created, backed by
gin's array buffer partition (which already lives inside the sandbox) the
same way Blink's is backed by its ArrayBufferContents partition.
PartitionAlloc refuses single allocations of ~2 GiB and more while the
placeholder had no such cap, so requests above PartitionAlloc's limit go to
the sandbox's address space directly (checked up front, so V8 never sees a
failed allocation to retry) and Buffer.allocUnsafe of 2 GiB and more keeps
working. Measured in the main process: allocUnsafe 64 KB 33 -> 5.6 us,
1 MB 297 -> 36 us, 4 MB 1691 -> 41 us, allocUnsafeSlow 1 MB 218 -> 13 us,
fs.promises.readFile 1 MB 1.2 -> 0.9 ms. Page zeroing moves from the
allocator to first touch, so a Buffer that is immediately written end to
end pays the same faults later instead.

* perf: serialize ipc values straight into the buffer they are sent in

Values passed through ipcRenderer.send/invoke/sendSync/sendToHost,
webContents.send, webFrameMain.send and their service worker equivalents
travel as blink.mojom.CloneableMessage. For payloads above mojo's 64 KB
inline limit that costs three fresh multi-megabyte buffers per hop: the
V8 serializer grows a zero-filled std::vector, mojo copies that into a
newly created shared memory region, and the receiving traits copy the
region back out into another vector before V8 reads a byte. In profiles
of 1-4 MB messages both main threads spent 30-40% of their time in page
faults for those buffers.

Electron owns both ends of these interfaces, so give them their own
payload type: electron.mojom.SerializedValue is a BigBuffer plus the
number of bytes written. The serializer keeps small values on the heap as
before and, once V8 asks for more than the inline limit, continues in the
shared memory region that will be sent, so the sender never holds a
second copy. On the receiving side a renderer parses the region in
place; the browser (and any other process) still parses a private copy,
because the sender keeps write access to shared memory while it is read.

MessagePort/postMessage paths keep blink's TransferableMessage, and the
CloneableMessage overloads stay for in-process users (contextBridge,
app 'second-instance' data).

The serializer moves to shared memory once V8's request shows the value
needs more than an inline message (not merely when the doubled request
crosses 64 KB), and grows regions 4x from there, so a value written in
many small pieces creates two or three regions rather than one per
doubling; each region is a brokered round trip for a sandboxed renderer
on Linux. Object-heavy payloads (arrays of small records) measure
renderer -> main 1 MB 10.2 -> 10.2 ms and 4 MB 41.5 -> 39.9 ms with this,
against +8% / +3% with per-doubling regions.

* perf: size protocol and asar response pipes to the body

Electron's URL loaders created their response data pipes with mojo's
64 KB default (the asar loader with a hard-coded 65536 copied from an old
FileURLLoader), so every response larger than that reached the renderer
in 64 KB slices, each one a producer wakeup on the browser main thread
plus a consumer wakeup in the renderer. Chromium since sized these up:
network::GetDataPipeDefaultAllocationSize() is 512 KB for ordinary loads
and 2 MB (kLargerSizeIfPossible) for file://.

A pipe is a shared memory buffer that base preallocates in full, though,
so a uniformly larger pipe makes every small response measurably slower
for the sake of the large ones. Instead, where the body size is known up
front, size the pipe to the body with the same caps upstream uses: archive
entries (file-like) up to the 2 MB file:// size, string/buffer responses
and any protocol response that declares a Content-Length up to the
512 KB default. Streams of unknown length keep mojo's default. Small
responses keep exactly the pipe they had; a 16 MB archive entry reaches
the renderer in 8 slices instead of 256, a 16 MB protocol body with a
known length in 32.

* perf: remember module stat results for paths inside an archive

Module resolution calls internalModuleStat() thousands of times while an
app loads from app.asar, and most of those repeat a path it already asked
about (parent node_modules directories, extension-less candidates):
loading a 1040-module tree issues 6357 calls for 2751 distinct paths, 5065
of them misses. Node's own stat cache only remembers hits and only within
one require() chain, so every repeat went back through the archive
lookup.

An archive's header never changes once it is open, so keep the answers
derived from it in a Map keyed by the path (bounded, cleared when it grows
past 16k entries). Answers that came from failing to open the archive are
not kept, so an archive that appears later is still picked up.

(cherry picked from commit 51d485b)
@trop trop Bot added merged/44-x-y PR was merged to the "44-x-y" branch. and removed in-flight/44-x-y labels Aug 19, 2026
codebytere added a commit that referenced this pull request Aug 19, 2026
…52923)

* perf: preallocate the file descriptor table before threads start (linux)

Linux grows a process's file descriptor table in powers of two on demand
and, once the table is shared with other threads, expand_fdtable() waits
for a full RCU grace period before freeing the old table. Whichever
open()/socket()/memfd call crosses the 64-descriptor boundary in an
already multi-threaded process eats that wait; in the browser process it
lands inside the first `new BrowserWindow` (the renderer's channel and
shared memory setup) and measured 55-115 ms of uninterruptible sleep in
openat() on a 64-vCPU host, 10-30 ms being typical elsewhere.

content's zygote already grows the table of every child it forks for this
reason (content/zygote/zygote_linux.cc). Do the same in main() for the
browser and the exec'd children: one F_DUPFD_CLOEXEC to descriptor 512
while the process is still single-threaded leaves a 1024-entry table
(8 KB) behind and nothing else. Where RLIMIT_NOFILE is lower the fcntl
fails and nothing changes.

* perf: stop rebuilding the whole path per component in asar::GetAsarArchivePath

GetAsarArchivePath() located the archive by calling DirName() once per
path component and MatchesExtension() on each intermediate path (a
BaseName() copy every time), then split the result with
AppendRelativePath(), which runs GetComponents() over both the parent
and the child. For a file inside app.asar that is dozens of string
allocations per lookup, and the fs wrapper performs this lookup
(asar.splitPath) for every fs call whose path mentions ".asar" - around
ten per require()'d module.

Scan the component boundaries as offsets into the existing string
instead, hand only the candidate component to MatchesExtension(), and
build the relative part with Append() from the components after the
match. The outputs are byte-identical to before, including the existing
corner cases (a real directory named *.asar is not an archive, the
deepest matching component wins, doubled separators and a leading "."
collapse the way Append() collapses them, a trailing separator on the
archive path itself is kept); new asar-spec cases pin those down.

* perf: keep context bridge proxy function state in the function's data array

The proxy that contextBridge installs for a function carried its target,
receiver and dynamic-property flag as three private properties on a state
object, so every call through the bridge did three v8::Private::ForApi
lookups (each allocating the key string) plus three GetPrivate calls
before it could invoke anything. Store the three values in a packed
v8::Array handed to v8::Function::New as the callback data and read them
back by index. The array is only reachable through the function's data
slot, exactly like the old state object, so nothing new becomes visible
to either world; the original-function marker on the proxy itself stays a
private property because that one has to live on a script-visible
object.

* perf: trim per-value work when the context bridge copies objects and arrays

Copying a plain object or array across the bridge visits every value, and
each visit paid for a trace event, three Blink wrapper probes
(WebElement, WebBlob, VideoFrame) that can only ever match API wrapper
objects, and, for object properties, an extra destination Context::Scope
enter/exit on top of the source one. Arrays were also filled with one
Set() per element.

Skip the Blink probes unless the value is an API wrapper object, drop the
inner trace event (the entry points keep theirs), read each property
under the source scope and define it under the destination scope that is
already active, and build array clones with the bulk v8::Array::New. The
values produced, the object cache and the recursion order are unchanged.

* perf: give V8's in-sandbox allocator a PartitionAlloc backend outside renderers

Node allocates Buffer.allocUnsafe(), Buffer.allocUnsafeSlow() and the
64 KB Buffer pool refill through ArrayBuffer::Allocator::NewDefaultAllocator()
(Electron's sandboxed-pointers patch), which under the V8 sandbox forwards
to V8's built-in in-sandbox allocator. V8 documents that allocator as a slow
placeholder embedders are expected to replace: it hands out page-granular
regions under a global mutex and zero-fills even "uninitialized" requests.
Blink replaces it for renderers (V8Initializer::InitializeInSandboxAllocator);
the browser, utility and ELECTRON_RUN_AS_NODE processes never did, so every
unpooled Buffer there paid a map/unmap round trip: Buffer.allocUnsafe of
1 MB ~300 us against ~12 us in a stock Node build, 64 KB ~30 us against
~2 us, and fs.readFile of a 1 MB file was several times slower than
reading into a preallocated buffer.

JavascriptEnvironment now installs an allocator with
v8::V8::SetInSandboxAllocator() before the isolate is created, backed by
gin's array buffer partition (which already lives inside the sandbox) the
same way Blink's is backed by its ArrayBufferContents partition.
PartitionAlloc refuses single allocations of ~2 GiB and more while the
placeholder had no such cap, so requests above PartitionAlloc's limit go to
the sandbox's address space directly (checked up front, so V8 never sees a
failed allocation to retry) and Buffer.allocUnsafe of 2 GiB and more keeps
working. Measured in the main process: allocUnsafe 64 KB 33 -> 5.6 us,
1 MB 297 -> 36 us, 4 MB 1691 -> 41 us, allocUnsafeSlow 1 MB 218 -> 13 us,
fs.promises.readFile 1 MB 1.2 -> 0.9 ms. Page zeroing moves from the
allocator to first touch, so a Buffer that is immediately written end to
end pays the same faults later instead.

* perf: serialize ipc values straight into the buffer they are sent in

Values passed through ipcRenderer.send/invoke/sendSync/sendToHost,
webContents.send, webFrameMain.send and their service worker equivalents
travel as blink.mojom.CloneableMessage. For payloads above mojo's 64 KB
inline limit that costs three fresh multi-megabyte buffers per hop: the
V8 serializer grows a zero-filled std::vector, mojo copies that into a
newly created shared memory region, and the receiving traits copy the
region back out into another vector before V8 reads a byte. In profiles
of 1-4 MB messages both main threads spent 30-40% of their time in page
faults for those buffers.

Electron owns both ends of these interfaces, so give them their own
payload type: electron.mojom.SerializedValue is a BigBuffer plus the
number of bytes written. The serializer keeps small values on the heap as
before and, once V8 asks for more than the inline limit, continues in the
shared memory region that will be sent, so the sender never holds a
second copy. On the receiving side a renderer parses the region in
place; the browser (and any other process) still parses a private copy,
because the sender keeps write access to shared memory while it is read.

MessagePort/postMessage paths keep blink's TransferableMessage, and the
CloneableMessage overloads stay for in-process users (contextBridge,
app 'second-instance' data).

The serializer moves to shared memory once V8's request shows the value
needs more than an inline message (not merely when the doubled request
crosses 64 KB), and grows regions 4x from there, so a value written in
many small pieces creates two or three regions rather than one per
doubling; each region is a brokered round trip for a sandboxed renderer
on Linux. Object-heavy payloads (arrays of small records) measure
renderer -> main 1 MB 10.2 -> 10.2 ms and 4 MB 41.5 -> 39.9 ms with this,
against +8% / +3% with per-doubling regions.

* perf: size protocol and asar response pipes to the body

Electron's URL loaders created their response data pipes with mojo's
64 KB default (the asar loader with a hard-coded 65536 copied from an old
FileURLLoader), so every response larger than that reached the renderer
in 64 KB slices, each one a producer wakeup on the browser main thread
plus a consumer wakeup in the renderer. Chromium since sized these up:
network::GetDataPipeDefaultAllocationSize() is 512 KB for ordinary loads
and 2 MB (kLargerSizeIfPossible) for file://.

A pipe is a shared memory buffer that base preallocates in full, though,
so a uniformly larger pipe makes every small response measurably slower
for the sake of the large ones. Instead, where the body size is known up
front, size the pipe to the body with the same caps upstream uses: archive
entries (file-like) up to the 2 MB file:// size, string/buffer responses
and any protocol response that declares a Content-Length up to the
512 KB default. Streams of unknown length keep mojo's default. Small
responses keep exactly the pipe they had; a 16 MB archive entry reaches
the renderer in 8 slices instead of 256, a 16 MB protocol body with a
known length in 32.

* perf: remember module stat results for paths inside an archive

Module resolution calls internalModuleStat() thousands of times while an
app loads from app.asar, and most of those repeat a path it already asked
about (parent node_modules directories, extension-less candidates):
loading a 1040-module tree issues 6357 calls for 2751 distinct paths, 5065
of them misses. Node's own stat cache only remembers hits and only within
one require() chain, so every repeat went back through the archive
lookup.

An archive's header never changes once it is open, so keep the answers
derived from it in a Map keyed by the path (bounded, cleared when it grows
past 16k entries). Answers that came from failing to open the archive are
not kept, so an archive that appears later is still picked up.

(cherry picked from commit 51d485b)
codebytere added a commit that referenced this pull request Aug 19, 2026
…52923)

* perf: preallocate the file descriptor table before threads start (linux)

Linux grows a process's file descriptor table in powers of two on demand
and, once the table is shared with other threads, expand_fdtable() waits
for a full RCU grace period before freeing the old table. Whichever
open()/socket()/memfd call crosses the 64-descriptor boundary in an
already multi-threaded process eats that wait; in the browser process it
lands inside the first `new BrowserWindow` (the renderer's channel and
shared memory setup) and measured 55-115 ms of uninterruptible sleep in
openat() on a 64-vCPU host, 10-30 ms being typical elsewhere.

content's zygote already grows the table of every child it forks for this
reason (content/zygote/zygote_linux.cc). Do the same in main() for the
browser and the exec'd children: one F_DUPFD_CLOEXEC to descriptor 512
while the process is still single-threaded leaves a 1024-entry table
(8 KB) behind and nothing else. Where RLIMIT_NOFILE is lower the fcntl
fails and nothing changes.

* perf: stop rebuilding the whole path per component in asar::GetAsarArchivePath

GetAsarArchivePath() located the archive by calling DirName() once per
path component and MatchesExtension() on each intermediate path (a
BaseName() copy every time), then split the result with
AppendRelativePath(), which runs GetComponents() over both the parent
and the child. For a file inside app.asar that is dozens of string
allocations per lookup, and the fs wrapper performs this lookup
(asar.splitPath) for every fs call whose path mentions ".asar" - around
ten per require()'d module.

Scan the component boundaries as offsets into the existing string
instead, hand only the candidate component to MatchesExtension(), and
build the relative part with Append() from the components after the
match. The outputs are byte-identical to before, including the existing
corner cases (a real directory named *.asar is not an archive, the
deepest matching component wins, doubled separators and a leading "."
collapse the way Append() collapses them, a trailing separator on the
archive path itself is kept); new asar-spec cases pin those down.

* perf: keep context bridge proxy function state in the function's data array

The proxy that contextBridge installs for a function carried its target,
receiver and dynamic-property flag as three private properties on a state
object, so every call through the bridge did three v8::Private::ForApi
lookups (each allocating the key string) plus three GetPrivate calls
before it could invoke anything. Store the three values in a packed
v8::Array handed to v8::Function::New as the callback data and read them
back by index. The array is only reachable through the function's data
slot, exactly like the old state object, so nothing new becomes visible
to either world; the original-function marker on the proxy itself stays a
private property because that one has to live on a script-visible
object.

* perf: trim per-value work when the context bridge copies objects and arrays

Copying a plain object or array across the bridge visits every value, and
each visit paid for a trace event, three Blink wrapper probes
(WebElement, WebBlob, VideoFrame) that can only ever match API wrapper
objects, and, for object properties, an extra destination Context::Scope
enter/exit on top of the source one. Arrays were also filled with one
Set() per element.

Skip the Blink probes unless the value is an API wrapper object, drop the
inner trace event (the entry points keep theirs), read each property
under the source scope and define it under the destination scope that is
already active, and build array clones with the bulk v8::Array::New. The
values produced, the object cache and the recursion order are unchanged.

* perf: give V8's in-sandbox allocator a PartitionAlloc backend outside renderers

Node allocates Buffer.allocUnsafe(), Buffer.allocUnsafeSlow() and the
64 KB Buffer pool refill through ArrayBuffer::Allocator::NewDefaultAllocator()
(Electron's sandboxed-pointers patch), which under the V8 sandbox forwards
to V8's built-in in-sandbox allocator. V8 documents that allocator as a slow
placeholder embedders are expected to replace: it hands out page-granular
regions under a global mutex and zero-fills even "uninitialized" requests.
Blink replaces it for renderers (V8Initializer::InitializeInSandboxAllocator);
the browser, utility and ELECTRON_RUN_AS_NODE processes never did, so every
unpooled Buffer there paid a map/unmap round trip: Buffer.allocUnsafe of
1 MB ~300 us against ~12 us in a stock Node build, 64 KB ~30 us against
~2 us, and fs.readFile of a 1 MB file was several times slower than
reading into a preallocated buffer.

JavascriptEnvironment now installs an allocator with
v8::V8::SetInSandboxAllocator() before the isolate is created, backed by
gin's array buffer partition (which already lives inside the sandbox) the
same way Blink's is backed by its ArrayBufferContents partition.
PartitionAlloc refuses single allocations of ~2 GiB and more while the
placeholder had no such cap, so requests above PartitionAlloc's limit go to
the sandbox's address space directly (checked up front, so V8 never sees a
failed allocation to retry) and Buffer.allocUnsafe of 2 GiB and more keeps
working. Measured in the main process: allocUnsafe 64 KB 33 -> 5.6 us,
1 MB 297 -> 36 us, 4 MB 1691 -> 41 us, allocUnsafeSlow 1 MB 218 -> 13 us,
fs.promises.readFile 1 MB 1.2 -> 0.9 ms. Page zeroing moves from the
allocator to first touch, so a Buffer that is immediately written end to
end pays the same faults later instead.

* perf: serialize ipc values straight into the buffer they are sent in

Values passed through ipcRenderer.send/invoke/sendSync/sendToHost,
webContents.send, webFrameMain.send and their service worker equivalents
travel as blink.mojom.CloneableMessage. For payloads above mojo's 64 KB
inline limit that costs three fresh multi-megabyte buffers per hop: the
V8 serializer grows a zero-filled std::vector, mojo copies that into a
newly created shared memory region, and the receiving traits copy the
region back out into another vector before V8 reads a byte. In profiles
of 1-4 MB messages both main threads spent 30-40% of their time in page
faults for those buffers.

Electron owns both ends of these interfaces, so give them their own
payload type: electron.mojom.SerializedValue is a BigBuffer plus the
number of bytes written. The serializer keeps small values on the heap as
before and, once V8 asks for more than the inline limit, continues in the
shared memory region that will be sent, so the sender never holds a
second copy. On the receiving side a renderer parses the region in
place; the browser (and any other process) still parses a private copy,
because the sender keeps write access to shared memory while it is read.

MessagePort/postMessage paths keep blink's TransferableMessage, and the
CloneableMessage overloads stay for in-process users (contextBridge,
app 'second-instance' data).

The serializer moves to shared memory once V8's request shows the value
needs more than an inline message (not merely when the doubled request
crosses 64 KB), and grows regions 4x from there, so a value written in
many small pieces creates two or three regions rather than one per
doubling; each region is a brokered round trip for a sandboxed renderer
on Linux. Object-heavy payloads (arrays of small records) measure
renderer -> main 1 MB 10.2 -> 10.2 ms and 4 MB 41.5 -> 39.9 ms with this,
against +8% / +3% with per-doubling regions.

* perf: size protocol and asar response pipes to the body

Electron's URL loaders created their response data pipes with mojo's
64 KB default (the asar loader with a hard-coded 65536 copied from an old
FileURLLoader), so every response larger than that reached the renderer
in 64 KB slices, each one a producer wakeup on the browser main thread
plus a consumer wakeup in the renderer. Chromium since sized these up:
network::GetDataPipeDefaultAllocationSize() is 512 KB for ordinary loads
and 2 MB (kLargerSizeIfPossible) for file://.

A pipe is a shared memory buffer that base preallocates in full, though,
so a uniformly larger pipe makes every small response measurably slower
for the sake of the large ones. Instead, where the body size is known up
front, size the pipe to the body with the same caps upstream uses: archive
entries (file-like) up to the 2 MB file:// size, string/buffer responses
and any protocol response that declares a Content-Length up to the
512 KB default. Streams of unknown length keep mojo's default. Small
responses keep exactly the pipe they had; a 16 MB archive entry reaches
the renderer in 8 slices instead of 256, a 16 MB protocol body with a
known length in 32.

* perf: remember module stat results for paths inside an archive

Module resolution calls internalModuleStat() thousands of times while an
app loads from app.asar, and most of those repeat a path it already asked
about (parent node_modules directories, extension-less candidates):
loading a 1040-module tree issues 6357 calls for 2751 distinct paths, 5065
of them misses. Node's own stat cache only remembers hits and only within
one require() chain, so every repeat went back through the archive
lookup.

An archive's header never changes once it is open, so keep the answers
derived from it in a Map keyed by the path (bounded, cleared when it grows
past 16k entries). Answers that came from failing to open the archive are
not kept, so an archive that appears later is still picked up.

(cherry picked from commit 51d485b)
codebytere added a commit that referenced this pull request Aug 19, 2026
…52923)

* perf: preallocate the file descriptor table before threads start (linux)

Linux grows a process's file descriptor table in powers of two on demand
and, once the table is shared with other threads, expand_fdtable() waits
for a full RCU grace period before freeing the old table. Whichever
open()/socket()/memfd call crosses the 64-descriptor boundary in an
already multi-threaded process eats that wait; in the browser process it
lands inside the first `new BrowserWindow` (the renderer's channel and
shared memory setup) and measured 55-115 ms of uninterruptible sleep in
openat() on a 64-vCPU host, 10-30 ms being typical elsewhere.

content's zygote already grows the table of every child it forks for this
reason (content/zygote/zygote_linux.cc). Do the same in main() for the
browser and the exec'd children: one F_DUPFD_CLOEXEC to descriptor 512
while the process is still single-threaded leaves a 1024-entry table
(8 KB) behind and nothing else. Where RLIMIT_NOFILE is lower the fcntl
fails and nothing changes.

* perf: stop rebuilding the whole path per component in asar::GetAsarArchivePath

GetAsarArchivePath() located the archive by calling DirName() once per
path component and MatchesExtension() on each intermediate path (a
BaseName() copy every time), then split the result with
AppendRelativePath(), which runs GetComponents() over both the parent
and the child. For a file inside app.asar that is dozens of string
allocations per lookup, and the fs wrapper performs this lookup
(asar.splitPath) for every fs call whose path mentions ".asar" - around
ten per require()'d module.

Scan the component boundaries as offsets into the existing string
instead, hand only the candidate component to MatchesExtension(), and
build the relative part with Append() from the components after the
match. The outputs are byte-identical to before, including the existing
corner cases (a real directory named *.asar is not an archive, the
deepest matching component wins, doubled separators and a leading "."
collapse the way Append() collapses them, a trailing separator on the
archive path itself is kept); new asar-spec cases pin those down.

* perf: keep context bridge proxy function state in the function's data array

The proxy that contextBridge installs for a function carried its target,
receiver and dynamic-property flag as three private properties on a state
object, so every call through the bridge did three v8::Private::ForApi
lookups (each allocating the key string) plus three GetPrivate calls
before it could invoke anything. Store the three values in a packed
v8::Array handed to v8::Function::New as the callback data and read them
back by index. The array is only reachable through the function's data
slot, exactly like the old state object, so nothing new becomes visible
to either world; the original-function marker on the proxy itself stays a
private property because that one has to live on a script-visible
object.

* perf: trim per-value work when the context bridge copies objects and arrays

Copying a plain object or array across the bridge visits every value, and
each visit paid for a trace event, three Blink wrapper probes
(WebElement, WebBlob, VideoFrame) that can only ever match API wrapper
objects, and, for object properties, an extra destination Context::Scope
enter/exit on top of the source one. Arrays were also filled with one
Set() per element.

Skip the Blink probes unless the value is an API wrapper object, drop the
inner trace event (the entry points keep theirs), read each property
under the source scope and define it under the destination scope that is
already active, and build array clones with the bulk v8::Array::New. The
values produced, the object cache and the recursion order are unchanged.

* perf: give V8's in-sandbox allocator a PartitionAlloc backend outside renderers

Node allocates Buffer.allocUnsafe(), Buffer.allocUnsafeSlow() and the
64 KB Buffer pool refill through ArrayBuffer::Allocator::NewDefaultAllocator()
(Electron's sandboxed-pointers patch), which under the V8 sandbox forwards
to V8's built-in in-sandbox allocator. V8 documents that allocator as a slow
placeholder embedders are expected to replace: it hands out page-granular
regions under a global mutex and zero-fills even "uninitialized" requests.
Blink replaces it for renderers (V8Initializer::InitializeInSandboxAllocator);
the browser, utility and ELECTRON_RUN_AS_NODE processes never did, so every
unpooled Buffer there paid a map/unmap round trip: Buffer.allocUnsafe of
1 MB ~300 us against ~12 us in a stock Node build, 64 KB ~30 us against
~2 us, and fs.readFile of a 1 MB file was several times slower than
reading into a preallocated buffer.

JavascriptEnvironment now installs an allocator with
v8::V8::SetInSandboxAllocator() before the isolate is created, backed by
gin's array buffer partition (which already lives inside the sandbox) the
same way Blink's is backed by its ArrayBufferContents partition.
PartitionAlloc refuses single allocations of ~2 GiB and more while the
placeholder had no such cap, so requests above PartitionAlloc's limit go to
the sandbox's address space directly (checked up front, so V8 never sees a
failed allocation to retry) and Buffer.allocUnsafe of 2 GiB and more keeps
working. Measured in the main process: allocUnsafe 64 KB 33 -> 5.6 us,
1 MB 297 -> 36 us, 4 MB 1691 -> 41 us, allocUnsafeSlow 1 MB 218 -> 13 us,
fs.promises.readFile 1 MB 1.2 -> 0.9 ms. Page zeroing moves from the
allocator to first touch, so a Buffer that is immediately written end to
end pays the same faults later instead.

* perf: serialize ipc values straight into the buffer they are sent in

Values passed through ipcRenderer.send/invoke/sendSync/sendToHost,
webContents.send, webFrameMain.send and their service worker equivalents
travel as blink.mojom.CloneableMessage. For payloads above mojo's 64 KB
inline limit that costs three fresh multi-megabyte buffers per hop: the
V8 serializer grows a zero-filled std::vector, mojo copies that into a
newly created shared memory region, and the receiving traits copy the
region back out into another vector before V8 reads a byte. In profiles
of 1-4 MB messages both main threads spent 30-40% of their time in page
faults for those buffers.

Electron owns both ends of these interfaces, so give them their own
payload type: electron.mojom.SerializedValue is a BigBuffer plus the
number of bytes written. The serializer keeps small values on the heap as
before and, once V8 asks for more than the inline limit, continues in the
shared memory region that will be sent, so the sender never holds a
second copy. On the receiving side a renderer parses the region in
place; the browser (and any other process) still parses a private copy,
because the sender keeps write access to shared memory while it is read.

MessagePort/postMessage paths keep blink's TransferableMessage, and the
CloneableMessage overloads stay for in-process users (contextBridge,
app 'second-instance' data).

The serializer moves to shared memory once V8's request shows the value
needs more than an inline message (not merely when the doubled request
crosses 64 KB), and grows regions 4x from there, so a value written in
many small pieces creates two or three regions rather than one per
doubling; each region is a brokered round trip for a sandboxed renderer
on Linux. Object-heavy payloads (arrays of small records) measure
renderer -> main 1 MB 10.2 -> 10.2 ms and 4 MB 41.5 -> 39.9 ms with this,
against +8% / +3% with per-doubling regions.

* perf: size protocol and asar response pipes to the body

Electron's URL loaders created their response data pipes with mojo's
64 KB default (the asar loader with a hard-coded 65536 copied from an old
FileURLLoader), so every response larger than that reached the renderer
in 64 KB slices, each one a producer wakeup on the browser main thread
plus a consumer wakeup in the renderer. Chromium since sized these up:
network::GetDataPipeDefaultAllocationSize() is 512 KB for ordinary loads
and 2 MB (kLargerSizeIfPossible) for file://.

A pipe is a shared memory buffer that base preallocates in full, though,
so a uniformly larger pipe makes every small response measurably slower
for the sake of the large ones. Instead, where the body size is known up
front, size the pipe to the body with the same caps upstream uses: archive
entries (file-like) up to the 2 MB file:// size, string/buffer responses
and any protocol response that declares a Content-Length up to the
512 KB default. Streams of unknown length keep mojo's default. Small
responses keep exactly the pipe they had; a 16 MB archive entry reaches
the renderer in 8 slices instead of 256, a 16 MB protocol body with a
known length in 32.

* perf: remember module stat results for paths inside an archive

Module resolution calls internalModuleStat() thousands of times while an
app loads from app.asar, and most of those repeat a path it already asked
about (parent node_modules directories, extension-less candidates):
loading a 1040-module tree issues 6357 calls for 2751 distinct paths, 5065
of them misses. Node's own stat cache only remembers hits and only within
one require() chain, so every repeat went back through the archive
lookup.

An archive's header never changes once it is open, so keep the answers
derived from it in a Map keyed by the path (bounded, cleared when it grows
past 16k entries). Answers that came from failing to open the archive are
not kept, so an archive that appears later is still picked up.

(cherry picked from commit 51d485b)
codebytere added a commit that referenced this pull request Aug 19, 2026
…52923)

* perf: preallocate the file descriptor table before threads start (linux)

Linux grows a process's file descriptor table in powers of two on demand
and, once the table is shared with other threads, expand_fdtable() waits
for a full RCU grace period before freeing the old table. Whichever
open()/socket()/memfd call crosses the 64-descriptor boundary in an
already multi-threaded process eats that wait; in the browser process it
lands inside the first `new BrowserWindow` (the renderer's channel and
shared memory setup) and measured 55-115 ms of uninterruptible sleep in
openat() on a 64-vCPU host, 10-30 ms being typical elsewhere.

content's zygote already grows the table of every child it forks for this
reason (content/zygote/zygote_linux.cc). Do the same in main() for the
browser and the exec'd children: one F_DUPFD_CLOEXEC to descriptor 512
while the process is still single-threaded leaves a 1024-entry table
(8 KB) behind and nothing else. Where RLIMIT_NOFILE is lower the fcntl
fails and nothing changes.

* perf: stop rebuilding the whole path per component in asar::GetAsarArchivePath

GetAsarArchivePath() located the archive by calling DirName() once per
path component and MatchesExtension() on each intermediate path (a
BaseName() copy every time), then split the result with
AppendRelativePath(), which runs GetComponents() over both the parent
and the child. For a file inside app.asar that is dozens of string
allocations per lookup, and the fs wrapper performs this lookup
(asar.splitPath) for every fs call whose path mentions ".asar" - around
ten per require()'d module.

Scan the component boundaries as offsets into the existing string
instead, hand only the candidate component to MatchesExtension(), and
build the relative part with Append() from the components after the
match. The outputs are byte-identical to before, including the existing
corner cases (a real directory named *.asar is not an archive, the
deepest matching component wins, doubled separators and a leading "."
collapse the way Append() collapses them, a trailing separator on the
archive path itself is kept); new asar-spec cases pin those down.

* perf: keep context bridge proxy function state in the function's data array

The proxy that contextBridge installs for a function carried its target,
receiver and dynamic-property flag as three private properties on a state
object, so every call through the bridge did three v8::Private::ForApi
lookups (each allocating the key string) plus three GetPrivate calls
before it could invoke anything. Store the three values in a packed
v8::Array handed to v8::Function::New as the callback data and read them
back by index. The array is only reachable through the function's data
slot, exactly like the old state object, so nothing new becomes visible
to either world; the original-function marker on the proxy itself stays a
private property because that one has to live on a script-visible
object.

* perf: trim per-value work when the context bridge copies objects and arrays

Copying a plain object or array across the bridge visits every value, and
each visit paid for a trace event, three Blink wrapper probes
(WebElement, WebBlob, VideoFrame) that can only ever match API wrapper
objects, and, for object properties, an extra destination Context::Scope
enter/exit on top of the source one. Arrays were also filled with one
Set() per element.

Skip the Blink probes unless the value is an API wrapper object, drop the
inner trace event (the entry points keep theirs), read each property
under the source scope and define it under the destination scope that is
already active, and build array clones with the bulk v8::Array::New. The
values produced, the object cache and the recursion order are unchanged.

* perf: give V8's in-sandbox allocator a PartitionAlloc backend outside renderers

Node allocates Buffer.allocUnsafe(), Buffer.allocUnsafeSlow() and the
64 KB Buffer pool refill through ArrayBuffer::Allocator::NewDefaultAllocator()
(Electron's sandboxed-pointers patch), which under the V8 sandbox forwards
to V8's built-in in-sandbox allocator. V8 documents that allocator as a slow
placeholder embedders are expected to replace: it hands out page-granular
regions under a global mutex and zero-fills even "uninitialized" requests.
Blink replaces it for renderers (V8Initializer::InitializeInSandboxAllocator);
the browser, utility and ELECTRON_RUN_AS_NODE processes never did, so every
unpooled Buffer there paid a map/unmap round trip: Buffer.allocUnsafe of
1 MB ~300 us against ~12 us in a stock Node build, 64 KB ~30 us against
~2 us, and fs.readFile of a 1 MB file was several times slower than
reading into a preallocated buffer.

JavascriptEnvironment now installs an allocator with
v8::V8::SetInSandboxAllocator() before the isolate is created, backed by
gin's array buffer partition (which already lives inside the sandbox) the
same way Blink's is backed by its ArrayBufferContents partition.
PartitionAlloc refuses single allocations of ~2 GiB and more while the
placeholder had no such cap, so requests above PartitionAlloc's limit go to
the sandbox's address space directly (checked up front, so V8 never sees a
failed allocation to retry) and Buffer.allocUnsafe of 2 GiB and more keeps
working. Measured in the main process: allocUnsafe 64 KB 33 -> 5.6 us,
1 MB 297 -> 36 us, 4 MB 1691 -> 41 us, allocUnsafeSlow 1 MB 218 -> 13 us,
fs.promises.readFile 1 MB 1.2 -> 0.9 ms. Page zeroing moves from the
allocator to first touch, so a Buffer that is immediately written end to
end pays the same faults later instead.

* perf: serialize ipc values straight into the buffer they are sent in

Values passed through ipcRenderer.send/invoke/sendSync/sendToHost,
webContents.send, webFrameMain.send and their service worker equivalents
travel as blink.mojom.CloneableMessage. For payloads above mojo's 64 KB
inline limit that costs three fresh multi-megabyte buffers per hop: the
V8 serializer grows a zero-filled std::vector, mojo copies that into a
newly created shared memory region, and the receiving traits copy the
region back out into another vector before V8 reads a byte. In profiles
of 1-4 MB messages both main threads spent 30-40% of their time in page
faults for those buffers.

Electron owns both ends of these interfaces, so give them their own
payload type: electron.mojom.SerializedValue is a BigBuffer plus the
number of bytes written. The serializer keeps small values on the heap as
before and, once V8 asks for more than the inline limit, continues in the
shared memory region that will be sent, so the sender never holds a
second copy. On the receiving side a renderer parses the region in
place; the browser (and any other process) still parses a private copy,
because the sender keeps write access to shared memory while it is read.

MessagePort/postMessage paths keep blink's TransferableMessage, and the
CloneableMessage overloads stay for in-process users (contextBridge,
app 'second-instance' data).

The serializer moves to shared memory once V8's request shows the value
needs more than an inline message (not merely when the doubled request
crosses 64 KB), and grows regions 4x from there, so a value written in
many small pieces creates two or three regions rather than one per
doubling; each region is a brokered round trip for a sandboxed renderer
on Linux. Object-heavy payloads (arrays of small records) measure
renderer -> main 1 MB 10.2 -> 10.2 ms and 4 MB 41.5 -> 39.9 ms with this,
against +8% / +3% with per-doubling regions.

* perf: size protocol and asar response pipes to the body

Electron's URL loaders created their response data pipes with mojo's
64 KB default (the asar loader with a hard-coded 65536 copied from an old
FileURLLoader), so every response larger than that reached the renderer
in 64 KB slices, each one a producer wakeup on the browser main thread
plus a consumer wakeup in the renderer. Chromium since sized these up:
network::GetDataPipeDefaultAllocationSize() is 512 KB for ordinary loads
and 2 MB (kLargerSizeIfPossible) for file://.

A pipe is a shared memory buffer that base preallocates in full, though,
so a uniformly larger pipe makes every small response measurably slower
for the sake of the large ones. Instead, where the body size is known up
front, size the pipe to the body with the same caps upstream uses: archive
entries (file-like) up to the 2 MB file:// size, string/buffer responses
and any protocol response that declares a Content-Length up to the
512 KB default. Streams of unknown length keep mojo's default. Small
responses keep exactly the pipe they had; a 16 MB archive entry reaches
the renderer in 8 slices instead of 256, a 16 MB protocol body with a
known length in 32.

* perf: remember module stat results for paths inside an archive

Module resolution calls internalModuleStat() thousands of times while an
app loads from app.asar, and most of those repeat a path it already asked
about (parent node_modules directories, extension-less candidates):
loading a 1040-module tree issues 6357 calls for 2751 distinct paths, 5065
of them misses. Node's own stat cache only remembers hits and only within
one require() chain, so every repeat went back through the archive
lookup.

An archive's header never changes once it is open, so keep the answers
derived from it in a Map keyed by the path (bounded, cleared when it grows
past 16k entries). Answers that came from failing to open the archive are
not kept, so an archive that appears later is still picked up.

(cherry picked from commit 51d485b)
MarshallOfSound pushed a commit that referenced this pull request Aug 19, 2026
…52923) (#53018)

* perf: preallocate the file descriptor table before threads start (linux)

Linux grows a process's file descriptor table in powers of two on demand
and, once the table is shared with other threads, expand_fdtable() waits
for a full RCU grace period before freeing the old table. Whichever
open()/socket()/memfd call crosses the 64-descriptor boundary in an
already multi-threaded process eats that wait; in the browser process it
lands inside the first `new BrowserWindow` (the renderer's channel and
shared memory setup) and measured 55-115 ms of uninterruptible sleep in
openat() on a 64-vCPU host, 10-30 ms being typical elsewhere.

content's zygote already grows the table of every child it forks for this
reason (content/zygote/zygote_linux.cc). Do the same in main() for the
browser and the exec'd children: one F_DUPFD_CLOEXEC to descriptor 512
while the process is still single-threaded leaves a 1024-entry table
(8 KB) behind and nothing else. Where RLIMIT_NOFILE is lower the fcntl
fails and nothing changes.

* perf: stop rebuilding the whole path per component in asar::GetAsarArchivePath

GetAsarArchivePath() located the archive by calling DirName() once per
path component and MatchesExtension() on each intermediate path (a
BaseName() copy every time), then split the result with
AppendRelativePath(), which runs GetComponents() over both the parent
and the child. For a file inside app.asar that is dozens of string
allocations per lookup, and the fs wrapper performs this lookup
(asar.splitPath) for every fs call whose path mentions ".asar" - around
ten per require()'d module.

Scan the component boundaries as offsets into the existing string
instead, hand only the candidate component to MatchesExtension(), and
build the relative part with Append() from the components after the
match. The outputs are byte-identical to before, including the existing
corner cases (a real directory named *.asar is not an archive, the
deepest matching component wins, doubled separators and a leading "."
collapse the way Append() collapses them, a trailing separator on the
archive path itself is kept); new asar-spec cases pin those down.

* perf: keep context bridge proxy function state in the function's data array

The proxy that contextBridge installs for a function carried its target,
receiver and dynamic-property flag as three private properties on a state
object, so every call through the bridge did three v8::Private::ForApi
lookups (each allocating the key string) plus three GetPrivate calls
before it could invoke anything. Store the three values in a packed
v8::Array handed to v8::Function::New as the callback data and read them
back by index. The array is only reachable through the function's data
slot, exactly like the old state object, so nothing new becomes visible
to either world; the original-function marker on the proxy itself stays a
private property because that one has to live on a script-visible
object.

* perf: trim per-value work when the context bridge copies objects and arrays

Copying a plain object or array across the bridge visits every value, and
each visit paid for a trace event, three Blink wrapper probes
(WebElement, WebBlob, VideoFrame) that can only ever match API wrapper
objects, and, for object properties, an extra destination Context::Scope
enter/exit on top of the source one. Arrays were also filled with one
Set() per element.

Skip the Blink probes unless the value is an API wrapper object, drop the
inner trace event (the entry points keep theirs), read each property
under the source scope and define it under the destination scope that is
already active, and build array clones with the bulk v8::Array::New. The
values produced, the object cache and the recursion order are unchanged.

* perf: give V8's in-sandbox allocator a PartitionAlloc backend outside renderers

Node allocates Buffer.allocUnsafe(), Buffer.allocUnsafeSlow() and the
64 KB Buffer pool refill through ArrayBuffer::Allocator::NewDefaultAllocator()
(Electron's sandboxed-pointers patch), which under the V8 sandbox forwards
to V8's built-in in-sandbox allocator. V8 documents that allocator as a slow
placeholder embedders are expected to replace: it hands out page-granular
regions under a global mutex and zero-fills even "uninitialized" requests.
Blink replaces it for renderers (V8Initializer::InitializeInSandboxAllocator);
the browser, utility and ELECTRON_RUN_AS_NODE processes never did, so every
unpooled Buffer there paid a map/unmap round trip: Buffer.allocUnsafe of
1 MB ~300 us against ~12 us in a stock Node build, 64 KB ~30 us against
~2 us, and fs.readFile of a 1 MB file was several times slower than
reading into a preallocated buffer.

JavascriptEnvironment now installs an allocator with
v8::V8::SetInSandboxAllocator() before the isolate is created, backed by
gin's array buffer partition (which already lives inside the sandbox) the
same way Blink's is backed by its ArrayBufferContents partition.
PartitionAlloc refuses single allocations of ~2 GiB and more while the
placeholder had no such cap, so requests above PartitionAlloc's limit go to
the sandbox's address space directly (checked up front, so V8 never sees a
failed allocation to retry) and Buffer.allocUnsafe of 2 GiB and more keeps
working. Measured in the main process: allocUnsafe 64 KB 33 -> 5.6 us,
1 MB 297 -> 36 us, 4 MB 1691 -> 41 us, allocUnsafeSlow 1 MB 218 -> 13 us,
fs.promises.readFile 1 MB 1.2 -> 0.9 ms. Page zeroing moves from the
allocator to first touch, so a Buffer that is immediately written end to
end pays the same faults later instead.

* perf: serialize ipc values straight into the buffer they are sent in

Values passed through ipcRenderer.send/invoke/sendSync/sendToHost,
webContents.send, webFrameMain.send and their service worker equivalents
travel as blink.mojom.CloneableMessage. For payloads above mojo's 64 KB
inline limit that costs three fresh multi-megabyte buffers per hop: the
V8 serializer grows a zero-filled std::vector, mojo copies that into a
newly created shared memory region, and the receiving traits copy the
region back out into another vector before V8 reads a byte. In profiles
of 1-4 MB messages both main threads spent 30-40% of their time in page
faults for those buffers.

Electron owns both ends of these interfaces, so give them their own
payload type: electron.mojom.SerializedValue is a BigBuffer plus the
number of bytes written. The serializer keeps small values on the heap as
before and, once V8 asks for more than the inline limit, continues in the
shared memory region that will be sent, so the sender never holds a
second copy. On the receiving side a renderer parses the region in
place; the browser (and any other process) still parses a private copy,
because the sender keeps write access to shared memory while it is read.

MessagePort/postMessage paths keep blink's TransferableMessage, and the
CloneableMessage overloads stay for in-process users (contextBridge,
app 'second-instance' data).

The serializer moves to shared memory once V8's request shows the value
needs more than an inline message (not merely when the doubled request
crosses 64 KB), and grows regions 4x from there, so a value written in
many small pieces creates two or three regions rather than one per
doubling; each region is a brokered round trip for a sandboxed renderer
on Linux. Object-heavy payloads (arrays of small records) measure
renderer -> main 1 MB 10.2 -> 10.2 ms and 4 MB 41.5 -> 39.9 ms with this,
against +8% / +3% with per-doubling regions.

* perf: size protocol and asar response pipes to the body

Electron's URL loaders created their response data pipes with mojo's
64 KB default (the asar loader with a hard-coded 65536 copied from an old
FileURLLoader), so every response larger than that reached the renderer
in 64 KB slices, each one a producer wakeup on the browser main thread
plus a consumer wakeup in the renderer. Chromium since sized these up:
network::GetDataPipeDefaultAllocationSize() is 512 KB for ordinary loads
and 2 MB (kLargerSizeIfPossible) for file://.

A pipe is a shared memory buffer that base preallocates in full, though,
so a uniformly larger pipe makes every small response measurably slower
for the sake of the large ones. Instead, where the body size is known up
front, size the pipe to the body with the same caps upstream uses: archive
entries (file-like) up to the 2 MB file:// size, string/buffer responses
and any protocol response that declares a Content-Length up to the
512 KB default. Streams of unknown length keep mojo's default. Small
responses keep exactly the pipe they had; a 16 MB archive entry reaches
the renderer in 8 slices instead of 256, a 16 MB protocol body with a
known length in 32.

* perf: remember module stat results for paths inside an archive

Module resolution calls internalModuleStat() thousands of times while an
app loads from app.asar, and most of those repeat a path it already asked
about (parent node_modules directories, extension-less candidates):
loading a 1040-module tree issues 6357 calls for 2751 distinct paths, 5065
of them misses. Node's own stat cache only remembers hits and only within
one require() chain, so every repeat went back through the archive
lookup.

An archive's header never changes once it is open, so keep the answers
derived from it in a Map keyed by the path (bounded, cleared when it grows
past 16k entries). Answers that came from failing to open the archive are
not kept, so an archive that appears later is still picked up.

(cherry picked from commit 51d485b)
@trop trop Bot added merged/43-x-y PR was merged to the "43-x-y" branch. and removed in-flight/43-x-y labels Aug 19, 2026
MarshallOfSound pushed a commit that referenced this pull request Aug 19, 2026
…52923) (#53019)

* perf: preallocate the file descriptor table before threads start (linux)

Linux grows a process's file descriptor table in powers of two on demand
and, once the table is shared with other threads, expand_fdtable() waits
for a full RCU grace period before freeing the old table. Whichever
open()/socket()/memfd call crosses the 64-descriptor boundary in an
already multi-threaded process eats that wait; in the browser process it
lands inside the first `new BrowserWindow` (the renderer's channel and
shared memory setup) and measured 55-115 ms of uninterruptible sleep in
openat() on a 64-vCPU host, 10-30 ms being typical elsewhere.

content's zygote already grows the table of every child it forks for this
reason (content/zygote/zygote_linux.cc). Do the same in main() for the
browser and the exec'd children: one F_DUPFD_CLOEXEC to descriptor 512
while the process is still single-threaded leaves a 1024-entry table
(8 KB) behind and nothing else. Where RLIMIT_NOFILE is lower the fcntl
fails and nothing changes.

* perf: stop rebuilding the whole path per component in asar::GetAsarArchivePath

GetAsarArchivePath() located the archive by calling DirName() once per
path component and MatchesExtension() on each intermediate path (a
BaseName() copy every time), then split the result with
AppendRelativePath(), which runs GetComponents() over both the parent
and the child. For a file inside app.asar that is dozens of string
allocations per lookup, and the fs wrapper performs this lookup
(asar.splitPath) for every fs call whose path mentions ".asar" - around
ten per require()'d module.

Scan the component boundaries as offsets into the existing string
instead, hand only the candidate component to MatchesExtension(), and
build the relative part with Append() from the components after the
match. The outputs are byte-identical to before, including the existing
corner cases (a real directory named *.asar is not an archive, the
deepest matching component wins, doubled separators and a leading "."
collapse the way Append() collapses them, a trailing separator on the
archive path itself is kept); new asar-spec cases pin those down.

* perf: keep context bridge proxy function state in the function's data array

The proxy that contextBridge installs for a function carried its target,
receiver and dynamic-property flag as three private properties on a state
object, so every call through the bridge did three v8::Private::ForApi
lookups (each allocating the key string) plus three GetPrivate calls
before it could invoke anything. Store the three values in a packed
v8::Array handed to v8::Function::New as the callback data and read them
back by index. The array is only reachable through the function's data
slot, exactly like the old state object, so nothing new becomes visible
to either world; the original-function marker on the proxy itself stays a
private property because that one has to live on a script-visible
object.

* perf: trim per-value work when the context bridge copies objects and arrays

Copying a plain object or array across the bridge visits every value, and
each visit paid for a trace event, three Blink wrapper probes
(WebElement, WebBlob, VideoFrame) that can only ever match API wrapper
objects, and, for object properties, an extra destination Context::Scope
enter/exit on top of the source one. Arrays were also filled with one
Set() per element.

Skip the Blink probes unless the value is an API wrapper object, drop the
inner trace event (the entry points keep theirs), read each property
under the source scope and define it under the destination scope that is
already active, and build array clones with the bulk v8::Array::New. The
values produced, the object cache and the recursion order are unchanged.

* perf: give V8's in-sandbox allocator a PartitionAlloc backend outside renderers

Node allocates Buffer.allocUnsafe(), Buffer.allocUnsafeSlow() and the
64 KB Buffer pool refill through ArrayBuffer::Allocator::NewDefaultAllocator()
(Electron's sandboxed-pointers patch), which under the V8 sandbox forwards
to V8's built-in in-sandbox allocator. V8 documents that allocator as a slow
placeholder embedders are expected to replace: it hands out page-granular
regions under a global mutex and zero-fills even "uninitialized" requests.
Blink replaces it for renderers (V8Initializer::InitializeInSandboxAllocator);
the browser, utility and ELECTRON_RUN_AS_NODE processes never did, so every
unpooled Buffer there paid a map/unmap round trip: Buffer.allocUnsafe of
1 MB ~300 us against ~12 us in a stock Node build, 64 KB ~30 us against
~2 us, and fs.readFile of a 1 MB file was several times slower than
reading into a preallocated buffer.

JavascriptEnvironment now installs an allocator with
v8::V8::SetInSandboxAllocator() before the isolate is created, backed by
gin's array buffer partition (which already lives inside the sandbox) the
same way Blink's is backed by its ArrayBufferContents partition.
PartitionAlloc refuses single allocations of ~2 GiB and more while the
placeholder had no such cap, so requests above PartitionAlloc's limit go to
the sandbox's address space directly (checked up front, so V8 never sees a
failed allocation to retry) and Buffer.allocUnsafe of 2 GiB and more keeps
working. Measured in the main process: allocUnsafe 64 KB 33 -> 5.6 us,
1 MB 297 -> 36 us, 4 MB 1691 -> 41 us, allocUnsafeSlow 1 MB 218 -> 13 us,
fs.promises.readFile 1 MB 1.2 -> 0.9 ms. Page zeroing moves from the
allocator to first touch, so a Buffer that is immediately written end to
end pays the same faults later instead.

* perf: serialize ipc values straight into the buffer they are sent in

Values passed through ipcRenderer.send/invoke/sendSync/sendToHost,
webContents.send, webFrameMain.send and their service worker equivalents
travel as blink.mojom.CloneableMessage. For payloads above mojo's 64 KB
inline limit that costs three fresh multi-megabyte buffers per hop: the
V8 serializer grows a zero-filled std::vector, mojo copies that into a
newly created shared memory region, and the receiving traits copy the
region back out into another vector before V8 reads a byte. In profiles
of 1-4 MB messages both main threads spent 30-40% of their time in page
faults for those buffers.

Electron owns both ends of these interfaces, so give them their own
payload type: electron.mojom.SerializedValue is a BigBuffer plus the
number of bytes written. The serializer keeps small values on the heap as
before and, once V8 asks for more than the inline limit, continues in the
shared memory region that will be sent, so the sender never holds a
second copy. On the receiving side a renderer parses the region in
place; the browser (and any other process) still parses a private copy,
because the sender keeps write access to shared memory while it is read.

MessagePort/postMessage paths keep blink's TransferableMessage, and the
CloneableMessage overloads stay for in-process users (contextBridge,
app 'second-instance' data).

The serializer moves to shared memory once V8's request shows the value
needs more than an inline message (not merely when the doubled request
crosses 64 KB), and grows regions 4x from there, so a value written in
many small pieces creates two or three regions rather than one per
doubling; each region is a brokered round trip for a sandboxed renderer
on Linux. Object-heavy payloads (arrays of small records) measure
renderer -> main 1 MB 10.2 -> 10.2 ms and 4 MB 41.5 -> 39.9 ms with this,
against +8% / +3% with per-doubling regions.

* perf: size protocol and asar response pipes to the body

Electron's URL loaders created their response data pipes with mojo's
64 KB default (the asar loader with a hard-coded 65536 copied from an old
FileURLLoader), so every response larger than that reached the renderer
in 64 KB slices, each one a producer wakeup on the browser main thread
plus a consumer wakeup in the renderer. Chromium since sized these up:
network::GetDataPipeDefaultAllocationSize() is 512 KB for ordinary loads
and 2 MB (kLargerSizeIfPossible) for file://.

A pipe is a shared memory buffer that base preallocates in full, though,
so a uniformly larger pipe makes every small response measurably slower
for the sake of the large ones. Instead, where the body size is known up
front, size the pipe to the body with the same caps upstream uses: archive
entries (file-like) up to the 2 MB file:// size, string/buffer responses
and any protocol response that declares a Content-Length up to the
512 KB default. Streams of unknown length keep mojo's default. Small
responses keep exactly the pipe they had; a 16 MB archive entry reaches
the renderer in 8 slices instead of 256, a 16 MB protocol body with a
known length in 32.

* perf: remember module stat results for paths inside an archive

Module resolution calls internalModuleStat() thousands of times while an
app loads from app.asar, and most of those repeat a path it already asked
about (parent node_modules directories, extension-less candidates):
loading a 1040-module tree issues 6357 calls for 2751 distinct paths, 5065
of them misses. Node's own stat cache only remembers hits and only within
one require() chain, so every repeat went back through the archive
lookup.

An archive's header never changes once it is open, so keep the answers
derived from it in a Map keyed by the path (bounded, cleared when it grows
past 16k entries). Answers that came from failing to open the archive are
not kept, so an archive that appears later is still picked up.

(cherry picked from commit 51d485b)
@trop trop Bot added merged/42-x-y PR was merged to the "42-x-y" branch. and removed in-flight/42-x-y labels Aug 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

45-x-y merged/42-x-y PR was merged to the "42-x-y" branch. merged/43-x-y PR was merged to the "43-x-y" branch. merged/44-x-y PR was merged to the "44-x-y" branch. semver/patch backwards-compatible bug fixes

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

4 participants