diff options
author | Wu Jian Ping <wujp@greatld.com> | 2021-12-02 18:13:42 +0800 |
---|---|---|
committer | Jens Geyer <Jens-G@users.noreply.github.com> | 2022-04-21 09:08:22 +0200 |
commit | 0dc52985ca06cb948eae251dce789f7f863fc243 (patch) | |
tree | d182a658efecf86197bffc3a95addb653c397dcc | |
parent | 625367f2169848802c9b885249571d8e4b3fcc6e (diff) | |
download | thrift-0dc52985ca06cb948eae251dce789f7f863fc243.tar.gz |
Buffer.concat has performance issue
-rw-r--r-- | lib/nodejs/lib/thrift/framed_transport.js | 37 |
1 files changed, 16 insertions, 21 deletions
diff --git a/lib/nodejs/lib/thrift/framed_transport.js b/lib/nodejs/lib/thrift/framed_transport.js index f7daa3f1c..0d424d260 100644 --- a/lib/nodejs/lib/thrift/framed_transport.js +++ b/lib/nodejs/lib/thrift/framed_transport.js @@ -34,36 +34,31 @@ function TFramedTransport(buffer, callback) { TFramedTransport.prototype = new THeaderTransport(); TFramedTransport.receiver = function(callback, seqid) { - var residual = null; - + var residual = []; + return function(data) { - // Prepend any residual data from our previous read - if (residual) { - data = Buffer.concat([residual, data]); - residual = null; + // push received data to residual + for(var i = 0; i < data.length; ++i) { + residual.push(data[i]) } - // framed transport - while (data.length) { - if (data.length < 4) { - // Not enough bytes to continue, save and resume on next packet - residual = data; + while (residual.length > 0) { + if (residual.length < 4) { + // Not enough bytes to continue, save and resume on next packet return; } - var frameSize = binary.readI32(data, 0); - if (data.length < 4 + frameSize) { - // Not enough bytes to continue, save and resume on next packet - residual = data; + // get single package sieze + var frameSize = binary.readI32(Buffer.from(residual.slice(0, 4)), 0); + // Not enough bytes to continue, save and resume on next packet + if (residual.length < 4 + frameSize) { return; } - var frame = data.slice(4, 4 + frameSize); - residual = data.slice(4 + frameSize); - + // splice first 4 bytes + residual.splice(0, 4) + // get package data + var frame = Buffer.from(residual.splice(0, frameSize)); callback(new TFramedTransport(frame), seqid); - - data = residual; - residual = null; } }; }; |