Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

xjst: first pass over match profiling

  • Loading branch information...
commit 811bd3ab6f0ce91c1d0944a89fb4562a0cef702b 1 parent b461e65
@indutny indutny authored
View
6 bin/xjst
@@ -42,6 +42,12 @@ require('coa').Cmd()
.flag()
.end()
.opt()
+ .name('profile-match')
+ .title('Profile matching')
+ .long('profile-match')
+ .flag()
+ .end()
+ .opt()
.name('uglify')
.title('Minimize js output using uglifyjs')
.short('u').long('uglify')
View
4 lib/xjst/compiler.js
@@ -180,6 +180,10 @@ exports.generate = function generate(ast, options) {
];
}
+ if (options['profile-match']) {
+ body.unshift(utils.profileMatch.toString() + ';\n');
+ }
+
if (options['export-graph']) {
xjst.exporter.write(tree, options['export-graph']);
}
View
17 lib/xjst/helpers/serializer.js
@@ -48,6 +48,18 @@ Serializer.prototype.serialize = function serialize(node, tails, _parents) {
if (!tails) tails = {};
+ function match(lhs, rhs, tag) {
+ var res = lhs + ' === ' + rhs;
+
+ if (options['profile-match']) {
+ res = 'profileMatch(' + res + ', ' +
+ JSON.stringify(tag) + ',' +
+ JSON.stringify(rhs) + ')';
+ }
+
+ return res;
+ }
+
// Returns a stringified path from root to current node
function getParents() {
return utils.stringify(_parents.map(function(parent) {
@@ -127,8 +139,9 @@ Serializer.prototype.serialize = function serialize(node, tails, _parents) {
// Generate a simple if/else statement if it has only one case
} else if (cases.length === 1) {
var c = cases[0];
+
res.push(
- 'if (', tag, ' === ', c[0], ') {\n',
+ 'if (', match(tag, c[0], tag), ') {\n',
c[1],
'} else {\n',
def,
@@ -142,7 +155,7 @@ Serializer.prototype.serialize = function serialize(node, tails, _parents) {
var grouped = [];
cases.map(function(branch) {
- return [['__t === ' + branch[0]], branch[1], branch[2]];
+ return [[match('__t', branch[0], tag)], branch[1], branch[2]];
}).reduce(function(prev, curr) {
if (prev !== null && prev[2] === curr[2]) {
prev[0] = prev[0].concat(curr[0]);
View
61 lib/xjst/utils.js
@@ -309,3 +309,64 @@ utils.errorHandler = function errorHandler(source) {
throw m;
};
};
+
+//
+// ### function profileMatch (match, tag, expr)
+// #### @match {Boolean} true if expr matched
+// #### @lhs {String} stringified tag
+// #### @rhs {String} stringified rhs
+//
+utils.profileMatch = function profileMatch(match, lhs, rhs) {
+ if (!profileMatch.hash) {
+ profileMatch.hash = {
+ total: {
+ lhs: {}, pair: {}
+ },
+ hit: {
+ lhs: {}, pair: {}
+ },
+ miss: {
+ lhs: {}, pair: {}
+ }
+ };
+
+ process.once('exit', function() {
+ var lists = {
+ total: { lhs: null, pair: null },
+ hit: { lhs: null, pair: null },
+ miss: { lhs: null, pair: null }
+ };
+
+ Object.keys(lists).forEach(function(type) {
+ Object.keys(lists[type]).forEach(function(subtype) {
+ var hash = profileMatch.hash[type][subtype];
+
+
+ lists[type][subtype] = Object.keys(hash).map(function(key) {
+ return [key, hash[key]];
+ }).sort(function(a, b) {
+ return b[1] - a[1];
+ }).map(function(item) {
+ return item[0] + ' [' + item[1] + ']';
+ });
+ });
+ });
+
+ console.log(lists);
+ });
+ }
+
+ function account(hash) {
+ hash.lhs[lhs] = (hash.lhs[lhs] | 0) + 1;
+ hash.pair[lhs + ' === ' + rhs] = (hash.pair[lhs + ' === ' + rhs] | 0) + 1;
+ }
+
+ account(profileMatch.hash.total);
+ if (match) {
+ account(profileMatch.hash.hit);
+ } else {
+ account(profileMatch.hash.miss);
+ }
+
+ return match;
+};
Please sign in to comment.
Something went wrong with that request. Please try again.