From 926efa2246287c51b72620f2bc5d3b2ab0074980 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 29 Mar 2026 19:00:53 +0000 Subject: [PATCH] Add NativeStringContext and update NativeString to use pool allocation in Utilities.h Agent-Logs-Url: https://github.com/uNetworking/uWebSockets.js/sessions/0a6be420-47f1-4fb3-a5d5-55f6d09eb1b5 Co-authored-by: uNetworkingAB <110806833+uNetworkingAB@users.noreply.github.com> --- src/Utilities.h | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/src/Utilities.h b/src/Utilities.h index 6a58d6cb..0e5c9e8e 100644 --- a/src/Utilities.h +++ b/src/Utilities.h @@ -21,6 +21,7 @@ #include #include #include +#include using namespace v8; /* Unfortunately we _have_ to depend on Node.js crap */ @@ -118,13 +119,26 @@ struct Callback { } }; +class NativeStringContext { + inline static thread_local std::vector pool = std::vector(8 * 1024 * 1024); + size_t pool_offset = 0; +public: + char *alloc(size_t size) { + if (pool_offset + size > pool.size()) { + pool.resize(pool_offset + size); + } + char *ptr = pool.data() + pool_offset; + pool_offset += size; + return ptr; + } +}; + class NativeString { char *data; size_t length; - bool strAllocated = false; bool invalid = false; public: - NativeString(Isolate *isolate, const Local &value) { + NativeString(NativeStringContext &ctx, Isolate *isolate, const Local &value) { if (value->IsUndefined()) { data = nullptr; length = 0; @@ -138,15 +152,13 @@ public: data = (char *) strView.data8(); } else { // utf16: copy and convert to utf8 - strAllocated = true; length = string->Utf8LengthV2(isolate); - data = new char[length]; + data = ctx.alloc(length); string->WriteUtf8V2(isolate, data, length); } #else // Fallback Node.js < 24 - strAllocated = true; length = string->Utf8Length(isolate); - data = new char[length]; + data = ctx.alloc(length); string->WriteUtf8(isolate, data, length, nullptr, String::WriteOptions::NO_NULL_TERMINATION); #endif } else if (value->IsArrayBufferView()) { /* DataView or TypedArray */ @@ -179,12 +191,6 @@ public: std::string_view getString() { return {data, length}; } - - ~NativeString() { - if (strAllocated) { - delete[] data; - } - } }; // Utility function to extract raw certificate data