Skip to content

Commit dea49e3

Browse files
committed
net: Fix string-concat hot path bug
Also removes functionality added in f9fec3a because it changes API. (That patch shouldn't have been added anyway.)
1 parent 942b92a commit dea49e3

File tree

2 files changed

+70
-6
lines changed

2 files changed

+70
-6
lines changed

lib/net.js

+14-6
Original file line numberDiff line numberDiff line change
@@ -349,11 +349,13 @@ Socket.prototype.write = function(data /* [encoding], [fd], [cb] */) {
349349
if (!this._writeQueueCallbacks[last]) {
350350
this._writeQueueCallbacks[last] = cb;
351351
} else {
352-
// awful
353-
this._writeQueueCallbacks[last] = function() {
354-
this._writeQueueCallbacks[last]();
355-
cb();
356-
};
352+
var original = this._writeQueueCallbacks[last];
353+
354+
if (Array.isArray(original)) {
355+
original.push(cb);
356+
} else {
357+
this._writeQueueCallbacks[last] = [ original, cb ];
358+
}
357359
}
358360
}
359361
} else {
@@ -465,7 +467,13 @@ Socket.prototype._writeOut = function(data, encoding, fd, cb) {
465467
return false;
466468
} else {
467469
if (cb) {
468-
process.nextTick(cb);
470+
if (Array.isArray(cb)) {
471+
for (var i = 0; i < cb.length; i++) {
472+
if (cb[i]) cb[i]();
473+
}
474+
} else {
475+
cb();
476+
}
469477
}
470478
return true;
471479
}
+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
var common = require('../common');
23+
var net = require('net');
24+
var assert = require('assert');
25+
26+
var cbcount = 0;
27+
var N = 500000;
28+
29+
var server = net.Server(function(socket) {
30+
socket.on('data', function(d) {
31+
console.error("got %d bytes", d.length);
32+
});
33+
34+
socket.on('end', function() {
35+
console.error("end");
36+
socket.destroy();
37+
server.close();
38+
});
39+
});
40+
41+
server.listen(common.PORT, function() {
42+
var client = net.createConnection(common.PORT);
43+
44+
client.on('connect', function() {
45+
for (var i = 0; i < N; i++) {
46+
client.write("hello world", function() {
47+
cbcount++;
48+
});
49+
}
50+
client.end();
51+
});
52+
});
53+
54+
process.on('exit', function() {
55+
assert.equal(N, cbcount);
56+
});

0 commit comments

Comments
 (0)