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

Improve performance #216

wants to merge 1 commit into from

Conversation

live627
Copy link

@live627 live627 commented May 10, 2024

I have identified three main bottlenecks when profiling the code with xdebug:

  1. array_shift(): The array of tokens ended up being renumbered on each iteration. So we instead set consumed tokens to null.
  2. mb_substr(): Using mutltibyte here is unnecessary and overkill, when we can just operate directly on the string.
  3. Liquid::get(): This one surprised me, but due to the number of calls, here it is. Shave off more processing time by accessing the config array directly.

I have identified three main bottlenecks when profiling the code with xdebug:

1. `array_shift()`: The array of tokens ended up being renumbered on each iteration. So we instead set consumed tokens to `null`.
2. `mb_substr()`: Using mutltibyte here is unnecessary and overkill, when we can just operate directly on the string.
3. `Liquid::get()`:  This one surprised me, but due to the number of calls, here it is. Shave off more processing time by accessing the config array directly.
@@ -254,7 +258,7 @@ private function blockName()
*/
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.

@@ -129,7 +133,7 @@ protected function whitespaceHandler($token)
* 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 🙂

Copy link
Collaborator

@sanmai sanmai left a comment

Choose a reason for hiding this comment

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

I'd like to merge this excellent PR and to get there, please switch to $this->config in this PR. I appreciate that!

$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'] . '/');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants