Skip to content

Commit 1403a44

Browse files
authored
feat: use byte.getRawStringByStringLength to read utf8 string (#94)
1 parent a7a3f92 commit 1403a44

4 files changed

Lines changed: 41 additions & 69 deletions

File tree

benchmark/cache.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ suite
3737
.run({ 'async': false });
3838

3939
// Cache Benchmark
40-
// node version: v8.5.0, date: Tue Oct 31 2017 17:17:54 GMT+0800 (CST)
40+
// node version: v8.9.0, date: Thu Nov 16 2017 13:30:47 GMT+0800 (CST)
4141
// Starting...
4242
// 2 tests completed.
4343
//
44-
// with cache x 14,006 ops/sec ±2.21% (83 runs sampled)
45-
// without cache x 5,506 ops/sec ±1.94% (83 runs sampled)
44+
// with cache x 19,947 ops/sec ±3.81% (80 runs sampled)
45+
// without cache x 8,360 ops/sec ±1.38% (87 runs sampled)

benchmark/decode.js

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -193,40 +193,40 @@ suite
193193
})
194194
.run({ 'async': false });
195195

196-
// Hessian Decode Benchmark
197-
// node version: v0.11.12, date: Wed Jun 25 2014 10:46:26 GMT+0800 (CST)
198-
// Starting...
199-
// 32 tests completed.
200-
201-
// hessian1 decode: number x 5,983,374 ops/sec ±1.57% (94 runs sampled)
202-
// hessian2 decode: number x 5,713,562 ops/sec ±2.22% (91 runs sampled)
203-
// hessian1 decode: date x 2,959,698 ops/sec ±1.19% (92 runs sampled)
204-
// hessian2 decode: date x 2,548,345 ops/sec ±1.32% (91 runs sampled)
205-
// hessian1 decode: long x 3,880,734 ops/sec ±1.66% (93 runs sampled)
206-
// hessian2 decode: long x 5,221,659 ops/sec ±2.30% (90 runs sampled)
207-
// hessian1 decode: string x 1,109,890 ops/sec ±1.59% (94 runs sampled)
208-
// hessian2 decode: string x 1,075,246 ops/sec ±1.43% (92 runs sampled)
209-
// hessian1 decode: [1, 2, 3] x 1,186,054 ops/sec ±1.24% (92 runs sampled)
210-
// hessian2 decode: [1, 2, 3] x 2,049,867 ops/sec ±1.75% (98 runs sampled)
211-
// hessian1 decode array x 511,250 ops/sec ±0.92% (95 runs sampled)
212-
// hessian2 decode array x 600,564 ops/sec ±1.27% (90 runs sampled)
213-
// hessian1 decode: simple object x 186,875 ops/sec ±0.81% (93 runs sampled)
214-
// hessian2 decode: simple object x 174,768 ops/sec ±0.74% (94 runs sampled)
215-
// hessian1 decode: complex object x 133,573 ops/sec ±1.31% (97 runs sampled)
216-
// hessian2 decode: complex object x 131,234 ops/sec ±1.45% (95 runs sampled)
217-
// hessian1 decode with type: number x 5,014,602 ops/sec ±1.97% (91 runs sampled)
218-
// hessian2 decode with type: number x 5,890,098 ops/sec ±1.81% (89 runs sampled)
219-
// hessian1 decode with type: date x 2,797,789 ops/sec ±1.82% (91 runs sampled)
220-
// hessian2 decode with type: date x 2,541,107 ops/sec ±2.40% (92 runs sampled)
221-
// hessian1 decode with type: long x 3,869,288 ops/sec ±1.31% (93 runs sampled)
222-
// hessian2 decode with type: long x 5,598,654 ops/sec ±1.20% (95 runs sampled)
223-
// hessian1 decode with type: string x 1,068,879 ops/sec ±1.89% (92 runs sampled)
224-
// hessian2 decode with type: string x 1,110,680 ops/sec ±0.89% (94 runs sampled)
225-
// hessian1 decode with type: [1, 2, 3] x 1,150,027 ops/sec ±0.94% (94 runs sampled)
226-
// hessian2 decode with type: [1, 2, 3] x 1,834,472 ops/sec ±0.90% (94 runs sampled)
227-
// hessian1 decode with type array x 508,028 ops/sec ±0.99% (96 runs sampled)
228-
// hessian2 decode with type array x 605,446 ops/sec ±1.10% (96 runs sampled)
229-
// hessian1 decode with type: simple object x 190,014 ops/sec ±0.92% (97 runs sampled)
230-
// hessian2 decode with type: simple object x 176,337 ops/sec ±1.16% (96 runs sampled)
231-
// hessian1 decode with type: complex object x 133,815 ops/sec ±1.21% (90 runs sampled)
232-
// hessian2 decode with type: complex object x 134,814 ops/sec ±0.96% (96 runs sampled)
196+
// Hessian Decode Benchmark
197+
// node version: v8.9.0, date: Thu Nov 16 2017 13:31:15 GMT+0800 (CST)
198+
// Starting...
199+
// 32 tests completed.
200+
//
201+
// hessian1 decode: number x 6,546,954 ops/sec ±1.41% (87 runs sampled)
202+
// hessian2 decode: number x 6,247,164 ops/sec ±2.10% (84 runs sampled)
203+
// hessian1 decode: date x 2,378,658 ops/sec ±2.29% (86 runs sampled)
204+
// hessian2 decode: date x 2,378,814 ops/sec ±1.71% (84 runs sampled)
205+
// hessian1 decode: long x 3,669,483 ops/sec ±1.88% (85 runs sampled)
206+
// hessian2 decode: long x 6,125,751 ops/sec ±1.13% (81 runs sampled)
207+
// hessian1 decode: string x 2,123,485 ops/sec ±1.26% (88 runs sampled)
208+
// hessian2 decode: string x 1,968,578 ops/sec ±1.53% (86 runs sampled)
209+
// hessian1 decode: [1, 2, 3] x 1,292,327 ops/sec ±3.02% (83 runs sampled)
210+
// hessian2 decode: [1, 2, 3] x 1,463,036 ops/sec ±2.95% (82 runs sampled)
211+
// hessian1 decode array x 764,305 ops/sec ±1.10% (85 runs sampled)
212+
// hessian2 decode array x 819,871 ops/sec ±4.13% (85 runs sampled)
213+
// hessian1 decode: simple object x 225,427 ops/sec ±1.02% (87 runs sampled)
214+
// hessian2 decode: simple object x 206,072 ops/sec ±1.41% (85 runs sampled)
215+
// hessian1 decode: complex object x 139,136 ops/sec ±2.46% (86 runs sampled)
216+
// hessian2 decode: complex object x 132,702 ops/sec ±3.12% (80 runs sampled)
217+
// hessian1 decode with type: number x 4,971,071 ops/sec ±1.97% (83 runs sampled)
218+
// hessian2 decode with type: number x 6,165,893 ops/sec ±1.59% (86 runs sampled)
219+
// hessian1 decode with type: date x 2,205,802 ops/sec ±1.86% (85 runs sampled)
220+
// hessian2 decode with type: date x 2,290,528 ops/sec ±2.34% (86 runs sampled)
221+
// hessian1 decode with type: long x 3,340,413 ops/sec ±1.27% (86 runs sampled)
222+
// hessian2 decode with type: long x 6,072,529 ops/sec ±0.94% (89 runs sampled)
223+
// hessian1 decode with type: string x 1,986,114 ops/sec ±1.30% (86 runs sampled)
224+
// hessian2 decode with type: string x 2,074,055 ops/sec ±0.97% (88 runs sampled)
225+
// hessian1 decode with type: [1, 2, 3] x 1,026,832 ops/sec ±1.26% (86 runs sampled)
226+
// hessian2 decode with type: [1, 2, 3] x 1,565,525 ops/sec ±1.06% (87 runs sampled)
227+
// hessian1 decode with type array x 632,280 ops/sec ±4.27% (80 runs sampled)
228+
// hessian2 decode with type array x 876,049 ops/sec ±2.26% (86 runs sampled)
229+
// hessian1 decode with type: simple object x 189,740 ops/sec ±3.36% (82 runs sampled)
230+
// hessian2 decode with type: simple object x 238,079 ops/sec ±1.85% (84 runs sampled)
231+
// hessian1 decode with type: complex object x 130,701 ops/sec ±2.27% (87 runs sampled)
232+
// hessian2 decode with type: complex object x 160,600 ops/sec ±1.63% (87 runs sampled)

lib/utils.js

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,6 @@ exports.handleLong = function (val) {
4848
return val.toNumber();
4949
};
5050

51-
exports.lengthOfUTF8 = function (head) {
52-
if (head < 0x80) {
53-
return 1;
54-
}
55-
if ((head & 0xe0) === 0xc0) {
56-
return 2;
57-
}
58-
59-
if ((head & 0xf0) === 0xe0) {
60-
return 3;
61-
}
62-
throw new Error('string is not valid UTF-8 encode');
63-
};
64-
6551
var _hasOwnProperty = Object.prototype.hasOwnProperty;
6652
/* jshint -W001 */
6753
exports.hasOwnProperty = function hasOwnProperty(obj, property) {

lib/v1/decoder.js

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -250,24 +250,10 @@ proto._readUTF8String = function (len) {
250250
if (!is.number(len)) {
251251
len = this.byteBuffer.getUInt16();
252252
}
253-
var startPos = this.byteBuffer.position();
254-
var head;
255-
var l;
256-
debug('read utf8 string tunk, chars length: %d', len);
257-
258253
if (len === 0) {
259254
return '';
260255
}
261-
262-
while (len--) {
263-
head = this.byteBuffer.get();
264-
l = utils.lengthOfUTF8(head);
265-
this.byteBuffer.skip(l - 1);
266-
}
267-
debug('get string trunk. start position: %d, byte length: %d',
268-
startPos, this.byteBuffer.position() - startPos);
269-
270-
return this.byteBuffer.getRawString(startPos, this.byteBuffer.position() - startPos);
256+
return this.byteBuffer.getRawStringByStringLength(len);
271257
};
272258

273259
/**

0 commit comments

Comments
 (0)