minor #10312 [Yaml] Optimise Inline::evaluateScalar() for parsing strings. (alexpott)
This PR was submitted for the 2.3-dev branch but it was merged into the 2.3 branch instead (closes #10312). Discussion ---------- [Yaml] Optimise Inline::evaluateScalar() for parsing strings. | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | The Drupal 8 installer does a lot of YAML parsing. With the patch attached it is significantly quicker. ![image](https://f.cloud.github.com/assets/769634/2237906/630c3308-9bcf-11e3-8038-35cbbeedd7e6.png) https://drupal.org/node/1851234 Commits ------- 10c898d Avoid unnecessary line indentation calculation. c65a647 Optimise Inline::evaluateScalar() for parsing strings.
This commit is contained in:
commit
6b0b504348
@ -386,12 +386,19 @@ class Inline
|
||||
private static function evaluateScalar($scalar)
|
||||
{
|
||||
$scalar = trim($scalar);
|
||||
|
||||
$scalarLower = strtolower($scalar);
|
||||
switch (true) {
|
||||
case 'null' == strtolower($scalar):
|
||||
case 'null' == $scalarLower:
|
||||
case '' == $scalar:
|
||||
case '~' == $scalar:
|
||||
return null;
|
||||
case 'true' === $scalarLower:
|
||||
return true;
|
||||
case 'false' === $scalarLower:
|
||||
return false;
|
||||
// Optimise for returning strings.
|
||||
case $scalar[0] === '+' || $scalar[0] === '-' || $scalar[0] === '.' || $scalar[0] === '!' || is_numeric($scalar[0]):
|
||||
switch (true) {
|
||||
case 0 === strpos($scalar, '!str'):
|
||||
return (string) substr($scalar, 5);
|
||||
case 0 === strpos($scalar, '! '):
|
||||
@ -416,10 +423,6 @@ class Inline
|
||||
$cast = intval($scalar);
|
||||
|
||||
return '0' == $scalar[1] ? octdec($scalar) : (((string) $raw == (string) $cast) ? $cast : $raw);
|
||||
case 'true' === strtolower($scalar):
|
||||
return true;
|
||||
case 'false' === strtolower($scalar):
|
||||
return false;
|
||||
case is_numeric($scalar):
|
||||
return '0x' == $scalar[0].$scalar[1] ? hexdec($scalar) : floatval($scalar);
|
||||
case 0 == strcasecmp($scalar, '.inf'):
|
||||
@ -431,6 +434,7 @@ class Inline
|
||||
return floatval(str_replace(',', '', $scalar));
|
||||
case preg_match(self::getTimestampRegex(), $scalar):
|
||||
return strtotime($scalar);
|
||||
}
|
||||
default:
|
||||
return (string) $scalar;
|
||||
}
|
||||
|
@ -312,7 +312,9 @@ class Parser
|
||||
$removeComments = !preg_match($removeCommentsPattern, $this->currentLine);
|
||||
|
||||
while ($this->moveToNextLine()) {
|
||||
if ($this->getCurrentLineIndentation() === $newIndent) {
|
||||
$indent = $this->getCurrentLineIndentation();
|
||||
|
||||
if ($indent === $newIndent) {
|
||||
$removeComments = !preg_match($removeCommentsPattern, $this->currentLine);
|
||||
}
|
||||
|
||||
@ -329,8 +331,6 @@ class Parser
|
||||
continue;
|
||||
}
|
||||
|
||||
$indent = $this->getCurrentLineIndentation();
|
||||
|
||||
if (preg_match('#^(?P<text> *)$#', $this->currentLine, $match)) {
|
||||
// empty line
|
||||
$data[] = $match['text'];
|
||||
|
Reference in New Issue
Block a user