Skip to content

Commit 8a94581

Browse files
committed
string_decoder: optimize write()
By limiting property getting/setting to only where they are absolutely necessary, we can achieve greater performance especially with small utf8 inputs and any size base64 inputs. PR-URL: #1209 Reviewed-By: Rod Vagg <[email protected]> Reviewed-By: Nicu Micleușanu <[email protected]> Reviewed-By: Chris Dickinson <[email protected]>
1 parent 3d46fef commit 8a94581

File tree

2 files changed

+128
-37
lines changed

2 files changed

+128
-37
lines changed

benchmark/misc/string-decoder.js

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
var common = require('../common.js');
2+
var StringDecoder = require('string_decoder').StringDecoder;
3+
4+
var bench = common.createBenchmark(main, {
5+
encoding: ['ascii', 'utf8', 'base64-utf8', 'base64-ascii'],
6+
inlen: [32, 128, 1024],
7+
chunk: [16, 64, 256, 1024],
8+
n: [25e4]
9+
});
10+
11+
var UTF_ALPHA = 'Blåbærsyltetøy';
12+
var ASC_ALPHA = 'Blueberry jam';
13+
14+
function main(conf) {
15+
var encoding = conf.encoding;
16+
var inLen = conf.inlen | 0;
17+
var chunkLen = conf.chunk | 0;
18+
var n = conf.n | 0;
19+
20+
var alpha;
21+
var chunks = [];
22+
var str = '';
23+
var isBase64 = (encoding === 'base64-ascii' || encoding === 'base64-utf8');
24+
25+
if (encoding === 'ascii' || encoding === 'base64-ascii')
26+
alpha = ASC_ALPHA;
27+
else if (encoding === 'utf8' || encoding === 'base64-utf8')
28+
alpha = UTF_ALPHA;
29+
else
30+
throw new Error('Bad encoding');
31+
32+
var sd = new StringDecoder(isBase64 ? 'base64' : encoding);
33+
34+
for (var i = 0; i < inLen; ++i) {
35+
if (i > 0 && (i % chunkLen) === 0 && !isBase64) {
36+
chunks.push(new Buffer(str, encoding));
37+
str = '';
38+
}
39+
str += alpha[i % alpha.length];
40+
}
41+
if (str.length > 0 && !isBase64)
42+
chunks.push(new Buffer(str, encoding));
43+
if (isBase64) {
44+
str = new Buffer(str, 'utf8').toString('base64');
45+
while (str.length > 0) {
46+
var len = Math.min(chunkLen, str.length);
47+
chunks.push(new Buffer(str.substring(0, len), 'utf8'));
48+
str = str.substring(len);
49+
}
50+
}
51+
52+
var nChunks = chunks.length;
53+
54+
bench.start();
55+
for (var i = 0; i < n; ++i) {
56+
for (var j = 0; j < nChunks; ++j)
57+
sd.write(chunks[j]);
58+
}
59+
bench.end(n);
60+
}

lib/string_decoder.js

+68-37
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
'use strict';
22

