Skip to content

Commit 2d1eaf1

Browse files
committed
Cache parser and compiler instances
This reduces memory usage by over 30%, and when compiling multiple templates improves performance by up to 20%.
1 parent af4fa67 commit 2d1eaf1

4 files changed

Lines changed: 26 additions & 22 deletions

File tree

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@
1919
],
2020
"require": {
2121
"php": ">=8.2",
22-
"devtheorem/php-handlebars-parser": "^1.1.1"
22+
"devtheorem/php-handlebars-parser": "^2.0"
2323
},
2424
"require-dev": {
2525
"friendsofphp/php-cs-fixer": "^3.94",
2626
"jbboehr/handlebars-spec": "dev-master",
27-
"phpstan/phpstan": "^2.1.44",
27+
"phpstan/phpstan": "^2.1.46",
2828
"phpunit/phpunit": "^11.5"
2929
},
3030
"autoload": {

composer.lock

Lines changed: 12 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Compiler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,7 @@ private function compilePartialTemplate(string $name, string $template): void
725725
return;
726726
}
727727

728-
$program = $this->parser->parse($template);
728+
$program = $this->parser->parse($template, $this->context->options->ignoreStandalone);
729729
$code = (new Compiler($this->parser))->compile($program, $this->context);
730730

731731
$this->context->partialCode[$name] = self::quote($name) . ' => ' . self::templateClosure($code);

src/Handlebars.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace DevTheorem\Handlebars;
44

55
use Closure;
6+
use DevTheorem\HandlebarsParser\Parser;
67
use DevTheorem\HandlebarsParser\ParserFactory;
78

89
/**
@@ -11,6 +12,9 @@
1112
*/
1213
final class Handlebars
1314
{
15+
private static ?Parser $parser = null;
16+
private static ?Compiler $compiler = null;
17+
1418
/**
1519
* Compiles a template so it can be executed immediately.
1620
* @return Template
@@ -25,15 +29,15 @@ public static function compile(string $template, Options $options = new Options(
2529
*/
2630
public static function precompile(string $template, Options $options = new Options()): string
2731
{
32+
self::$parser ??= (new ParserFactory())->create();
33+
self::$compiler ??= new Compiler(self::$parser);
34+
2835
$context = new Context($options);
29-
$parser = (new ParserFactory())->create($options->ignoreStandalone);
30-
$program = $parser->parse($template);
31-
$compiler = new Compiler($parser);
32-
$code = $compiler->compile($program, $context);
33-
$compiler->handleDynamicPartials();
36+
$program = self::$parser->parse($template, $options->ignoreStandalone);
37+
$code = self::$compiler->compile($program, $context);
38+
self::$compiler->handleDynamicPartials();
3439

35-
// return full PHP render code as string
36-
return $compiler->composePHPRender($code);
40+
return self::$compiler->composePHPRender($code);
3741
}
3842

3943
/**

0 commit comments

Comments
 (0)