Embed internal module and builtin function bytecode in --compile --bytecode executables#28461
Draft
sosukesuzuki wants to merge 10 commits intomainfrom
Draft
Embed internal module and builtin function bytecode in --compile --bytecode executables#28461sosukesuzuki wants to merge 10 commits intomainfrom
sosukesuzuki wants to merge 10 commits intomainfrom
Conversation
Collaborator
|
|
||
| // Standalone executables built with --bytecode embed pre-generated bytecode | ||
| // for internal modules. Try decoding that first to skip parsing. | ||
| if (void* bunVM = defaultGlobalObject(globalObject)->bunVM()) { |
Collaborator
There was a problem hiding this comment.
can we set an extern "C" bool that says "please check for this here"
| // Builtin bytecode generation needs Bun's private identifiers (@isCallable etc) | ||
| // registered in the VM. The plain getVMForBytecodeCache() VM doesn't have | ||
| // JSVMClientData, so parsing builtin sources would fail. | ||
| static JSC::VM& getVMForBuiltinBytecodeCache() |
Collaborator
There was a problem hiding this comment.
can we always use this instead of using either?
…he VM - Add Bun__hasEmbeddedBuiltinBytecode so C++ can skip the extern lookup call entirely on normal Bun runs. The Zig lookup functions now read the global StandaloneModuleGraph instance instead of threading bunVM through every call site. - Fold getVMForBuiltinBytecodeCache into getVMForBytecodeCache by always attaching JSVMClientData. Module/CJS codegen doesn't need it but works fine with it.
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.
bun build --compile --bytecodealready embeds bytecode for user code.This PR extends it to also embed bytecode for Bun's internal JavaScript:
src/js/node/,src/js/bun/, etc.) — 138 moduleslike
node:fs,node:httpsrc/js/builtins/) — 362 functions likeReadableStreaminternals,consolemethodsAt runtime,
require("node:fs")ornew ReadableStream()can decode theembedded bytecode instead of parsing the source.
Benchmarks
Lightweight app (streams + 6 modules)
Large CLI app (startup)
Binary size
Standalone executable grows by ~7MB (internal modules: +6.1MB, builtin
functions: +1.1MB). The Bun binary itself is unchanged.
How it works
Build time (
bun build --compile --bytecode):generateBuiltinBytecodesloops over all internal module / builtinfunction sources (via codegen-generated
Bun__getInternalModuleSource/Bun__getBuiltinFunctionSourceextern functions)recursivelyGenerateUnlinkedCodeBlockForBuiltinFunction+encodeBuiltinFunctionExecutable.bunsection as 128-byte-aligned entries with(kind, id, bytecode)tuplesRuntime (standalone executable):
fromBytesdeserializes theBuiltinBytecodeEntry[]tableInternalModuleRegistry::generateModule/DEFINE_BUILTIN_EXECUTABLESmacro first try
Bun__findInternalModuleBytecode/Bun__findBuiltinFunctionBytecodedecodeBuiltinFunctionExecutablerestores the executablewithout parsing; otherwise falls back to the existing parse path
Constraints
BUN_DYNAMIC_JS_LOAD_PATH)Depends on WebKit PR: oven-sh/WebKit#177