diff options
author | 2023-07-30 23:51:43 -0700 | |
---|---|---|
committer | 2023-07-30 23:51:43 -0700 | |
commit | 9ecae59bbb1a302977afa94fd879a0c6f8d6195f (patch) | |
tree | 7bfff6d9c3264b8d1fce85f63891a1f3e5833a52 /src/bun.js/bindings/BunString.cpp | |
parent | 2ea7290172da21f4a9cd58232bf9c1ea0997d6e2 (diff) | |
download | bun-9ecae59bbb1a302977afa94fd879a0c6f8d6195f.tar.gz bun-9ecae59bbb1a302977afa94fd879a0c6f8d6195f.tar.zst bun-9ecae59bbb1a302977afa94fd879a0c6f8d6195f.zip |
Fix memory leak in response.clone(), further reduce memory usage of Request & Response (#3902)
* 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>
Diffstat (limited to 'src/bun.js/bindings/BunString.cpp')
-rw-r--r-- | src/bun.js/bindings/BunString.cpp | 25 |
1 files changed, 18 insertions, 7 deletions
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 <JavaScriptCore/JSONObject.h> +#include <wtf/text/AtomString.h> 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; |