Skip to content

Commit

Permalink
Can finally run with --trace-deopt again without crashes
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Dec 17, 2016
1 parent 9c9a66b commit f8451f0
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 53 deletions.
2 changes: 1 addition & 1 deletion bench/prof.js
Expand Up @@ -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'
});
Expand Down
38 changes: 20 additions & 18 deletions src/encode.js
Expand Up @@ -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();
Expand All @@ -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();
}

}
Expand All @@ -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)
Expand Down Expand Up @@ -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<ks.length;++i){", prop)
("w.uint32(%d).fork().uint32(%d).%s(ks[i])", field.id << 3 | 2, 8 | types.mapKey[keyType], keyType);
("w.uint32(%d).fork().uint32(%d).%s(ks[i])", (field.id << 3 | 2) >>> 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
Expand All @@ -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<m%s.length;++i)", prop)
("w.%s(m%s[i])", type, prop)
("w.ldelim(%d)", field.id)
("w.ldelim()", field.id)
("}");

// Non-packed
Expand All @@ -147,9 +149,9 @@ encode.generate = function generate(mtype) {
("if(m%s)", prop)
("for(var i=0;i<m%s.length;++i)", prop);
if (wireType !== undefined) gen
("w.uint32(%d).%s(m%s[i])", field.id << 3 | wireType, type, prop);
("w.uint32(%d).%s(m%s[i])", (field.id << 3 | wireType) >>> 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);

}

Expand All @@ -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

Expand All @@ -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

Expand Down
44 changes: 22 additions & 22 deletions src/reader.js
Expand Up @@ -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;
}
}
Expand Down
24 changes: 12 additions & 12 deletions src/writer.js
Expand Up @@ -16,25 +16,19 @@ 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.
* @type {function(Uint8Array, number, *)}
*/
this.fn = fn;

/**
* Value to write.
* @type {*}
*/
this.val = val;

/**
* Value byte length.
* @type {number}
Expand All @@ -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;
Expand Down Expand Up @@ -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;
};
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit f8451f0

Please sign in to comment.