aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlon Zakai <azakai@mozilla.com>2011-03-12 13:37:43 -0800
committerAlon Zakai <azakai@mozilla.com>2011-03-12 13:37:43 -0800
commit490af7780df1e986999faf1be3c44cb79082af7a (patch)
tree193aa59b761d13f69f7a55b088672b1a1a8cb8ef
parent443107fc61b83caa49fbfc57a6b2b5adf09581e2 (diff)
optimize memory usage in jsifier
-rw-r--r--src/jsifier.js40
-rw-r--r--src/modules.js6
-rw-r--r--src/parseTools.js2
3 files changed, 34 insertions, 14 deletions
diff --git a/src/jsifier.js b/src/jsifier.js
index b1b8797b..c230d22d 100644
--- a/src/jsifier.js
+++ b/src/jsifier.js
@@ -36,6 +36,12 @@ function JSify(data, functionsOnly, givenFunctions, givenGlobalVariables) {
delete func.lines; // clean up memory as much as possible
}
+ if (data.unparsedFunctions.length > 0) {
+ // We are now doing the final JS generation
+ dprint('unparsedFunctions', '== Completed unparsedFunctions ==\n');
+ Debugging.clear(); // Save some memory, before the final heavy lifting
+ }
+
// Load library
// TODO: optimize this so it isn't done over and over for each unparsedFunction
@@ -268,6 +274,7 @@ function JSify(data, functionsOnly, givenFunctions, givenGlobalVariables) {
substrate.addActor('GlobalVariable', {
processItem: function(item) {
item.intertype = 'GlobalVariableStub';
+ delete item.lines; // Save some memory
var ret = [item];
if (item.ident == '_llvm_global_ctors') {
item.JS = '\n__globalConstructor__ = function() {\n' +
@@ -1119,28 +1126,35 @@ function JSify(data, functionsOnly, givenFunctions, givenGlobalVariables) {
// Final combiner
function finalCombiner(items) {
+ dprint('unparsedFunctions', 'Starting finalCombiner');
+ var itemsDict = { type: [], GlobalVariableStub: [], functionStub: [], function: [], GlobalVariable: [], GlobalVariablePostSet: [] };
+ items.forEach(function(item) {
+ item.lines = null;
+ var small = { intertype: item.intertype, JS: item.JS }; // Release memory
+ itemsDict[small.intertype].push(small);
+ });
+ items = null;
+
var ret = [];
if (!functionsOnly) {
- ret = ret.concat(items.filter(function(item) { return item.intertype == 'type' }));
- ret = ret.concat(items.filter(function(item) { return item.intertype == 'GlobalVariableStub' }));
- ret.push('\n');
- ret = ret.concat(items.filter(function(item) { return item.intertype == 'functionStub' }));
- ret.push('\n');
+ ret = ret.concat(itemsDict.type).concat(itemsDict.GlobalVariableStub).concat(itemsDict.functionStub);
}
- ret = ret.concat(items.filter(function(item) { return item.intertype == 'function' }));
- ret = ret.concat(data.unparsedFunctions);
+ ret = ret.concat(itemsDict.function).concat(data.unparsedFunctions);
ret = ret.map(function(item) { return item.JS }).join('\n');
if (functionsOnly) return ret;
var body = preprocess(read('preamble.js').replace('{{RUNTIME}}', getRuntime()) + ret + read('postamble.js'), CONSTANTS);
- var globalVars = items.filter(function(item) { return item.intertype == 'GlobalVariable' }).map(function(item) { return item.JS }).join('\n');
- var globalVarsPostSets = items.filter(function(item) { return item.intertype == 'GlobalVariablePostSet' }).map(function(item) { return item.JS }).join('\n');
- return processMacros(
- read('shell.js').replace('{{BODY}}', indentify(body, 2))
- .replace('{{GLOBAL_VARS}}', indentify(globalVars+'\n\n\n'+globalVarsPostSets, 4))
- );
+ var globalVars = itemsDict.GlobalVariable.map(function(item) { return item.JS }).join('\n');
+ var globalVarsPostSets = itemsDict.GlobalVariablePostSet.map(function(item) { return item.JS }).join('\n');
+ body = indentify(body, 2);
+ // body may be a very large string at this point - we may not be able to allocate two of it. So must be careful in these last steps
+ var shellParts = read('shell.js').split('{{BODY}}');
+ body = shellParts[0] + body + shellParts[1];
+ globalVars = indentify(globalVars+'\n\n\n'+globalVarsPostSets, 4);
+ body = body.replace('{{GLOBAL_VARS}}', globalVars);
+ return processMacros(body);
}
// Data
diff --git a/src/modules.js b/src/modules.js
index 235683f8..31ede3a3 100644
--- a/src/modules.js
+++ b/src/modules.js
@@ -85,6 +85,12 @@ var Debugging = {
return ret;
},
+ clear: function() {
+ this.llvmLineToSourceLine = {};
+ this.llvmLineToSourceFile = {};
+ this.on = false;
+ },
+
getComment: function(lineNum) {
if (!this.on) return null;
return lineNum in this.llvmLineToSourceLine ? ' //@line ' + this.llvmLineToSourceLine[lineNum] + ' "' +
diff --git a/src/parseTools.js b/src/parseTools.js
index be3a6b4c..92041f21 100644
--- a/src/parseTools.js
+++ b/src/parseTools.js
@@ -550,7 +550,7 @@ function indentify(text, indent) {
indent = '';
for (var i = 0; i < len; i++) indent += ' ';
}
- return text.split('\n').map(function(line) { return indent + line }).join('\n');
+ return text.replace(/\n/g, '\n' + indent);
}
// Correction tools