GeezerVison is showing github.com — src: improve StreamBase read throughput · nodejs/node@48ed81f
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: benchmark/net/tcp-raw-c2s.js +4-4Lines changed: 4 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -46,15 +46,15 @@ function main({ dur, len, type }) { | 46 | 46 | process.exit(0); | 47 | 47 | }, dur * 1000); | 48 | 48 |
| 49 | | - clientHandle.onread = function(nread, buffer) { | | 49 | + clientHandle.onread = function(buffer) { | 50 | 50 | // we're not expecting to ever get an EOF from the client. | 51 | 51 | // just lots of data forever. | 52 | | - if (nread < 0) | 53 | | - fail(nread, 'read'); | | 52 | + if (!buffer) | | 53 | + fail('read'); | 54 | 54 |
| 55 | 55 | // don't slice the buffer. the point of this is to isolate, not | 56 | 56 | // simulate real traffic. | 57 | | - bytes += buffer.length; | | 57 | + bytes += buffer.byteLength; | 58 | 58 | }; | 59 | 59 |
| 60 | 60 | clientHandle.readStart(); | |
Collapse file Copy file name to clipboardExpand all lines: benchmark/net/tcp-raw-pipe.js +8-8Lines changed: 8 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -43,15 +43,15 @@ function main({ dur, len, type }) { | 43 | 43 | if (err) | 44 | 44 | fail(err, 'connect'); | 45 | 45 |
| 46 | | - clientHandle.onread = function(nread, buffer) { | | 46 | + clientHandle.onread = function(buffer) { | 47 | 47 | // we're not expecting to ever get an EOF from the client. | 48 | 48 | // just lots of data forever. | 49 | | - if (nread < 0) | 50 | | - fail(nread, 'read'); | | 49 | + if (!buffer) | | 50 | + fail('read'); | 51 | 51 |
| 52 | 52 | const writeReq = new WriteWrap(); | 53 | 53 | writeReq.async = false; | 54 | | - err = clientHandle.writeBuffer(writeReq, buffer); | | 54 | + err = clientHandle.writeBuffer(writeReq, Buffer.from(buffer)); | 55 | 55 |
| 56 | 56 | if (err) | 57 | 57 | fail(err, 'write'); | @@ -89,11 +89,11 @@ function main({ dur, len, type }) { | 89 | 89 | if (err) | 90 | 90 | fail(err, 'connect'); | 91 | 91 |
| 92 | | - clientHandle.onread = function(nread, buffer) { | 93 | | - if (nread < 0) | 94 | | - fail(nread, 'read'); | | 92 | + clientHandle.onread = function(buffer) { | | 93 | + if (!buffer) | | 94 | + fail('read'); | 95 | 95 |
| 96 | | - bytes += buffer.length; | | 96 | + bytes += buffer.byteLength; | 97 | 97 | }; | 98 | 98 |
| 99 | 99 | connectReq.oncomplete = function(err) { | |
Collapse file Copy file name to clipboardExpand all lines: benchmark/net/tcp-raw-s2c.js +4-4Lines changed: 4 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -109,15 +109,15 @@ function main({ dur, len, type }) { | 109 | 109 |
| 110 | 110 | connectReq.oncomplete = function() { | 111 | 111 | var bytes = 0; | 112 | | - clientHandle.onread = function(nread, buffer) { | | 112 | + clientHandle.onread = function(buffer) { | 113 | 113 | // we're not expecting to ever get an EOF from the client. | 114 | 114 | // just lots of data forever. | 115 | | - if (nread < 0) | 116 | | - fail(nread, 'read'); | | 115 | + if (!buffer) | | 116 | + fail('read'); | 117 | 117 |
| 118 | 118 | // don't slice the buffer. the point of this is to isolate, not | 119 | 119 | // simulate real traffic. | 120 | | - bytes += buffer.length; | | 120 | + bytes += buffer.byteLength; | 121 | 121 | }; | 122 | 122 |
| 123 | 123 | clientHandle.readStart(); | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/child_process.js +11-4Lines changed: 11 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -22,7 +22,12 @@ const util = require('util'); | 22 | 22 | const assert = require('assert'); | 23 | 23 |
| 24 | 24 | const { Process } = internalBinding('process_wrap'); | 25 | | -const { WriteWrap } = internalBinding('stream_wrap'); | | 25 | +const { | | 26 | + WriteWrap, | | 27 | + kReadBytesOrError, | | 28 | + kArrayBufferOffset, | | 29 | + streamBaseState | | 30 | +} = internalBinding('stream_wrap'); | 26 | 31 | const { Pipe, constants: PipeConstants } = internalBinding('pipe_wrap'); | 27 | 32 | const { TCP } = internalBinding('tcp_wrap'); | 28 | 33 | const { TTY } = internalBinding('tty_wrap'); | @@ -486,11 +491,13 @@ function setupChannel(target, channel) { | 486 | 491 | var pendingHandle = null; | 487 | 492 | channel.buffering = false; | 488 | 493 | channel.pendingHandle = null; | 489 | | - channel.onread = function(nread, pool) { | | 494 | + channel.onread = function(arrayBuffer) { | 490 | 495 | const recvHandle = channel.pendingHandle; | 491 | 496 | channel.pendingHandle = null; | 492 | | - // TODO(bnoordhuis) Check that nread > 0. | 493 | | - if (pool) { | | 497 | + if (arrayBuffer) { | | 498 | + const nread = streamBaseState[kReadBytesOrError]; | | 499 | + const offset = streamBaseState[kArrayBufferOffset]; | | 500 | + const pool = new Uint8Array(arrayBuffer, offset, nread); | 494 | 501 | if (recvHandle) | 495 | 502 | pendingHandle = recvHandle; | 496 | 503 |
| |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/http2/core.js +7-2Lines changed: 7 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -120,7 +120,11 @@ const { isArrayBufferView } = require('internal/util/types'); | 120 | 120 |
| 121 | 121 | const { FileHandle } = process.binding('fs'); | 122 | 122 | const binding = internalBinding('http2'); | 123 | | -const { ShutdownWrap } = internalBinding('stream_wrap'); | | 123 | +const { | | 124 | + ShutdownWrap, | | 125 | + kReadBytesOrError, | | 126 | + streamBaseState | | 127 | +} = internalBinding('stream_wrap'); | 124 | 128 | const { UV_EOF } = internalBinding('uv'); | 125 | 129 |
| 126 | 130 | const { StreamPipe } = internalBinding('stream_pipe'); | @@ -2043,7 +2047,8 @@ function onFileUnpipe() { | 2043 | 2047 |
| 2044 | 2048 | // This is only called once the pipe has returned back control, so | 2045 | 2049 | // it only has to handle errors and End-of-File. | 2046 | | -function onPipedFileHandleRead(err) { | | 2050 | +function onPipedFileHandleRead() { | | 2051 | + const err = streamBaseState[kReadBytesOrError]; | 2047 | 2052 | if (err < 0 && err !== UV_EOF) { | 2048 | 2053 | this.stream.close(NGHTTP2_INTERNAL_ERROR); | 2049 | 2054 | } | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/stream_base_commons.js +12-2Lines changed: 12 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | 'use strict'; | 2 | 2 |
| 3 | 3 | const { Buffer } = require('buffer'); | 4 | | -const { WriteWrap } = internalBinding('stream_wrap'); | | 4 | +const { FastBuffer } = require('internal/buffer'); | | 5 | +const { | | 6 | + WriteWrap, | | 7 | + kReadBytesOrError, | | 8 | + kArrayBufferOffset, | | 9 | + streamBaseState | | 10 | +} = internalBinding('stream_wrap'); | 5 | 11 | const { UV_EOF } = internalBinding('uv'); | 6 | 12 | const { errnoException } = require('internal/errors'); | 7 | 13 | const { owner_symbol } = require('internal/async_hooks').symbols; | @@ -84,13 +90,17 @@ function afterWriteDispatched(self, req, err, cb) { | 84 | 90 | } | 85 | 91 | } | 86 | 92 |
| 87 | | -function onStreamRead(nread, buf) { | | 93 | +function onStreamRead(arrayBuffer) { | | 94 | + const nread = streamBaseState[kReadBytesOrError]; | | 95 | + | 88 | 96 | const handle = this; | 89 | 97 | const stream = this[owner_symbol]; | 90 | 98 |
| 91 | 99 | stream[kUpdateTimer](); | 92 | 100 |
| 93 | 101 | if (nread > 0 && !stream.destroyed) { | | 102 | + const offset = streamBaseState[kArrayBufferOffset]; | | 103 | + const buf = new FastBuffer(arrayBuffer, offset, nread); | 94 | 104 | if (!stream.push(buf)) { | 95 | 105 | handle.reading = false; | 96 | 106 | if (!stream.destroyed) { | |
Collapse file Copy file name to clipboardExpand all lines: src/env-inl.h +5Lines changed: 5 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -446,6 +446,11 @@ Environment::trace_category_state() { | 446 | 446 | return trace_category_state_; | 447 | 447 | } | 448 | 448 |
| | 449 | +inline AliasedBuffer<int32_t, v8::Int32Array>& | | 450 | +Environment::stream_base_state() { | | 451 | + return stream_base_state_; | | 452 | +} | | 453 | + | 449 | 454 | inline uint32_t Environment::get_next_module_id() { | 450 | 455 | return module_id_counter_++; | 451 | 456 | } | |
Collapse file Copy file name to clipboardExpand all lines: src/env.cc +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -158,6 +158,7 @@ Environment::Environment(IsolateData* isolate_data, | 158 | 158 | makecallback_cntr_(0), | 159 | 159 | should_abort_on_uncaught_toggle_(isolate_, 1), | 160 | 160 | trace_category_state_(isolate_, kTraceCategoryCount), | | 161 | + stream_base_state_(isolate_, StreamBase::kNumStreamBaseStateFields), | 161 | 162 | http_parser_buffer_(nullptr), | 162 | 163 | fs_stats_field_array_(isolate_, kFsStatsFieldsLength * 2), | 163 | 164 | fs_stats_field_bigint_array_(isolate_, kFsStatsFieldsLength * 2), | |
Collapse file Copy file name to clipboardExpand all lines: src/env.h +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -668,6 +668,7 @@ class Environment { | 668 | 668 | should_abort_on_uncaught_toggle(); | 669 | 669 |
| 670 | 670 | inline AliasedBuffer<uint8_t, v8::Uint8Array>& trace_category_state(); | | 671 | + inline AliasedBuffer<int32_t, v8::Int32Array>& stream_base_state(); | 671 | 672 |
| 672 | 673 | // The necessary API for async_hooks. | 673 | 674 | inline double new_async_id(); | @@ -951,6 +952,8 @@ class Environment { | 951 | 952 | AliasedBuffer<uint8_t, v8::Uint8Array> trace_category_state_; | 952 | 953 | std::unique_ptr<TrackingTraceStateObserver> trace_state_observer_; | 953 | 954 |
| | 955 | + AliasedBuffer<int32_t, v8::Int32Array> stream_base_state_; | | 956 | + | 954 | 957 | std::unique_ptr<performance::performance_state> performance_state_; | 955 | 958 | std::unordered_map<std::string, uint64_t> performance_marks_; | 956 | 959 |
| |
Collapse file Copy file name to clipboardExpand all lines: src/node_http2.cc +1-4Lines changed: 1 addition & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1256,10 +1256,7 @@ void Http2StreamListener::OnStreamRead(ssize_t nread, const uv_buf_t& buf) { | 1256 | 1256 | CHECK_LE(offset, session->stream_buf_.len); | 1257 | 1257 | CHECK_LE(offset + buf.len, session->stream_buf_.len); | 1258 | 1258 |
| 1259 | | - Local<Object> buffer = | 1260 | | - Buffer::New(env, session->stream_buf_ab_, offset, nread).ToLocalChecked(); | 1261 | | - | 1262 | | - stream->CallJSOnreadMethod(nread, buffer); | | 1259 | + stream->CallJSOnreadMethod(nread, session->stream_buf_ab_, offset); | 1263 | 1260 | } | 1264 | 1261 |
| 1265 | 1262 |
| |
|