Skip to content

Commit

Permalink
Optimize buffer generate first and all edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
kpdecker committed Feb 9, 2014
1 parent d6e86af commit 7a7ad74
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 29 deletions.
65 changes: 39 additions & 26 deletions lib/handlebars/compiler/javascript-compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,50 +140,35 @@ 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++) {
params.push("depth" + this.environment.depths.list[i]);
}

// 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);
Expand All @@ -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) {
Expand All @@ -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;
},

Expand Down
5 changes: 2 additions & 3 deletions spec/expected/empty.amd.js
Original file line number Diff line number Diff line change
@@ -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 changes: 2 additions & 0 deletions spec/javascript-compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
Expand Down

0 comments on commit 7a7ad74

Please sign in to comment.