-
Notifications
You must be signed in to change notification settings - Fork 58
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
base: master
Are you sure you want to change the base?
Improve performance #216
Conversation
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'); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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']; |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 🙂
There was a problem hiding this 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'] . '/'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$variableStartRegexp = new Regexp('/^' . Liquid::$config['VARIABLE_START'] . '/'); | |
$variableStartRegexp = new Regexp('/^' . $this->config['VARIABLE_START'] . '/'); |
I have identified three main bottlenecks when profiling the code with xdebug:
array_shift()
: The array of tokens ended up being renumbered on each iteration. So we instead set consumed tokens tonull
.mb_substr()
: Using mutltibyte here is unnecessary and overkill, when we can just operate directly on the string.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.