Replace NativeString heap allocations with thread-local pool allocator - #1263
Merged
Merged
Conversation
Add NativeStringContext nativeStringContext; as the first local variable in each function containing NativeString constructions, and pass it as the first argument to each NativeString constructor. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Co-authored-by: uNetworkingAB <110806833+uNetworkingAB@users.noreply.github.com>
…n 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>
Copilot
AI
changed the title
[WIP] Remove dynamic memory allocation in NativeString
Replace NativeString heap allocations with thread-local pool allocator
Mar 29, 2026
uNetworkingAB
marked this pull request as ready for review
March 29, 2026 19:05
uNetworkingAB
added a commit
that referenced
this pull request
Apr 2, 2026
* Revert "Fix NativeStringContext: fixed 8MB pool, malloc fallback, and proper free (#1264)" This reverts commit 71cac73. Co-authored-by: uNetworkingAB <110806833+uNetworkingAB@users.noreply.github.com> * Revert "Replace NativeString heap allocations with thread-local pool allocator (#1263)" This reverts commit 3e2ea85. Co-authored-by: uNetworkingAB <110806833+uNetworkingAB@users.noreply.github.com> * Revert "Update NativeString to use better copy mechanism and ValueView for utf8 string data direct access (#1262)" This reverts commit 6b4968f. Co-authored-by: uNetworkingAB <110806833+uNetworkingAB@users.noreply.github.com> * Revert "Add DataView support, update RecognizedString type (#1258)" This reverts commit 0b0c8f9. Co-authored-by: uNetworkingAB <110806833+uNetworkingAB@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: uNetworkingAB <110806833+uNetworkingAB@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
NativeStringwas callingnew char[]/delete[]for every UTF-8 string conversion, adding per-call heap pressure on hot paths.Changes
NativeStringContext(new,Utilities.h) — bump allocator backed by athread_local static std::vector<char>pool, initialized to 8 MB on first use per thread and grown on demand. Exposeschar *alloc(size_t).NativeString— constructor now takesNativeStringContext &ctxas first argument; UTF-8 conversion paths replacenew char[length]withctx.alloc(length).strAllocatedfield and destructor removed.All call sites (
addon.cpp,AppWrapper.h,WebSocketWrapper.h,HttpResponseWrapper.h,HttpRequestWrapper.h) — each function that constructs aNativeStringdeclares aNativeStringContext nativeStringContext;up front and passes it through.Usage pattern
Pool memory is reused across calls (each
NativeStringContextinstance starts at offset 0 of the shared thread-local pool), so there is no per-request allocation cost for the common ASCII/Latin-1 fast path, and only a bump-pointer increment for the UTF-16→UTF-8 conversion path.⌨️ Start Copilot coding agent tasks without leaving your editor — available in VS Code, Visual Studio, JetBrains IDEs and Eclipse.