Skip to content

Commit 32c1fdf

Browse files
committed
Rename estimateDirectMemoryUsageOf to estimateShallowMemoryUsageOf
1 parent aada6f9 commit 32c1fdf

File tree

4 files changed

+25
-25
lines changed

4 files changed

+25
-25
lines changed

docs/api/utils.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -772,27 +772,27 @@ console.log(obj); // => { foo: "bar" }
772772

773773
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.
774774

775-
## `estimateDirectMemoryUsageOf` in `bun:jsc`
775+
## `estimateShallowMemoryUsageOf` in `bun:jsc`
776776

777-
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`.
777+
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`.
778778

779779
```js
780-
import { estimateDirectMemoryUsageOf } from "bun:jsc";
780+
import { estimateShallowMemoryUsageOf } from "bun:jsc";
781781

782782
const obj = { foo: "bar" };
783-
const usage = estimateDirectMemoryUsageOf(obj);
783+
const usage = estimateShallowMemoryUsageOf(obj);
784784
console.log(usage); // => 16
785785

786786
const buffer = Buffer.alloc(1024 * 1024);
787-
estimateDirectMemoryUsageOf(buffer);
787+
estimateShallowMemoryUsageOf(buffer);
788788
// => 1048624
789789

790790
const req = new Request("https://bun.sh");
791-
estimateDirectMemoryUsageOf(req);
791+
estimateShallowMemoryUsageOf(req);
792792
// => 167
793793

794794
const array = Array(1024).fill({ a: 1 });
795795
// Arrays are usually not stored contiguously in memory, so this will not return a useful value (which isn't a bug).
796-
estimateDirectMemoryUsageOf(array);
796+
estimateShallowMemoryUsageOf(array);
797797
// => 16
798798
```

packages/bun-types/jsc.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -225,5 +225,5 @@ declare module "bun:jsc" {
225225
*
226226
* Passing a primitive type that isn't heap allocated returns 0.
227227
*/
228-
function estimateDirectMemoryUsageOf(value: object | CallableFunction | bigint | symbol | string): number;
228+
function estimateShallowMemoryUsageOf(value: object | CallableFunction | bigint | symbol | string): number;
229229
}

src/bun.js/modules/BunJSCModule.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -935,7 +935,7 @@ JSC_DEFINE_HOST_FUNCTION(functionEstimateDirectMemoryUsageOf, (JSGlobalObject *
935935
setTimeZone functionSetTimeZone Function 0
936936
serialize functionSerialize Function 0
937937
deserialize functionDeserialize Function 0
938-
estimateDirectMemoryUsageOf functionEstimateDirectMemoryUsageOf Function 1
938+
estimateShallowMemoryUsageOf functionEstimateDirectMemoryUsageOf Function 1
939939
@end
940940
*/
941941

@@ -975,7 +975,7 @@ DEFINE_NATIVE_MODULE(BunJSC)
975975
putNativeFn(Identifier::fromString(vm, "setTimeZone"_s), functionSetTimeZone);
976976
putNativeFn(Identifier::fromString(vm, "serialize"_s), functionSerialize);
977977
putNativeFn(Identifier::fromString(vm, "deserialize"_s), functionDeserialize);
978-
putNativeFn(Identifier::fromString(vm, "estimateDirectMemoryUsageOf"_s), functionEstimateDirectMemoryUsageOf);
978+
putNativeFn(Identifier::fromString(vm, "estimateShallowMemoryUsageOf"_s), functionEstimateDirectMemoryUsageOf);
979979

980980
// Deprecated
981981
putNativeFn(Identifier::fromString(vm, "describe"_s), functionDescribe);

test/js/bun/util/heap-snapshot.test.ts

+15-15
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
import { describe, it, expect } from "bun:test";
22
import { parseHeapSnapshot, summarizeByType } from "./heap";
3-
import { estimateDirectMemoryUsageOf } from "bun:jsc";
3+
import { estimateShallowMemoryUsageOf } from "bun:jsc";
44

55
describe("Native types report their size correctly", () => {
66
it("FormData", () => {
77
var formData = new FormData();
88
globalThis.formData = formData;
9-
let original = estimateDirectMemoryUsageOf(formData);
9+
let original = estimateShallowMemoryUsageOf(formData);
1010
formData.append("a", Buffer.alloc(1024 * 1024 * 8, "abc").toString());
11-
const afterBuffer = estimateDirectMemoryUsageOf(formData);
11+
const afterBuffer = estimateShallowMemoryUsageOf(formData);
1212
expect(afterBuffer).toBeGreaterThan(original + 1024 * 1024 * 8);
1313
formData.append("a", new Blob([Buffer.alloc(1024 * 1024 * 2, "yooa")]));
14-
const afterBlob = estimateDirectMemoryUsageOf(formData);
14+
const afterBlob = estimateShallowMemoryUsageOf(formData);
1515
expect(afterBlob).toBeGreaterThan(afterBuffer + 1024 * 1024 * 2);
1616
formData.append("a", new Blob([Buffer.alloc(1024 * 1024 * 2, "yooa")]));
17-
const afterBlob2 = estimateDirectMemoryUsageOf(formData);
17+
const afterBlob2 = estimateShallowMemoryUsageOf(formData);
1818
expect(afterBlob2).toBeGreaterThan(afterBlob + 1024 * 1024 * 2);
1919

2020
const snapshot = Bun.generateHeapSnapshot();
@@ -88,11 +88,11 @@ describe("Native types report their size correctly", () => {
8888
it("URLSearchParams", () => {
8989
const searchParams = new URLSearchParams();
9090
globalThis.searchParams = searchParams;
91-
const original = estimateDirectMemoryUsageOf(searchParams);
91+
const original = estimateShallowMemoryUsageOf(searchParams);
9292
for (let i = 0; i < 1000; i++) {
9393
searchParams.set(`a${i}`, `b${i}`);
9494
}
95-
const after = estimateDirectMemoryUsageOf(searchParams);
95+
const after = estimateShallowMemoryUsageOf(searchParams);
9696
expect(after).toBeGreaterThan(original + 1000 * 2);
9797

9898
const snapshot = Bun.generateHeapSnapshot();
@@ -110,11 +110,11 @@ describe("Native types report their size correctly", () => {
110110

111111
it("Headers", () => {
112112
const headers = new Headers();
113-
const original = estimateDirectMemoryUsageOf(headers);
113+
const original = estimateShallowMemoryUsageOf(headers);
114114
for (let i = 0; i < 1000; i++) {
115115
headers.set(`a${i}`, `b${i}`);
116116
}
117-
const after = estimateDirectMemoryUsageOf(headers);
117+
const after = estimateShallowMemoryUsageOf(headers);
118118
expect(after).toBeGreaterThan(original + 1000 * 2);
119119

120120
globalThis.headers = headers;
@@ -137,9 +137,9 @@ describe("Native types report their size correctly", () => {
137137
open(ws) {},
138138
drain(ws) {},
139139
message(ws, message) {
140-
const before = estimateDirectMemoryUsageOf(ws);
140+
const before = estimateShallowMemoryUsageOf(ws);
141141
ws.send(message);
142-
const after = estimateDirectMemoryUsageOf(ws);
142+
const after = estimateShallowMemoryUsageOf(ws);
143143
const bufferedAmount = ws.getBufferedAmount();
144144
if (bufferedAmount > 0) {
145145
expect(after).toBeGreaterThan(before + bufferedAmount);
@@ -148,9 +148,9 @@ describe("Native types report their size correctly", () => {
148148
},
149149

150150
fetch(req, server) {
151-
const before = estimateDirectMemoryUsageOf(req);
151+
const before = estimateShallowMemoryUsageOf(req);
152152
server.upgrade(req);
153-
const after = estimateDirectMemoryUsageOf(req);
153+
const after = estimateShallowMemoryUsageOf(req);
154154

155155
// We detach the request context from the request object on upgrade.
156156
expect(after).toBeLessThan(before);
@@ -159,7 +159,7 @@ describe("Native types report their size correctly", () => {
159159
},
160160
});
161161
const ws = new WebSocket(server.url);
162-
const original = estimateDirectMemoryUsageOf(ws);
162+
const original = estimateShallowMemoryUsageOf(ws);
163163
globalThis.ws = ws;
164164

165165
const { promise, resolve } = Promise.withResolvers();
@@ -172,7 +172,7 @@ describe("Native types report their size correctly", () => {
172172
};
173173
await promise;
174174

175-
const after = estimateDirectMemoryUsageOf(ws);
175+
const after = estimateShallowMemoryUsageOf(ws);
176176
expect(after).toBeGreaterThan(original + 1024 * 128);
177177

178178
const snapshot = Bun.generateHeapSnapshot();

0 commit comments

Comments
 (0)