Skip to content

Commit 2e1d958

Browse files
panvaaduh95
authored andcommitted
Revert "buffer: move SlowBuffer to EOL"
This reverts commit 0579e0e PR-URL: #58211 Reviewed-By: Marco Ippolito <[email protected]> Reviewed-By: LiviaMedeiros <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Darshan Sen <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Chengzhong Wu <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Richard Lau <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Rafael Gonzaga <[email protected]> Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Juan José Arboleda <[email protected]>
1 parent fd37a14 commit 2e1d958

23 files changed

+118
-61
lines changed

benchmark/buffers/buffer-iterate.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use strict';
2-
const { Buffer } = require('buffer');
2+
const SlowBuffer = require('buffer').SlowBuffer;
33
const common = require('../common.js');
44
const assert = require('assert');
55

@@ -19,7 +19,7 @@ const methods = {
1919
function main({ size, type, method, n }) {
2020
const buffer = type === 'fast' ?
2121
Buffer.alloc(size) :
22-
Buffer.allocUnsafeSlow(size).fill(0);
22+
SlowBuffer(size).fill(0);
2323

2424
const fn = methods[method];
2525

benchmark/buffers/buffer-read-with-byteLength.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use strict';
22
const common = require('../common.js');
3-
const { Buffer } = require('buffer');
43

54
const types = [
65
'IntBE',
@@ -19,7 +18,7 @@ const bench = common.createBenchmark(main, {
1918
function main({ n, buf, type, byteLength }) {
2019
const buff = buf === 'fast' ?
2120
Buffer.alloc(8) :
22-
Buffer.allocUnsafeSlow(8);
21+
require('buffer').SlowBuffer(8);
2322
const fn = `read${type}`;
2423

2524
buff.writeDoubleLE(0, 0);

benchmark/buffers/buffer-read.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use strict';
22
const common = require('../common.js');
3-
const { Buffer } = require('buffer');
43

54
const types = [
65
'BigUInt64LE',
@@ -28,7 +27,7 @@ const bench = common.createBenchmark(main, {
2827
function main({ n, buf, type }) {
2928
const buff = buf === 'fast' ?
3029
Buffer.alloc(8) :
31-
Buffer.allocUnsafeSlow(8);
30+
require('buffer').SlowBuffer(8);
3231
const fn = `read${type}`;
3332

3433
buff.writeDoubleLE(0, 0);

benchmark/buffers/buffer-slice.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
'use strict';
22
const common = require('../common.js');
3-
const { Buffer } = require('buffer');
3+
const SlowBuffer = require('buffer').SlowBuffer;
44

55
const bench = common.createBenchmark(main, {
66
type: ['fast', 'slow', 'subarray'],
77
n: [1e6],
88
});
99

1010
const buf = Buffer.allocUnsafe(1024);
11-
const slowBuf = Buffer.allocUnsafeSlow(1024);
11+
const slowBuf = new SlowBuffer(1024);
1212

1313
function main({ n, type }) {
1414
const b = type === 'slow' ? slowBuf : buf;

benchmark/buffers/buffer-write.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22
const common = require('../common.js');
3-
const { Buffer } = require('buffer');
3+
44
const types = [
55
'BigUInt64LE',
66
'BigUInt64BE',
@@ -73,7 +73,7 @@ const byteLength = {
7373
function main({ n, buf, type }) {
7474
const buff = buf === 'fast' ?
7575
Buffer.alloc(8) :
76-
Buffer.allocUnsafeSlow(8);
76+
require('buffer').SlowBuffer(8);
7777
const fn = `write${type}`;
7878

7979
if (!/\d/.test(fn))

doc/api/buffer.md

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5340,6 +5340,28 @@ console.log(newBuf.toString('ascii'));
53405340
Because the Euro (``) sign is not representable in US-ASCII, it is replaced
53415341
with `?` in the transcoded `Buffer`.
53425342

5343+
### Class: `SlowBuffer`
5344+
5345+
<!-- YAML
5346+
deprecated: v6.0.0
5347+
-->
5348+
5349+
> Stability: 0 - Deprecated: Use [`Buffer.allocUnsafeSlow()`][] instead.
5350+
5351+
See [`Buffer.allocUnsafeSlow()`][]. This was never a class in the sense that
5352+
the constructor always returned a `Buffer` instance, rather than a `SlowBuffer`
5353+
instance.
5354+
5355+
#### `new SlowBuffer(size)`
5356+
5357+
<!-- YAML
5358+
deprecated: v6.0.0
5359+
-->
5360+
5361+
* `size` {integer} The desired length of the new `SlowBuffer`.
5362+
5363+
See [`Buffer.allocUnsafeSlow()`][].
5364+
53435365
### Buffer constants
53445366

53455367
<!-- YAML
@@ -5472,11 +5494,11 @@ added: v5.10.0
54725494

54735495
Node.js can be started using the `--zero-fill-buffers` command-line option to
54745496
cause all newly-allocated `Buffer` instances to be zero-filled upon creation by
5475-
default. Without the option, buffers created with [`Buffer.allocUnsafe()`][] and
5476-
[`Buffer.allocUnsafeSlow()`][] are not zero-filled. Use of this flag can have a
5477-
measurable negative impact on performance. Use the `--zero-fill-buffers` option
5478-
only when necessary to enforce that newly allocated `Buffer` instances cannot
5479-
contain old data that is potentially sensitive.
5497+
default. Without the option, buffers created with [`Buffer.allocUnsafe()`][],
5498+
[`Buffer.allocUnsafeSlow()`][], and `new SlowBuffer(size)` are not zero-filled.
5499+
Use of this flag can have a measurable negative impact on performance. Use the
5500+
`--zero-fill-buffers` option only when necessary to enforce that newly allocated
5501+
`Buffer` instances cannot contain old data that is potentially sensitive.
54805502

54815503
```console
54825504
$ node --zero-fill-buffers

doc/api/cli.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3111,7 +3111,8 @@ node --watch --watch-preserve-output test.js
31113111
added: v6.0.0
31123112
-->
31133113

3114-
Automatically zero-fills all newly allocated [`Buffer`][] instances.
3114+
Automatically zero-fills all newly allocated [`Buffer`][] and [`SlowBuffer`][]
3115+
instances.
31153116

31163117
## Environment variables
31173118

@@ -3886,6 +3887,7 @@ node --stack-trace-limit=12 -p -e "Error.stackTraceLimit" # prints 12
38863887
[`ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX`]: errors.md#err_unsupported_typescript_syntax
38873888
[`NODE_OPTIONS`]: #node_optionsoptions
38883889
[`NO_COLOR`]: https://no-color.org
3890+
[`SlowBuffer`]: buffer.md#class-slowbuffer
38893891
[`Web Storage`]: https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API
38903892
[`WebSocket`]: https://developer.mozilla.org/en-US/docs/Web/API/WebSocket
38913893
[`YoungGenerationSizeFromSemiSpaceSize`]: https://chromium.googlesource.com/v8/v8.git/+/refs/tags/10.3.129/src/heap/heap.cc#328

doc/api/deprecations.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -695,9 +695,6 @@ Type: End-of-Life
695695

696696
<!-- YAML
697697
changes:
698-
- version: v24.0.0
699-
pr-url: https://github.com/nodejs/node/pull/58008
700-
description: End-of-Life.
701698
- version: v24.0.0
702699
pr-url: https://github.com/nodejs/node/pull/55175
703700
description: Runtime deprecation.
@@ -709,9 +706,9 @@ changes:
709706
description: Documentation-only deprecation.
710707
-->
711708

712-
Type: End-of-Life
709+
Type: Runtime
713710

714-
The `SlowBuffer` class has been removed. Please use
711+
The [`SlowBuffer`][] class is deprecated. Please use
715712
[`Buffer.allocUnsafeSlow(size)`][] instead.
716713

717714
### DEP0031: `ecdh.setPublicKey()`
@@ -3921,6 +3918,7 @@ upon `require('node:module').builtinModules`.
39213918
[`ReadStream.open()`]: fs.md#class-fsreadstream
39223919
[`Server.getConnections()`]: net.md#servergetconnectionscallback
39233920
[`Server.listen({fd: <number>})`]: net.md#serverlistenhandle-backlog-callback
3921+
[`SlowBuffer`]: buffer.md#class-slowbuffer
39243922
[`String.prototype.toWellFormed`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toWellFormed
39253923
[`WriteStream.open()`]: fs.md#class-fswritestream
39263924
[`assert.CallTracker`]: assert.md#class-assertcalltracker

doc/contributing/writing-and-running-benchmarks.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ the code inside the `main` function if it's more than just declaration.
572572
```js
573573
'use strict';
574574
const common = require('../common.js');
575-
const { Buffer } = require('node:buffer');
575+
const { SlowBuffer } = require('node:buffer');
576576

577577
const configs = {
578578
// Number of operations, specified here so they show up in the report.
@@ -603,11 +603,10 @@ function main(conf) {
603603
bench.start();
604604

605605
// Do operations here
606+
const BufferConstructor = conf.type === 'fast' ? Buffer : SlowBuffer;
606607

607608
for (let i = 0; i < conf.n; i++) {
608-
conf.type === 'fast' ?
609-
Buffer.allocUnsafe(conf.size) :
610-
Buffer.allocUnsafeSlow(conf.size);
609+
new BufferConstructor(conf.size);
611610
}
612611

613612
// End the timer, pass in the number of operations

doc/node.1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ If set to 0 then V8 will choose an appropriate size of the thread pool based on
619619
If the value provided is larger than V8's maximum, then the largest value will be chosen.
620620
.
621621
.It Fl -zero-fill-buffers
622-
Automatically zero-fills all newly allocated Buffer instances.
622+
Automatically zero-fills all newly allocated Buffer and SlowBuffer instances.
623623
.
624624
.It Fl c , Fl -check
625625
Check the script's syntax without executing it.

lib/buffer.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ const {
5252
TypedArrayPrototypeSet,
5353
TypedArrayPrototypeSlice,
5454
Uint8Array,
55+
Uint8ArrayPrototype,
5556
} = primordials;
5657

5758
const {
@@ -88,6 +89,7 @@ const {
8889
kIsEncodingSymbol,
8990
defineLazyProperties,
9091
encodingsMap,
92+
deprecate,
9193
} = require('internal/util');
9294
const {
9395
isAnyArrayBuffer,
@@ -409,15 +411,25 @@ Buffer.allocUnsafe = function allocUnsafe(size) {
409411
};
410412

411413
/**
412-
* By default creates a non-zero-filled Buffer instance that is not allocated
413-
* off the pre-initialized pool. If `--zero-fill-buffers` is set, will zero-fill
414-
* the buffer.
414+
* Equivalent to SlowBuffer(num), by default creates a non-zero-filled
415+
* Buffer instance that is not allocated off the pre-initialized pool.
416+
* If `--zero-fill-buffers` is set, will zero-fill the buffer.
415417
*/
416418
Buffer.allocUnsafeSlow = function allocUnsafeSlow(size) {
417419
validateNumber(size, 'size', 0, kMaxLength);
418420
return createUnsafeBuffer(size);
419421
};
420422

423+
// If --zero-fill-buffers command line argument is set, a zero-filled
424+
// buffer is returned.
425+
function SlowBuffer(size) {
426+
validateNumber(size, 'size', 0, kMaxLength);
427+
return createUnsafeBuffer(size);
428+
}
429+
430+
ObjectSetPrototypeOf(SlowBuffer.prototype, Uint8ArrayPrototype);
431+
ObjectSetPrototypeOf(SlowBuffer, Uint8Array);
432+
421433
function allocate(size) {
422434
if (size <= 0) {
423435
return new FastBuffer();
@@ -1319,6 +1331,10 @@ function isAscii(input) {
13191331

13201332
module.exports = {
13211333
Buffer,
1334+
SlowBuffer: deprecate(
1335+
SlowBuffer,
1336+
'SlowBuffer() is deprecated. Please use Buffer.allocUnsafeSlow()',
1337+
'DEP0030'),
13221338
transcode,
13231339
isUtf8,
13241340
isAscii,

src/node_options.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1052,7 +1052,8 @@ PerProcessOptionsParser::PerProcessOptionsParser(
10521052
&PerProcessOptions::v8_thread_pool_size,
10531053
kAllowedInEnvvar);
10541054
AddOption("--zero-fill-buffers",
1055-
"automatically zero-fill all newly allocated Buffer instances",
1055+
"automatically zero-fill all newly allocated Buffer and "
1056+
"SlowBuffer instances",
10561057
&PerProcessOptions::zero_fill_all_buffers,
10571058
kAllowedInEnvvar);
10581059
AddOption("--debug-arraybuffer-allocations",

test/parallel/test-buffer-alloc.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const assert = require('assert');
55
const vm = require('vm');
66

77
const {
8-
Buffer,
8+
SlowBuffer,
99
kMaxLength,
1010
} = require('buffer');
1111

@@ -1104,6 +1104,9 @@ assert.throws(() => Buffer.from(null), {
11041104
// Test prototype getters don't throw
11051105
assert.strictEqual(Buffer.prototype.parent, undefined);
11061106
assert.strictEqual(Buffer.prototype.offset, undefined);
1107+
assert.strictEqual(SlowBuffer.prototype.parent, undefined);
1108+
assert.strictEqual(SlowBuffer.prototype.offset, undefined);
1109+
11071110

11081111
{
11091112
// Test that large negative Buffer length inputs don't affect the pool offset.
@@ -1136,7 +1139,7 @@ assert.throws(() => {
11361139
a.copy(b, 0, 0x100000000, 0x100000001);
11371140
}, outOfRangeError);
11381141

1139-
// Unpooled buffer
1142+
// Unpooled buffer (replaces SlowBuffer)
11401143
{
11411144
const ubuf = Buffer.allocUnsafeSlow(10);
11421145
assert(ubuf);

test/parallel/test-buffer-bytelength.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const common = require('../common');
44
const assert = require('assert');
5-
const { Buffer } = require('buffer');
5+
const SlowBuffer = require('buffer').SlowBuffer;
66
const vm = require('vm');
77

88
[
@@ -24,6 +24,7 @@ const vm = require('vm');
2424
});
2525

2626
assert(ArrayBuffer.isView(new Buffer(10)));
27+
assert(ArrayBuffer.isView(new SlowBuffer(10)));
2728
assert(ArrayBuffer.isView(Buffer.alloc(10)));
2829
assert(ArrayBuffer.isView(Buffer.allocUnsafe(10)));
2930
assert(ArrayBuffer.isView(Buffer.allocUnsafeSlow(10)));

test/parallel/test-buffer-failed-alloc-typed-arrays.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
require('../common');
44
const assert = require('assert');
5-
const { Buffer } = require('buffer');
5+
const SlowBuffer = require('buffer').SlowBuffer;
66

77
// Test failed or zero-sized Buffer allocations not affecting typed arrays.
88
// This test exists because of a regression that occurred. Because Buffer
@@ -15,6 +15,7 @@ const zeroArray = new Uint32Array(10).fill(0);
1515
const sizes = [1e20, 0, 0.1, -1, 'a', undefined, null, NaN];
1616
const allocators = [
1717
Buffer,
18+
SlowBuffer,
1819
Buffer.alloc,
1920
Buffer.allocUnsafe,
2021
Buffer.allocUnsafeSlow,

test/parallel/test-buffer-inspect.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ buffer.INSPECT_MAX_BYTES = 2;
3030
let b = Buffer.allocUnsafe(4);
3131
b.fill('1234');
3232

33-
let s = Buffer.allocUnsafeSlow(4);
33+
let s = buffer.SlowBuffer(4);
3434
s.fill('1234');
3535

3636
let expected = '<Buffer 31 32 ... 2 more bytes>';
@@ -41,7 +41,7 @@ assert.strictEqual(util.inspect(s), expected);
4141
b = Buffer.allocUnsafe(2);
4242
b.fill('12');
4343

44-
s = Buffer.allocUnsafeSlow(2);
44+
s = buffer.SlowBuffer(2);
4545
s.fill('12');
4646

4747
expected = '<Buffer 31 32>';

test/parallel/test-buffer-no-negative-allocation.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
require('../common');
44
const assert = require('assert');
5+
const { SlowBuffer } = require('buffer');
56

67
const msg = {
78
code: 'ERR_OUT_OF_RANGE',
@@ -29,3 +30,8 @@ assert.throws(() => Buffer.allocUnsafeSlow(-Buffer.poolSize), msg);
2930
assert.throws(() => Buffer.allocUnsafeSlow(-100), msg);
3031
assert.throws(() => Buffer.allocUnsafeSlow(-1), msg);
3132
assert.throws(() => Buffer.allocUnsafeSlow(NaN), msg);
33+
34+
assert.throws(() => SlowBuffer(-Buffer.poolSize), msg);
35+
assert.throws(() => SlowBuffer(-100), msg);
36+
assert.throws(() => SlowBuffer(-1), msg);
37+
assert.throws(() => SlowBuffer(NaN), msg);

test/parallel/test-buffer-over-max-length.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ require('../common');
44
const assert = require('assert');
55

66
const buffer = require('buffer');
7+
const SlowBuffer = buffer.SlowBuffer;
78

89
const kMaxLength = buffer.kMaxLength;
910
const bufferMaxSizeMsg = {
@@ -12,6 +13,7 @@ const bufferMaxSizeMsg = {
1213
};
1314

1415
assert.throws(() => Buffer(kMaxLength + 1), bufferMaxSizeMsg);
16+
assert.throws(() => SlowBuffer(kMaxLength + 1), bufferMaxSizeMsg);
1517
assert.throws(() => Buffer.alloc(kMaxLength + 1), bufferMaxSizeMsg);
1618
assert.throws(() => Buffer.allocUnsafe(kMaxLength + 1), bufferMaxSizeMsg);
1719
assert.throws(() => Buffer.allocUnsafeSlow(kMaxLength + 1), bufferMaxSizeMsg);

0 commit comments

Comments
 (0)