Optimize buffer generate first and all edge cases

This commit is contained in:
kpdecker
2014-01-19 18:54:13 -06:00
parent d6e86af41a
commit 7a7ad74ff1
3 changed files with 43 additions and 29 deletions
+39 -26
View File
@@ -140,42 +140,27 @@ JavaScriptCompiler.prototype = {
},
preamble: function() {
var out = [];
if (!this.environment.isSimple) {
out.push(", buffer = " + this.initializeBuffer());
} else {
out.push("");
}
// track the last context pushed into place to allow skipping the
// getContext opcode when it would be a noop
this.lastContext = 0;
this.source = out;
this.source = [];
},
createFunctionContext: function(asObject) {
var locals = this.stackVars.concat(this.registers.list);
var varDeclarations = '';
var locals = this.stackVars.concat(this.registers.list);
if(locals.length > 0) {
this.source[0] += ", " + locals.join(", ");
varDeclarations += ", " + locals.join(", ");
}
// Generate minimizer alias mappings
for (var alias in this.aliases) {
if (this.aliases.hasOwnProperty(alias)) {
this.source[0] += ', ' + alias + '=' + this.aliases[alias];
varDeclarations += ', ' + alias + '=' + this.aliases[alias];
}
}
if (this.source[0]) {
this.source[0] = "var " + this.source[0].substring(2) + ";";
}
if (!this.environment.isSimple) {
this.pushSource("return buffer;");
}
var params = ["depth0", "helpers", "partials", "data"];
for(var i=0, l=this.environment.depths.list.length; i<l; i++) {
@@ -183,7 +168,7 @@ JavaScriptCompiler.prototype = {
}
// Perform a second pass over the output to merge content when possible
var source = this.mergeSource();
var source = this.mergeSource(varDeclarations);
if (asObject) {
params.push(source);
@@ -193,11 +178,12 @@ JavaScriptCompiler.prototype = {
return 'function(' + params.join(',') + ') {\n ' + source + '}';
}
},
mergeSource: function() {
// WARN: We are not handling the case where buffer is still populated as the source should
// not have buffer append operations as their final action.
mergeSource: function(varDeclarations) {
var source = '',
buffer;
buffer,
appendOnly = !this.forceBuffer,
appendFirst;
for (var i = 0, len = this.source.length; i < len; i++) {
var line = this.source[i];
if (line.appendToBuffer) {
@@ -208,12 +194,39 @@ JavaScriptCompiler.prototype = {
}
} else {
if (buffer) {
source += 'buffer += ' + buffer + ';\n ';
if (!source) {
appendFirst = true;
source = buffer + ';\n ';
} else {
source += 'buffer += ' + buffer + ';\n ';
}
buffer = undefined;
}
source += line + '\n ';
if (!this.environment.isSimple) {
appendOnly = false;
}
}
}
if (appendOnly) {
if (buffer || !source) {
source += 'return ' + (buffer || '""') + ';\n';
}
} else {
varDeclarations += ", buffer = " + (appendFirst ? '' : this.initializeBuffer());
if (buffer) {
source += 'return buffer + ' + buffer + ';\n';
} else {
source += 'return buffer;\n';
}
}
if (varDeclarations) {
source = 'var ' + varDeclarations.substring(2) + (appendFirst ? '' : ';\n ') + source;
}
return source;
},
+2 -3
View File
@@ -1,7 +1,6 @@
define(['handlebars.runtime'], function(Handlebars) {
Handlebars = Handlebars["default"]; var template = Handlebars.template, templates = Handlebars.templates = Handlebars.templates || {};
return templates['empty'] = template({"compiler":[4,">= 1.0.0"],"main":function(depth0,helpers,partials,data) {
var buffer = "";
return buffer;
},"useData":true});
return "";
},"useData":true});
});
+2
View File
@@ -45,10 +45,12 @@ describe('javascript-compiler api', function() {
describe('buffer', function() {
var $superAppend, $superCreate;
beforeEach(function() {
handlebarsEnv.JavaScriptCompiler.prototype.forceBuffer = true;
$superAppend = handlebarsEnv.JavaScriptCompiler.prototype.appendToBuffer;
$superCreate = handlebarsEnv.JavaScriptCompiler.prototype.initializeBuffer;
});
afterEach(function() {
handlebarsEnv.JavaScriptCompiler.prototype.forceBuffer = false;
handlebarsEnv.JavaScriptCompiler.prototype.appendToBuffer = $superAppend;
handlebarsEnv.JavaScriptCompiler.prototype.initializeBuffer = $superCreate;
});