Skip to content

Commit

Permalink
zero-alloc: istanbul ignore some stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Russ Frank committed Jan 28, 2016
1 parent b4bdc70 commit 8255f84
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
19 changes: 19 additions & 0 deletions base.js
Expand Up @@ -37,6 +37,7 @@ function BufferRW(byteLength, readFrom, writeInto, isPooled) {
assert(typeof byteLength === 'function', 'expected byteLength to be function');
assert(typeof readFrom === 'function', 'expected readFrom to be function');
assert(typeof writeInto === 'function', 'expected writeInto to be function');
// istanbul ignore else
if (isPooled) {
this.poolByteLength = byteLength;
this.poolReadFrom = readFrom;
Expand Down Expand Up @@ -89,16 +90,19 @@ BufferRW.prototype.byteLength = function byteLength(arg1, arg2, arg3) {
return lengthResult;
};

// istanbul ignore next
BufferRW.prototype.poolReadFrom = function poolReadFrom(destResult, arg1, arg2, arg3) {
var res = this.readFrom(arg1, arg2, arg3);
return destResult.copyFrom(res);
};

// istanbul ignore next
BufferRW.prototype.poolWriteInto = function poolWriteInto(destResult, value, buffer, offset) {
var res = this.writeInto(value, buffer, offset);
return destResult.copyFrom(res);
};

// istanbul ignore next
BufferRW.prototype.poolByteLength = function poolByteLength(destResult, arg1, arg2, arg3) {
var res = this.byteLength(arg1, arg2, arg3);
return destResult.copyFrom(res);
Expand All @@ -115,16 +119,19 @@ LengthResult.prototype.reset = function reset(err, length) {
return this;
};

// istanbul ignore next
LengthResult.prototype.copyFrom = function copyFrom(srcRes) {
this.err = srcRes.err;
this.length = srcRes.length;
return this;
};

// istanbul ignore next
LengthResult.error = function error(err, length) {
return new LengthResult(err, length);
};

// istanbul ignore next
LengthResult.just = function just(length) {
return new LengthResult(null, length);
};
Expand All @@ -140,11 +147,13 @@ WriteResult.prototype.reset = function reset(err, offset) {
return this;
};

// istanbul ignore next
WriteResult.prototype.copyFrom = function copyFrom(srcResult) {
this.err = srcResult.err;
this.offset = srcResult.offset;
};

// istanbul ignore next
WriteResult.error = function error(err, offset) {
return new WriteResult(err, offset);
};
Expand All @@ -159,19 +168,23 @@ WriteResult.poolRangedError = function poolRangedError(destResult, err, start, e
return destResult.reset(err, start, value);
};

// istanbul ignore next
WriteResult.rangedError = function rangedError(err, start, end, value) {
return WriteResult.poolRangedError(new WriteResult(), start, end, value);
};

// istanbul ignore next
WriteResult.just = function just(offset) {
return new WriteResult(null, offset);
};


// istanbul ignore next
WriteResult.shortError = function shortError(expected, actual, offset) {
return WriteResult.poolShortError(new WriteResult(), expected, actual, offset);
};

// istanbul ignore next
WriteResult.poolShortError = function poolShortError(destResult, expected, actual, offset) {
assert(typeof destResult === 'object' && destResult.constructor.name === 'WriteResult');

Expand All @@ -188,20 +201,23 @@ function ReadResult(err, offset, value) {
this.value = value === undefined ? null : value;
}

// istanbul ignore next
ReadResult.prototype.copyFrom = function copyFrom(srcResult) {
this.err = srcResult.err;
this.offset = srcResult.offset;
this.value = srcResult.value;
return this;
};

// istanbul ignore next
ReadResult.prototype.reset = function reset(err, offset, value) {
this.err = err;
this.offset = offset;
this.value = value;
return this;
};

// istanbul ignore next
ReadResult.error = function error(err, offset, value) {
return new ReadResult(err, offset, value);
};
Expand All @@ -215,14 +231,17 @@ ReadResult.poolRangedError = function poolRangedError(destResult, err, start, en
return destResult.reset(err, start, value);
};

// istanbul ignore next
ReadResult.rangedError = function rangedError(err, start, end, value) {
return ReadResult.poolRangedError(new ReadResult(), err, start, end, value);
};

// istanbul ignore next
ReadResult.just = function just(offset, value) {
return new ReadResult(null, offset, value);
};

// istanbul ignore next
ReadResult.shortError = function shortError(destResult, expected, actual, offset, endOffset) {
return ReadResult.poolShortError(new ReadResult(), expected, actual, offset, endOffset);
};
Expand Down
2 changes: 2 additions & 0 deletions repeat.js
Expand Up @@ -77,13 +77,15 @@ RepeatRW.prototype.poolReadFrom = function poolReadFrom(destResult, buffer, offs
if (res.err) return res;
offset = res.offset;

// istanbul ignore if
if (Array.isArray(res.value)) values[i] = res.value.slice(0);
else if (typeof res.value === 'object') values[i] = shallowCopy(res.value);
else values[i] = res.value;
}
return destResult.reset(null, offset, values);
};

// istanbul ignore next
function shallowCopy(obj) {
var keys = Object.keys(obj);
var i;
Expand Down
1 change: 1 addition & 0 deletions struct.js
Expand Up @@ -112,6 +112,7 @@ StructRW.prototype.poolWriteInto = function poolWriteInto(destResult, obj, buffe
var readRes = new ReadResult();
StructRW.prototype.poolReadFrom = function poolReadFrom(destResult, buffer, offset) {
if (typeof destResult.value === 'object' && destResult.value !== null) {
// istanbul ignore next
if (destResult.value.constructor !== this.cons) {
destResult.value = new this.cons();
}
Expand Down

0 comments on commit 8255f84

Please sign in to comment.