diff --git a/bench/prof.js b/bench/prof.js index 8763278dd..664fb36f3 100644 --- a/bench/prof.js +++ b/bench/prof.js @@ -19,7 +19,7 @@ if (process.execArgv.indexOf("--prof") < 0) { fs.unlink(file); }); console.log("generating profile (may take a while) ..."); - var child = child_process.execSync("node --prof " + process.argv.slice(1).join(' '), { + var child = child_process.execSync("node --prof --trace-deopt " + process.argv.slice(1).join(' '), { cwd: process.cwd(), stdio: 'inherit' }); diff --git a/src/encode.js b/src/encode.js index 4d5e0dbf6..181a7052f 100644 --- a/src/encode.js +++ b/src/encode.js @@ -30,7 +30,7 @@ function encode(message, writer) { var keyType = field.resolvedKeyType /* only valid is enum */ ? "uint32" : field.keyType; if (message[field.name] && message[field.name] !== util.emptyObject) { for (var keys = Object.keys(message[field.name]), i = 0; i < keys.length; ++i) { - writer.uint32(field.id << 3 | 2).fork() + writer.uint32((field.id << 3 | 2) >>> 0).fork() .uint32(/*1*/8 | types.mapKey[keyType])[keyType](keys[i]); if (wireType === undefined) field.resolvedType.encode(message[field.name][keys[i]], writer.uint32(/*2,2*/18).fork()).ldelim(); @@ -47,21 +47,23 @@ function encode(message, writer) { // Packed repeated if (field.packed && types.packed[type] !== undefined) { - writer.fork(); - var i = 0; - while (i < values.length) - writer[type](values[i++]); - writer.ldelim(field.id); + if (values.length) { + writer.uint32((field.id << 3 | 2) >>> 0).fork(); + var i = 0; + while (i < values.length) + writer[type](values[i++]); + writer.ldelim(); + } // Non-packed } else { var i = 0; if (wireType !== undefined) while (i < values.length) - writer.uint32(field.id << 3 | wireType)[type](values[i++]); + writer.uint32((field.id << 3 | wireType) >>> 0)[type](values[i++]); else while (i < values.length) - field.resolvedType.encode(values[i++], writer.uint32(field.id << 3 | 2).fork()).ldelim(); + field.resolvedType.encode(values[i++], writer.uint32((field.id << 3 | 2) >>> 0).fork()).ldelim(); } } @@ -75,7 +77,7 @@ function encode(message, writer) { (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); + writer.uint32((field.id << 3 | wireType) >>> 0)[type](value); else { field.resolvedType.encode(value, writer.fork()); if (writer.len || field.required) @@ -118,7 +120,7 @@ encode.generate = function generate(mtype) { gen ("if(m%s&&m%s!==util.emptyObject){", prop, prop) ("for(var ks=Object.keys(m%s),i=0;i>> 0, 8 | types.mapKey[keyType], keyType); if (wireType === undefined) gen ("types[%d].encode(m%s[ks[i]],w.uint32(18).fork()).ldelim()", i, prop); else gen @@ -135,10 +137,10 @@ encode.generate = function generate(mtype) { if (field.packed && types.packed[type] !== undefined) { gen ("if(m%s&&m%s.length){", prop, prop) - ("w.fork()") + ("w.uint32(%d).fork()", (field.id << 3 | 2) >>> 0) ("for(var i=0;i>> 0, type, prop); else gen - ("types[%d].encode(m%s[i],w.uint32(%d).fork()).ldelim()", i, prop, field.id << 3 | 2); + ("types[%d].encode(m%s[i],w.uint32(%d).fork()).ldelim()", i, prop, (field.id << 3 | 2) >>> 0); } @@ -167,11 +169,11 @@ encode.generate = function generate(mtype) { if (wireType !== undefined) gen - ("w.uint32(%d).%s(m%s)", field.id << 3 | wireType, type, prop); + ("w.uint32(%d).%s(m%s)", (field.id << 3 | wireType) >>> 0, type, prop); else if (field.required) gen - ("types[%d].encode(m%s,w.uint32(%d).fork()).ldelim()", i, prop, field.id << 3 | 2); + ("types[%d].encode(m%s,w.uint32(%d).fork()).ldelim()", i, prop, (field.id << 3 | 2) >>> 0); else gen @@ -195,11 +197,11 @@ encode.generate = function generate(mtype) { if (wireType !== undefined) gen - ("w.uint32(%d).%s(m%s)", field.id << 3 | wireType, type, prop); + ("w.uint32(%d).%s(m%s)", (field.id << 3 | wireType) >>> 0, type, prop); else if (field.required) gen - ("types[%d].encode(m%s,w.uint32(%d).fork()).ldelim()", fields.indexOf(field), prop, field.id << 3 | 2); + ("types[%d].encode(m%s,w.uint32(%d).fork()).ldelim()", fields.indexOf(field), prop, (field.id << 3 | 2) >>> 0); else gen diff --git a/src/reader.js b/src/reader.js index d99150583..95a26d5fb 100644 --- a/src/reader.js +++ b/src/reader.js @@ -101,52 +101,52 @@ ReaderPrototype.sint32 = function read_sint32() { /* eslint-disable no-invalid-this */ function readLongVarint() { + // tends to deopt with local vars for octet etc. var bits = new LongBits(0, 0), - i = 0, - octet = 0; + i = 0; if (this.len - this.pos > 4) { // fast route (lo) for (i = 0; i < 4; ++i) { - octet= this.buf[this.pos++]; // 1st..4th - bits.lo = (bits.lo | (octet & 127) << i * 7) >>> 0; - if (octet < 128) + // 1st..4th + bits.lo = (bits.lo | (this.buf[this.pos] & 127) << i * 7) >>> 0; + if (this.buf[this.pos++] < 128) return bits; } - octet = this.buf[this.pos++]; // 5th - bits.lo = (bits.lo | (octet & 127) << 28) >>> 0; - bits.hi = (bits.hi | (octet & 127) >> 4) >>> 0; - if (octet < 128) + // 5th + bits.lo = (bits.lo | (this.buf[this.pos] & 127) << 28) >>> 0; + bits.hi = (bits.hi | (this.buf[this.pos] & 127) >> 4) >>> 0; + if (this.buf[this.pos++] < 128) return bits; } else { for (i = 0; i < 4; ++i) { if (this.pos >= this.len) throw indexOutOfRange(this); - octet = this.buf[this.pos++]; // 1st..4th - bits.lo = (bits.lo | (octet & 127) << i * 7) >>> 0; - if (octet < 128) + // 1st..4th + bits.lo = (bits.lo | (this.buf[this.pos] & 127) << i * 7) >>> 0; + if (this.buf[this.pos++] < 128) return bits; } if (this.pos >= this.len) throw indexOutOfRange(this); - octet = this.buf[this.pos++]; // 5th - bits.lo = (bits.lo | (octet & 127) << 28) >>> 0; - bits.hi = (bits.hi | (octet & 127) >> 4) >>> 0; - if (octet < 128) + // 5th + bits.lo = (bits.lo | (this.buf[this.pos] & 127) << 28) >>> 0; + bits.hi = (bits.hi | (this.buf[this.pos] & 127) >> 4) >>> 0; + if (this.buf[this.pos++] < 128) return bits; } if (this.len - this.pos > 4) { // fast route (hi) for (i = 0; i < 5; ++i) { - octet = this.buf[this.pos++]; // 6th..10th - bits.hi = (bits.hi | (octet & 127) << i * 7 + 3) >>> 0; - if (octet < 128) + // 6th..10th + bits.hi = (bits.hi | (this.buf[this.pos] & 127) << i * 7 + 3) >>> 0; + if (this.buf[this.pos++] < 128) return bits; } } else { for (i = 0; i < 5; ++i) { if (this.pos >= this.len) throw indexOutOfRange(this); - octet = this.buf[this.pos++]; // 6th..10th - bits.hi = (bits.hi | (octet & 127) << i * 7 + 3) >>> 0; - if (octet < 128) + // 6th..10th + bits.hi = (bits.hi | (this.buf[this.pos] & 127) << i * 7 + 3) >>> 0; + if (this.buf[this.pos++] < 128) return bits; } } diff --git a/src/writer.js b/src/writer.js index ec69517eb..2ed6069ff 100644 --- a/src/writer.js +++ b/src/writer.js @@ -16,12 +16,12 @@ var ArrayImpl = typeof Uint8Array !== "undefined" ? Uint8Array : Array; * @memberof Writer * @constructor * @param {function(*, Uint8Array, number)} fn Function to call - * @param {*} val Value to write * @param {number} len Value byte length + * @param {*} val Value to write * @private * @ignore */ -function Op(fn, val, len) { +function Op(fn, len, val) { /** * Function to call. @@ -29,12 +29,6 @@ function Op(fn, val, len) { */ this.fn = fn; - /** - * Value to write. - * @type {*} - */ - this.val = val; - /** * Value byte length. * @type {number} @@ -43,9 +37,15 @@ function Op(fn, val, len) { /** * Next operation. - * @type {?Writer.Op} + * @type {Writer.Op|undefined} + */ + // this.next = undefined; + + /** + * Value to write. + * @type {*} */ - this.next = null; + this.val = val; // type varies } Writer.Op = Op; @@ -160,7 +160,7 @@ var WriterPrototype = Writer.prototype; * @returns {Writer} `this` */ WriterPrototype.push = function push(fn, len, val) { - this.tail = this.tail.next = new Op(fn, val, len); + this.tail = this.tail.next = new Op(fn, len, val); this.len += len; return this; }; @@ -469,7 +469,7 @@ WriterPrototype.ldelim = function ldelim(id) { tail = this.tail, len = this.len; this.reset(); - if (id) + if (typeof id === 'number') this.uint32((id << 3 | 2) >>> 0); this.uint32(len); this.tail.next = head.next; // skip noop