Skip to content

Replace NativeString heap allocations with thread-local pool allocator - #1263

Merged
uNetworkingAB merged 3 commits into
masterfrom
copilot/remove-dynamic-memory-allocation
Mar 29, 2026
Merged

Replace NativeString heap allocations with thread-local pool allocator#1263
uNetworkingAB merged 3 commits into
masterfrom
copilot/remove-dynamic-memory-allocation

Conversation

Copilot AI commented Mar 29, 2026 •

Copy link
Copy Markdown
Contributor

NativeString was calling new 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 a thread_local static std::vector<char> pool, initialized to 8 MB on first use per thread and grown on demand. Exposes char *alloc(size_t).

  • NativeString — constructor now takes NativeStringContext &ctx as first argument; UTF-8 conversion paths replace new char[length] with ctx.alloc(length). strAllocated field and destructor removed.

  • All call sites (addon.cpp, AppWrapper.h, WebSocketWrapper.h, HttpResponseWrapper.h, HttpRequestWrapper.h) — each function that constructs a NativeString declares a NativeStringContext nativeStringContext; up front and passes it through.

Usage pattern

// Before
NativeString key(isolate, args[0]);

// After
NativeStringContext nativeStringContext;
NativeString key(nativeStringContext, isolate, args[0]);

Pool memory is reused across calls (each NativeStringContext instance 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.

Copilot AI and others added 2 commits March 29, 2026 18:56
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
Copilot AI requested a review from uNetworkingAB March 29, 2026 19:01
@uNetworkingAB
uNetworkingAB marked this pull request as ready for review March 29, 2026 19:05
@uNetworkingAB
uNetworkingAB merged commit 3e2ea85 into master Mar 29, 2026
1 check was pending
Copilot AI added a commit that referenced this pull request Apr 2, 2026
…allocator (#1263)"

This reverts commit 3e2ea85.

Co-authored-by: uNetworkingAB <110806833+uNetworkingAB@users.noreply.github.com>
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants