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
Provenance
- Commit
4b4de29- Tree
6567c5e- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
99b49d2
2 files changed+45 added−51 removed
modified:lib/view/view.js+6 added-18 removed
15 unmodified lines16 , fs = require('fs')17 , stat = fs.statSync;18 19 /**20- * Memory cache.20+ * Require cache.21 */22 23 var cache = {};24 25 /**26- * Existance cache.27- */28-29-var exists = {};30-31-/**26 * Initialize a new `View` with the given `view` path and `options`.27 *28 * @param {String} view29 * @param {Object} options1 unmodified line31 */32 33 var 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 lines52 * @api public53 */54 55 View.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 });63 64 /**135 unmodified linesmodified:lib/view.js+39 added-33 removed
239 unmodified lines240 , 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');245 246 // merge "view options"247 if (viewOptions) merge(options, viewOptions);248 34 unmodified lines283 284 // charset option285 if (options.charset) this.charset = options.charset;286 287- // Populate view288- var orig = view = new View(view, options);287 290- // 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);295-296- // 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);299-300- // Try root ex: <root>/user.jade301- if (!view.exists) view = new View(orig.rootPath, options);302-303- // Try root _ prefix ex: <root>/_user.jade304- if (!view.exists && partial) view = new View(view.prefixPath, options);305-306- // 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- }313-288 // Dynamic helper support289 if (false !== options.dynamicHelpers) {290 // cache291 if (!this.__dynamicHelpers) {20 unmodified lines312 313 // Provide filename to engine314 options.filename = view.path;315 342- // 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);322 323+ // 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);328+329+ // 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);332+333+ // Try root ex: <root>/user.jade334+ if (!view.exists) view = new View(orig.rootPath, options);335+336+ // Try root _ prefix ex: <root>/_user.jade337+ if (!view.exists && partial) view = new View(view.prefixPath, options);338+339+ // 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+ } 346+347+ var engine = view.templateEngine;348+ view.fn = engine.compile(view.contents, options)349+ if (cacheViews) cache[orig.view] = view;350+ }351+352+ // render353+ var str = view.fn.call(options.scope, options);354+355 // layout expected356 if (layout) {357 options.isLayout = true;358 options.layout = false;32 unmodified lines