From cfce823095fccc5843a78e017a5e9947399f3e0d Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Sun, 1 Apr 2012 21:11:45 -0500 Subject: [PATCH 1/2] improved view file loading performance. --- changes.md | 1 + laravel/view.php | 40 ++++++++++++++++++++++++++++++++-------- 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/changes.md b/changes.md index 5a4ae496..cdade5bc 100644 --- a/changes.md +++ b/changes.md @@ -23,6 +23,7 @@ ## Laravel 3.2 - Added "to_array" method to the base Eloquent model. - Added "$hidden" static variable to the base Eloquent model. - Added "sync" method to has_many_and_belongs_to Eloquent relationship. +- Improved View performance by only loading contents from file once. ## Upgrading From 3.1 diff --git a/laravel/view.php b/laravel/view.php index 76d8d40d..ab0331ec 100644 --- a/laravel/view.php +++ b/laravel/view.php @@ -37,6 +37,13 @@ class View implements ArrayAccess { */ public static $names = array(); + /** + * The cache content of loaded view files. + * + * @var array + */ + public static $cache = array(); + /** * The Laravel view loader event name. * @@ -286,12 +293,7 @@ public static function render_each($view, array $data, $iterator, $empty = 'raw| */ public function render() { - // To allow bundles or other pieces of the application to modify the - // view before it is rendered, we'll fire an event, passing in the - // view instance so it can modified. - $composer = "laravel.composing: {$this->view}"; - - Event::fire($composer, array($this)); + Event::fire("laravel.composing: {$this->view}", array($this)); // If there are listeners to the view engine event, we'll pass them // the view so they can render it according to their needs, which @@ -315,6 +317,11 @@ public function get() { $__data = $this->data(); + // The contents of each view file is cached in an array for the + // request since partial views may be rendered inside of for + // loops which could incur performance penalties. + $__content = $this->load(); + ob_start() and extract($__data, EXTR_SKIP); // We'll include the view contents for parsing within a catcher @@ -322,12 +329,12 @@ public function get() // will throw it out to the exception handler. try { - include $this->path; + eval('?>'.$__contents); } // If we caught an exception, we'll silently flush the output // buffer so that no partially rendered views get thrown out - // to the client and confuse the user. + // to the client and confuse the user with junk. catch (\Exception $e) { ob_get_clean(); throw $e; @@ -336,6 +343,23 @@ public function get() return ob_get_clean(); } + /** + * Get the contents of the view file from disk. + * + * @return string + */ + protected function load() + { + if (isset(static::$cache[$this->path])) + { + return static::$cache[$this->path]; + } + else + { + return static::$cache[$this->path] = include $this->path; + } + } + /** * Get the array of view data for the view instance. * From d41b1db722c9e6fa8b0453b8eace13c94d2b2a80 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Sun, 1 Apr 2012 21:13:07 -0500 Subject: [PATCH 2/2] fixing bug in view class. --- laravel/view.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/laravel/view.php b/laravel/view.php index ab0331ec..4219c56d 100644 --- a/laravel/view.php +++ b/laravel/view.php @@ -320,7 +320,7 @@ public function get() // The contents of each view file is cached in an array for the // request since partial views may be rendered inside of for // loops which could incur performance penalties. - $__content = $this->load(); + $__contents = $this->load(); ob_start() and extract($__data, EXTR_SKIP);