Fix double Buffer.concat

Though I'm not sure is it gonna be faster.
This commit is contained in:
Kagami Hiiragi 2015-02-11 16:52:40 +03:00
parent 782b7e7d27
commit 3ca97f294c
2 changed files with 10 additions and 8 deletions

View File

@ -217,8 +217,9 @@ var addr = exports.addr = {
*/
encodePayload: function(addrs) {
assert(addrs.length <= 1000, "Too many address entires");
var addrsBuf = Buffer.concat(addrs.map(structs.net_addr.encode));
return Buffer.concat([structs.var_int.encode(addrs.length), addrsBuf]);
var addrBufs = addrs.map(structs.net_addr.encode);
var bufs = [structs.var_int.encode(addrs.length)].concat(addrBufs);
return Buffer.concat(bufs);
},
};
@ -281,8 +282,8 @@ var inv = exports.inv = {
*/
encodePayload: function(inventory) {
assert(inventory.length <= 50000, "Too many inventory entires");
var invBuf = Buffer.concat(inventory);
return Buffer.concat([structs.var_int.encode(inventory.length), invBuf]);
var bufs = [structs.var_int.encode(inventory.length)].concat(inventory);
return Buffer.concat(bufs);
},
};

View File

@ -190,7 +190,7 @@ var message = exports.message = {
*/
/**
* Decode message structure.
* Decode message.
* NOTE: `payload` is copied, `rest` references input buffer.
* @param {Buffer} buf - Buffer that starts with encoded message
* @return {DecodeResult}
@ -227,7 +227,7 @@ var message = exports.message = {
},
/**
* Encode message structure.
* Encode message.
* @param {string} command - Message command
* @param {Bufer} payload - Message payload
* @return {Buffer} Encoded message structure.
@ -610,8 +610,9 @@ exports.var_int_list = {
* @return {Buffer} Encoded `var_int_list`.
*/
encode: function(list) {
var listBuf = Buffer.concat(list.map(var_int.encode));
return Buffer.concat([var_int.encode(list.length), listBuf]);
var var_ints = list.map(var_int.encode);
var bufs = [var_int.encode(list.length)].concat(var_ints);
return Buffer.concat(bufs);
},
};