| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent e136903 commit 1ac1424
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -469,6 +469,14 @@ inline void Environment::set_http_parser_buffer(char* buffer) { | |||
| 469 | 469 | http_parser_buffer_ = buffer; | |
| 470 | 470 | } | |
| 471 | 471 | ||
| 472 | + inline bool Environment::http_parser_buffer_in_use() const { | ||
| 473 | + return http_parser_buffer_in_use_; | ||
| 474 | + } | ||
| 475 | + | ||
| 476 | + inline void Environment::set_http_parser_buffer_in_use(bool in_use) { | ||
| 477 | + http_parser_buffer_in_use_ = in_use; | ||
| 478 | + } | ||
| 479 | + | ||
| 472 | 480 | inline http2::http2_state* Environment::http2_state() const { | |
| 473 | 481 | return http2_state_.get(); | |
| 474 | 482 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -646,6 +646,8 @@ class Environment { | |||
| 646 | 646 | ||
| 647 | 647 | inline char* http_parser_buffer() const; | |
| 648 | 648 | inline void set_http_parser_buffer(char* buffer); | |
| 649 | + inline bool http_parser_buffer_in_use() const; | ||
| 650 | + inline void set_http_parser_buffer_in_use(bool in_use); | ||
| 649 | 651 | ||
| 650 | 652 | inline http2::http2_state* http2_state() const; | |
| 651 | 653 | inline void set_http2_state(std::unique_ptr<http2::http2_state> state); | |
@@ -828,6 +830,7 @@ class Environment { | |||
| 828 | 830 | double* heap_space_statistics_buffer_ = nullptr; | |
| 829 | 831 | ||
| 830 | 832 | char* http_parser_buffer_; | |
| 833 | + bool http_parser_buffer_in_use_ = false; | ||
| 831 | 834 | std::unique_ptr<http2::http2_state> http2_state_; | |
| 832 | 835 | ||
| 833 | 836 | // stat fields contains twice the number of entries because `fs.StatWatcher` | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -525,6 +525,14 @@ class Parser : public AsyncWrap, public StreamListener { | |||
| 525 | 525 | static const size_t kAllocBufferSize = 64 * 1024; | |
| 526 | 526 | ||
| 527 | 527 | uv_buf_t OnStreamAlloc(size_t suggested_size) override { | |
| 528 | + // For most types of streams, OnStreamRead will be immediately after | ||
| 529 | + // OnStreamAlloc, and will consume all data, so using a static buffer for | ||
| 530 | + // reading is more efficient. For other streams, just use the default | ||
| 531 | + // allocator, which uses Malloc(). | ||
| 532 | + if (env()->http_parser_buffer_in_use()) | ||
| 533 | + return StreamListener::OnStreamAlloc(suggested_size); | ||
| 534 | + env()->set_http_parser_buffer_in_use(true); | ||
| 535 | + | ||
| 528 | 536 | if (env()->http_parser_buffer() == nullptr) | |
| 529 | 537 | env()->set_http_parser_buffer(new char[kAllocBufferSize]); | |
| 530 | 538 | ||
@@ -534,6 +542,15 @@ class Parser : public AsyncWrap, public StreamListener { | |||
| 534 | 542 | ||
| 535 | 543 | void OnStreamRead(ssize_t nread, const uv_buf_t& buf) override { | |
| 536 | 544 | HandleScope scope(env()->isolate()); | |
| 545 | + // Once we’re done here, either indicate that the HTTP parser buffer | ||
| 546 | + // is free for re-use, or free() the data if it didn’t come from there | ||
| 547 | + // in the first place. | ||
| 548 | + OnScopeLeave on_scope_leave([&]() { | ||
| 549 | + if (buf.base == env()->http_parser_buffer()) | ||
| 550 | + env()->set_http_parser_buffer_in_use(false); | ||
| 551 | + else | ||
| 552 | + free(buf.base); | ||
| 553 | + }); | ||
| 537 | 554 | ||
| 538 | 555 | if (nread < 0) { | |
| 539 | 556 | PassReadErrorToPreviousListener(nread); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -436,6 +436,14 @@ class BufferValue : public MaybeStackBuffer<char> { | |||
| 436 | 436 | template <typename T> inline void USE(T&&) {} | |
| 437 | 437 | } // namespace node | |
| 438 | 438 | ||
| 439 | + // Run a function when exiting the current scope. | ||
| 440 | + struct OnScopeLeave { | ||
| 441 | + std::function<void()> fn_; | ||
| 442 | + | ||
| 443 | + explicit OnScopeLeave(std::function<void()> fn) : fn_(fn) {} | ||
| 444 | + ~OnScopeLeave() { fn_(); } | ||
| 445 | + }; | ||
| 446 | + | ||
| 439 | 447 | #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS | |
| 440 | 448 | ||
| 441 | 449 | #endif // SRC_UTIL_H_ | |
| Back | FazBrowse Home | New Git URL |
0 commit comments