Browse Source

tls: use slab allocator

v0.8.10-release
Fedor Indutny 13 years ago
committed by isaacs
parent
commit
7651228ab2
  1. 65
      lib/tls.js

65
lib/tls.js

@ -176,6 +176,42 @@ function checkServerIdentity(host, cert) {
exports.checkServerIdentity = checkServerIdentity; exports.checkServerIdentity = checkServerIdentity;
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);
@ -189,6 +225,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);
@ -438,18 +475,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();
@ -458,13 +490,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);
@ -476,7 +507,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');
@ -492,7 +523,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…
Cancel
Save