From 156f2f35df53efdf649a5f90bb7677d4115b1dff Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Sat, 10 Aug 2019 23:18:13 +0200 Subject: [PATCH] http2: shrink default `vector::reserve()` allocations Allocating memory upfront comes with overhead, and in particular, `std::vector` implementations do not necessarily return memory to the system when one might expect that (e.g. after shrinking the vector). Backport-PR-URL: https://github.com/nodejs/node/pull/29124 PR-URL: https://github.com/nodejs/node/pull/29122 Reviewed-By: Rich Trott Reviewed-By: James M Snell --- src/node_http2.cc | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/node_http2.cc b/src/node_http2.cc index 6ef0f5105b32bc..a3694e73c052b5 100644 --- a/src/node_http2.cc +++ b/src/node_http2.cc @@ -668,7 +668,7 @@ Http2Session::Http2Session(Environment* env, // fails. CHECK_EQ(fn(&session_, callbacks, this, *opts, *allocator_info), 0); - outgoing_storage_.reserve(4096); + outgoing_storage_.reserve(1024); outgoing_buffers_.reserve(32); { @@ -1993,9 +1993,10 @@ Http2Stream::Http2Stream( // Limit the number of header pairs max_header_pairs_ = session->GetMaxHeaderPairs(); - if (max_header_pairs_ == 0) - max_header_pairs_ = DEFAULT_MAX_HEADER_LIST_PAIRS; - current_headers_.reserve(max_header_pairs_); + if (max_header_pairs_ == 0) { + max_header_pairs_ = DEFAULT_MAX_HEADER_LIST_PAIRS; + } + current_headers_.reserve(std::min(max_header_pairs_, 12u)); // Limit the number of header octets max_header_length_ =