First of all I'm not asking about alternative ways of how to achieve the same result for example code proposed. I know there is better ways to do that.
The example code provided below is only for forcing Laravel to read multiple times the same view file (resources/views/hello.blade.php)
The example code:
Route::get('/testperformance3', function () {
$faker = Faker\Factory::create();
$ouput = "";
for ($i = 1; $i <= 5000; $i++) {
$test = View::make('hello',["name" => $faker->firstName])->render();
$ouput = $ouput . $test;
}
echo $ouput;
});
Where hello view is:
<div> Hello {{ $name }}</div>
The performance of this code is not very good because Laravel is executing multiple times and include operation for evaluating blade compiled view as you can see on evaluatePath method in PhpEngine.php file (https://github.com/illuminate/view/blob/master/Engines/PhpEngine.php)
protected function evaluatePath($__path, $__data)
{
$obLevel = ob_get_level();
ob_start();
extract($__data);
// We'll evaluate the contents of the view inside a try/catch block so we can
// flush out any stray output that might get out before an error occurs or
// an exception is thrown. This prevents any partial views from leaking.
try {
include $__path;
} catch (Exception $e) {
$this->handleViewException($e, $obLevel);
} catch (Throwable $e) {
$this->handleViewException(new FatalThrowableError($e), $obLevel);
}
return ltrim(ob_get_clean());
}
In my example case $data passed to view is changing every loop iteration (so I can't cache/save view result at first time). The expensive operation is at line:
include $__path;
But in this example the path is the same over all loop iterations.
The question: Any idea how to improve Laravel to avoid repeating the same include operation again an again maybe caching the result when path is the same.
For whow it may concern I'm trying to improve Sleeping Owl admin performance because it's a case where a lot of view operation are made with the same view file when retrieving lists of items. Tou can see more infor at discuss:
https://laracasts.com/discuss/channels/laravel/how-to-improve-performance-when-calling-same-view-multiple-times