Skip to content

Commit

Permalink
Can't just inline longNeq but can be simplified
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Dec 16, 2016
1 parent 8f25377 commit 4531d75
Show file tree
Hide file tree
Showing 22 changed files with 922 additions and 70 deletions.
38 changes: 18 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ var protobuf = require("protobufjs");

Development:
```
<script src="//cdn.rawgit.com/dcodeIO/protobuf.js/6.1.0/dist/protobuf.js"></script>
<script src="//cdn.rawgit.com/dcodeIO/protobuf.js/6.X.X/dist/protobuf.js"></script>
```

Production:
```
<script src="//cdn.rawgit.com/dcodeIO/protobuf.js/6.1.0/dist/protobuf.min.js"></script>
<script src="//cdn.rawgit.com/dcodeIO/protobuf.js/6.X.X/dist/protobuf.min.js"></script>
```

**NOTE:** Remember to replace the version tag with the exact [release](https://github.com/dcodeIO/protobuf.js/releases) your project depends upon.
Expand Down Expand Up @@ -442,39 +442,37 @@ The package includes a benchmark that tries to compare performance to native JSO
```
benchmarking encoding performance ...
Type.encode to buffer x 481,172 ops/sec ±0.48% (92 runs sampled)
JSON.stringify to string x 307,509 ops/sec ±1.04% (92 runs sampled)
JSON.stringify to buffer x 164,463 ops/sec ±1.37% (89 runs sampled)
Type.encode to buffer x 521,803 ops/sec ±0.84% (88 runs sampled)
JSON.stringify to string x 300,362 ops/sec ±1.11% (86 runs sampled)
JSON.stringify to buffer x 169,413 ops/sec ±1.49% (86 runs sampled)
Type.encode to buffer was fastest
JSON.stringify to string was 36.4% slower
JSON.stringify to buffer was 66.1% slower
JSON.stringify to string was 42.6% slower
JSON.stringify to buffer was 67.7% slower
benchmarking decoding performance ...
Type.decode from buffer x 1,319,810 ops/sec ±0.71% (92 runs sampled)
JSON.parse from string x 298,578 ops/sec ±0.98% (90 runs sampled)
JSON.parse from buffer x 267,471 ops/sec ±0.81% (89 runs sampled)
Type.decode from buffer x 1,325,308 ops/sec ±1.46% (88 runs sampled)
JSON.parse from string x 283,907 ops/sec ±1.39% (86 runs sampled)
JSON.parse from buffer x 255,372 ops/sec ±1.28% (88 runs sampled)
Type.decode from buffer was fastest
JSON.parse from string was 77.4% slower
JSON.parse from buffer was 79.8% slower
JSON.parse from string was 78.6% slower
JSON.parse from buffer was 80.7% slower
benchmarking combined performance ...
Type to/from buffer x 262,728 ops/sec ±0.92% (92 runs sampled)
JSON to/from string x 129,405 ops/sec ±0.78% (94 runs sampled)
JSON to/from buffer x 89,523 ops/sec ±0.71% (89 runs sampled)
Type to/from buffer x 269,719 ops/sec ±0.87% (91 runs sampled)
JSON to/from string x 122,878 ops/sec ±1.59% (87 runs sampled)
JSON to/from buffer x 89,310 ops/sec ±1.01% (88 runs sampled)
Type to/from buffer was fastest
JSON to/from string was 50.7% slower
JSON to/from buffer was 65.9% slower
JSON to/from string was 54.8% slower
JSON to/from buffer was 66.9% slower
benchmarking verifying performance ...
Type.verify x 5,833,382 ops/sec ±0.98% (85 runs sampled)
Type.verify was fastest
Type.verify x 5,857,856 ops/sec ±0.82% (91 runs sampled)
```

Note that JSON is a native binding nowadays and as such is about as fast as it possibly can get. So, how can protobuf.js be faster?
Expand Down
2 changes: 1 addition & 1 deletion bench/prof.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ if (process.execArgv.indexOf("--prof") < 0) {
// Actual profiling code
var protobuf = require("..");

protobuf.util.codegen.verbose = true;
// protobuf.util.codegen.verbose = true;

var root = protobuf.parse(fs.readFileSync(require.resolve("../bench/bench.proto")).toString("utf8")).root;
var Test = root.lookup("Test");
Expand Down
4 changes: 2 additions & 2 deletions dist/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ CDN usage

Development:
```
<script src="//cdn.rawgit.com/dcodeIO/protobuf.js/6.1.0/dist/protobuf.js"></script>
<script src="//cdn.rawgit.com/dcodeIO/protobuf.js/6.X.X/dist/protobuf.js"></script>
```

Production:
```
<script src="//cdn.rawgit.com/dcodeIO/protobuf.js/6.1.0/dist/protobuf.min.js"></script>
<script src="//cdn.rawgit.com/dcodeIO/protobuf.js/6.X.X/dist/protobuf.min.js"></script>
```

**NOTE:** Remember to replace the version tag with the exact [release](https://github.com/dcodeIO/protobuf.js/releases) your project depends upon.
Expand Down
35 changes: 21 additions & 14 deletions dist/protobuf.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/protobuf.js.map

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions dist/protobuf.min.js

Large diffs are not rendered by default.

Binary file modified dist/protobuf.min.js.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion dist/protobuf.min.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/runtime/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ CDN usage

Development:
```
<script src="//cdn.rawgit.com/dcodeIO/protobuf.js/6.1.0/dist/runtime/protobuf.js"></script>
<script src="//cdn.rawgit.com/dcodeIO/protobuf.js/6.X.X/dist/runtime/protobuf.js"></script>
```

Production:
```
<script src="//cdn.rawgit.com/dcodeIO/protobuf.js/6.1.0/dist/runtime/protobuf.min.js"></script>
<script src="//cdn.rawgit.com/dcodeIO/protobuf.js/6.X.X/dist/runtime/protobuf.min.js"></script>
```

**NOTE:** Remember to replace the version tag with the exact [release](https://github.com/dcodeIO/protobuf.js/releases) your project depends upon.
Expand Down
23 changes: 18 additions & 5 deletions dist/runtime/protobuf.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/runtime/protobuf.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/runtime/protobuf.min.js

Large diffs are not rendered by default.

Binary file modified dist/runtime/protobuf.min.js.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion dist/runtime/protobuf.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
},
"dependencies": {
"@protobufjs/aspromise": "^1.0.3",
"@protobufjs/base64": "^1.0.2",
"@protobufjs/base64": "^1.0.3",
"@protobufjs/codegen": "^1.0.2",
"@protobufjs/eventemitter": "^1.0.3",
"@protobufjs/extend": "^1.0.0",
Expand Down
3 changes: 2 additions & 1 deletion scripts/gentests.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ var fs = require("fs"),

[
"tests/data/package.proto",
"tests/data/rpc.proto"
"tests/data/rpc.proto",
"tests/data/mapbox/vector_tile.proto"
]
.forEach(function(file) {
var out = file.replace(/\.proto$/, ".js");
Expand Down
12 changes: 3 additions & 9 deletions src/encode.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,11 @@ function encode(message, writer) {

// Non-repeated
} else {
var value = message[field.name],
longVal = field.long && typeof value === "number" ? util.LongBits.fromNumber(value) : value;
var value = message[field.name];
if (
field.partOf && message[field.partOf.name] === field.name
||
(field.required || value !== undefined) && (field.long ? longVal.lo !== field.defaultValue.low || longVal.hi !== field.defaultValue.high : value !== field.defaultValue)
(field.required || value !== undefined) && (field.long ? util.longNe(value, field.defaultValue.low, field.defaultValue.high) : value !== field.defaultValue)
) {
if (wireType !== undefined)
writer.uint32(field.id << 3 | wireType)[type](value);
Expand Down Expand Up @@ -107,7 +106,6 @@ encode.generate = function generate(mtype) {
("w||(w=Writer.create())");

var i;
var hasLongVar = false;
for (var i = 0; i < fields.length; ++i) {
var field = fields[i].resolve(),
type = field.resolvedType instanceof Enum ? "uint32" : field.type,
Expand Down Expand Up @@ -160,12 +158,8 @@ encode.generate = function generate(mtype) {
if (!field.required) {

if (field.long) {
if (!hasLongVar) { gen
("var l");
hasLongVar = true;
}
gen
("if(m%s!==undefined&&((l=typeof m%s===\"object\"?m%s:util.LongBits.from(m%s)).lo!==%d||l.hi!==%d))", prop, prop, prop, prop, field.defaultValue.low, field.defaultValue.high);
("if(m%s!==undefined&&util.longNe(m%s,%d,%d))", prop, prop, field.defaultValue.low, field.defaultValue.high);
} else gen
("if(m%s!==undefined&&m%s!==%j)", prop, prop, field.defaultValue);

Expand Down
2 changes: 1 addition & 1 deletion src/util/base64/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@protobufjs/base64",
"description": "A minimal base64 implementation for number arrays.",
"version": "1.0.2",
"version": "1.0.3",
"author": "Daniel Wirtz <[email protected]>",
"repository": {
"type": "git",
Expand Down
15 changes: 15 additions & 0 deletions src/util/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ util.longFromHash = function longFromHash(hash, unsigned) {
* @param {number|Long} b Second value
* @returns {boolean} `true` if not equal
* @deprecated
* @see Use {@link util.longNe} instead
*/
util.longNeq = function longNeq(a, b) {
return typeof a === "number"
Expand All @@ -104,6 +105,20 @@ util.longNeq = function longNeq(a, b) {
: a.low !== b.low || a.high !== b.high;
};

/**
* Tests if a possibily long value equals the specified low and high bits.
* @param {number|string|Long} val Value to test
* @param {number} lo Low bits to test against
* @param {number} hi High bits to test against
* @returns {boolean} `true` if not equal
*/
util.longNe = function longNe(val, lo, hi) {
if (typeof val === 'object') // Long-like, null is invalid and throws
return val.low !== lo || val.high !== hi;
var bits = util.LongBits.from(val);
return bits.lo !== lo || bits.hi !== hi;
};

/**
* Defines the specified properties on the specified target. Also adds getters and setters for non-ES5 environments.
* @param {Object} target Target object
Expand Down
Loading

0 comments on commit 4531d75

Please sign in to comment.