From 5f4b2115cd2600caef989fb0daa9e3e68e0ea362 Mon Sep 17 00:00:00 2001 From: Ben Grant Date: Mon, 6 Jan 2025 16:31:35 -0800 Subject: [PATCH] Use separate function to make V8 heap snapshots instead of parameter --- packages/bun-types/bun.d.ts | 10 ++++--- src/bun.js/bindings/BunObject.cpp | 32 ++++++++--------------- src/js/node/v8.ts | 4 +-- test/js/bun/util/v8-heap-snapshot.test.ts | 2 +- 4 files changed, 20 insertions(+), 28 deletions(-) diff --git a/packages/bun-types/bun.d.ts b/packages/bun-types/bun.d.ts index edf6a27e5a..0ed9969971 100644 --- a/packages/bun-types/bun.d.ts +++ b/packages/bun-types/bun.d.ts @@ -4176,9 +4176,11 @@ declare module "bun" { /** * Show precise statistics about memory usage of your application * - * Generate a heap snapshot in JavaScriptCore's format that can be viewed with `bun --inspect` or Safari's Web Inspector + * Generate a heap snapshot in JavaScriptCore's format that can be viewed with `bun --inspect` or Safari's Web Inspector. + * + * This returns an object that should be JSON stringified then written to a file. */ - function generateHeapSnapshot(format?: "jsc"): HeapSnapshot; + function generateHeapSnapshot(): HeapSnapshot; /** * Show precise statistics about memory usage of your application @@ -4187,11 +4189,11 @@ declare module "bun" { * * This is a JSON string that can be saved to a file. * ```ts - * const snapshot = Bun.generateHeapSnapshot("v8"); + * const snapshot = Bun.generateV8HeapSnapshot(); * await Bun.write("heap.heapsnapshot", snapshot); * ``` */ - function generateHeapSnapshot(format: "v8"): string; + function generateV8HeapSnapshot(): string; /** * The next time JavaScriptCore is idle, clear unused memory and attempt to reduce the heap size. diff --git a/src/bun.js/bindings/BunObject.cpp b/src/bun.js/bindings/BunObject.cpp index bd1760cfb0..1a5e4b0998 100644 --- a/src/bun.js/bindings/BunObject.cpp +++ b/src/bun.js/bindings/BunObject.cpp @@ -540,28 +540,7 @@ JSC_DEFINE_HOST_FUNCTION(functionGenerateHeapSnapshot, (JSC::JSGlobalObject * gl auto& heapProfiler = *vm.heapProfiler(); heapProfiler.clearSnapshots(); - JSValue arg0 = callFrame->argument(0); auto throwScope = DECLARE_THROW_SCOPE(vm); - bool useV8 = false; - if (!arg0.isUndefined()) { - if (arg0.isString()) { - auto str = arg0.toWTFString(globalObject); - RETURN_IF_EXCEPTION(throwScope, {}); - if (str == "v8"_s) { - useV8 = true; - } else if (str == "jsc"_s) { - // do nothing - } else { - throwTypeError(globalObject, throwScope, "Expected 'v8' or 'jsc' or undefined"_s); - return {}; - } - } - } - - if (useV8) { - JSC::BunV8HeapSnapshotBuilder builder(heapProfiler); - return JSC::JSValue::encode(jsString(vm, builder.json())); - } JSC::HeapSnapshotBuilder builder(heapProfiler); builder.buildSnapshot(); @@ -572,6 +551,16 @@ JSC_DEFINE_HOST_FUNCTION(functionGenerateHeapSnapshot, (JSC::JSGlobalObject * gl RELEASE_AND_RETURN(throwScope, JSC::JSValue::encode(jsonValue)); } +JSC_DEFINE_HOST_FUNCTION(functionGenerateV8HeapSnapshot, (JSC::JSGlobalObject * globalObject, JSC::CallFrame* callFrame)) +{ + auto& vm = globalObject->vm(); + vm.ensureHeapProfiler(); + auto& heapProfiler = *vm.heapProfiler(); + heapProfiler.clearSnapshots(); + JSC::BunV8HeapSnapshotBuilder builder(heapProfiler); + return JSC::JSValue::encode(jsString(vm, builder.json())); +} + JSC_DEFINE_HOST_FUNCTION(functionFileURLToPath, (JSC::JSGlobalObject * globalObject, JSC::CallFrame* callFrame)) { auto& vm = globalObject->vm(); @@ -648,6 +637,7 @@ JSC_DEFINE_HOST_FUNCTION(functionFileURLToPath, (JSC::JSGlobalObject * globalObj fileURLToPath functionFileURLToPath DontDelete|Function 1 gc Generated::BunObject::jsGc DontDelete|Function 1 generateHeapSnapshot functionGenerateHeapSnapshot DontDelete|Function 1 + generateV8HeapSnapshot functionGenerateV8HeapSnapshot DontDelete|Function 1 gunzipSync BunObject_callback_gunzipSync DontDelete|Function 1 gzipSync BunObject_callback_gzipSync DontDelete|Function 1 hash BunObject_getter_wrap_hash DontDelete|PropertyCallback diff --git a/src/js/node/v8.ts b/src/js/node/v8.ts index 5303d1b4e0..4fc3be09fe 100644 --- a/src/js/node/v8.ts +++ b/src/js/node/v8.ts @@ -36,7 +36,7 @@ function getHeapSnapshot() { class HeapSnapshotReadable extends Readable { constructor() { super(); - this.push(Bun.generateHeapSnapshot("v8")); + this.push(Bun.generateV8HeapSnapshot()); this.push(null); } } @@ -142,7 +142,7 @@ function writeHeapSnapshot(path, options) { if (!fs) { fs = require("node:fs"); } - fs.writeFileSync(path, Bun.generateHeapSnapshot("v8"), "utf-8"); + fs.writeFileSync(path, Bun.generateV8HeapSnapshot(), "utf-8"); return path; } diff --git a/test/js/bun/util/v8-heap-snapshot.test.ts b/test/js/bun/util/v8-heap-snapshot.test.ts index 5125b26667..47eadc5bc6 100644 --- a/test/js/bun/util/v8-heap-snapshot.test.ts +++ b/test/js/bun/util/v8-heap-snapshot.test.ts @@ -5,7 +5,7 @@ import * as v8 from "v8"; import * as v8HeapSnapshot from "v8-heapsnapshot"; test("v8 heap snapshot", async () => { - const snapshot = Bun.generateHeapSnapshot("v8"); + const snapshot = Bun.generateV8HeapSnapshot(); // Sanity check: run the validations from this library const parsed = await v8HeapSnapshot.parseSnapshot(JSON.parse(snapshot));