Commit 2231574a443 for nodejs
commit 2231574a4431316d448a0f06efe1e908eac44875
Author: Nigro Simone <nigro.simone@gmail.com>
Date: Tue Nov 17 21:38:20 2026 +0100
src: do not shrink async resources on every pop
AsyncHooks::pop_async_context() calls shrink_to_fit() on
native_execution_async_resources_ after every pop. With the MSVC and
libc++ standard libraries, shrink_to_fit() on an empty std::deque frees
its storage, so every top-level callback scope allocates it again on
push and frees it on pop. libstdc++ does nothing here.
A program with the same pattern, a std::deque of the same variant going
from 0 to 1 to 0 elements, takes 136-147 ns per callback on Windows
with MSVC, and 6 ns without shrink_to_fit(). The async_ids_stack_ next
to it never shrinks either.
Refs: https://github.com/nodejs/performance/issues/24
Signed-off-by: Nigro Simone <nigro.simone@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/66344
Refs: https://github.com/nodejs/node/pull/66316
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
diff --git a/src/env.cc b/src/env.cc
index 0e2baa810d0..b55c7c46b69 100644
--- a/src/env.cc
+++ b/src/env.cc
@@ -206,7 +206,6 @@ bool AsyncHooks::pop_async_context(double async_id) {
} else {
native_execution_async_resources_.resize(offset);
}
- native_execution_async_resources_.shrink_to_fit();
}
if (js_execution_async_resources()->Length() > offset) [[unlikely]] {