3+
const isEncoding = Buffer.isEncoding;
4+
35
function assertEncoding(encoding) {
4-
if (encoding && !Buffer.isEncoding(encoding)) {
6+
if (encoding && !isEncoding(encoding)) {
57
throw new Error('Unknown encoding: ' + encoding);
68
}
79
}
@@ -59,65 +61,83 @@ const StringDecoder = exports.StringDecoder = function(encoding) {
5961
// replacement character. See https://codereview.chromium.org/121173009/ .
6062
StringDecoder.prototype.write = function(buffer) {
6163
var charStr = '';
64+
var buflen = buffer.length;
65+
var charBuffer = this.charBuffer;
66+
var charLength = this.charLength;
67+
var charReceived = this.charReceived;
68+
var surrogateSize = this.surrogateSize;
69+
var encoding = this.encoding;
6270
// if our last write ended with an incomplete multibyte character
63-
while (this.charLength) {
71+
while (charLength) {
6472
// determine how many remaining bytes this buffer has to offer for this char
65-
var available = (buffer.length >= this.charLength - this.charReceived) ?
66-
this.charLength - this.charReceived :
67-
buffer.length;
73+
var diff = charLength - charReceived;
74+
var available = (buflen >= diff) ? diff : buflen;
6875

6976
// add the new bytes to the char buffer
70-
buffer.copy(this.charBuffer, this.charReceived, 0, available);
71-
this.charReceived += available;
77+
buffer.copy(charBuffer, charReceived, 0, available);
78+
charReceived += available;
7279

73-
if (this.charReceived < this.charLength) {
80+
if (charReceived < charLength) {
7481
// still not enough chars in this buffer? wait for more ...
82+
83+
this.charLength = charLength;
84+
this.charReceived = charReceived;
85+
7586
return '';
7687
}
7788

7889
// remove bytes belonging to the current character from the buffer
79-
buffer = buffer.slice(available, buffer.length);
90+
buffer = buffer.slice(available, buflen);
91+
buflen = buffer.length;
8092

8193
// get the character that was split
82-
charStr = this.charBuffer.slice(0, this.charLength).toString(this.encoding);
94+
charStr = charBuffer.toString(encoding, 0, charLength);
8395

8496
// CESU-8: lead surrogate (D800-DBFF) is also the incomplete character
8597
var charCode = charStr.charCodeAt(charStr.length - 1);
8698
if (charCode >= 0xD800 && charCode <= 0xDBFF) {
87-
this.charLength += this.surrogateSize;
99+
charLength += surrogateSize;
88100
charStr = '';
89101
continue;
90102
}
91-
this.charReceived = this.charLength = 0;
103+
charReceived = charLength = 0;
92104

93105
// if there are no more bytes in this buffer, just emit our char
94-
if (buffer.length === 0) {
106+
if (buflen === 0) {
107+
this.charLength = charLength;
108+
this.charReceived = charReceived;
109+
95110
return charStr;
96111
}
97-
break;
98112
}
99113

100114
// determine and set charLength / charReceived
101-
this.detectIncompleteChar(buffer);
115+
if (this.detectIncompleteChar(buffer))
116+
charLength = this.charLength;
117+
charReceived = this.charReceived;
102118

103-
var end = buffer.length;
104-
if (this.charLength) {
119+
var end = buflen;
120+
if (charLength) {
105121
// buffer the incomplete character bytes we got
106-
buffer.copy(this.charBuffer, 0, buffer.length - this.charReceived, end);
107-
end -= this.charReceived;
122+
buffer.copy(charBuffer, 0, buflen - charReceived, end);
123+
end -= charReceived;
108124
}
109125

110-
charStr += buffer.toString(this.encoding, 0, end);
126+
this.charLength = charLength;
127+
charStr += buffer.toString(encoding, 0, end);
111128

112129
var end = charStr.length - 1;
113130
var charCode = charStr.charCodeAt(end);
114131
// CESU-8: lead surrogate (D800-DBFF) is also the incomplete character
115132
if (charCode >= 0xD800 && charCode <= 0xDBFF) {
116-
var size = this.surrogateSize;
117-
this.charLength += size;
118-
this.charReceived += size;
119-
this.charBuffer.copy(this.charBuffer, size, 0, size);
120-
buffer.copy(this.charBuffer, 0, 0, size);
133+
charLength += surrogateSize;
134+
charReceived += surrogateSize;
135+
charBuffer.copy(charBuffer, surrogateSize, 0, surrogateSize);
136+
buffer.copy(charBuffer, 0, 0, surrogateSize);
137+
138+
this.charLength = charLength;
139+
this.charReceived = charReceived;
140+
121141
return charStr.substring(0, end);
122142
}
123143

@@ -130,47 +150,56 @@ StringDecoder.prototype.write = function(buffer) {
130150
// length that character, and sets this.charReceived to the number of bytes
131151
// that are available for this character.
132152
StringDecoder.prototype.detectIncompleteChar = function(buffer) {
153+
var buflen = buffer.length;
133154
// determine how many bytes we have to check at the end of this buffer
134-
var i = (buffer.length >= 3) ? 3 : buffer.length;
155+
var i = (buflen >= 3) ? 3 : buflen;
156+
var newlen = false;
135157

136158
// Figure out if one of the last i bytes of our buffer announces an
137159
// incomplete char.
138160
for (; i > 0; i--) {
139-
var c = buffer[buffer.length - i];
161+
var c = buffer[buflen - i];
140162

141163
// See http://en.wikipedia.org/wiki/UTF-8#Description
142164

143165
// 110XXXXX
144-
if (i == 1 && c >> 5 == 0x06) {
166+
if (i === 1 && c >> 5 === 0x06) {
145167
this.charLength = 2;
168+
newlen = true;
146169
break;
147170
}
148171

149172
// 1110XXXX
150-
if (i <= 2 && c >> 4 == 0x0E) {
173+
if (i <= 2 && c >> 4 === 0x0E) {
151174
this.charLength = 3;
175+
newlen = true;
152176
break;
153177
}
154178

155179
// 11110XXX
156-
if (i <= 3 && c >> 3 == 0x1E) {
180+
if (i <= 3 && c >> 3 === 0x1E) {
157181
this.charLength = 4;
182+
newlen = true;
158183
break;
159184
}
160185
}
186+
161187
this.charReceived = i;
188+
189+
return newlen;
162190
};
163191

164192
StringDecoder.prototype.end = function(buffer) {
165193
var res = '';
166194
if (buffer && buffer.length)
167195
res = this.write(buffer);
168196

169-
if (this.charReceived) {
170-
var cr = this.charReceived;
197+
var charReceived = this.charReceived;
198+
if (charReceived) {
199+
var cr = charReceived;
171200
var buf = this.charBuffer;
172201
var enc = this.encoding;
173-
res += buf.slice(0, cr).toString(enc);
202+
res += buf.toString(enc, 0, cr);
174203
}
175204

176205
return res;
@@ -181,11 +210,13 @@ function passThroughWrite(buffer) {
181210
}
182211

183212
function utf16DetectIncompleteChar(buffer) {
184-
this.charReceived = buffer.length % 2;
185-
this.charLength = this.charReceived ? 2 : 0;
213+
var charReceived = this.charReceived = buffer.length % 2;
214+
this.charLength = charReceived ? 2 : 0;
215+
return true;
186216
}
187217

188218
function base64DetectIncompleteChar(buffer) {
189-
this.charReceived = buffer.length % 3;
190-
this.charLength = this.charReceived ? 3 : 0;
219+
var charReceived = this.charReceived = buffer.length % 3;
220+
this.charLength = charReceived ? 3 : 0;
221+
return true;
191222
}

0 commit comments

Comments
 (0)