buffer: speed up swap16/32, add swap64 · nodejs/node@4014ecb · GitHub
Skip to content

Commit 4014ecb

Browse files
zbjornsonFishrock123
authored andcommitted
buffer: speed up swap16/32, add swap64
* Speed up buffer.swap16 and swap32 by using builtins. Up to ~6x gain. Drop transition point between JS and C++ implementations accordingly. Amount of performance improvement not only depends on buffer size but also memory alignment. * Fix tests: C++ impl tests were testing 0-filled buffers so were always passing. * Add similar buffer.swap64 method. * Make buffer-swap benchmark mirror JS impl. doc/api/buffer.markdown has an entry of "added: REPLACEME" that should be changed to the correct release number before tagged. Because node is currently using a very old version of cpplint.py it doesn't know that std::swap() has moved from <algorithm> to <utility> in c++11. So until cpplint.py is updated simply NOLINT the line. Technically it should be NOLINT(build/include_what_you_use), but that puts the line over 80 characters causing another lint error. PR-URL: #7157 Reviewed-By: Trevor Norris <trev.norris@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Backport-URL: #7546
1 parent 63d361b commit 4014ecb

5 files changed

Lines changed: 351 additions & 111 deletions

File tree

benchmark/buffers/buffer-swap.js

Lines changed: 52 additions & 23 deletions

doc/api/buffer.md

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1461,10 +1461,10 @@ calls can be chained.
14611461
```js
14621462
const buf = Buffer.from([0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8]);
14631463
console.log(buf);
1464-
// Prints Buffer(0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8)
1464+
// Prints <Buffer 01 02 03 04 05 06 07 08>
14651465
buf.swap16();
14661466
console.log(buf);
1467-
// Prints Buffer(0x2, 0x1, 0x4, 0x3, 0x6, 0x5, 0x8, 0x7)
1467+
// Prints <Buffer 02 01 04 03 06 05 08 07>
14681468
```
14691469

14701470
### buf.swap32()
@@ -1482,12 +1482,36 @@ calls can be chained.
14821482
```js
14831483
const buf = Buffer.from([0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8]);
14841484
console.log(buf);
1485-
// Prints Buffer(0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8)
1485+
// Prints <Buffer 01 02 03 04 05 06 07 08>
14861486
buf.swap32();
14871487
console.log(buf);
1488-
// Prints Buffer(0x4, 0x3, 0x2, 0x1, 0x8, 0x7, 0x6, 0x5)
1488+
// Prints <Buffer 04 03 02 01 08 07 06 05>
14891489
```
14901490

1491+
### buf.swap64()
1492+
<!-- YAML
1493+
added: REPLACEME
1494+
-->
1495+
1496+
* Return: {Buffer}
1497+
1498+
Interprets the `Buffer` as an array of 64-bit numbers and swaps
1499+
the byte-order *in-place*. Throws a `RangeError` if the `Buffer` length is
1500+
not a multiple of 64 bits. The method returns a reference to the Buffer, so
1501+
calls can be chained.
1502+
1503+
```js
1504+
const buf = Buffer.from([0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8]);
1505+
console.log(buf);
1506+
// Prints <Buffer 01 02 03 04 05 06 07 08>
1507+
buf.swap64();
1508+
console.log(buf);
1509+
// Prints <Buffer 08 07 06 05 04 03 02 01>
1510+
```
1511+
1512+
Note that JavaScript cannot encode 64-bit integers. This method is intended
1513+
for working with 64-bit floats.
1514+
14911515
### buf.toString([encoding[, start[, end]]])
14921516

14931517
* `encoding` {String} Default: `'utf8'`

lib/buffer.js

Lines changed: 64 additions & 41 deletions

0 commit comments

Comments
 (0)