Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
tls: use slab allocator
  • Loading branch information
indutny authored and isaacs committed Sep 25, 2012
1 parent 83d39c8 commit 7651228
Showing 1 changed file with 48 additions and 17 deletions.
65 changes: 48 additions & 17 deletions lib/tls.js
Expand Up @@ -176,6 +176,42 @@ function checkServerIdentity(host, cert) {
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
function CryptoStream(pair) {
Stream.call(this);
Expand All @@ -189,6 +225,7 @@ function CryptoStream(pair) {
this._pending = [];
this._pendingCallbacks = [];
this._pendingBytes = 0;
this._buffer = slabBuffer;
}
util.inherits(CryptoStream, Stream);

Expand Down Expand Up @@ -438,18 +475,13 @@ CryptoStream.prototype._push = function() {
}

while (!this._paused) {
var chunkBytes = 0;
if (!this._pool || (this._poolStart >= this._poolEnd)) {
this._pool = new Buffer(16 * 4096);
this._poolStart = 0;
this._poolEnd = this._pool.length;
}
var start = this._poolStart;
var chunkBytes = 0,
bytesRead = 0,
start = this._buffer.offset;

do {
chunkBytes = this._pusher(this._pool,
this._poolStart,
this._poolEnd - this._poolStart);
chunkBytes = this._buffer.use(this, this._pusher);
if (chunkBytes > 0) bytesRead += chunkBytes;

if (this.pair.ssl && this.pair.ssl.error) {
this.pair.error();
Expand All @@ -458,13 +490,12 @@ CryptoStream.prototype._push = function() {

this.pair.maybeInitFinished();

if (chunkBytes >= 0) {
this._poolStart += chunkBytes;
}
} while (chunkBytes > 0 && !this._buffer.isFull);

} 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);

Expand All @@ -476,7 +507,7 @@ CryptoStream.prototype._push = function() {
return;
}

var chunk = this._pool.slice(start, this._poolStart);
var chunk = pool.slice(start, start + bytesRead);

if (this === this.pair.cleartext) {
debug('cleartext emit "data" with ' + bytesRead + ' bytes');
Expand All @@ -492,7 +523,7 @@ CryptoStream.prototype._push = function() {
}

// 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);
}
};

Expand Down

0 comments on commit 7651228

Please sign in to comment.