Skip to content

Commit e61a661

Browse files
committed
Fixed benchmarks, updated README
1 parent 6089781 commit e61a661

4 files changed

Lines changed: 79 additions & 79 deletions

File tree

Compiler.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ module.exports = {
1717
}
1818
const compiled = structure.compiledEncoder('source', alloc_tmp_var);
1919
const code = `
20-
(source, buffer=null, offset=0) => {
20+
(source, buffer=null, offset=0, override_alloc=null) => {
2121
let position = offset;
2222
let buffer_flexible = false;
2323
let i = 0;
2424
let tmp;
2525
${custom_vars.length > 0 ? 'let '+custom_vars.join(', ')+';' : ''}
2626
if(!buffer){
27-
buffer = Buffer.alloc(${non_alloc_size});
27+
buffer = Buffer.alloc(override_alloc || ${non_alloc_size});
2828
buffer_flexible = true;
2929
}
3030
${compiled}

README.md

Lines changed: 68 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -86,19 +86,18 @@ const buffer = MyMessage.encode(my_message);
8686
const message = MyMessage.decode(buffer);
8787
// t.deepEqual(message, my_message, 'This would pass');
8888

89-
// EXPERIMENTAL API
90-
// The following functions will be removed in the future.
89+
9190
// Generate a more efficient function to encode/decode.
9291
const encoder = bin.compileEncoder(MyMessage);
9392
const decoder = bin.compileEncoder(MyMessage);
9493

95-
// This is approximately 100% faster than recursive functions, but experimental at this time.
94+
// This is a bit faster than using MyMessage.encode/decode
9695
// For the maximum speed use a temporary buffer (allocated once)
9796
const tmp = Buffer.alloc(1024);
9897
encoder(my_message, tmp);
9998
// ...
10099
message = decoder(tmp);
101-
message = MyMessage.decode(tmp); // Backwards compatible
100+
message = MyMessage.decode(tmp); // Calls are compatible
102101
```
103102

104103
## API
@@ -130,97 +129,97 @@ String | A UTF-8 encoded string. Equivalent to Data with some post processing. C
130129

131130
## Benchmarks
132131

133-
The following benchmark compares Protobuf to this implementation for some basic data structure and a long string of length at least N. `binary-encoder-buf` uses a preallocated buffer for all operations. `binary-encoder-compiled` uses an experimental API that generates a structural function (rather than recursive) that is much more efficient.
132+
The following benchmark compares Protobuf to this implementation for some basic data structure and a long string of length at least N. `binary-encoder-buf` uses a preallocated buffer for all operations. `binary-encoder-compiled` uses an API that generates a structural function (rather than recursive) that is much more efficient. This API has replaced the old transcoding functions in version 0.4
134133

135134
```
136-
protobuf (encode) x 263,847 ops/sec ±2.28% (85 runs sampled)
137-
binary-encoder (encode) x 64,397 ops/sec ±3.27% (77 runs sampled)
138-
binary-encoder-buf (encode) x 166,912 ops/sec ±2.63% (80 runs sampled)
139-
binary-encoder-compiled (encode) x 252,473 ops/sec ±2.53% (83 runs sampled)
140-
binary-encoder-compiled-buf (encode) x 368,113 ops/sec ±2.07% (85 runs sampled)
141-
json (encode) x 304,341 ops/sec ±2.20% (88 runs sampled)
135+
protobuf (encode) x 275,101 ops/sec ±2.73% (86 runs sampled)
136+
binary-encoder (encode) x 279,292 ops/sec ±1.89% (86 runs sampled)
137+
binary-encoder-buf (encode) x 378,238 ops/sec ±3.42% (91 runs sampled)
138+
binary-encoder-compiled (encode) x 302,381 ops/sec ±1.64% (84 runs sampled)
139+
binary-encoder-compiled-buf (encode) x 396,369 ops/sec ±0.80% (94 runs sampled)
140+
json (encode) x 334,773 ops/sec ±1.24% (91 runs sampled)
142141
Fastest Encoding for N=10 is binary-encoder-compiled-buf (encode)
143142
144-
protobuf (decode) x 888,844 ops/sec ±2.16% (81 runs sampled)
145-
binary-encoder (decode) x 305,803 ops/sec ±2.70% (76 runs sampled)
146-
binary-encoder-compiled (decode) x 570,504 ops/sec ±1.87% (88 runs sampled)
147-
json (decode) x 1,087,999 ops/sec ±2.08% (87 runs sampled)
143+
protobuf (decode) x 1,003,710 ops/sec ±2.11% (95 runs sampled)
144+
binary-encoder (decode) x 577,905 ops/sec ±0.59% (92 runs sampled)
145+
binary-encoder-compiled (decode) x 619,247 ops/sec ±1.12% (92 runs sampled)
146+
json (decode) x 1,218,423 ops/sec ±1.39% (93 runs sampled)
148147
Fastest Decoding for N=10 is json (decode)
149148
150149
Message sizes:
151150
protobuf 102 bytes
152151
binary-encoder 95
153152
json 265
154153
155-
protobuf (encode) x 248,611 ops/sec ±2.20% (84 runs sampled)
156-
binary-encoder (encode) x 69,762 ops/sec ±1.77% (84 runs sampled)
157-
binary-encoder-buf (encode) x 171,689 ops/sec ±3.98% (81 runs sampled)
158-
binary-encoder-compiled (encode) x 224,163 ops/sec ±3.91% (75 runs sampled)
159-
binary-encoder-compiled-buf (encode) x 332,148 ops/sec ±3.53% (79 runs sampled)
160-
json (encode) x 125,643 ops/sec ±2.27% (83 runs sampled)
161-
Fastest Encoding for N=100 is binary-encoder-compiled-buf (encode)
162-
163-
protobuf (decode) x 849,158 ops/sec ±3.50% (83 runs sampled)
164-
binary-encoder (decode) x 309,057 ops/sec ±2.86% (78 runs sampled)
165-
binary-encoder-compiled (decode) x 481,219 ops/sec ±4.96% (74 runs sampled)
166-
json (decode) x 631,153 ops/sec ±1.97% (87 runs sampled)
154+
protobuf (encode) x 302,829 ops/sec ±0.61% (93 runs sampled)
155+
binary-encoder (encode) x 292,029 ops/sec ±2.80% (83 runs sampled)
156+
binary-encoder-buf (encode) x 387,583 ops/sec ±2.05% (92 runs sampled)
157+
binary-encoder-compiled (encode) x 317,967 ops/sec ±1.79% (90 runs sampled)
158+
binary-encoder-compiled-buf (encode) x 390,195 ops/sec ±2.22% (94 runs sampled)
159+
json (encode) x 139,347 ops/sec ±1.84% (90 runs sampled)
160+
Fastest Encoding for N=100 is binary-encoder-compiled-buf (encode),binary-encoder-buf (encode)
161+
162+
protobuf (decode) x 988,917 ops/sec ±1.54% (89 runs sampled)
163+
binary-encoder (decode) x 611,869 ops/sec ±2.07% (90 runs sampled)
164+
binary-encoder-compiled (decode) x 625,291 ops/sec ±0.65% (93 runs sampled)
165+
json (decode) x 701,532 ops/sec ±2.00% (93 runs sampled)
167166
Fastest Decoding for N=100 is protobuf (decode)
168167
169168
Message sizes:
170169
protobuf 190 bytes
171170
binary-encoder 183
172171
json 529
173172
174-
protobuf (encode) x 218,729 ops/sec ±3.15% (78 runs sampled)
175-
binary-encoder (encode) x 64,804 ops/sec ±2.43% (85 runs sampled)
176-
binary-encoder-buf (encode) x 173,645 ops/sec ±2.57% (82 runs sampled)
177-
binary-encoder-compiled (encode) x 218,610 ops/sec ±3.87% (73 runs sampled)
178-
binary-encoder-compiled-buf (encode) x 357,404 ops/sec ±2.50% (84 runs sampled)
179-
json (encode) x 18,060 ops/sec ±2.86% (85 runs sampled)
173+
protobuf (encode) x 265,475 ops/sec ±1.54% (89 runs sampled)
174+
binary-encoder (encode) x 272,316 ops/sec ±2.26% (89 runs sampled)
175+
binary-encoder-buf (encode) x 361,452 ops/sec ±2.31% (89 runs sampled)
176+
binary-encoder-compiled (encode) x 276,349 ops/sec ±2.03% (87 runs sampled)
177+
binary-encoder-compiled-buf (encode) x 389,293 ops/sec ±0.95% (91 runs sampled)
178+
json (encode) x 20,450 ops/sec ±2.52% (90 runs sampled)
180179
Fastest Encoding for N=1000 is binary-encoder-compiled-buf (encode)
181180
182-
protobuf (decode) x 907,313 ops/sec ±1.46% (89 runs sampled)
183-
binary-encoder (decode) x 339,065 ops/sec ±1.98% (86 runs sampled)
184-
binary-encoder-compiled (decode) x 580,952 ops/sec ±1.68% (86 runs sampled)
185-
json (decode) x 138,365 ops/sec ±1.58% (90 runs sampled)
181+
protobuf (decode) x 1,005,756 ops/sec ±0.64% (90 runs sampled)
182+
binary-encoder (decode) x 600,982 ops/sec ±2.93% (93 runs sampled)
183+
binary-encoder-compiled (decode) x 608,827 ops/sec ±1.73% (86 runs sampled)
184+
json (decode) x 150,480 ops/sec ±0.92% (97 runs sampled)
186185
Fastest Decoding for N=1000 is protobuf (decode)
187186
188187
Message sizes:
189188
protobuf 1091 bytes
190189
binary-encoder 1084
191190
json 3229
192191
193-
protobuf (encode) x 143,791 ops/sec ±4.72% (76 runs sampled)
194-
binary-encoder (encode) x 44,473 ops/sec ±3.73% (81 runs sampled)
195-
binary-encoder-buf (encode) x 162,560 ops/sec ±2.35% (82 runs sampled)
196-
binary-encoder-compiled (encode) x 82,039 ops/sec ±6.18% (65 runs sampled)
197-
binary-encoder-compiled-buf (encode) x 289,004 ops/sec ±3.98% (81 runs sampled)
198-
json (encode) x 1,675 ops/sec ±5.04% (77 runs sampled)
192+
protobuf (encode) x 188,602 ops/sec ±1.95% (90 runs sampled)
193+
binary-encoder (encode) x 96,150 ops/sec ±6.07% (75 runs sampled)
194+
binary-encoder-buf (encode) x 323,255 ops/sec ±2.10% (92 runs sampled)
195+
binary-encoder-compiled (encode) x 91,660 ops/sec ±6.28% (70 runs sampled)
196+
binary-encoder-compiled-buf (encode) x 324,058 ops/sec ±1.82% (93 runs sampled)
197+
json (encode) x 2,152 ops/sec ±2.42% (94 runs sampled)
199198
Fastest Encoding for N=10000 is binary-encoder-compiled-buf (encode)
200199
201-
protobuf (decode) x 848,061 ops/sec ±3.28% (82 runs sampled)
202-
binary-encoder (decode) x 338,013 ops/sec ±1.77% (84 runs sampled)
203-
binary-encoder-compiled (decode) x 563,351 ops/sec ±2.63% (84 runs sampled)
204-
json (decode) x 16,073 ops/sec ±0.91% (93 runs sampled)
200+
protobuf (decode) x 1,003,324 ops/sec ±1.62% (94 runs sampled)
201+
binary-encoder (decode) x 620,480 ops/sec ±1.18% (93 runs sampled)
202+
binary-encoder-compiled (decode) x 604,111 ops/sec ±1.75% (91 runs sampled)
203+
json (decode) x 16,745 ops/sec ±1.37% (89 runs sampled)
205204
Fastest Decoding for N=10000 is protobuf (decode)
206205
207206
Message sizes:
208207
protobuf 10091 bytes
209208
binary-encoder 10084
210209
json 30229
211210
212-
protobuf (encode) x 35,712 ops/sec ±8.44% (72 runs sampled)
213-
binary-encoder (encode) x 11,461 ops/sec ±6.95% (75 runs sampled)
214-
binary-encoder-buf (encode) x 99,910 ops/sec ±1.93% (87 runs sampled)
215-
binary-encoder-compiled (encode) x 15,237 ops/sec ±5.31% (82 runs sampled)
216-
binary-encoder-compiled-buf (encode) x 149,437 ops/sec ±1.35% (90 runs sampled)
217-
json (encode) x 171 ops/sec ±3.47% (72 runs sampled)
218-
Fastest Encoding for N=100000 is binary-encoder-compiled-buf (encode)
219-
220-
protobuf (decode) x 842,279 ops/sec ±2.77% (82 runs sampled)
221-
binary-encoder (decode) x 316,650 ops/sec ±2.88% (81 runs sampled)
222-
binary-encoder-compiled (decode) x 591,702 ops/sec ±1.93% (85 runs sampled)
223-
json (decode) x 1,632 ops/sec ±0.68% (91 runs sampled)
211+
protobuf (encode) x 43,479 ops/sec ±7.56% (76 runs sampled)
212+
binary-encoder (encode) x 17,175 ops/sec ±3.98% (84 runs sampled)
213+
binary-encoder-buf (encode) x 144,706 ops/sec ±1.55% (91 runs sampled)
214+
binary-encoder-compiled (encode) x 16,500 ops/sec ±5.55% (83 runs sampled)
215+
binary-encoder-compiled-buf (encode) x 139,586 ops/sec ±2.27% (91 runs sampled)
216+
json (encode) x 196 ops/sec ±0.73% (82 runs sampled)
217+
Fastest Encoding for N=100000 is binary-encoder-buf (encode)
218+
219+
protobuf (decode) x 1,005,751 ops/sec ±1.22% (95 runs sampled)
220+
binary-encoder (decode) x 626,437 ops/sec ±0.65% (91 runs sampled)
221+
binary-encoder-compiled (decode) x 613,772 ops/sec ±1.72% (94 runs sampled)
222+
json (decode) x 1,725 ops/sec ±1.14% (93 runs sampled)
224223
Fastest Decoding for N=100000 is protobuf (decode)
225224
226225
Message sizes:
@@ -230,18 +229,18 @@ json 300229
230229
231230
Results in CSV for plots:
232231
,protobuf (encode),binary-encoder (encode),binary-encoder-buf (encode),binary-encoder-compiled (encode),binary-encoder-compiled-buf (encode),json (encode)
233-
10,263846.9073576735,64396.66812325595,166912.4244514077,252472.81891420262,368113.2204249347,304340.9733025798
234-
100,248611.2680726218,69761.85906312823,171688.58036597492,224162.88279805557,332147.5526819311,125643.16435198345
235-
1000,218729.48935518216,64804.45415508803,173644.57690728805,218610.08661295244,357403.71497006307,18059.71502669489
236-
10000,143790.94828889717,44472.53288733788,162559.73559819476,82039.03267299477,289004.0518322682,1674.9432137929612
237-
100000,35712.408241261044,11461.013689383273,99909.66569366025,15236.606656340335,149436.75087655275,170.69591493006303
232+
10,275100.89346097526,279292.22569242545,378238.19335844467,302381.22546956234,396368.88476240553,334772.6771391154
233+
100,302829.2292918837,292028.7469037824,387583.1739925365,317966.9913567071,390194.88813506975,139346.78461104393
234+
1000,265474.88563085516,272316.1912953785,361451.80561332963,276349.15915051894,389293.1210141299,20450.177162941625
235+
10000,188601.77150885522,96150.19232190275,323255.18721761,91659.97990245724,324058.26913309,2152.266905785588
236+
100000,43479.42604434454,17174.572831579495,144705.5132266813,16499.57203120676,139585.7696546299,196.42320811682777
238237
239238
,protobuf (decode),binary-encoder (decode),binary-encoder-compiled (decode),json (decode)
240-
10,888843.5927172618,305803.3415753891,570503.5158795862,1087998.6920098183
241-
100,849158.20272219,309056.5345350961,481219.4509456205,631152.9125705716
242-
1000,907313.1785145177,339065.13895150385,580952.110157544,138365.14403254417
243-
10000,848061.1820993097,338013.1009018717,563350.7465400948,16073.098278377967
244-
100000,842279.1236521575,316650.2037582322,591702.3236950247,1632.3852198601567
239+
10,1003710.2172942789,577904.5272137446,619247.4478993919,1218423.0929970394
240+
100,988917.2158851528,611868.8883996161,625291.1413850804,701531.7270248663
241+
1000,1005756.3483413532,600981.523941358,608826.877034854,150480.01344809862
242+
10000,1003323.8427481409,620480.0659664081,604110.5598468462,16744.669505525177
243+
100000,1005751.3702685619,626437.4469872701,613771.6145731651,1725.0958798853685
245244
```
246245

