Commit
… when possible (closes #5471) My benchmarks showed a performance improvement of 20% when matching routes that make use of possesive quantifiers because it prevents backtracking when it's not needed
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -79,6 +79,14 @@ public function compile(Route $route) | |
// part of {_format} when generating the URL, e.g. _format = 'mobile.html'. | ||
$nextSeparator = $this->findNextSeparator($followingPattern); | ||
$regexp = sprintf('[^/%s]+', '/' !== $nextSeparator && '' !== $nextSeparator ? preg_quote($nextSeparator, self::REGEX_DELIMITER) : ''); | ||
if (('' !== $nextSeparator && !preg_match('#^\{\w+\}#', $followingPattern)) || '' === $followingPattern) { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
Tobion
Author
Member
|
||
// When we have a separator, which is disallowed for the variable, we can optimize the regex with a possessive | ||
// quantifier. This prevents useless backtracking of PCRE and improves performance by 20% for matching those patterns. | ||
// Given the above example, there is no point in backtracking into {page} (that forbids the dot) when a dot must follow | ||
// after it. This optimization cannot be applied when the next char is no real separator or when the next variable is | ||
// directly adjacent, e.g. '/{x}{y}'. | ||
$regexp .= '+'; | ||
} | ||
} | ||
|
||
$tokens[] = array('variable', $isSeparator ? $precedingChar : '', $regexp, $varName); | ||
|
dont know how hot this path is, but maybe doing the comparison the other way arround, would trigger the most expensive operation, the preg_match(), just when really required...
I also stripped one pair of parentesis, which were obsolete