Permalink
Browse files

Trading package size for float speed

  • Loading branch information...
dcodeIO committed Dec 5, 2016
1 parent 95c5538 commit 8a2dbc610a06fe3a1a2695a3ab032d073b77760d
Showing with 275 additions and 90 deletions.
  1. +3 −0 .eslintrc.json
  2. +4 −2 bench/bench.json
  3. +2 −0 bench/bench.proto
  4. +41 −11 bench/write.js
  5. +115 −36 dist/protobuf.js
  6. +1 −1 dist/protobuf.js.map
  7. +3 −3 dist/protobuf.min.js
  8. BIN dist/protobuf.min.js.gz
  9. +1 −1 dist/protobuf.min.js.map
  10. +7 −0 src/enum.js
  11. +6 −0 src/field.js
  12. +0 −2 src/index.js
  13. +92 −34 src/writer.js
@@ -5,6 +5,9 @@
},
"globals": {
"Uint8Array": true,
"Float32Array": true,
"Float64Array": true,
"define": true,
"XMLHttpRequest": true,
"Promise": true
},
@@ -13,7 +13,9 @@
"sint32": -42
},
"outer" : {
"bool" : [ true, false, false, true, false, false, true ]
"bool" : [ true, false, false, true, false, false, true ],
"double": 0.5
}
}
},
"float": 0.25
}
@@ -5,6 +5,7 @@ message Test {
string string = 1;
uint32 uint32 = 2;
Inner inner = 3;
float float = 4;
message Inner {
@@ -33,4 +34,5 @@ message Test {
message Outer {
repeated bool bool = 1;
double double = 2;
}
@@ -1,45 +1,75 @@
var protobuf = require("../src/index"),
newSuite = require("./suite");
// This benchmark compares raw data type performance of Uint8Array and Buffer.
// This uses internal finish machinery because overall difference is otherwise mainly caused by allocation.
var array = new Uint8Array(8);
var buffer = new Buffer(8);
// The following is a possible alternative to float / double writing - where supported. Quite a bit faster.
var f32 = new Float32Array(1);
var i8 = new Uint8Array(f32.buffer);
function writeF32Array(buf, pos, val) {
f32[0] = val;
for (var i = 0; i < 4; ++i)
buf[pos + i] = i8[i];
}
// raw float write speed
newSuite("float")
.add("Writer#float", function() {
var writer = new protobuf.Writer();
writer.float(0.1);
writer.finish();
writer._finish(writer.head, array);
})
.add("Writer#writeF32Array", function() {
var writer = new protobuf.Writer();
writer.push(writeF32Array, 4, 0.1);
writer._finish(writer.head, array);
})
.add("BufferWriter#float", function() {
var writer = new protobuf.BufferWriter();
writer.float(0.1);
writer.finish();
writer._finish(writer.head, buffer);
})
.add("BufferWriter#writeF32Array", function() {
var writer = new protobuf.BufferWriter();
writer.push(writeF32Array, 4, 0.1);
writer._finish(writer.head, buffer);
})
.run();
// raw double write speed
newSuite("double")
.add("Writer#double", function() {
var writer = new protobuf.Writer();
writer.double(0.1);
writer.finish();
writer._finish(writer.head, array);
})
.add("BufferWriter#double", function() {
var writer = new protobuf.BufferWriter();
writer.double(0.1);
writer.finish();
writer._finish(writer.head, buffer);
})
.run();
var bytes = [0, 0, 0, 0, 0, 0, 0, 0];
var arrayBytes = new Uint8Array(bytes);
var bufferBytes = Buffer.from(bytes);
var arrayPlus1 = new Uint8Array(array.length + 1);
var bufferPlus1 = new Buffer(buffer.length + 1);
// raw bytes write speed
// interestingly, Uint8Array#set is faster than Buffer#copy, but only on actual Uint8Arrays.
newSuite("bytes")
.add("Writer#bytes", function() {
var writer = new protobuf.Writer();
writer.bytes(arrayBytes);
writer.finish();
writer.bytes(array);
writer._finish(writer.head, arrayPlus1);
})
.add("BufferWriter#bytes", function() {
var writer = new protobuf.BufferWriter();
writer.bytes(bufferBytes);
writer.finish();
writer.bytes(buffer);
writer._finish(writer.head, bufferPlus1);
})
.run();

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.
Oops, something went wrong.

0 comments on commit 8a2dbc6

Please sign in to comment.