Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,5 @@
- Allow later Windows pipe stdout/stderr writes after `end()` and pipeline completion, matching Node while preserving POSIX socket shutdown.

- Preserve already-fetched CommonJS-to-ESM module keys through linking so Windows forward-slash aliases and paths containing dot segments do not crash when reading exports.

- Cache repeated `node:vm` compilations after 1,750 distinct source fingerprints with a 256 MiB per-VM byte LRU, configurable through `BUN_VM_COMPILE_CACHE_SIZE`, while preserving fresh evaluations, context globals, import callback identities, and cached-data validation.
10 changes: 10 additions & 0 deletions docs/runtime/nodejs-compat.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,16 @@ Map and Set serialization copies entries and ignores custom own properties, like

🟢 Fully implemented, including `vm.Script`, `vm.createContext`, `vm.runInContext`, `vm.runInNewContext`, `vm.runInThisContext`, `vm.compileFunction`, `vm.isContext`, the ES module classes `vm.Module`, `vm.SourceTextModule` and `vm.SyntheticModule` (exported without `--experimental-vm-modules`), and `importModuleDynamically` support. The `timeout`, `breakOnSigint`, `cachedData`, `microtaskMode` and `codeGeneration` options are supported. An `importModuleDynamically` callback that returns a promise for a `vm.Module` resolves `import()` to the module object rather than its namespace. `vm.measureMemory()` reports whole-heap figures for every context.

The OpenClaw fork caches repeated `vm.Script` and `vm.compileFunction` compilations, including the `runInThisContext`, `runInContext`, and `runInNewContext` helpers. Each evaluation still creates fresh values and uses the target context's global object. Source text, filenames, offsets, compilation options, and dynamic import callback identity distinguish entries. Explicit `cachedData` uses the existing validation path.

Admission starts after a VM observes **1,750 distinct successfully compiled source fingerprints** (the memoized JSC source hash and source length). This leaves 250 entries below JSC’s 2,000-entry pruning boundary for other compilation users. Before admission, compilation uses the existing JSC path without source copies, bytecode serialization or retained payloads. A bounded fingerprint set tracks admission; hash collisions can postpone activation but cannot produce a false compilation hit. Activation lasts for the VM’s lifetime, and the fingerprint set is then released. `BUN_VM_COMPILE_CACHE_THRESHOLD` accepts an integer from `0` (immediate activation) through `2000`; invalid values use `1750`. Both settings are read on first use.

The cache has a **256 MiB budget per JavaScript VM** (each worker has its own VM). Entries are evicted in least-recently-used order. Set `BUN_VM_COMPILE_CACHE_SIZE` to a decimal byte count before starting Bun; `0` disables this cache. Invalid values use the default. The value is read on first use. The admission tracker holds at most 2,001 64-bit fingerprints (about 32 KiB of table storage, excluding allocator overhead) before activation; setting the byte budget to zero disables tracking too. The active cache budget charges copied source and filename bytes, immutable bytecode payloads, and a 1 KiB metadata reservation per entry. Allocator overhead, temporary compilation buffers, and code or payloads still held by live Scripts/functions are outside the cache budget. Decoded code is held weakly, so its lazily compiled children do not become additional cache-owned memory. The first request saves the compiled program; a repeated request also caches nested function bytecode when it fits the budget.

Admission tracking adds a small cost to smaller graphs. In a Linux replay of captured module wrappers, 500 modules took **6.95 ms on fork main and 7.19 ms with the cache**: 3.5% slower, or about 0.5 µs per compilation. The paired 95% bootstrap interval across 20 pairs was 1.011–1.054 times main. An initial paired run measured about 256 KiB higher median peak RSS; later RSS samples overlapped main. At 2,500 modules, warm replay fell from **191.6 to 52.6 ms** (72.5% faster). At 4,000, it fell from **371.4 to 85.1 ms** (77.1% faster). Those larger-graph results use five interleaved runs per runtime. The trade-off targets graphs near or beyond JSC's pruning boundary. Set `BUN_VM_COMPILE_CACHE_SIZE=0` to disable it.

The same replay took Node 24 **39.7 ms at 2,500 modules and 63.4 ms at 4,000**. Bun's bounded cache remained 32.7% and 34.3% slower, respectively. These workload-specific measurements do not promise Node parity. Further engine work could provide a retained-size and growth contract for complete unlinked-code graphs, or explicit ownership for decoded code that borrows storage from evictable bytecode payloads. Either approach would need separate lifetime and performance validation. This cache changes neither WebKit nor application dependencies.

### [`node:wasi`](https://nodejs.org/api/wasi.html)

🟡 Partially implemented. `WASI` supports `args`, `env`, `preopens`, `wasiImport` and `start()`, and `bun ./program.wasm` runs a WASI command directly. Missing `getImportObject()` (use `wasiImport`), `initialize()` and the `sock_accept` import. Bun ignores the `version`, `returnOnExit`, `stdin`, `stdout` and `stderr` options, so `proc_exit` exits the Bun process.
Expand Down
13 changes: 13 additions & 0 deletions src/js/internal-for-testing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -877,3 +877,16 @@ export const internalModuleBytecode: {
(index: number): { name: string; bytecode: Uint8Array; strings: Uint8Array } | null;
(source: string, name: string): { name: string; bytecode: Uint8Array; strings: Uint8Array };
} = $newCppFunction("InternalModuleRegistry.cpp", "jsInternalModuleBytecode", 2);

