Skip to content

Commit f8451f0

Browse files
committed
Can finally run with --trace-deopt again without crashes
1 parent 9c9a66b commit f8451f0

File tree

4 files changed

+55
-53
lines changed

4 files changed

+55
-53
lines changed

bench/prof.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ if (process.execArgv.indexOf("--prof") < 0) {
1919
fs.unlink(file);
2020
});
2121
console.log("generating profile (may take a while) ...");
22-
var child = child_process.execSync("node --prof " + process.argv.slice(1).join(' '), {
22+
var child = child_process.execSync("node --prof --trace-deopt " + process.argv.slice(1).join(' '), {
2323
cwd: process.cwd(),
2424
stdio: 'inherit'
2525
});

src/encode.js

+20-18
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function encode(message, writer) {
3030
var keyType = field.resolvedKeyType /* only valid is enum */ ? "uint32" : field.keyType;
3131
if (message[field.name] && message[field.name] !== util.emptyObject) {
3232
for (var keys = Object.keys(message[field.name]), i = 0; i < keys.length; ++i) {
33-
writer.uint32(field.id << 3 | 2).fork()
33+
writer.uint32((field.id << 3 | 2) >>> 0).fork()
3434
.uint32(/*1*/8 | types.mapKey[keyType])[keyType](keys[i]);
3535
if (wireType === undefined)
3636
field.resolvedType.encode(message[field.name][keys[i]], writer.uint32(/*2,2*/18).fork()).ldelim();
@@ -47,21 +47,23 @@ function encode(message, writer) {
4747

4848
// Packed repeated
4949
if (field.packed && types.packed[type] !== undefined) {
50-
writer.fork();
51-
var i = 0;
52-
while (i < values.length)
53-
writer[type](values[i++]);
54-
writer.ldelim(field.id);
50+
if (values.length) {
51+
writer.uint32((field.id << 3 | 2) >>> 0).fork();
52+
var i = 0;
53+
while (i < values.length)
54+
writer[type](values[i++]);
55+
writer.ldelim();
56+
}
5557

5658
// Non-packed
5759
} else {
5860
var i = 0;
5961
if (wireType !== undefined)
6062
while (i < values.length)
61-
writer.uint32(field.id << 3 | wireType)[type](values[i++]);
63+
writer.uint32((field.id << 3 | wireType) >>> 0)[type](values[i++]);
6264
else
6365
while (i < values.length)
64-
field.resolvedType.encode(values[i++], writer.uint32(field.id << 3 | 2).fork()).ldelim();
66+
field.resolvedType.encode(values[i++], writer.uint32((field.id << 3 | 2) >>> 0).fork()).ldelim();
6567
}
6668

6769
}
@@ -75,7 +77,7 @@ function encode(message, writer) {
7577
(field.required || value !== undefined) && (field.long ? util.longNe(value, field.defaultValue.low, field.defaultValue.high) : value !== field.defaultValue)
7678
) {
7779
if (wireType !== undefined)
78-
writer.uint32(field.id << 3 | wireType)[type](value);
80+
writer.uint32((field.id << 3 | wireType) >>> 0)[type](value);
7981
else {
8082
field.resolvedType.encode(value, writer.fork());
8183
if (writer.len || field.required)
@@ -118,7 +120,7 @@ encode.generate = function generate(mtype) {
118120
gen
119121
("if(m%s&&m%s!==util.emptyObject){", prop, prop)
120122
("for(var ks=Object.keys(m%s),i=0;i<ks.length;++i){", prop)
121-
("w.uint32(%d).fork().uint32(%d).%s(ks[i])", field.id << 3 | 2, 8 | types.mapKey[keyType], keyType);
123+
("w.uint32(%d).fork().uint32(%d).%s(ks[i])", (field.id << 3 | 2) >>> 0, 8 | types.mapKey[keyType], keyType);
122124
if (wireType === undefined) gen
123125
("types[%d].encode(m%s[ks[i]],w.uint32(18).fork()).ldelim()", i, prop);
124126
else gen
@@ -135,10 +137,10 @@ encode.generate = function generate(mtype) {
135137
if (field.packed && types.packed[type] !== undefined) { gen
136138

137139
("if(m%s&&m%s.length){", prop, prop)
138-
("w.fork()")
140+
("w.uint32(%d).fork()", (field.id << 3 | 2) >>> 0)
139141
("for(var i=0;i<m%s.length;++i)", prop)
140142
("w.%s(m%s[i])", type, prop)
141-
("w.ldelim(%d)", field.id)
143+
("w.ldelim()", field.id)
142144
("}");
143145

144146
// Non-packed
@@ -147,9 +149,9 @@ encode.generate = function generate(mtype) {
147149
("if(m%s)", prop)
148150
("for(var i=0;i<m%s.length;++i)", prop);
149151
if (wireType !== undefined) gen
150-
("w.uint32(%d).%s(m%s[i])", field.id << 3 | wireType, type, prop);
152+
("w.uint32(%d).%s(m%s[i])", (field.id << 3 | wireType) >>> 0, type, prop);
151153
else gen
152-
("types[%d].encode(m%s[i],w.uint32(%d).fork()).ldelim()", i, prop, field.id << 3 | 2);
154+
("types[%d].encode(m%s[i],w.uint32(%d).fork()).ldelim()", i, prop, (field.id << 3 | 2) >>> 0);
153155

154156
}
155157

@@ -167,11 +169,11 @@ encode.generate = function generate(mtype) {
167169

168170
if (wireType !== undefined) gen
169171

170-
("w.uint32(%d).%s(m%s)", field.id << 3 | wireType, type, prop);
172+
("w.uint32(%d).%s(m%s)", (field.id << 3 | wireType) >>> 0, type, prop);
171173

172174
else if (field.required) gen
173175

174-
("types[%d].encode(m%s,w.uint32(%d).fork()).ldelim()", i, prop, field.id << 3 | 2);
176+
("types[%d].encode(m%s,w.uint32(%d).fork()).ldelim()", i, prop, (field.id << 3 | 2) >>> 0);
175177

176178
else gen
177179

@@ -195,11 +197,11 @@ encode.generate = function generate(mtype) {
195197

196198
if (wireType !== undefined) gen
197199

198-
("w.uint32(%d).%s(m%s)", field.id << 3 | wireType, type, prop);
200+
("w.uint32(%d).%s(m%s)", (field.id << 3 | wireType) >>> 0, type, prop);
199201

200202
else if (field.required) gen
201203

202-
("types[%d].encode(m%s,w.uint32(%d).fork()).ldelim()", fields.indexOf(field), prop, field.id << 3 | 2);
204+
("types[%d].encode(m%s,w.uint32(%d).fork()).ldelim()", fields.indexOf(field), prop, (field.id << 3 | 2) >>> 0);
203205

204206
else gen
205207

src/reader.js

+22-22
Original file line numberDiff line numberDiff line change
@@ -101,52 +101,52 @@ ReaderPrototype.sint32 = function read_sint32() {
101101
/* eslint-disable no-invalid-this */
102102

103103
function readLongVarint() {
104+
// tends to deopt with local vars for octet etc.
104105
var bits = new LongBits(0, 0),
105-
i = 0,
106-
octet = 0;
106+
i = 0;
107107
if (this.len - this.pos > 4) { // fast route (lo)
108108
for (i = 0; i < 4; ++i) {
109-
octet= this.buf[this.pos++]; // 1st..4th
110-
bits.lo = (bits.lo | (octet & 127) << i * 7) >>> 0;
111-
if (octet < 128)
109+
// 1st..4th
110+
bits.lo = (bits.lo | (this.buf[this.pos] & 127) << i * 7) >>> 0;
111+
if (this.buf[this.pos++] < 128)
112112
return bits;
113113
}
114-
octet = this.buf[this.pos++]; // 5th
115-
bits.lo = (bits.lo | (octet & 127) << 28) >>> 0;
116-
bits.hi = (bits.hi | (octet & 127) >> 4) >>> 0;
117-
if (octet < 128)
114+
// 5th
115+
bits.lo = (bits.lo | (this.buf[this.pos] & 127) << 28) >>> 0;
116+
bits.hi = (bits.hi | (this.buf[this.pos] & 127) >> 4) >>> 0;
117+
if (this.buf[this.pos++] < 128)
118118
return bits;
119119
} else {
120120
for (i = 0; i < 4; ++i) {
121121
if (this.pos >= this.len)
122122
throw indexOutOfRange(this);
123-
octet = this.buf[this.pos++]; // 1st..4th
124-
bits.lo = (bits.lo | (octet & 127) << i * 7) >>> 0;
125-
if (octet < 128)
123+
// 1st..4th
124+
bits.lo = (bits.lo | (this.buf[this.pos] & 127) << i * 7) >>> 0;
125+
if (this.buf[this.pos++] < 128)
126126
return bits;
127127
}
128128
if (this.pos >= this.len)
129129
throw indexOutOfRange(this);
130-
octet = this.buf[this.pos++]; // 5th
131-
bits.lo = (bits.lo | (octet & 127) << 28) >>> 0;
132-
bits.hi = (bits.hi | (octet & 127) >> 4) >>> 0;
133-
if (octet < 128)
130+
// 5th
131+
bits.lo = (bits.lo | (this.buf[this.pos] & 127) << 28) >>> 0;
132+
bits.hi = (bits.hi | (this.buf[this.pos] & 127) >> 4) >>> 0;
133+
if (this.buf[this.pos++] < 128)
134134
return bits;
135135
}
136136
if (this.len - this.pos > 4) { // fast route (hi)
137137
for (i = 0; i < 5; ++i) {
138-
octet = this.buf[this.pos++]; // 6th..10th
139-
bits.hi = (bits.hi | (octet & 127) << i * 7 + 3) >>> 0;
140-
if (octet < 128)
138+
// 6th..10th
139+
bits.hi = (bits.hi | (this.buf[this.pos] & 127) << i * 7 + 3) >>> 0;
140+
if (this.buf[this.pos++] < 128)
141141
return bits;
142142
}
143143
} else {
144144
for (i = 0; i < 5; ++i) {
145145
if (this.pos >= this.len)
146146
throw indexOutOfRange(this);
147-
octet = this.buf[this.pos++]; // 6th..10th
148-
bits.hi = (bits.hi | (octet & 127) << i * 7 + 3) >>> 0;
149-
if (octet < 128)
147+
// 6th..10th
148+
bits.hi = (bits.hi | (this.buf[this.pos] & 127) << i * 7 + 3) >>> 0;
149+
if (this.buf[this.pos++] < 128)
150150
return bits;
151151
}
152152
}

src/writer.js

+12-12
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,19 @@ var ArrayImpl = typeof Uint8Array !== "undefined" ? Uint8Array : Array;
1616
* @memberof Writer
1717
* @constructor
1818
* @param {function(*, Uint8Array, number)} fn Function to call
19-
* @param {*} val Value to write
2019
* @param {number} len Value byte length
20+
* @param {*} val Value to write
2121
* @private
2222
* @ignore
2323
*/
24-
function Op(fn, val, len) {
24+
function Op(fn, len, val) {
2525

2626
/**
2727
* Function to call.
2828
* @type {function(Uint8Array, number, *)}
2929
*/
3030
this.fn = fn;
3131

32-
/**
33-
* Value to write.
34-
* @type {*}
35-
*/
36-
this.val = val;
37-
3832
/**
3933
* Value byte length.
4034
* @type {number}
@@ -43,9 +37,15 @@ function Op(fn, val, len) {
4337

4438
/**
4539
* Next operation.
46-
* @type {?Writer.Op}
40+
* @type {Writer.Op|undefined}
41+
*/
42+
// this.next = undefined;
43+
44+
/**
45+
* Value to write.
46+
* @type {*}
4747
*/
48-
this.next = null;
48+
this.val = val; // type varies
4949
}
5050

5151
Writer.Op = Op;
@@ -160,7 +160,7 @@ var WriterPrototype = Writer.prototype;
160160
* @returns {Writer} `this`
161161
*/
162162
WriterPrototype.push = function push(fn, len, val) {
163-
this.tail = this.tail.next = new Op(fn, val, len);
163+
this.tail = this.tail.next = new Op(fn, len, val);
164164
this.len += len;
165165
return this;
166166
};
@@ -469,7 +469,7 @@ WriterPrototype.ldelim = function ldelim(id) {
469469
tail = this.tail,
470470
len = this.len;
471471
this.reset();
472-
if (id)
472+
if (typeof id === 'number')
473473
this.uint32((id << 3 | 2) >>> 0);
474474
this.uint32(len);
475475
this.tail.next = head.next; // skip noop

0 commit comments

Comments
 (0)