Skip to content
Myr

Performance improved with better view caching

Before adoption: this commit came in with the history the project adopted, so no landing records it.

the entire View object is now cached in-memory, along with the lookup logic as well. This increases rendering (with jade at least) by about 260 rps

authored by Tj Holowaychuk · committed by Tj Holowaychuk · parent 99b49d2 · Browse files · 2 files+45 added−51 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+45 added−51 removed

  • other:+45 added−51 removed

2 files+45 added−51 removed

modified:lib/view/view.js+6 added-18 removed
15 unmodified lines
16 , fs = require('fs')17 , stat = fs.statSync;1819/**20 * Memory cache.20 * Require cache.21 */2223var cache = {};2425/**26 * Existance cache.27 */2829var exists = {};3031/**26 * Initialize a new `View` with the given `view` path and `options`.27 *28 * @param {String} view29 * @param {Object} options
1 unmodified line
31 */3233var View = exports = module.exports = function View(view, options) {34 options = options || {};41 // TODO: more caching35 this.view = view;36 this.root = options.root;37 this.relative = false !== options.relative;38 this.defaultEngine = options.defaultEngine;
13 unmodified lines
52 * @api public53 */5455View.prototype.__defineGetter__('exists', function(){63 var path = this.path;64 if (null != exists[path]) {65 return exists[path];66 } else {67 try {68 stat(path);69 return exists[path] = true;70 } catch (err) {71 return exists[path] = false;56 try {57 stat(this.path);58 return true;59 } catch (err) {60 return false;72 }61 }62});6364/**
135 unmodified lines
modified:lib/view.js+39 added-33 removed
239 unmodified lines
240 , app = this.app241 , helpers = app.viewHelpers242 , dynamicHelpers = app.dynamicViewHelpers243 , viewOptions = app.set('view options')244 , cacheTemplates = app.set('cache views');244 , cacheViews = app.set('cache views');245246 // merge "view options"247 if (viewOptions) merge(options, viewOptions);248
34 unmodified lines
283284 // charset option285 if (options.charset) this.charset = options.charset;286287 // Populate view288 var orig = view = new View(view, options);287290 // Try _ prefix ex: ./views/_<name>.jade291 if (!view.exists) view = new View(orig.prefixPath, options);292 293 // Try index ex: ./views/user/index.jade294 if (!view.exists) view = new View(orig.indexPath, options);295296 // Try ../<name>/index ex: ../user/index.jade297 // when calling partial('user') within the same dir298 if (!view.exists && !options.isLayout) view = new View(orig.upIndexPath, options);299300 // Try root ex: <root>/user.jade301 if (!view.exists) view = new View(orig.rootPath, options);302303 // Try root _ prefix ex: <root>/_user.jade304 if (!view.exists && partial) view = new View(view.prefixPath, options);305306 // Does not exist307 if (!view.exists) {308 if (app.enabled('hints')) hintAtViewPaths(orig, options);309 var err = new Error('failed to locate view "' + orig.view + '"');310 err.view = orig;311 throw err;312 }313288 // Dynamic helper support289 if (false !== options.dynamicHelpers) {290 // cache291 if (!this.__dynamicHelpers) {
20 unmodified lines
312313 // Provide filename to engine314 options.filename = view.path;315342 // Attempt render343 var engine = view.templateEngine344 , template = cacheTemplates345 ? cache[view.path] || (cache[view.path] = engine.compile(view.contents, options))346 : engine.compile(view.contents, options)347 , str = template.call(options.scope, options);316 // cached view317 if (cache[view]) {318 view = cache[view];319 // resolve view320 } else {321 var orig = view = new View(view, options);322323 // Try _ prefix ex: ./views/_<name>.jade324 if (!view.exists) view = new View(orig.prefixPath, options);325 326 // Try index ex: ./views/user/index.jade327 if (!view.exists) view = new View(orig.indexPath, options);328329 // Try ../<name>/index ex: ../user/index.jade330 // when calling partial('user') within the same dir331 if (!view.exists && !options.isLayout) view = new View(orig.upIndexPath, options);332333 // Try root ex: <root>/user.jade334 if (!view.exists) view = new View(orig.rootPath, options);335336 // Try root _ prefix ex: <root>/_user.jade337 if (!view.exists && partial) view = new View(view.prefixPath, options);338339 // Does not exist340 if (!view.exists) {341 if (app.enabled('hints')) hintAtViewPaths(orig, options);342 var err = new Error('failed to locate view "' + orig.view + '"');343 err.view = orig;344 throw err;345 } 346347 var engine = view.templateEngine;348 view.fn = engine.compile(view.contents, options)349 if (cacheViews) cache[orig.view] = view;350 }351352 // render353 var str = view.fn.call(options.scope, options);354355 // layout expected356 if (layout) {357 options.isLayout = true;358 options.layout = false;
32 unmodified lines