[tools] Manually write JSON in profile.js to avoid huge strings

In the tick processor, in cases where there are a lot of ticks (e.g.
long running programs), JSON.stringify could throw a range exception
because the created string is too large.

Instead of creating the entire JSON string in memory, we now write the
top-level parts of the JSON manually, writing out the ticks individually
instead of all together.

Review-Url: https://codereview.chromium.org/2754683002
Cr-Commit-Position: refs/heads/master@{#43973}
This commit is contained in:
leszeks 2017-03-21 03:58:24 -07:00 committed by Commit bot
parent c0c55250f2
commit 0956e58792

View File

@ -969,10 +969,20 @@ JsonProfile.prototype.recordTick = function(time_ns, vmState, stack) {
}; };
JsonProfile.prototype.writeJson = function() { JsonProfile.prototype.writeJson = function() {
var toplevel = { // Write out the JSON in a partially manual way to avoid creating too-large
code : this.codeEntries_, // strings in one JSON.stringify call when there are a lot of ticks.
functions : this.functionEntries_, write('{\n')
ticks : this.ticks_ write(' "code": ' + JSON.stringify(this.codeEntries_) + ',\n');
}; write(' "functions": ' + JSON.stringify(this.functionEntries_) + ',\n');
write(JSON.stringify(toplevel)); write(' "ticks": [\n');
for (var i = 0; i < this.ticks_.length; i++) {
write(' ' + JSON.stringify(this.ticks_[i]));
if (i < this.ticks_.length - 1) {
write(',\n');
} else {
write('\n');
}
}
write(' ]\n');
write('}\n');
}; };