mirror of https://github.com/nodejs/node.git
tls: use slab allocator
parent
d923269e13
commit
f210530f46
65
lib/tls.js
65
lib/tls.js
|
@ -77,6 +77,42 @@ function convertNPNProtocols(NPNProtocols, out) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function SlabBuffer() {
|
||||||
|
this.create();
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
SlabBuffer.prototype.create = function create() {
|
||||||
|
this.isFull = false;
|
||||||
|
this.pool = new Buffer(10 * 1024 * 1024);
|
||||||
|
this.offset = 0;
|
||||||
|
this.remaining = this.pool.length;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
SlabBuffer.prototype.use = function use(context, fn) {
|
||||||
|
if (this.remaining === 0) {
|
||||||
|
this.isFull = true;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
var bytes = fn.call(context, this.pool, this.offset, this.remaining);
|
||||||
|
|
||||||
|
if (bytes > 0) {
|
||||||
|
this.offset += bytes;
|
||||||
|
this.remaining -= bytes;
|
||||||
|
}
|
||||||
|
|
||||||
|
assert(this.remaining >= 0);
|
||||||
|
|
||||||
|
return bytes;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
var slabBuffer = new SlabBuffer();
|
||||||
|
|
||||||
|
|
||||||
// Base class of both CleartextStream and EncryptedStream
|
// Base class of both CleartextStream and EncryptedStream
|
||||||
function CryptoStream(pair) {
|
function CryptoStream(pair) {
|
||||||
Stream.call(this);
|
Stream.call(this);
|
||||||
|
@ -90,6 +126,7 @@ function CryptoStream(pair) {
|
||||||
this._pending = [];
|
this._pending = [];
|
||||||
this._pendingCallbacks = [];
|
this._pendingCallbacks = [];
|
||||||
this._pendingBytes = 0;
|
this._pendingBytes = 0;
|
||||||
|
this._buffer = slabBuffer;
|
||||||
}
|
}
|
||||||
util.inherits(CryptoStream, Stream);
|
util.inherits(CryptoStream, Stream);
|
||||||
|
|
||||||
|
@ -339,18 +376,13 @@ CryptoStream.prototype._push = function() {
|
||||||
}
|
}
|
||||||
|
|
||||||
while (!this._paused) {
|
while (!this._paused) {
|
||||||
var chunkBytes = 0;
|
var chunkBytes = 0,
|
||||||
if (!this._pool || (this._poolStart >= this._poolEnd)) {
|
bytesRead = 0,
|
||||||
this._pool = new Buffer(16 * 4096);
|
start = this._buffer.offset;
|
||||||
this._poolStart = 0;
|
|
||||||
this._poolEnd = this._pool.length;
|
|
||||||
}
|
|
||||||
var start = this._poolStart;
|
|
||||||
|
|
||||||
do {
|
do {
|
||||||
chunkBytes = this._pusher(this._pool,
|
chunkBytes = this._buffer.use(this, this._pusher);
|
||||||
this._poolStart,
|
if (chunkBytes > 0) bytesRead += chunkBytes;
|
||||||
this._poolEnd - this._poolStart);
|
|
||||||
|
|
||||||
if (this.pair.ssl && this.pair.ssl.error) {
|
if (this.pair.ssl && this.pair.ssl.error) {
|
||||||
this.pair.error();
|
this.pair.error();
|
||||||
|
@ -359,13 +391,12 @@ CryptoStream.prototype._push = function() {
|
||||||
|
|
||||||
this.pair.maybeInitFinished();
|
this.pair.maybeInitFinished();
|
||||||
|
|
||||||
if (chunkBytes >= 0) {
|
} while (chunkBytes > 0 && !this._buffer.isFull);
|
||||||
this._poolStart += chunkBytes;
|
|
||||||
}
|
|
||||||
|
|
||||||
} while (chunkBytes > 0 && this._poolStart < this._poolEnd);
|
var pool = this._buffer.pool;
|
||||||
|
|
||||||
var bytesRead = this._poolStart - start;
|
// Create new buffer if previous was filled up
|
||||||
|
if (this._buffer.isFull) this._buffer.create();
|
||||||
|
|
||||||
assert(bytesRead >= 0);
|
assert(bytesRead >= 0);
|
||||||
|
|
||||||
|
@ -377,7 +408,7 @@ CryptoStream.prototype._push = function() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var chunk = this._pool.slice(start, this._poolStart);
|
var chunk = pool.slice(start, start + bytesRead);
|
||||||
|
|
||||||
if (this === this.pair.cleartext) {
|
if (this === this.pair.cleartext) {
|
||||||
debug('cleartext emit "data" with ' + bytesRead + ' bytes');
|
debug('cleartext emit "data" with ' + bytesRead + ' bytes');
|
||||||
|
@ -393,7 +424,7 @@ CryptoStream.prototype._push = function() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Optimization: emit the original buffer with end points
|
// Optimization: emit the original buffer with end points
|
||||||
if (this.ondata) this.ondata(this._pool, start, this._poolStart);
|
if (this.ondata) this.ondata(pool, start, start + bytesRead);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue