Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
make write hot path efficient
  • Loading branch information
thefourtheye committed Nov 6, 2017
commit f34b251aab6067f3310bee4f43ab3c1b25b238e0
29 changes: 18 additions & 11 deletions lib/string_decoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,13 @@ function StringDecoder(encoding) {
this.lastNeed = 0;
this.lastTotal = 0;
this.lastChar = Buffer.allocUnsafe(this._nb || 0);
this._closed = false;
return;
}

this.encoding = normalizeEncoding(encoding);
switch (this.encoding) {
case 'utf16le':
this.text = utf16Text;
this.end = utf16End;
this._nb = 4;
break;
case 'utf8':
Expand All @@ -62,28 +60,21 @@ function StringDecoder(encoding) {
break;
case 'base64':
this.text = base64Text;
this.end = base64End;
this._nb = 3;
break;
default:
this.write = simpleWrite;
this.end = simpleEnd;
this._nb = 0;
this._closed = false;
return;
}
this.lastNeed = 0;
this.lastTotal = 0;
this.lastChar = Buffer.allocUnsafe(this._nb);
this._closed = false;
}

StringDecoder.prototype.reset = StringDecoder;

StringDecoder.prototype.write = function(buf) {
if (this._closed === true)
this.reset();

if (buf.length === 0)
return '';

Expand All @@ -103,7 +94,7 @@ StringDecoder.prototype.write = function(buf) {
return r || '';
};

StringDecoder.prototype.end = utf8End;
StringDecoder.prototype.end = end;

// Returns only complete characters in a Buffer
StringDecoder.prototype.text = utf8Text;
Expand Down Expand Up @@ -226,7 +217,6 @@ function utf8Text(buf, i) {
// character.
function utf8End(buf) {
const r = (buf && buf.length ? this.write(buf) : '');
this._closed = true;
if (this.lastNeed)
return r + '\ufffd';
return r;
Expand Down Expand Up @@ -299,3 +289,20 @@ function simpleWrite(buf) {
function simpleEnd(buf) {
return (buf && buf.length ? this.write(buf) : '');
}

function end(buf) {
let result = '';

if (this.encoding === 'utf16le')
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might consider using a switch here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ack.

result = utf16End.call(this, buf);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're the only ones calling these methods directly, then we should be able to avoid the overhead of .call() and just pass an instance as another argument.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ack.

else if (this.encoding === 'utf8')
result = utf8End.call(this, buf);
else if (this.encoding === 'base64')
result = base64End.call(this, buf);
else
result = simpleEnd.call(this, buf);

this.reset();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be better to use this.reset(this.encoding) to avoid the switch in the constructor?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We still cannot get rid of the switch in the constructor, right?

Copy link
Copy Markdown
Contributor

@mscdex mscdex Dec 5, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, but this.reset(this.encoding) would only hit that first if in the constructor, which might be better.


return result;
}
20 changes: 0 additions & 20 deletions test/parallel/test-string-decoder-end.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,23 +76,3 @@ function testBuf(encoding, buf) {
const result = decoder.write(euroPart2);
assert.notStrictEqual(result, '€');
}

{
// test to check if write after end reopens the decoder
const decoder = new SD();
assert.strictEqual(decoder._closed, false);
decoder.end();
assert.strictEqual(decoder._closed, true);
decoder.write(Buffer.from([0xE2]));
assert.strictEqual(decoder._closed, false);
}

{
// test to check if reset after end reopens the decoder
const decoder = new SD();
assert.strictEqual(decoder._closed, false);
decoder.end();
assert.strictEqual(decoder._closed, true);
decoder.reset();
assert.strictEqual(decoder._closed, false);
}