Compare commits

...

1 Commits

Author SHA1 Message Date
Ben Grant
5f4b2115cd Use separate function to make V8 heap snapshots instead of parameter 2025-01-06 16:31:35 -08:00
4 changed files with 20 additions and 28 deletions

View File

@@ -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.

View File

@@ -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

View File

@@ -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;
}

View File

@@ -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));