Skip to content

Commit

Permalink
Merge branch 'alloc-unsafe' of https://github.com/chkimes/amqplib int…
Browse files Browse the repository at this point in the history
…o chkimes-alloc-unsafe
  • Loading branch information
cressie176 committed Aug 7, 2022
2 parents a98003e + 9da3c9b commit 92a7cdd
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,10 @@ C.sendMessage = function(channel,
var allLen = methodHeaderLen + bodyLen;

if (allLen < SINGLE_CHUNK_THRESHOLD) {
var all = Buffer.alloc(allLen);
// Use `allocUnsafe` to avoid excessive allocations and CPU usage
// from zeroing. The returned Buffer is not zeroed and so must be
// completely filled to be used safely.
var all = Buffer.allocUnsafe(allLen);
var offset = mframe.copy(all, 0);
offset += pframe.copy(all, offset);

Expand All @@ -570,7 +573,10 @@ C.sendMessage = function(channel,
}
else {
if (methodHeaderLen < SINGLE_CHUNK_THRESHOLD) {
var both = Buffer.alloc(methodHeaderLen);
// Use `allocUnsafe` to avoid excessive allocations and CPU usage
// from zeroing. The returned Buffer is not zeroed and so must be
// completely filled to be used safely.
var both = Buffer.allocUnsafe(methodHeaderLen);
var offset = mframe.copy(both, 0);
pframe.copy(both, offset);
buffer.write(both);
Expand Down

0 comments on commit 92a7cdd

Please sign in to comment.