-
Notifications
You must be signed in to change notification settings - Fork 30k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
What about SlowBuffer? #5799
Comments
So, the proposal is to replace This would be a semver-major change (due to the soft deprecation), and it should probably be paired with those Buffer changes that were already landed in #4682. /cc @nodejs/ctc |
Buffer.alloc already does an unpooled zero filled allocation. So we're covered there. All that is needed is an unpooled non-zeroed allocation which is what SlowBuffer provides. |
Edit: ah, the documentation states:
So yes, that looks like documented, though it might be better to add a clear notice that |
It used to be a class. Now it simply returns an unpooled Buffer.
It's the exact same with
The primary use case (and pretty much the only use case) for
The reason it existed it because in a prior implementation Buffer took slices of SlowBuffer. When I reimplemented Buffer the first time this was no longer needed. Though it was, and currently is, the only way to get a non-pooled uninitialized chunk of memory. So instead of writing a new API I decided to keep around and slightly repurpose the old one (which as far as the user was concerned still did the same thing).
Sorry but making sure the API is "scary-looking" to make sure it's not misused by developers seems a little strange. Forcing the user to |
Is there a reason for it to be a separate method (and documented as a class)? Also, the documentation is misleading then: it suggests to use |
That's not exactly what I meant there. Imo |
Hah. Sorry for the misinterpretation. It's late here. I'll go to bed now. |
Prior to my first Buffer rewrite SlowBuffer did in fact return a different object. |
How about we keep SlowBuffer as is, doc deprecate it, and add an optional
|
@jasnell That would solve it =). |
Sounds like a reasonable solution. For how long the Though I believe the API is necessary, it also has a narrow use case. Which is if someone wants to retain a Buffer slice for an extended period of time. The purpose is to prevent retaining excess memory. For example: const SlowBuffer = require('buffer').SlowBuffer;
const retainData = [];
net.createServer((c) => {
c.on('data', (chunk) => {
const first = chunk.indexOf('abc');
const last = chunk.lastIndexOf('xyz');
if (first >= last)
return;
const size = last - first;
const sb = SlowBuffer(size);
chunk.copy(sb, 0, first, size);
// Doing retainData.push(chunk.slice(start, end)) will retain
// the entire packet. This can lead to unnecessary memory
// growth over time.
retainData.push(sb);
});
}); Despite this narrow use case, it would also not enough to create a method that achieves the same. There are also circumstances where multiple Buffers need to be copied into the same new allocation. Granted this can be achieved by doing: Buffer.concat([ buf1.slice(start, end), buf2.slice(start, end), ...]); but this comes with unnecessary overhead. |
I'll work up the new PR adding the additional parameter and the docs-only deprecation for |
SlowBuffer (and its documentation) has several issues atm:
new SlowBuffer(size)
. On the other hand, all the tests, benchmarks, and even the code samples in the documentation exclude thenew
keyword and just callSlowBuffer(size)
: link. That has to be clarified in the docs.SlowBuffer(size)
is unsafe, pretty much likeBuffer(size)
was. It doesn't have accidential call problem, though — there is noSlowBuffer(value)
. But it tells users that they should zero-fill the buffer themselves, which isn't very nice (the reasons were already discussed).SlowBuffer
), btw,Buffer(something)
documentation still includes thenew
keyword: link. It's deprecated though, and can perhaps be left like that.Buffer.from('abc')
also returns a pooled buffer afaik. There is an unpooled counterpart toBuffer.allocUnsafe(size)
, but not to all the otherBuffer
creation methods. Would anyone want those?So, perhaps we should soft-deprecate SlowBuffer altogether and provide an API as
Buffer
methods, that would conform withBuffer.from()
andBuffer.alloc()
?I'm not yet sure how that would look like — it could be some options to the current methods, scary-looking names like
Buffer.allocSlow
/Buffer.allocSlowUnsafe
or even something likeBuffer.Slow.alloc
/Buffer.Slow.allocUnsafe
.The text was updated successfully, but these errors were encountered: