summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorisaacs <i@izs.me>2013-03-06 09:55:00 -0800
committerisaacs <i@izs.me>2013-03-06 11:44:30 -0800
commit9208c890582305218716a2bdadb7461ef24f5830 (patch)
treec5ffe879c87619efd7ebd3b88e71bda4e05da93f /lib
parenta978bedee79eee31b583a2bc9bdab27fa6cd95f3 (diff)
downloadandroid-node-v8-9208c890582305218716a2bdadb7461ef24f5830.tar.gz
android-node-v8-9208c890582305218716a2bdadb7461ef24f5830.tar.bz2
android-node-v8-9208c890582305218716a2bdadb7461ef24f5830.zip
stream: Raise readable high water mark in powers of 2
This prevents excessively raising the buffer level in tiny increments in pathological cases.
Diffstat (limited to 'lib')
-rw-r--r--lib/_stream_readable.js19
1 files changed, 17 insertions, 2 deletions
diff --git a/lib/_stream_readable.js b/lib/_stream_readable.js
index bf646f99ac..c4405314a4 100644
--- a/lib/_stream_readable.js
+++ b/lib/_stream_readable.js
@@ -161,6 +161,19 @@ Readable.prototype.setEncoding = function(enc) {
this._readableState.decoder = new StringDecoder(enc);
};
+// Don't raise the hwm > 128MB
+var MAX_HWM = 0x800000;
+function roundUpToNextPowerOf2(n) {
+ if (n >= MAX_HWM) {
+ n = MAX_HWM;
+ } else {
+ // Get the next highest power of 2
+ n--;
+ for (var p = 1; p < 32; p <<= 1) n |= n >> p;
+ n++;
+ }
+ return n;
+}
function howMuchToRead(n, state) {
if (state.length === 0 && state.ended)
@@ -181,9 +194,11 @@ function howMuchToRead(n, state) {
return 0;
// If we're asking for more than the target buffer level,
- // then raise the water mark.
+ // then raise the water mark. Bump up to the next highest
+ // power of 2, to prevent increasing it excessively in tiny
+ // amounts.
if (n > state.highWaterMark)
- state.highWaterMark = n;
+ state.highWaterMark = roundUpToNextPowerOf2(n);
// don't have that much. return null, unless we've ended.
if (n > state.length) {