diff options
author | 2023-07-17 04:15:13 -0700 | |
---|---|---|
committer | 2023-07-17 04:15:13 -0700 | |
commit | 36a25c358044b0c9a56a06d8246ae2b5098b3ae4 (patch) | |
tree | a869888ad4d39e9a5a6dc0b9f7d5e8b1c7528691 /src/bun.js/bindings/ZigGlobalObject.cpp | |
parent | 13b5d9d4de4e9fe897d373a6ed7fdf2c9884c71f (diff) | |
download | bun-36a25c358044b0c9a56a06d8246ae2b5098b3ae4.tar.gz bun-36a25c358044b0c9a56a06d8246ae2b5098b3ae4.tar.zst bun-36a25c358044b0c9a56a06d8246ae2b5098b3ae4.zip |
Fix memory leak in `await new Response(latin1String).arrayBuffer()` and `await Response.json(obj).json()` (#3656)
❯ mem bun --smol response-arrayBuffer.mjs
cpu: Apple M1 Max
runtime: bun 0.6.15 (arm64-darwin)
benchmark time (avg) (min … max) p75 p99 p995
--------------------------------------------------------------------------------------------------- -----------------------------
new Response().arrayBuffer() (new string each call, latin1) 12.9 µs/iter (625 ns … 4.18 ms) 1 µs 567.17 µs 711.79 µs
new Response().arrayBuffer() (new string each call, utf16) 12.85 µs/iter (1.67 µs … 1.56 ms) 2.17 µs 462.75 µs 621.13 µs
new Response().arrayBuffer() (existing string, latin1) 6.53 µs/iter (6.21 µs … 7.07 µs) 6.64 µs 7.07 µs 7.07 µs
Peak memory usage: 49 MB
bun on jarred/memory-leak-fix took 2s
❯ mem bun response-arrayBuffer.mjs
cpu: Apple M1 Max
runtime: bun 0.6.15 (arm64-darwin)
benchmark time (avg) (min … max) p75 p99 p995
--------------------------------------------------------------------------------------------------- -----------------------------
new Response().arrayBuffer() (new string each call, latin1) 1.2 µs/iter (1.04 µs … 1.42 µs) 1.22 µs 1.42 µs 1.42 µs
new Response().arrayBuffer() (new string each call, utf16) 2.74 µs/iter (2.42 µs … 6.37 µs) 2.68 µs 6.37 µs 6.37 µs
new Response().arrayBuffer() (existing string, latin1) 746.37 ns/iter (643.82 ns … 1.04 µs) 776.11 ns 1.04 µs 1.04 µs
Peak memory usage: 104 MB
bun on jarred/memory-leak-fix took 2s
❯ mem ~/.bun/bin/bun response-arrayBuffer.mjs
cpu: Apple M1 Max
runtime: bun 0.6.15 (arm64-darwin)
benchmark time (avg) (min … max) p75 p99 p995
--------------------------------------------------------------------------------------------------- -----------------------------
new Response().arrayBuffer() (new string each call, latin1) 1.69 µs/iter (1.56 µs … 2.1 µs) 1.73 µs 2.1 µs 2.1 µs
new Response().arrayBuffer() (new string each call, utf16) 2.65 µs/iter (2.47 µs … 3.17 µs) 2.69 µs 3.17 µs 3.17 µs
new Response().arrayBuffer() (existing string, latin1) 667.67 ns/iter (547.67 ns … 1.28 µs) 694.21 ns 1.28 µs 1.28 µs
Peak memory usage: 2735 MB
bun on jarred/memory-leak-fix took 2s
❯ mem ~/.bun/bin/bun --smol response-arrayBuffer.mjs
cpu: Apple M1 Max
runtime: bun 0.6.15 (arm64-darwin)
benchmark time (avg) (min … max) p75 p99 p995
--------------------------------------------------------------------------------------------------- -----------------------------
new Response().arrayBuffer() (new string each call, latin1) 13.51 µs/iter (541 ns … 3.2 ms) 1.92 µs 553.42 µs 709.92 µs
new Response().arrayBuffer() (new string each call, utf16) 13.07 µs/iter (1.71 µs … 3.43 ms) 2.13 µs 451.21 µs 651.67 µs
new Response().arrayBuffer() (existing string, latin1) 6.25 µs/iter (5.79 µs … 6.81 µs) 6.4 µs 6.81 µs 6.81 µs
Peak memory usage: 292 MB
Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
Diffstat (limited to 'src/bun.js/bindings/ZigGlobalObject.cpp')
-rw-r--r-- | src/bun.js/bindings/ZigGlobalObject.cpp | 70 |
1 files changed, 0 insertions, 70 deletions
diff --git a/src/bun.js/bindings/ZigGlobalObject.cpp b/src/bun.js/bindings/ZigGlobalObject.cpp index 7c0e1668b..a5c96ee08 100644 --- a/src/bun.js/bindings/ZigGlobalObject.cpp +++ b/src/bun.js/bindings/ZigGlobalObject.cpp @@ -249,76 +249,6 @@ extern "C" void JSCInitialize(const char* envp[], size_t envc, void (*onCrash)(c #endif JSC::Options::useSetMethods() = true; - /* - * The Magic "Use Less RAM" button - */ - // # With /= 16,184 - // cpu: Apple M1 Max - // runtime: bun 0.6.10 (arm64-darwin) - // benchmark time (avg) (min … max) p75 p99 p995 - // ------------------------------------------------------------------- ----------------------------- - // crypto.createHash("sha512") 953.74 ns/iter (842.34 ns … 1.11 µs) 1 µs 1.11 µs 1.11 µs - // crypto.createHash("sha256") 704.35 ns/iter (594.42 ns … 912.4 ns) 754.89 ns 912.4 ns 912.4 ns - // crypto.createHash("sha1") 608.53 ns/iter (488.66 ns … 724.54 ns) 637.75 ns 724.54 ns 724.54 ns - // Peak memory usage: 59 MB - // # With /= 8096 - // cpu: Apple M1 Max - // runtime: bun 0.6.10 (arm64-darwin) - // benchmark time (avg) (min … max) p75 p99 p995 - // ------------------------------------------------------------------- ----------------------------- - // crypto.createHash("sha512") 821.71 ns/iter (716.88 ns … 1.01 µs) 859.07 ns 1.01 µs 1.01 µs - // crypto.createHash("sha256") 534.89 ns/iter (439.35 ns … 737.17 ns) 576.15 ns 633.4 ns 737.17 ns - // crypto.createHash("sha1") 529.1 ns/iter (432.46 ns … 766.94 ns) 566.07 ns 653.13 ns 766.94 ns - // Peak memory usage: 60 MB - // # With /= 1024 - // cpu: Apple M1 Max - // runtime: bun 0.6.10 (arm64-darwin) - // benchmark time (avg) (min … max) p75 p99 p995 - // ------------------------------------------------------------------- ----------------------------- - // crypto.createHash("sha512") 751.13 ns/iter (703.45 ns … 947.73 ns) 754.54 ns 947.73 ns 947.73 ns - // crypto.createHash("sha256") 501.47 ns/iter (467.19 ns … 580.84 ns) 505.66 ns 572.42 ns 580.84 ns - // crypto.createHash("sha1") 482.16 ns/iter (452.7 ns … 570.29 ns) 482.74 ns 565.52 ns 570.29 ns - // Peak memory usage: 169 MB - // # With /= 256 - // cpu: Apple M1 Max - // runtime: bun 0.6.10 (arm64-darwin) - // benchmark time (avg) (min … max) p75 p99 p995 - // ------------------------------------------------------------------- ----------------------------- - // crypto.createHash("sha512") 745.25 ns/iter (705.91 ns … 863.73 ns) 747.8 ns 863.73 ns 863.73 ns - // crypto.createHash("sha256") 506.94 ns/iter (459.27 ns … 692.93 ns) 514.95 ns 611.72 ns 692.93 ns - // crypto.createHash("sha1") 486.19 ns/iter (456.13 ns … 749.72 ns) 487.5 ns 578.84 ns 749.72 ns - // Peak memory usage: 270 MB - // # Unset - // cpu: Apple M1 Max - // runtime: bun 0.6.10 (arm64-darwin) - // benchmark time (avg) (min … max) p75 p99 p995 - // ------------------------------------------------------------------- ----------------------------- - // crypto.createHash("sha512") 752.96 ns/iter (705.88 ns … 954.63 ns) 758.78 ns 954.63 ns 954.63 ns - // crypto.createHash("sha256") 503.72 ns/iter (466.7 ns … 602.83 ns) 507.22 ns 593.72 ns 602.83 ns - // crypto.createHash("sha1") 484.25 ns/iter (454.55 ns … 555.84 ns) 487.96 ns 553.41 ns 555.84 ns - // Peak memory usage: 273 MB - // # Node.js, for comparison - // cpu: Apple M1 Max - // runtime: node v20.1.0 (arm64-darwin) - // benchmark time (avg) (min … max) p75 p99 p995 - // ------------------------------------------------------------------- ----------------------------- - // crypto.createHash("sha512") 1.82 µs/iter (1.77 µs … 2.17 µs) 1.84 µs 2.17 µs 2.17 µs - // crypto.createHash("sha256") 964 ns/iter (946.02 ns … 1.1 µs) 962.95 ns 1.1 µs 1.1 µs - // crypto.createHash("sha1") 985.26 ns/iter (956.7 ns … 1.12 µs) 1 µs 1.12 µs 1.12 µs - // Peak memory usage: 56 MB - size_t ramSize = WTF::ramSize(); - - // We originally went with a hardcoded /= 1024 here - // But if you don't have much memory, that becomes a problem. - // Instead, we do 65% - double ramSizeDouble = static_cast<double>(ramSize); - ramSizeDouble *= 0.65; - ramSize = static_cast<size_t>(ramSizeDouble); - - if (ramSize > 0) { - JSC::Options::forceRAMSize() = ramSize; - } - if (LIKELY(envc > 0)) { while (envc--) { const char* env = (const char*)envp[envc]; |