247246
## Contributing

Type.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@ class TranscodableType {
1414
* @param {*} object The object/value to be encoded.
1515
* @param {Buffer} [buffer] The buffer to write to. If the buffer is too small an exception will be thrown.
1616
* @param {Number} [offset] The offset in buffer to write at.
17+
* @param {Number} [non_alloc_size] If _buffer_ is not passed then allocate a buffer of this size.
1718
* @returns {Buffer} The buffer that contains the encoded data. If a new buffer is allocated then the data was written from the begining.
1819
*/
19-
encode(object, buffer, offset){
20+
encode(object, buffer, offset, non_alloc_size=null){
2021
if(!this._tmp_encoder_compiled)
2122
this._tmp_encoder_compiled = compileEncoder(this);
22-
return this._tmp_encoder_compiled(object, buffer, offset);
23+
return this._tmp_encoder_compiled(object, buffer, offset, non_alloc_size);
2324
}
2425
/**
2526
* Decode an object from a binary representation.

benchmark.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,28 @@ function runTestsForN(n){
2121
]
2222
};
2323
const buf = Buffer.allocUnsafe(2*n+1024);
24-
const turboEncoder = Compiler.compileEncoder(MyMessage, buf.length);
24+
const turboEncoder = Compiler.compileEncoder(MyMessage);
2525
const turboDecoder = Compiler.compileDecoder(MyMessage);
2626

2727
const enc_protobuf = defs.MyMessage.encode(original);
2828
const enc_json = JSON.stringify(original);
29-
const enc_bin = MyMessage.encode(original);
29+
const enc_bin = MyMessage.encode(original, null, null, buf.length);
3030

3131
new Benchmark.Suite('Encode')
3232
.add('protobuf (encode)', () => {
3333
defs.MyMessage.encode(original);
3434
})
3535
.add('binary-encoder (encode)', () => {
36-
MyMessage.encode(original);
36+
MyMessage.encode(original, null, null, buf.length);
3737
})
3838
.add('binary-encoder-buf (encode)', () => {
39-
MyMessage.encode(original, buf);
39+
MyMessage.encode(original, buf, null, buf.length);
4040
})
4141
.add('binary-encoder-compiled (encode)', () => {
42-
turboEncoder(original);
42+
turboEncoder(original, null, null, buf.length);
4343
})
4444
.add('binary-encoder-compiled-buf (encode)', () => {
45-
turboEncoder(original, buf);
45+
turboEncoder(original, buf, null, buf.length);
4646
})
4747
.add('json (encode)', () => {
4848
JSON.stringify(original);

0 commit comments

Comments
 (0)