-
Notifications
You must be signed in to change notification settings - Fork 29.9k
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
Some questions about buffer doc #9786
Comments
Note that this does not use memory from the existing memory pool, and instead always allocates a new block of memory. |
@Fishrock123 Yeah, I've added |
I think that’s a bug in the docs. |
I've made two GIFs for the second question. In the first GIF you can see a test attempt with the old code example. See how both output lines are the same in contravention of the doc prediction. In the second GIF you can see a test attempt with the new code example. See how both output lines are different as the doc predicts. |
I've run the initial code in some last Node.js versions (in addition to 7.2.0 above) and I've got the same results several times (except for 5.12.0, it varies):
|
On some systems, some first bytes of allocated buffer can be zeroed by default, so the example doesn't work well - the buffer looks zeroed even before the fill. Fixes: #9786 PR-URL: #9795 Reviewed-By: Sam Roberts <[email protected]> Reviewed-By: Roman Reiss <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
On some systems, some first bytes of allocated buffer can be zeroed by default, so the example doesn't work well - the buffer looks zeroed even before the fill. Fixes: nodejs#9786 PR-URL: nodejs#9795 Reviewed-By: Sam Roberts <[email protected]> Reviewed-By: Roman Reiss <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
On some systems, some first bytes of allocated buffer can be zeroed by default, so the example doesn't work well - the buffer looks zeroed even before the fill. Fixes: #9786 PR-URL: #9795 Reviewed-By: Sam Roberts <[email protected]> Reviewed-By: Roman Reiss <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
On some systems, some first bytes of allocated buffer can be zeroed by default, so the example doesn't work well - the buffer looks zeroed even before the fill. Fixes: #9786 PR-URL: #9795 Reviewed-By: Sam Roberts <[email protected]> Reviewed-By: Roman Reiss <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
I'm preparing a PR with some small fixes in code examples of the buffer doc and I've stumbled on some puzzles I want to clarify before the PR.
buf.lastIndexOf()
byteOffset
argument explanation.What does the "(not inclusive)" remark actually mean? In this code (not in the doc) the
byteOffset
seems to be inclusive in the search and the result.The same for this code in the doc:
So the question is this one: should this remark be clarified more or should it be removed?
new Buffer(size)
andBuffer.allocUnsafe(size)
examples.It happens that the actual outputs of these fragments do not match with described ones for me for most of the tests:
While they do output as described in the REPL, tested by script files they output the same lines in some circumstances:
After some research, I've found out there is a rather strong correlation between script file size and zeroing of a buffer beginning. If the before mentioned fragments are saved in 105-176 bytes file size (adjusted by these changes: ASCII/UTF-8, +/- BOM, +/- comments/space), the first 5 bytes are almost always empty. So I've written a weird script to find out more about this correlation. Here are some notes about results:
a. Zeroing scheme of buffer bytes is the same inside cycles of a script file size increasing by 8 bytes.
b. Correlation is flaky, so the statistics data need to be averaged in the research script and manually filtered after the tests.
c.
Buffer.alloc(size)
always works safely as intended (the stat log is empty, i.e. no non-zero bytes schemes are detected and saved).d.
Buffer.allocUnsafeSlow(size)
also has some correlation, but it is significantly flakier and does not make big zero fragments in the buffer beginning, so it could be ignored in this case.If somebody is interested in the research code, here is the script. In the comments there, I also give the stat data from a test run on my machine. The stat data format is "script file size: scheme with non-zero bytes indexes in created buffers". Repeated lines inside 8-bytes cycles are filtered. If some flaky case happens inside this cycles, it is marked by
*
at the beginning of the line. Here is also the diff betweennew Buffer(size)
andBuffer.allocUnsafe(size)
stats.So the question is this one: is this correlation depends on some hardware/software features or is it something stable? In the first case, it could be ignored, in the second case the code examples should be adjusted to fall out of size diapasons that produce big zero fragments at the beginning or buffers, otherwise, novices could be confused by these examples if they run them by themselves.
Sorry for my bad English and all the possible obscurity.
The text was updated successfully, but these errors were encountered: