| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent b6d33f6 commit 2c0b249
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3,7 +3,7 @@ const common = require('../common.js'); | |||
| 3 | 3 | const bench = common.createBenchmark(main, { | |
| 4 | 4 | dur: [5], | |
| 5 | 5 | type: ['buf', 'asc', 'utf'], | |
| 6 | - size: [2, 1024, 1024 * 1024] | ||
| 6 | + size: [2, 1024, 1024 * 1024, 4 * 1024 * 1024, 16 * 1024 * 1024] | ||
| 7 | 7 | }); | |
| 8 | 8 | ||
| 9 | 9 | const fixtures = require('../../test/common/fixtures'); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -438,6 +438,13 @@ void NodeBIO::TryAllocateForWrite(size_t hint) { | |||
| 438 | 438 | kThroughputBufferLength; | |
| 439 | 439 | if (len < hint) | |
| 440 | 440 | len = hint; | |
| 441 | + | ||
| 442 | + // If there is a one time allocation size hint, use it. | ||
| 443 | + if (allocate_hint_ > len) { | ||
| 444 | + len = allocate_hint_; | ||
| 445 | + allocate_hint_ = 0; | ||
| 446 | + } | ||
| 447 | + | ||
| 441 | 448 | Buffer* next = new Buffer(env_, len); | |
| 442 | 449 | ||
| 443 | 450 | if (w == nullptr) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -96,6 +96,21 @@ class NodeBIO : public MemoryRetainer { | |||
| 96 | 96 | return length_; | |
| 97 | 97 | } | |
| 98 | 98 | ||
| 99 | + // Provide a hint about the size of the next pending set of writes. TLS | ||
| 100 | + // writes records of a maximum length of 16k of data plus a 5-byte header, | ||
| 101 | + // a MAC (up to 20 bytes for SSLv3, TLS 1.0, TLS 1.1, and up to 32 bytes | ||
| 102 | + // for TLS 1.2), and padding if a block cipher is used. If there is a | ||
| 103 | + // large write this will result in potentially many buffers being | ||
| 104 | + // allocated and gc'ed which can cause long pauses. By providing a | ||
| 105 | + // guess about the amount of buffer space that will be needed in the | ||
| 106 | + // next allocation this overhead is removed. | ||
| 107 | + inline void set_allocate_tls_hint(size_t size) { | ||
| 108 | + constexpr size_t kThreshold = 16 * 1024; | ||
| 109 | + if (size >= kThreshold) { | ||
| 110 | + allocate_hint_ = (size / kThreshold + 1) * (kThreshold + 5 + 32); | ||
| 111 | + } | ||
| 112 | + } | ||
| 113 | + | ||
| 99 | 114 | inline void set_eof_return(int num) { | |
| 100 | 115 | eof_return_ = num; | |
| 101 | 116 | } | |
@@ -164,6 +179,7 @@ class NodeBIO : public MemoryRetainer { | |||
| 164 | 179 | Environment* env_ = nullptr; | |
| 165 | 180 | size_t initial_ = kInitialBufferLength; | |
| 166 | 181 | size_t length_ = 0; | |
| 182 | + size_t allocate_hint_ = 0; | ||
| 167 | 183 | int eof_return_ = -1; | |
| 168 | 184 | Buffer* read_head_ = nullptr; | |
| 169 | 185 | Buffer* write_head_ = nullptr; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -587,6 +587,7 @@ void TLSWrap::ClearIn() { | |||
| 587 | 587 | AllocatedBuffer data = std::move(pending_cleartext_input_); | |
| 588 | 588 | crypto::MarkPopErrorOnReturn mark_pop_error_on_return; | |
| 589 | 589 | ||
| 590 | + crypto::NodeBIO::FromBIO(enc_out_)->set_allocate_tls_hint(data.size()); | ||
| 590 | 591 | int written = SSL_write(ssl_.get(), data.data(), data.size()); | |
| 591 | 592 | Debug(this, "Writing %zu bytes, written = %d", data.size(), written); | |
| 592 | 593 | CHECK(written == -1 || written == static_cast<int>(data.size())); | |
@@ -701,8 +702,15 @@ int TLSWrap::DoWrite(WriteWrap* w, | |||
| 701 | 702 | ||
| 702 | 703 | size_t length = 0; | |
| 703 | 704 | size_t i; | |
| 704 | - for (i = 0; i < count; i++) | ||
| 705 | + size_t nonempty_i = 0; | ||
| 706 | + size_t nonempty_count = 0; | ||
| 707 | + for (i = 0; i < count; i++) { | ||
| 705 | 708 | length += bufs[i].len; | |
| 709 | + if (bufs[i].len > 0) { | ||
| 710 | + nonempty_i = i; | ||
| 711 | + nonempty_count += 1; | ||
| 712 | + } | ||
| 713 | + } | ||
| 706 | 714 | ||
| 707 | 715 | // We want to trigger a Write() on the underlying stream to drive the stream | |
| 708 | 716 | // system, but don't want to encrypt empty buffers into a TLS frame, so see | |
@@ -747,20 +755,34 @@ int TLSWrap::DoWrite(WriteWrap* w, | |||
| 747 | 755 | crypto::MarkPopErrorOnReturn mark_pop_error_on_return; | |
| 748 | 756 | ||
| 749 | 757 | int written = 0; | |
| 750 | - if (count != 1) { | ||
| 758 | + | ||
| 759 | + // It is common for zero length buffers to be written, | ||
| 760 | + // don't copy data if there there is one buffer with data | ||
| 761 | + // and one or more zero length buffers. | ||
| 762 | + // _http_outgoing.js writes a zero length buffer in | ||
| 763 | + // in OutgoingMessage.prototype.end. If there was a large amount | ||
| 764 | + // of data supplied to end() there is no sense allocating | ||
| 765 | + // and copying it when it could just be used. | ||
| 766 | + | ||
| 767 | + if (nonempty_count != 1) { | ||
| 751 | 768 | data = env()->AllocateManaged(length); | |
| 752 | 769 | size_t offset = 0; | |
| 753 | 770 | for (i = 0; i < count; i++) { | |
| 754 | 771 | memcpy(data.data() + offset, bufs[i].base, bufs[i].len); | |
| 755 | 772 | offset += bufs[i].len; | |
| 756 | 773 | } | |
| 774 | + | ||
| 775 | + crypto::NodeBIO::FromBIO(enc_out_)->set_allocate_tls_hint(length); | ||
| 757 | 776 | written = SSL_write(ssl_.get(), data.data(), length); | |
| 758 | 777 | } else { | |
| 759 | 778 | // Only one buffer: try to write directly, only store if it fails | |
| 760 | - written = SSL_write(ssl_.get(), bufs[0].base, bufs[0].len); | ||
| 779 | + uv_buf_t* buf = &bufs[nonempty_i]; | ||
| 780 | + crypto::NodeBIO::FromBIO(enc_out_)->set_allocate_tls_hint(buf->len); | ||
| 781 | + written = SSL_write(ssl_.get(), buf->base, buf->len); | ||
| 782 | + | ||
| 761 | 783 | if (written == -1) { | |
| 762 | 784 | data = env()->AllocateManaged(length); | |
| 763 | - memcpy(data.data(), bufs[0].base, bufs[0].len); | ||
| 785 | + memcpy(data.data(), buf->base, buf->len); | ||
| 764 | 786 | } | |
| 765 | 787 | } | |
| 766 | 788 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments