phug
phug copied to clipboard
The Optimizer is slow in loop
Hi,
When executed in a loop (actually when using phug inside an event-based framework like workerman), the performance of the optimizer is slow (6ms when compared to Fenom 0.1ms).
This can be improved by caching the 2nd parameter to isExpired at https://github.com/phug-php/phug/blob/master/src/Phug/Phug/Phug/Optimizer.php#L130.
In my test, caching the 2nd parameter of isExpired reduced the execution time from 6ms to 0.6ms. Still slower than Fenom's 0.1ms.
The remaining difference of 0.5ms I assume is because Fenom compiles template to an anomyous function + echo compared to pug which uses extract and include.
Hello 👋
Is up_to_date_check set to false? (It should in production).
If it is then the problem is likely the hasExpiredImport chack and I guess it should be skipped to when up_to_date_check === false.
Looking at the code it seems that getSourceAndCachePaths is still called whether up_to_date_check is set to true or false. In my profiling data, getSourceAndCachePaths takes about 4.6ms.
This is the options I used in my profiling
$options = [
'paths' => [__DIR__],
'php_token_handlers' => [T_VARIABLE => null],
'cache_dir' => __DIR__.'\\phug',
'up_to_date_check' => false,
'enable_profiler' => false,
];