Rename estimateDirectMemoryUsageOf to estimateShallowMemoryUsageOf

This commit is contained in:
Jarred Sumner
2024-12-16 20:18:04 -08:00
committed by Kai Tamkun
parent d8043f9a5c
commit 219c067b69
4 changed files with 25 additions and 25 deletions

View File

@@ -772,27 +772,27 @@ console.log(obj); // => { foo: "bar" }
Internally, [`structuredClone`](https://developer.mozilla.org/en-US/docs/Web/API/structuredClone) and [`postMessage`](https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage) serialize and deserialize the same way. This exposes the underlying [HTML Structured Clone Algorithm](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm) to JavaScript as an ArrayBuffer.
## `estimateDirectMemoryUsageOf` in `bun:jsc`
## `estimateShallowMemoryUsageOf` in `bun:jsc`
The `estimateDirectMemoryUsageOf` function returns a best-effort estimate of the memory usage of an object in bytes, excluding the memory usage of properties or other objects it references. For accurate per-object memory usage, use `Bun.generateHeapSnapshot`.
The `estimateShallowMemoryUsageOf` function returns a best-effort estimate of the memory usage of an object in bytes, excluding the memory usage of properties or other objects it references. For accurate per-object memory usage, use `Bun.generateHeapSnapshot`.
```js
import { estimateDirectMemoryUsageOf } from "bun:jsc";
import { estimateShallowMemoryUsageOf } from "bun:jsc";
const obj = { foo: "bar" };
const usage = estimateDirectMemoryUsageOf(obj);
const usage = estimateShallowMemoryUsageOf(obj);
console.log(usage); // => 16
const buffer = Buffer.alloc(1024 * 1024);
estimateDirectMemoryUsageOf(buffer);
estimateShallowMemoryUsageOf(buffer);
// => 1048624
const req = new Request("https://bun.sh");
estimateDirectMemoryUsageOf(req);
estimateShallowMemoryUsageOf(req);
// => 167
const array = Array(1024).fill({ a: 1 });
// Arrays are usually not stored contiguously in memory, so this will not return a useful value (which isn't a bug).
estimateDirectMemoryUsageOf(array);
estimateShallowMemoryUsageOf(array);
// => 16
```

View File

@@ -225,5 +225,5 @@ declare module "bun:jsc" {
*
* Passing a primitive type that isn't heap allocated returns 0.
*/
function estimateDirectMemoryUsageOf(value: object | CallableFunction | bigint | symbol | string): number;
function estimateShallowMemoryUsageOf(value: object | CallableFunction | bigint | symbol | string): number;
}

View File

@@ -935,7 +935,7 @@ JSC_DEFINE_HOST_FUNCTION(functionEstimateDirectMemoryUsageOf, (JSGlobalObject *
setTimeZone functionSetTimeZone Function 0
serialize functionSerialize Function 0
deserialize functionDeserialize Function 0
estimateDirectMemoryUsageOf functionEstimateDirectMemoryUsageOf Function 1
estimateShallowMemoryUsageOf functionEstimateDirectMemoryUsageOf Function 1
@end
*/
@@ -975,7 +975,7 @@ DEFINE_NATIVE_MODULE(BunJSC)
putNativeFn(Identifier::fromString(vm, "setTimeZone"_s), functionSetTimeZone);
putNativeFn(Identifier::fromString(vm, "serialize"_s), functionSerialize);
putNativeFn(Identifier::fromString(vm, "deserialize"_s), functionDeserialize);
putNativeFn(Identifier::fromString(vm, "estimateDirectMemoryUsageOf"_s), functionEstimateDirectMemoryUsageOf);
putNativeFn(Identifier::fromString(vm, "estimateShallowMemoryUsageOf"_s), functionEstimateDirectMemoryUsageOf);
// Deprecated
putNativeFn(Identifier::fromString(vm, "describe"_s), functionDescribe);

View File

@@ -1,20 +1,20 @@
import { describe, it, expect } from "bun:test";
import { parseHeapSnapshot, summarizeByType } from "./heap";
import { estimateDirectMemoryUsageOf } from "bun:jsc";
import { estimateShallowMemoryUsageOf } from "bun:jsc";
describe("Native types report their size correctly", () => {
it("FormData", () => {
var formData = new FormData();
globalThis.formData = formData;
let original = estimateDirectMemoryUsageOf(formData);
let original = estimateShallowMemoryUsageOf(formData);
formData.append("a", Buffer.alloc(1024 * 1024 * 8, "abc").toString());
const afterBuffer = estimateDirectMemoryUsageOf(formData);
const afterBuffer = estimateShallowMemoryUsageOf(formData);
expect(afterBuffer).toBeGreaterThan(original + 1024 * 1024 * 8);
formData.append("a", new Blob([Buffer.alloc(1024 * 1024 * 2, "yooa")]));
const afterBlob = estimateDirectMemoryUsageOf(formData);
const afterBlob = estimateShallowMemoryUsageOf(formData);
expect(afterBlob).toBeGreaterThan(afterBuffer + 1024 * 1024 * 2);
formData.append("a", new Blob([Buffer.alloc(1024 * 1024 * 2, "yooa")]));
const afterBlob2 = estimateDirectMemoryUsageOf(formData);
const afterBlob2 = estimateShallowMemoryUsageOf(formData);
expect(afterBlob2).toBeGreaterThan(afterBlob + 1024 * 1024 * 2);
const snapshot = Bun.generateHeapSnapshot();
@@ -88,11 +88,11 @@ describe("Native types report their size correctly", () => {
it("URLSearchParams", () => {
const searchParams = new URLSearchParams();
globalThis.searchParams = searchParams;
const original = estimateDirectMemoryUsageOf(searchParams);
const original = estimateShallowMemoryUsageOf(searchParams);
for (let i = 0; i < 1000; i++) {
searchParams.set(`a${i}`, `b${i}`);
}
const after = estimateDirectMemoryUsageOf(searchParams);
const after = estimateShallowMemoryUsageOf(searchParams);
expect(after).toBeGreaterThan(original + 1000 * 2);
const snapshot = Bun.generateHeapSnapshot();
@@ -110,11 +110,11 @@ describe("Native types report their size correctly", () => {
it("Headers", () => {
const headers = new Headers();
const original = estimateDirectMemoryUsageOf(headers);
const original = estimateShallowMemoryUsageOf(headers);
for (let i = 0; i < 1000; i++) {
headers.set(`a${i}`, `b${i}`);
}
const after = estimateDirectMemoryUsageOf(headers);
const after = estimateShallowMemoryUsageOf(headers);
expect(after).toBeGreaterThan(original + 1000 * 2);
globalThis.headers = headers;
@@ -137,9 +137,9 @@ describe("Native types report their size correctly", () => {
open(ws) {},
drain(ws) {},
message(ws, message) {
const before = estimateDirectMemoryUsageOf(ws);
const before = estimateShallowMemoryUsageOf(ws);
ws.send(message);
const after = estimateDirectMemoryUsageOf(ws);
const after = estimateShallowMemoryUsageOf(ws);
const bufferedAmount = ws.getBufferedAmount();
if (bufferedAmount > 0) {
expect(after).toBeGreaterThan(before + bufferedAmount);
@@ -148,9 +148,9 @@ describe("Native types report their size correctly", () => {
},
fetch(req, server) {
const before = estimateDirectMemoryUsageOf(req);
const before = estimateShallowMemoryUsageOf(req);
server.upgrade(req);
const after = estimateDirectMemoryUsageOf(req);
const after = estimateShallowMemoryUsageOf(req);
// We detach the request context from the request object on upgrade.
expect(after).toBeLessThan(before);
@@ -159,7 +159,7 @@ describe("Native types report their size correctly", () => {
},
});
const ws = new WebSocket(server.url);
const original = estimateDirectMemoryUsageOf(ws);
const original = estimateShallowMemoryUsageOf(ws);
globalThis.ws = ws;
const { promise, resolve } = Promise.withResolvers();
@@ -172,7 +172,7 @@ describe("Native types report their size correctly", () => {
};
await promise;
const after = estimateDirectMemoryUsageOf(ws);
const after = estimateShallowMemoryUsageOf(ws);
expect(after).toBeGreaterThan(original + 1024 * 128);
const snapshot = Bun.generateHeapSnapshot();