From a99e537bbe4454e93e81870c9de75a093d42da52 Mon Sep 17 00:00:00 2001 From: Benjamin Gruenbaum Date: Wed, 19 Jan 2022 18:27:04 +0200 Subject: [PATCH 1/4] buffer: deprecate buffer.slice --- doc/api/buffer.md | 12 ++++++++---- doc/api/deprecations.md | 17 +++++++++++++++++ 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/doc/api/buffer.md b/doc/api/buffer.md index 80a97ababd2d06..b5d4a4e0e917f6 100644 --- a/doc/api/buffer.md +++ b/doc/api/buffer.md @@ -254,7 +254,7 @@ In particular: without copying. This behavior can be surprising, and only exists for legacy compatibility. [`TypedArray.prototype.subarray()`][] can be used to achieve the behavior of [`Buffer.prototype.slice()`][`buf.slice()`] on both `Buffer`s - and other `TypedArray`s. + and other `TypedArray`s and should be preferred. * [`buf.toString()`][] is incompatible with its `TypedArray` equivalent. * A number of methods, e.g. [`buf.indexOf()`][], support additional arguments. @@ -2056,7 +2056,7 @@ If `value` is: * a string, `value` is interpreted according to the character encoding in `encoding`. * a `Buffer` or [`Uint8Array`][], `value` will be used in its entirety. - To compare a partial `Buffer`, use [`buf.slice()`][]. + To compare a partial `Buffer`, use [`buf.subarray`][]. * a number, `value` will be interpreted as an unsigned 8-bit integer value between `0` and `255`. @@ -3389,6 +3389,9 @@ console.log(buf.subarray(-5, -2).toString()); + +Type: Documentation-only + +This method was deprecated because it is not compatible with +`Uint8Array.prototype.slice()`, which is a superclass of `Buffer`. + +Use [`buffer.subarray`][] which does the same thing instead. + [Legacy URL API]: url.md#legacy-url-api [NIST SP 800-38D]: https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf [RFC 6066]: https://tools.ietf.org/html/rfc6066#section-3 @@ -3062,6 +3078,7 @@ const w = new Writable({ [`WriteStream.open()`]: fs.md#class-fswritestream [`assert`]: assert.md [`asyncResource.runInAsyncScope()`]: async_context.md#asyncresourceruninasyncscopefn-thisarg-args +[`buffer.subarray`]: buffer.md#bufsubarraystart-end [`child_process`]: child_process.md [`clearInterval()`]: timers.md#clearintervaltimeout [`clearTimeout()`]: timers.md#cleartimeouttimeout From 4d9368b2e4aa1ea7ff4e1ec85dd75fa9e429b715 Mon Sep 17 00:00:00 2001 From: Benjamin Gruenbaum Date: Wed, 19 Jan 2022 19:58:11 +0200 Subject: [PATCH 2/4] buffer: add subarray to slice bench --- benchmark/buffers/buffer-slice.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/benchmark/buffers/buffer-slice.js b/benchmark/buffers/buffer-slice.js index a64cbd2ab2db7c..523f949ea98601 100644 --- a/benchmark/buffers/buffer-slice.js +++ b/benchmark/buffers/buffer-slice.js @@ -3,7 +3,7 @@ const common = require('../common.js'); const SlowBuffer = require('buffer').SlowBuffer; const bench = common.createBenchmark(main, { - type: ['fast', 'slow'], + type: ['fast', 'slow', 'subarray'], n: [1e6] }); @@ -11,10 +11,14 @@ const buf = Buffer.allocUnsafe(1024); const slowBuf = new SlowBuffer(1024); function main({ n, type }) { - const b = type === 'fast' ? buf : slowBuf; + const b = type === 'slow' ? slowBuf : buf; + const fn = type === 'subarray' ? + () => b.subarray(10, 256) : + () => b.slice(10, 256); + bench.start(); for (let i = 0; i < n; i++) { - b.slice(10, 256); + fn(); } bench.end(n); } From f9b96da1f69d5cddf3083c1b0aefd45da479dc61 Mon Sep 17 00:00:00 2001 From: Benjamin Gruenbaum Date: Wed, 19 Jan 2022 20:31:49 +0200 Subject: [PATCH 3/4] buffer: alias subarray and slice --- lib/buffer.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/buffer.js b/lib/buffer.js index 1f4f0a2e89deb8..a2ded906f8ebc4 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -1112,7 +1112,7 @@ function adjustOffset(offset, length) { return NumberIsNaN(offset) ? 0 : length; } -Buffer.prototype.slice = function slice(start, end) { +Buffer.prototype.subarray = function subarray(start, end) { const srcLength = this.length; start = adjustOffset(start, srcLength); end = end !== undefined ? adjustOffset(end, srcLength) : srcLength; @@ -1120,6 +1120,10 @@ Buffer.prototype.slice = function slice(start, end) { return new FastBuffer(this.buffer, this.byteOffset + start, newLength); }; +Buffer.prototype.slice = function slice(start, end) { + return this.subarray(start, end); +}; + function swap(b, n, m) { const i = b[n]; b[n] = b[m]; From d4376e2bcd8d5659d644a6780d7e0889eff7cfa2 Mon Sep 17 00:00:00 2001 From: Benjamin Gruenbaum Date: Wed, 19 Jan 2022 20:43:38 +0200 Subject: [PATCH 4/4] fixup! trailing space --- benchmark/buffers/buffer-slice.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/benchmark/buffers/buffer-slice.js b/benchmark/buffers/buffer-slice.js index 523f949ea98601..dcb18754e8420d 100644 --- a/benchmark/buffers/buffer-slice.js +++ b/benchmark/buffers/buffer-slice.js @@ -12,7 +12,7 @@ const slowBuf = new SlowBuffer(1024); function main({ n, type }) { const b = type === 'slow' ? slowBuf : buf; - const fn = type === 'subarray' ? + const fn = type === 'subarray' ? () => b.subarray(10, 256) : () => b.slice(10, 256);