Created
April 10, 2011 00:27
-
-
Save xk/911928 to your computer and use it in GitHub Desktop.
This is the problem of http-simple.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
problem.js, 20110417 jorge@jorgechamorro.com | |
Demonstrates a problem in Buffer.prototype.write() with cons-strings (also known as ropes) | |
run this as node --trace-gc problem.js | |
and you'll see that too often the buf.write() takes ~ 50x times as long to complete (marked as '**** BAD'). | |
After a GC cycle it returns to normal/good for a little while. | |
If the string str is joined it works fine, always. | |
Tested in Ubuntu 10.10 and OSX 10.6.7 | |
*/ | |
var str = ''; | |
var kSize= 256*1024; | |
var i = kSize; | |
var flipflop = 0; | |
while (i--) { | |
if (flipflop = !flipflop) str= 'C' + str; | |
else str = str + 'C'; | |
} | |
//str= [str, ''].join(''); // Uncomment this and the problem disappears. | |
console.log('str.length -> ' + str.length); | |
var ctr= 0; | |
var maxT= -1; | |
var pre= "Anything"; | |
Object.prototype.padLeft= function (len) { | |
var str= '' + this; | |
while (str.length < len) str = " " + str; | |
return str; | |
}; | |
var buf; | |
(function loop () { | |
buf = new Buffer(kSize); | |
var t0= Date.now(); | |
buf.write(pre + str); | |
//buf.write([pre,str].join('')); // this works too. | |
t0= Date.now() - t0; | |
if (t0 > maxT) maxT= t0; | |
console.log('ctr ' + (ctr++).padLeft(4) | |
+ " maxT " + (maxT).padLeft(4) | |
+ " t0 "+ (t0).padLeft(4) | |
+ (t0 < 100 ? ' good' : ' **** BAD') | |
); | |
process.nextTick(loop); | |
})(); | |
/* | |
Sample output: | |
$ node --trace-gc problem.js | |
Scavenge 2.5 -> 2.2 MB, 1 ms. | |
Mark-sweep 3.1 -> 2.8 MB, 4 ms. | |
Scavenge 3.8 -> 3.8 MB, 1 ms. | |
Scavenge 5.3 -> 5.3 MB, 4 ms. | |
Scavenge 6.8 -> 6.8 MB, 3 ms. | |
Mark-sweep 9.8 -> 9.8 MB, 21 ms. | |
str.length -> 262144 | |
ctr 0 maxT 6 t0 6 good | |
ctr 1 maxT 7 t0 7 good | |
ctr 2 maxT 7 t0 6 good | |
ctr 3 maxT 7 t0 6 good | |
ctr 4 maxT 7 t0 6 good | |
ctr 5 maxT 7 t0 7 good | |
ctr 6 maxT 332 t0 332 **** BAD | |
ctr 7 maxT 332 t0 332 **** BAD | |
ctr 8 maxT 332 t0 332 **** BAD | |
ctr 9 maxT 332 t0 332 **** BAD | |
ctr 10 maxT 333 t0 333 **** BAD | |
ctr 11 maxT 356 t0 356 **** BAD | |
ctr 12 maxT 356 t0 347 **** BAD | |
ctr 13 maxT 356 t0 336 **** BAD | |
ctr 14 maxT 356 t0 332 **** BAD | |
ctr 15 maxT 356 t0 330 **** BAD | |
ctr 16 maxT 356 t0 331 **** BAD | |
ctr 17 maxT 356 t0 332 **** BAD | |
ctr 18 maxT 356 t0 331 **** BAD | |
ctr 19 maxT 356 t0 332 **** BAD | |
ctr 20 maxT 356 t0 333 **** BAD | |
ctr 21 maxT 356 t0 331 **** BAD | |
ctr 22 maxT 356 t0 333 **** BAD | |
ctr 23 maxT 356 t0 332 **** BAD | |
ctr 24 maxT 356 t0 330 **** BAD | |
ctr 25 maxT 356 t0 332 **** BAD | |
ctr 26 maxT 356 t0 348 **** BAD | |
ctr 27 maxT 356 t0 334 **** BAD | |
ctr 28 maxT 356 t0 334 **** BAD | |
ctr 29 maxT 356 t0 332 **** BAD | |
ctr 30 maxT 356 t0 330 **** BAD | |
ctr 31 maxT 356 t0 332 **** BAD | |
ctr 32 maxT 356 t0 333 **** BAD | |
ctr 33 maxT 356 t0 331 **** BAD | |
ctr 34 maxT 356 t0 332 **** BAD | |
ctr 35 maxT 356 t0 332 **** BAD | |
ctr 36 maxT 356 t0 332 **** BAD | |
ctr 37 maxT 356 t0 332 **** BAD | |
ctr 38 maxT 356 t0 334 **** BAD | |
ctr 39 maxT 356 t0 330 **** BAD | |
ctr 40 maxT 356 t0 333 **** BAD | |
Scavenge 13.9 -> 12.3 MB, 2 ms. | |
ctr 41 maxT 356 t0 345 **** BAD | |
ctr 42 maxT 356 t0 6 good | |
ctr 43 maxT 356 t0 6 good | |
ctr 44 maxT 356 t0 6 good | |
ctr 45 maxT 356 t0 7 good | |
ctr 46 maxT 356 t0 6 good | |
ctr 47 maxT 356 t0 7 good | |
ctr 48 maxT 356 t0 7 good | |
ctr 49 maxT 356 t0 6 good | |
ctr 50 maxT 356 t0 6 good | |
ctr 51 maxT 356 t0 6 good | |
ctr 52 maxT 356 t0 6 good | |
ctr 53 maxT 356 t0 6 good | |
ctr 54 maxT 356 t0 8 good | |
ctr 55 maxT 356 t0 7 good | |
ctr 56 maxT 356 t0 7 good | |
ctr 57 maxT 356 t0 6 good | |
ctr 58 maxT 356 t0 6 good | |
ctr 59 maxT 356 t0 6 good | |
ctr 60 maxT 356 t0 7 good | |
ctr 61 maxT 356 t0 6 good | |
ctr 62 maxT 356 t0 6 good | |
ctr 63 maxT 356 t0 7 good | |
ctr 64 maxT 356 t0 6 good | |
ctr 65 maxT 356 t0 335 **** BAD | |
ctr 66 maxT 356 t0 334 **** BAD | |
ctr 67 maxT 356 t0 332 **** BAD | |
ctr 68 maxT 356 t0 330 **** BAD | |
ctr 69 maxT 356 t0 332 **** BAD | |
*/ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment