sea: add vfsArchive to serve the assets from a ZIP archive - #65810
Draft
mcollina wants to merge 2 commits into
Draft
sea: add vfsArchive to serve the assets from a ZIP archive#65810mcollina wants to merge 2 commits into
mcollina wants to merge 2 commits into
Conversation
FindSingleExecutableResource() returned the deserialized SeaResource by value, copying the whole assets map on every call. getAsset() calls it once per asset read, which made every fs operation served by the SEA virtual file system pay a cost linear in the number of bundled assets: with 8192 assets, reading each of them took seconds instead of milliseconds. Return a reference to the cached resource instead. Signed-off-by: Matteo Collina <[email protected]>
When "vfsArchive" names a ZIP archive in the SEA configuration (with "useVfs": true), --build-sea embeds the archive verbatim as one reserved asset, and at runtime the SEA virtual file system mounts the existing ZipProvider over a zero-copy view of the embedded archive instead of the SEAProvider. The main script is injected into the in-memory archive index as a stored entry, so the mounted tree looks the same as with "assets". The archive can be produced with any ZIP tool or with the ZIP support in node:zlib; no ZIP serialization logic is added to the build side. This trades asset read speed (entries are inflated when opened) for a substantially smaller executable when the assets are compressible. Signed-off-by: Matteo Collina <[email protected]>
Collaborator
|
Review requested:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This adds a
"vfsArchive"option to the SEA configuration that serves the bundled assets from a ZIP archive, and fixes a performance bug in the SEA asset lookup found while benchmarking it."vfsArchive"Instead of listing individual
"assets", the configuration can point at a prebuilt ZIP archive:{ "main": "main.js", "output": "app", "useVfs": true, "vfsArchive": "assets.zip" }--build-seaembeds the archive verbatim as a single reserved asset (only aPKsignature sanity check happens at build time β no ZIP serialization logic is added to the build side). At runtime the SEA virtual file system mounts the existingZipProviderover a zero-copy view of the embedded archive instead of theSEAProvider, and the main script is injected into the in-memory archive index as a stored entry, so the mounted tree looks exactly like plain"useVfs":__dirname-relative reads, relativerequire(), and bare specifier lookups are unchanged.The archive can be produced with any ZIP tool or with the ZIP support in
node:zlib(zlib.zipFiles()), which is also what the test does."vfsArchive"requires"useVfs": trueand cannot be combined with"assets". Since only the archive is embedded,sea.getAsset()/sea.getAssetAsBlob()do not serve the individual files; they are read through the fs APIs instead.Perf fix: return the SEA resource by reference
FindSingleExecutableResource()returned the deserializedSeaResourceby value, copying the whole assets map on every call.getAsset()calls it once per asset read, making every fs operation served by the SEA VFS pay a cost linear in the number of bundled assets: with 8192 assets, reading each of them took ~7.9s instead of ~270ms (~30x). Fixed in the first commit by returning a reference to the cached resource.Size / speed tradeoff
Measured with a 75% text / 25% random asset mix in 16KB files (Linux x64, warm cache, medians of 15 runs; scenarios: start the binary touching no asset, read one small asset, read every asset):
internal/zipmachinery and parsing the central directory); nothing is inflated until a file is opened.SEAProvidermemcpy path; per-file stat cost is identical (VFS dispatch dominates).Rule of thumb: the archive is a clear win above roughly 30MB of compressible assets when a run reads a subset of them; plain
"useVfs"remains better for small bundles or workloads that repeatedly read large assets.