-
Notifications
You must be signed in to change notification settings - Fork 7.3k
buffer: return offset for end of last write #5843
Conversation
A silly feature, but it puts the "return buffer instance for chainability" and "implicit offset" to bed permanently, which is nice, because those are even sillier features. LGTM. |
Wait, why is the “implicit offset” silly and how does this feature put that to bed permanently? |
@Mithgol Because this means that crud like |
The returning of Numbers obviously buries such a chainability, but an implicit offset does not necessarily imply any chainability. Could be the following: buf.writeInt32(10);
buf.writeDouble(8);
buf.writeFloat(99); In my opinion, an implicit offset would actually be good. (I've experienced it once with jDataView's interfaces.) That's why I'm interested in your reasons to think that's silly. A code based on implicit offsets tends to look more compact than the explicit, which is the following: offset = buf.writeInt32LE(offset, 10);
offset = buf.writeDoubleBE(offset, 8);
offset = buf.writeFloatLE(offset, 99); |
@Mithgol I created a patch to do this. For performance reasons it won't work. The elegant way is setting the necessary object property from cc, but this slows down |
Of course those three additions (to set, to increment, to read an object property from cc) slow down The intention behind the “implicit offset” feature request, as far as I understand it, is to pay that price consciously and in a hope that typical userland code utilizing those functions becomes faster because it gets rid of setting a variable, and incrementing it, and passing it to the And it's the overall effect that is expected to be beneficial. Seems feasible because the cc code of core functions is expected to run faster than userland javascripts. And because centralized code (built once) is expected to run faster than scattered JIT-compiled lines. But still there's a valid counter-argument to the above statement. The performance drop of That's why a more correct proposal of changes in
(A set of offset-related methods would also be necessary, such as Those methods would then be used only when a benefit is obvious for the user. (I would expect the sequential readers/writers to switch to When two sets of methods coexist, users have a choice and can make a conscious and a comfortable decision in each case they face. (That's almost like What do you think about it? |
Additional note: the above statements of “incrementing the offset” in userland are currently valid only for sequential reading because For the sake of However, most userland writes are likely to be sequential, so you'd refrain from reverting 4a34c69 until/unless |
@Mithgol The approaches you're suggesting have been explored, and were found to introduce performance problems. Additionally, while Bottom line: I'm sorry, we're not changing this behavior. |
Wait a minute. The last of my suggestions wasn't really about changing the behavior of I have suggested a new set of methods for sequential writing and reading, meant as additional (and not as a replacement): I believe that would diminish most of the problems you recounted.
If that wasn't enough to address a concern, please mention it. |
@Mithgol We have no interest in adding those methods to node core. Please feel free to do so in a userland module in npm, or use http://npm.im/buffercursor |
Thank you for mentioning node-buffercursor. (I was not aware that it existed.) |
Users asked to have
Buffer#write*
methods return last offset. Now the do.Fixes #3597