-
Notifications
You must be signed in to change notification settings - Fork 30.5k
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
net: fix bytesWritten during writev #14236
Changes from 1 commit
8191c01
ab6917b
1979f7f
9aadbf3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -825,7 +825,17 @@ protoGetter('bytesWritten', function bytesWritten() { | |
bytes += Buffer.byteLength(el.chunk, el.encoding); | ||
}); | ||
|
||
if (data) { | ||
if (Array.isArray(data)) { | ||
// was a writev, iterate over chunks to get total length | ||
for (var i = 0; i < data.length; i++) { | ||
const chunk = data[i]; | ||
|
||
if (chunk instanceof Buffer) | ||
bytes += chunk.length; | ||
else | ||
bytes += Buffer.byteLength(chunk.chunk, chunk.encoding); | ||
} | ||
} else if (data) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps this can be simplified a bit by merging with the if/else below: if (Array.isArray(data)) {
// ...
} else if (data instanceof Buffer) {
// ...
} else if (data) {
// ...
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good tip! I just collapsed the branches. |
||
if (data instanceof Buffer) | ||
bytes += data.length; | ||
else | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const net = require('net'); | ||
const Buffer = require('buffer').Buffer; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Using the global There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed! |
||
|
||
const server = net.createServer(function(socket) { | ||
socket.end(); | ||
}); | ||
|
||
server.listen(common.PORT, common.mustCall(function() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use a dynamic port for parallel tests. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks! fixed |
||
const socket = net.connect(common.PORT); | ||
|
||
// Cork the socket, then write twice; this should cause a writev, which | ||
// previously caused an err in the bytesWritten count. | ||
socket.cork(); | ||
|
||
socket.write('one'); | ||
socket.write(new Buffer('twø', 'utf8')); | ||
|
||
socket.uncork(); | ||
|
||
// one = 3 bytes, twø = 4 bytes | ||
assert.strictEqual(socket.bytesWritten, 3 + 4); | ||
|
||
socket.on('connect', common.mustCall(function() { | ||
assert.strictEqual(socket.bytesWritten, 3 + 4); | ||
})); | ||
|
||
socket.on('end', common.mustCall(function() { | ||
assert.strictEqual(socket.bytesWritten, 3 + 4); | ||
|
||
server.close(); | ||
})); | ||
})); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can avoid this check for each item in the array if
data.allBuffers === true
.writeGeneric()
uses this optimization already./cc @lpinca ^
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it worth optimizing? This is a pretty rarely-traveled code area, so I'd favor simplicity here, but I can change it if you'd like.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know how much
bytesWritten
is used, but I mostly mentioned the optimization because of @lpinca's comment.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think @mscdex’s suggestion makes sense. Also, using
process.binding('util').isUint8Array(chunk)
should be a faster alternative to a fullinstanceof
check, that should be okay here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added the
data.allBuffers
check, thanks.@addaleax I'm not so sure that's faster — this benchmark says its a bit slower, probably because of going past the C++ boundary.