From 9ecae59bbb1a302977afa94fd879a0c6f8d6195f Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Sun, 30 Jul 2023 23:51:43 -0700 Subject: Fix memory leak in response.clone(), further reduce memory usage of Request & Response (#3902) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Atomize respsone.url & response.statusText * Fix warning * Atomize Request & Response URLs when possible * Fix memory leak in response.clone() bun/bench/snippets on  jarred/atomize ❯ mem bun --smol request-response-clone.mjs cpu: Apple M1 Max runtime: bun 0.7.2 (arm64-darwin) benchmark time (avg) (min … max) p75 p99 p995 -------------------------------------------------------- ----------------------------- req.clone().url 77.3 ns/iter (40.35 ns … 222.64 ns) 91.53 ns 128.11 ns 172.78 ns resp.clone().url 162.43 ns/iter (116 ns … 337.77 ns) 177.4 ns 232.38 ns 262.65 ns Peak memory usage: 60 MB bun/bench/snippets on  jarred/atomize ❯ mem bun-0.7.1 --smol request-response-clone.mjs cpu: Apple M1 Max runtime: bun 0.7.1 (arm64-darwin) benchmark time (avg) (min … max) p75 p99 p995 -------------------------------------------------------- ----------------------------- req.clone().url 115.85 ns/iter (80.35 ns … 247.39 ns) 128.19 ns 181.93 ns 207.23 ns resp.clone().url 252.32 ns/iter (202.6 ns … 351.07 ns) 266.56 ns 325.88 ns 334.73 ns Peak memory usage: 1179 MB * Update tests * Update js_ast.zig * Update test --------- Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> --- src/bun.js/bindings/BunString.cpp | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) (limited to 'src/bun.js/bindings/BunString.cpp') diff --git a/src/bun.js/bindings/BunString.cpp b/src/bun.js/bindings/BunString.cpp index 5eb5f9f9b..7864ee94e 100644 --- a/src/bun.js/bindings/BunString.cpp +++ b/src/bun.js/bindings/BunString.cpp @@ -6,6 +6,7 @@ #include "wtf/text/ExternalStringImpl.h" #include "GCDefferalContext.h" #include +#include using namespace JSC; @@ -25,11 +26,23 @@ extern "C" void Bun__WTFStringImpl__ref(WTF::StringImpl* impl) extern "C" bool BunString__fromJS(JSC::JSGlobalObject* globalObject, JSC::EncodedJSValue encodedValue, BunString* bunString) { + JSC::JSValue value = JSC::JSValue::decode(encodedValue); *bunString = Bun::toString(globalObject, value); return bunString->tag != BunStringTag::Dead; } +extern "C" BunString BunString__createAtom(const char* bytes, size_t length) +{ + if (simdutf::validate_ascii(bytes, length)) { + auto atom = makeAtomString(String(StringImpl::createWithoutCopying(bytes, length))); + atom.impl()->ref(); + return { BunStringTag::WTFStringImpl, { .wtf = atom.impl() } }; + } + + return { BunStringTag::Dead, {} }; +} + namespace Bun { JSC::JSValue toJS(JSC::JSGlobalObject* globalObject, BunString bunString) { @@ -179,7 +192,6 @@ extern "C" BunString BunString__fromUTF16Unitialized(size_t length) if (UNLIKELY(!ptr)) return { BunStringTag::Dead }; - impl->ref(); return { BunStringTag::WTFStringImpl, { .wtf = &impl.leakRef() } }; } @@ -190,7 +202,6 @@ extern "C" BunString BunString__fromLatin1Unitialized(size_t length) auto impl = WTF::StringImpl::createUninitialized(latin1Length, ptr); if (UNLIKELY(!ptr)) return { BunStringTag::Dead }; - impl->ref(); return { BunStringTag::WTFStringImpl, { .wtf = &impl.leakRef() } }; } @@ -302,10 +313,10 @@ extern "C" EncodedJSValue BunString__createArray( extern "C" void BunString__toWTFString(BunString* bunString) { if (bunString->tag == BunStringTag::ZigString) { - if (Zig::isTaggedUTF8Ptr(bunString->impl.zig.ptr)) { - bunString->impl.wtf = Zig::toStringCopy(bunString->impl.zig).impl(); - } else { + if (Zig::isTaggedExternalPtr(bunString->impl.zig.ptr)) { bunString->impl.wtf = Zig::toString(bunString->impl.zig).impl(); + } else { + bunString->impl.wtf = Zig::toStringCopy(bunString->impl.zig).impl(); } bunString->tag = BunStringTag::WTFStringImpl; @@ -356,7 +367,7 @@ extern "C" BunString URL__getHrefFromJS(EncodedJSValue encodedValue, JSC::JSGlob extern "C" BunString URL__getHref(BunString* input) { - auto str = Bun::toWTFString(*input); + auto&& str = Bun::toWTFString(*input); auto url = WTF::URL(str); if (!url.isValid() || url.isEmpty()) return { BunStringTag::Dead }; @@ -366,7 +377,7 @@ extern "C" BunString URL__getHref(BunString* input) extern "C" WTF::URL* URL__fromString(BunString* input) { - auto str = Bun::toWTFString(*input); + auto&& str = Bun::toWTFString(*input); auto url = WTF::URL(str); if (!url.isValid()) return nullptr; -- cgit v1.2.3