You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@thrift.apache.org by je...@apache.org on 2022/04/21 07:08:27 UTC

[thrift] branch master updated: Buffer.concat has performance issue

This is an automated email from the ASF dual-hosted git repository.

jensg pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/thrift.git


The following commit(s) were added to refs/heads/master by this push:
     new 0dc52985c Buffer.concat has performance issue
0dc52985c is described below

commit 0dc52985ca06cb948eae251dce789f7f863fc243
Author: Wu Jian Ping <wu...@greatld.com>
AuthorDate: Thu Dec 2 18:13:42 2021 +0800

    Buffer.concat has performance issue
---
 lib/nodejs/lib/thrift/framed_transport.js | 37 +++++++++++++------------------
 1 file 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;
     }
   };
 };