diff options
author | Anna Henningsen <anna@addaleax.net> | 2018-10-23 09:23:02 +0300 |
---|---|---|
committer | Michaƫl Zasso <targos@protonmail.com> | 2018-10-28 17:15:15 +0300 |
commit | f01518edfd83e2235d84485d87621e61f675b4a7 (patch) | |
tree | 20a562e8e3d86cce4787fef5847baaced6015a53 /lib/internal/stream_base_commons.js | |
parent | 0a23538e49e27b95ee35b051b6507eca74e2bb20 (diff) |
src: improve StreamBase write throughput
Improve performance by transferring information about write status
to JS through an `AliasedBuffer`, rather than object properties
set from C++.
PR-URL: https://github.com/nodejs/node/pull/23843
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
Diffstat (limited to 'lib/internal/stream_base_commons.js')
-rw-r--r-- | lib/internal/stream_base_commons.js | 22 |
1 files changed, 20 insertions, 2 deletions
diff --git a/lib/internal/stream_base_commons.js b/lib/internal/stream_base_commons.js index 870b5b3e3b0..709395fa910 100644 --- a/lib/internal/stream_base_commons.js +++ b/lib/internal/stream_base_commons.js @@ -6,6 +6,8 @@ const { WriteWrap, kReadBytesOrError, kArrayBufferOffset, + kBytesWritten, + kLastWriteWasAsync, streamBaseState } = internalBinding('stream_wrap'); const { UV_EOF } = internalBinding('uv'); @@ -20,7 +22,12 @@ function handleWriteReq(req, data, encoding) { switch (encoding) { case 'buffer': - return handle.writeBuffer(req, data); + { + const ret = handle.writeBuffer(req, data); + if (streamBaseState[kLastWriteWasAsync]) + req.buffer = data; + return ret; + } case 'latin1': case 'binary': return handle.writeLatin1String(req, data); @@ -35,7 +42,13 @@ function handleWriteReq(req, data, encoding) { case 'utf-16le': return handle.writeUcs2String(req, data); default: - return handle.writeBuffer(req, Buffer.from(data, encoding)); + { + const buffer = Buffer.from(data, encoding); + const ret = handle.writeBuffer(req, buffer); + if (streamBaseState[kLastWriteWasAsync]) + req.buffer = buffer; + return ret; + } } } @@ -45,6 +58,8 @@ function createWriteWrap(handle, oncomplete) { req.handle = handle; req.oncomplete = oncomplete; req.async = false; + req.bytes = 0; + req.buffer = null; return req; } @@ -80,6 +95,9 @@ function writeGeneric(self, req, data, encoding, cb) { } function afterWriteDispatched(self, req, err, cb) { + req.bytes = streamBaseState[kBytesWritten]; + req.async = !!streamBaseState[kLastWriteWasAsync]; + if (err !== 0) return self.destroy(errnoException(err, 'write', req.error), cb); |