Skip to content

Commit 86aa27f

Browse files
joyeecheungBridgeAR
authored andcommitted
http: remove pushValueToArray in Parser::CreateHeaders()
Instead of calling into JS from C++ to push values into an array, use the new Array::New API that takes a pointer and a length directly. PR-URL: #24264 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com> Reviewed-By: Refael Ackermann <refack@gmail.com>
1 parent 62fefd8 commit 86aa27f

File tree

1 file changed

+14
-23
lines changed

1 file changed

+14
-23
lines changed

src/node_http_parser.cc

+14-23
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ const uint32_t kOnHeadersComplete = 1;
7373
const uint32_t kOnBody = 2;
7474
const uint32_t kOnMessageComplete = 3;
7575
const uint32_t kOnExecute = 4;
76-
76+
// Any more fields than this will be flushed into JS
77+
const size_t kMaxHeaderFieldsCount = 32;
7778

7879
// helper class for the Parser
7980
struct StringPtr {
@@ -203,7 +204,7 @@ class Parser : public AsyncWrap, public StreamListener {
203204
if (num_fields_ == num_values_) {
204205
// start of new field name
205206
num_fields_++;
206-
if (num_fields_ == arraysize(fields_)) {
207+
if (num_fields_ == kMaxHeaderFieldsCount) {
207208
// ran out of space - flush to javascript land
208209
Flush();
209210
num_fields_ = 1;
@@ -212,7 +213,7 @@ class Parser : public AsyncWrap, public StreamListener {
212213
fields_[num_fields_ - 1].Reset();
213214
}
214215

215-
CHECK_LT(num_fields_, arraysize(fields_));
216+
CHECK_LT(num_fields_, kMaxHeaderFieldsCount);
216217
CHECK_EQ(num_fields_, num_values_ + 1);
217218

218219
fields_[num_fields_ - 1].Update(at, length);
@@ -731,25 +732,15 @@ class Parser : public AsyncWrap, public StreamListener {
731732
}
732733

733734
Local<Array> CreateHeaders() {
734-
Local<Array> headers = Array::New(env()->isolate());
735-
Local<Function> fn = env()->push_values_to_array_function();
736-
Local<Value> argv[NODE_PUSH_VAL_TO_ARRAY_MAX * 2];
737-
size_t i = 0;
738-
739-
do {
740-
size_t j = 0;
741-
while (i < num_values_ && j < arraysize(argv) / 2) {
742-
argv[j * 2] = fields_[i].ToString(env());
743-
argv[j * 2 + 1] = values_[i].ToString(env());
744-
i++;
745-
j++;
746-
}
747-
if (j > 0) {
748-
fn->Call(env()->context(), headers, j * 2, argv).ToLocalChecked();
749-
}
750-
} while (i < num_values_);
735+
// There could be extra entries but the max size should be fixed
736+
Local<Value> headers_v[kMaxHeaderFieldsCount * 2];
737+
738+
for (size_t i = 0; i < num_values_; ++i) {
739+
headers_v[i * 2] = fields_[i].ToString(env());
740+
headers_v[i * 2 + 1] = values_[i].ToString(env());
741+
}
751742

752-
return headers;
743+
return Array::New(env()->isolate(), headers_v, num_values_ * 2);
753744
}
754745

755746

@@ -824,8 +815,8 @@ class Parser : public AsyncWrap, public StreamListener {
824815
}
825816

826817
parser_t parser_;
827-
StringPtr fields_[32]; // header fields
828-
StringPtr values_[32]; // header values
818+
StringPtr fields_[kMaxHeaderFieldsCount]; // header fields
819+
StringPtr values_[kMaxHeaderFieldsCount]; // header values
829820
StringPtr url_;
830821
StringPtr status_message_;
831822
size_t num_fields_;

0 commit comments

Comments
 (0)