Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve performance #216

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions src/Liquid/AbstractBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,20 @@
*/
public function parse(array &$tokens)
{
$startRegexp = new Regexp('/^' . Liquid::get('TAG_START') . '/');
$tagRegexp = new Regexp('/^' . Liquid::get('TAG_START') . Liquid::get('WHITESPACE_CONTROL') . '?\s*(\w+)\s*(.*?)' . Liquid::get('WHITESPACE_CONTROL') . '?' . Liquid::get('TAG_END') . '$/s');
$variableStartRegexp = new Regexp('/^' . Liquid::get('VARIABLE_START') . '/');
$startRegexp = new Regexp('/^' . Liquid::$config['TAG_START'] . '/');
$tagRegexp = new Regexp('/^' . Liquid::$config['TAG_START'] . Liquid::$config['WHITESPACE_CONTROL'] . '?\s*(\w+)\s*(.*?)' . Liquid::$config['WHITESPACE_CONTROL'] . '?' . Liquid::$config['TAG_END'] . '$/s');
$variableStartRegexp = new Regexp('/^' . Liquid::$config['VARIABLE_START'] . '/');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$variableStartRegexp = new Regexp('/^' . Liquid::$config['VARIABLE_START'] . '/');
$variableStartRegexp = new Regexp('/^' . $this->config['VARIABLE_START'] . '/');


$this->nodelist = array();

$tags = Template::getTags();

while (count($tokens)) {
$token = array_shift($tokens);
for ($i = 0, $n = count($tokens); $i < $n; $i++) {
if ($tokens[$i] === null) {
continue;
}
$token = $tokens[$i];
$tokens[$i] = null;

if ($startRegexp->match($token)) {
$this->whitespaceHandler($token);
Expand Down Expand Up @@ -112,13 +116,13 @@
*
* @param string $token
*/
protected function whitespaceHandler($token)

Check warning on line 119 in src/Liquid/AbstractBlock.php

View workflow job for this annotation

GitHub Actions / PHP 8.2

Escaped Mutant for Mutator "ProtectedVisibility": --- Original +++ New @@ @@ * * @param string $token */ - protected function whitespaceHandler($token) + private function whitespaceHandler($token) { /* * This assumes that TAG_START is always '{%', and a whitespace control indicator
{
/*
* This assumes that TAG_START is always '{%', and a whitespace control indicator
* is exactly one character long, on a third position.
*/
if (mb_substr($token, 2, 1) === Liquid::get('WHITESPACE_CONTROL')) {
if ($token[2] === Liquid::$config['WHITESPACE_CONTROL']) {
$previousToken = end($this->nodelist);
if (is_string($previousToken)) { // this can also be a tag or a variable
$this->nodelist[key($this->nodelist)] = rtrim($previousToken);
Expand All @@ -129,7 +133,7 @@
* This assumes that TAG_END is always '%}', and a whitespace control indicator
* is exactly one character long, on a third position from the end.
*/
self::$trimWhitespace = mb_substr($token, -3, 1) === Liquid::get('WHITESPACE_CONTROL');
self::$trimWhitespace = $token[-3] === Liquid::$config['WHITESPACE_CONTROL'];
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this requires bumping min php to 7.1 because of negative string offset https://wiki.php.net/rfc/negative-string-offsets

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already require 7.4 min, so that's non-issue 🙂

}

/**
Expand Down Expand Up @@ -157,7 +161,7 @@
$result = '';

foreach ($list as $token) {
if (is_object($token) && method_exists($token, 'render')) {

Check warning on line 164 in src/Liquid/AbstractBlock.php

View workflow job for this annotation

GitHub Actions / PHP 8.2

Escaped Mutant for Mutator "LogicalAnd": --- Original +++ New @@ @@ { $result = ''; foreach ($list as $token) { - if (is_object($token) && method_exists($token, 'render')) { + if (is_object($token) || method_exists($token, 'render')) { $value = $token->render($context); } else { $value = $token;
$value = $token->render($context);
} else {
$value = $token;
Expand Down Expand Up @@ -185,7 +189,7 @@
/**
* An action to execute when the end tag is reached
*/
protected function endTag()

Check warning on line 192 in src/Liquid/AbstractBlock.php

View workflow job for this annotation

GitHub Actions / PHP 8.2

Escaped Mutant for Mutator "ProtectedVisibility": --- Original +++ New @@ @@ /** * An action to execute when the end tag is reached */ - protected function endTag() + private function endTag() { // Do nothing by default }
{
// Do nothing by default
}
Expand All @@ -203,9 +207,9 @@
{
switch ($tag) {
case 'else':
throw new ParseException($this->blockName() . " does not expect else tag");

Check warning on line 210 in src/Liquid/AbstractBlock.php

View workflow job for this annotation

GitHub Actions / PHP 8.2

Escaped Mutant for Mutator "Concat": --- Original +++ New @@ @@ { switch ($tag) { case 'else': - throw new ParseException($this->blockName() . " does not expect else tag"); + throw new ParseException(" does not expect else tag" . $this->blockName()); case 'end': throw new ParseException("'end' is not a valid delimiter for " . $this->blockName() . " tags. Use " . $this->blockDelimiter()); default:

Check warning on line 210 in src/Liquid/AbstractBlock.php

View workflow job for this annotation

GitHub Actions / PHP 8.2

Escaped Mutant for Mutator "ConcatOperandRemoval": --- Original +++ New @@ @@ { switch ($tag) { case 'else': - throw new ParseException($this->blockName() . " does not expect else tag"); + throw new ParseException(" does not expect else tag"); case 'end': throw new ParseException("'end' is not a valid delimiter for " . $this->blockName() . " tags. Use " . $this->blockDelimiter()); default:
case 'end':
throw new ParseException("'end' is not a valid delimiter for " . $this->blockName() . " tags. Use " . $this->blockDelimiter());

Check warning on line 212 in src/Liquid/AbstractBlock.php

View workflow job for this annotation

GitHub Actions / PHP 8.2

Escaped Mutant for Mutator "Concat": --- Original +++ New @@ @@ case 'else': throw new ParseException($this->blockName() . " does not expect else tag"); case 'end': - throw new ParseException("'end' is not a valid delimiter for " . $this->blockName() . " tags. Use " . $this->blockDelimiter()); + throw new ParseException($this->blockName() . "'end' is not a valid delimiter for " . " tags. Use " . $this->blockDelimiter()); default: throw new ParseException("Unknown tag {$tag}"); }

Check warning on line 212 in src/Liquid/AbstractBlock.php

View workflow job for this annotation

GitHub Actions / PHP 8.2

Escaped Mutant for Mutator "ConcatOperandRemoval": --- Original +++ New @@ @@ case 'else': throw new ParseException($this->blockName() . " does not expect else tag"); case 'end': - throw new ParseException("'end' is not a valid delimiter for " . $this->blockName() . " tags. Use " . $this->blockDelimiter()); + throw new ParseException($this->blockName() . " tags. Use " . $this->blockDelimiter()); default: throw new ParseException("Unknown tag {$tag}"); }

Check warning on line 212 in src/Liquid/AbstractBlock.php

View workflow job for this annotation

GitHub Actions / PHP 8.2

Escaped Mutant for Mutator "ConcatOperandRemoval": --- Original +++ New @@ @@ case 'else': throw new ParseException($this->blockName() . " does not expect else tag"); case 'end': - throw new ParseException("'end' is not a valid delimiter for " . $this->blockName() . " tags. Use " . $this->blockDelimiter()); + throw new ParseException("'end' is not a valid delimiter for " . " tags. Use " . $this->blockDelimiter()); default: throw new ParseException("Unknown tag {$tag}"); }

Check warning on line 212 in src/Liquid/AbstractBlock.php

View workflow job for this annotation

GitHub Actions / PHP 8.2

Escaped Mutant for Mutator "Concat": --- Original +++ New @@ @@ case 'else': throw new ParseException($this->blockName() . " does not expect else tag"); case 'end': - throw new ParseException("'end' is not a valid delimiter for " . $this->blockName() . " tags. Use " . $this->blockDelimiter()); + throw new ParseException("'end' is not a valid delimiter for " . " tags. Use " . $this->blockName() . $this->blockDelimiter()); default: throw new ParseException("Unknown tag {$tag}"); }

Check warning on line 212 in src/Liquid/AbstractBlock.php

View workflow job for this annotation

GitHub Actions / PHP 8.2

Escaped Mutant for Mutator "ConcatOperandRemoval": --- Original +++ New @@ @@ case 'else': throw new ParseException($this->blockName() . " does not expect else tag"); case 'end': - throw new ParseException("'end' is not a valid delimiter for " . $this->blockName() . " tags. Use " . $this->blockDelimiter()); + throw new ParseException("'end' is not a valid delimiter for " . $this->blockName() . $this->blockDelimiter()); default: throw new ParseException("Unknown tag {$tag}"); }
default:
throw new ParseException("Unknown tag $tag");
}
Expand Down Expand Up @@ -254,7 +258,7 @@
*/
private function createVariable($token)
{
$variableRegexp = new Regexp('/^' . Liquid::get('VARIABLE_START') . Liquid::get('WHITESPACE_CONTROL') . '?(.*?)' . Liquid::get('WHITESPACE_CONTROL') . '?' . Liquid::get('VARIABLE_END') . '$/s');
$variableRegexp = new Regexp('/^' . Liquid::$config['VARIABLE_START'] . Liquid::$config['WHITESPACE_CONTROL'] . '?(.*?)' . Liquid::$config['WHITESPACE_CONTROL'] . '?' . Liquid::$config['VARIABLE_END'] . '$/s');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about making it an instance variable instead so we would fetch the constants only once?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I.e. $this->variableRegexp and initialize only once.

if ($variableRegexp->match($token)) {
return new Variable($variableRegexp->matches[1]);
}
Expand Down
8 changes: 6 additions & 2 deletions src/Liquid/Tag/TagRaw.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,12 @@ public function parse(array &$tokens)

$this->nodelist = array();

while (count($tokens)) {
$token = array_shift($tokens);
for ($i = 0, $n = count($tokens); $i < $n; $i++) {
if ($tokens[$i] === null) {
continue;
}
$token = $tokens[$i];
$tokens[$i] = null;

if ($tagRegexp->match($token)) {
// If we found the proper block delimiter just end parsing here and let the outer block proceed
Expand Down
Loading