export const nodeVMCompilationCacheStats: () => {
limit: number;
admissionThreshold: number;
observedSources: number;
active: boolean;
bytes: number;
entries: number;
hits: number;
decodes: number;
misses: number;
evictions: number;
} = $cpp("NodeVMCompilationCache.cpp", "createNodeVMCompilationCacheStatsForTesting");
5 changes: 5 additions & 0 deletions src/jsc/bindings/BunClientData.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class DOMWrapperWorld;
#include "DOMURLBaseCache.h"
#include "NodeVMOptionNames.h"
#include "NodeVMSourceOriginCache.h"
#include "NodeVMCompilationCache.h"
#include <JavaScriptCore/HeapObserver.h>
namespace Zig {
class GlobalObject;
Expand Down Expand Up @@ -347,6 +348,10 @@ class JSVMClientData : public JSC::VM::ClientData {
Bun::NodeVMOptionNames m_nodeVMOptionNames;
Bun::NodeVMSourceOriginCache m_nodeVMSourceOriginCache;

public:
Bun::NodeVMCompilationCache nodeVMCompilationCache;

private:
Bun::HeapSizeAfterLastCollection m_heapSizeAfterLastCollection;

SentinelLinkedList<JSVMClientDataClient, BasicRawSentinelNode<JSVMClientDataClient>> m_clients;
Expand Down
19 changes: 15 additions & 4 deletions src/jsc/bindings/NodeVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ JSC::JSFunction* constructAnonymousFunction(JSC::JSGlobalObject* globalObject, c

SourceCode sourceCode(JSC::StringSourceProvider::create(program, sourceOrigin, WTF::move(options.filename), sourceTaintOrigin, wrappedPosition, SourceProviderSourceType::Program));

CodeCache* cache = vm.codeCache();
NodeVMCompilationCache::Identity cacheIdentity { options.lineOffset.zeroBasedInt(), options.columnOffset.zeroBasedInt(), NodeVMCompilationCache::Kind::Function, options.filenameProvided, options.produceCachedData, {}, lexicallyScopedFeatures };
ProgramExecutable* programExecutable = ProgramExecutable::create(globalObject, sourceCode);

UnlinkedProgramCodeBlock* unlinkedProgramCodeBlock = nullptr;
Expand All @@ -231,7 +231,14 @@ JSC::JSFunction* constructAnonymousFunction(JSC::JSGlobalObject* globalObject, c
ParserError error;

if (unlinkedProgramCodeBlock == nullptr) {
unlinkedProgramCodeBlock = cache->getUnlinkedProgramCodeBlock(vm, programExecutable, sourceCode, {}, error);
auto& cache = WebCore::clientData(vm)->nodeVMCompilationCache;
if (cache.isActive())
unlinkedProgramCodeBlock = cache.getOrCompile(globalObject, programExecutable, sourceCode, cacheIdentity, options.hasCachedData, error);
else {
unlinkedProgramCodeBlock = vm.codeCache()->getUnlinkedProgramCodeBlock(vm, programExecutable, sourceCode, {}, error);
if (unlinkedProgramCodeBlock)
cache.observeCompilation(globalObject, sourceCode, cacheIdentity, options.hasCachedData, unlinkedProgramCodeBlock);
}
}

if (!unlinkedProgramCodeBlock || error.isValid()) {
Expand Down Expand Up @@ -259,7 +266,9 @@ JSC::JSFunction* constructAnonymousFunction(JSC::JSGlobalObject* globalObject, c

if (bytecodeAccepted == TriState::Indeterminate) {
if (options.produceCachedData) {
RefPtr<JSC::CachedBytecode> producedBytecode = getBytecode(globalObject, JSC::SourceCodeType::ProgramType, sourceCode);
RefPtr<JSC::CachedBytecode> producedBytecode = WebCore::clientData(vm)->nodeVMCompilationCache.bytecode(sourceCode, cacheIdentity);
if (!producedBytecode)
producedBytecode = getBytecode(globalObject, JSC::SourceCodeType::ProgramType, sourceCode);
if (producedBytecode) {
JSC::JSUint8Array* buffer = WebCore::createBuffer(globalObject, producedBytecode->span());
RETURN_IF_EXCEPTION(throwScope, nullptr);
Expand Down Expand Up @@ -1979,8 +1988,10 @@ bool CompileFunctionOptions::fromJS(JSC::JSGlobalObject* globalObject, JSC::VM&
// The validators return false both for "absent" and for "threw".
RETURN_IF_EXCEPTION(scope, false);

if (validateCachedData(globalObject, vm, scope, options, this->cachedData))
if (validateCachedData(globalObject, vm, scope, options, this->cachedData)) {
this->hasCachedData = true;
any = true;
}
RETURN_IF_EXCEPTION(scope, false);

JSValue parsingContextValue = options->getIfPropertyExists(globalObject, optionNames(vm).parsingContext(vm));
Expand Down
1 change: 1 addition & 0 deletions src/jsc/bindings/NodeVM.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class CompileFunctionOptions : public BaseVMOptions {
JSGlobalObject* parsingContext = nullptr;
JSValue contextExtensions {};
bool produceCachedData = false;
bool hasCachedData = false;

using BaseVMOptions::BaseVMOptions;

Expand Down
Loading
Loading