Skip to content

Commit

Permalink
Style: Speed up resolving compiler-optimized functions
Browse files Browse the repository at this point in the history
The `Style` class is where some of these will be called most often.
Especially `array_key_exists` and `is_string` are in critical paths.
Optimize all calls to the following functions for consistency:

* `array_key_exists`
* `array_slice`
* `count`
* `in_array`
* `is_array`
* `is_float`
* `is_string`

Ideally, this optimization would be automated via a code-style tool,
e.g. the `native_function_invocation` rule of PHP CS Fixer.

https://tideways.com/profiler/blog/compiler-optimized-php-functions
https://cs.symfony.com/doc/rules/function_notation/native_function_invocation.html
  • Loading branch information
Mellthas committed Jun 13, 2022
1 parent ced55c7 commit d88d496
Showing 1 changed file with 41 additions and 41 deletions.
82 changes: 41 additions & 41 deletions src/Css/Style.php
Expand Up @@ -964,7 +964,7 @@ public function length_in_pt($length, ?float $ref_size = null)
$font_size = $this->__get("font_size");
$ref_size = $ref_size ?? $font_size;

if (!is_array($length)) {
if (!\is_array($length)) {
$length = [$length];
}

Expand Down Expand Up @@ -1241,7 +1241,7 @@ public function set_prop(string $prop, $val, bool $important = false, bool $clea
return;
}

if ($prop !== "content" && is_string($val) && mb_strpos($val, "url") === false && mb_strlen($val) > 1) {
if ($prop !== "content" && \is_string($val) && mb_strpos($val, "url") === false && mb_strlen($val) > 1) {
$val = mb_strtolower(trim(str_replace(["\n", "\t"], [" "], $val)));
}

Expand Down Expand Up @@ -1293,7 +1293,7 @@ public function set_prop(string $prop, $val, bool $important = false, bool $clea

// https://www.w3.org/TR/css-cascade-3/#inherit-initial
if ($val === "unset") {
$val = in_array($prop, self::$_inherited, true)
$val = \in_array($prop, self::$_inherited, true)
? "inherit"
: "initial";
}
Expand Down Expand Up @@ -1463,7 +1463,7 @@ public function __get(string $prop)
} else {
return implode(" ", array_map(function ($sub_prop) {
$val = $this->__get($sub_prop);
return is_array($val) ? implode(" ", $val) : $val;
return \is_array($val) ? implode(" ", $val) : $val;
}, self::$_props_shorthand[$prop]));
}
} else {
Expand Down Expand Up @@ -1493,7 +1493,7 @@ protected function compute_prop(string $prop, $val)
}

// Check for values which are already computed
if (!is_string($val)) {
if (!\is_string($val)) {
return $val;
}

Expand Down Expand Up @@ -1656,7 +1656,7 @@ protected function _get_font_family($computed): string
*/
protected function _get_word_spacing($computed)
{
if (is_float($computed)) {
if (\is_float($computed)) {
return $computed;
}

Expand All @@ -1673,7 +1673,7 @@ protected function _get_word_spacing($computed)
*/
protected function _get_letter_spacing($computed)
{
if (is_float($computed)) {
if (\is_float($computed)) {
return $computed;
}

Expand All @@ -1691,7 +1691,7 @@ protected function _get_letter_spacing($computed)
protected function _get_line_height($computed)
{
// Lengths have been computed to float, number values to string
if (is_float($computed)) {
if (\is_float($computed)) {
return $computed;
}

Expand Down Expand Up @@ -1865,7 +1865,7 @@ protected function get_border_side(string $side): string

return $this->__get("border_{$side}_width") . " " .
$this->__get("border_{$side}_style") . " " .
(is_array($color) ? $color["hex"] : $color);
(\is_array($color) ? $color["hex"] : $color);
}

/**
Expand Down Expand Up @@ -2053,7 +2053,7 @@ protected function _get_outline(): string

return $this->__get("outline_width") . " " .
$this->__get("outline_style") . " " .
(is_array($color) ? $color["hex"] : $color);
(\is_array($color) ? $color["hex"] : $color);
}

/**
Expand Down Expand Up @@ -2195,7 +2195,7 @@ protected function compute_color_value(string $val): ?string
return null;
}

return is_array($munged_color) ? $munged_color["hex"] : $munged_color;
return \is_array($munged_color) ? $munged_color["hex"] : $munged_color;
}

/**
Expand Down Expand Up @@ -2307,7 +2307,7 @@ protected function compute_line_width(string $val, string $style_prop): ?float
*/
protected function compute_border_style(string $val): ?string
{
return in_array($val, self::BORDER_STYLES, true) ? $val : null;
return \in_array($val, self::BORDER_STYLES, true) ? $val : null;
}

/**
Expand All @@ -2323,7 +2323,7 @@ protected function set_quad_shorthand(string $prop, string $value): array
{
$v = $this->parse_property_value($value);

switch (count($v)) {
switch (\count($v)) {
case 1:
$values = [$v[0], $v[0], $v[0], $v[0]];
break;
Expand Down Expand Up @@ -2429,7 +2429,7 @@ protected function _compute_background_image(string $val)
protected function _compute_background_repeat(string $val)
{
$keywords = ["repeat", "repeat-x", "repeat-y", "no-repeat"];
return in_array($val, $keywords, true) ? $val : null;
return \in_array($val, $keywords, true) ? $val : null;
}

/**
Expand All @@ -2438,7 +2438,7 @@ protected function _compute_background_repeat(string $val)
protected function _compute_background_attachment(string $val)
{
$keywords = ["scroll", "fixed"];
return in_array($val, $keywords, true) ? $val : null;
return \in_array($val, $keywords, true) ? $val : null;
}

/**
Expand All @@ -2448,7 +2448,7 @@ protected function _compute_background_position(string $val)
{
$parts = preg_split("/\s+/", $val);

if (count($parts) > 2) {
if (\count($parts) > 2) {
return null;
}

Expand Down Expand Up @@ -2542,7 +2542,7 @@ protected function _compute_background_size(string $val)

$parts = preg_split("/\s+/", $val);

if (count($parts) > 2) {
if (\count($parts) > 2) {
return null;
}

Expand Down Expand Up @@ -2586,21 +2586,21 @@ protected function _set_background(string $value): array
}
}

if (count($pos_size)) {
if (\count($pos_size)) {
// Split value list at "/"
$index = array_search("/", $pos_size, true);

if ($index !== false) {
$pos = array_slice($pos_size, 0, $index);
$size = array_slice($pos_size, $index + 1);
$pos = \array_slice($pos_size, 0, $index);
$size = \array_slice($pos_size, $index + 1);
} else {
$pos = $pos_size;
$size = [];
}

$props["background_position"] = implode(" ", $pos);

if (count($size)) {
if (\count($size)) {
$props["background_size"] = implode(" ", $size);
}
}
Expand Down Expand Up @@ -2694,12 +2694,12 @@ protected function _set_font(string $value): array
}

// `font-style`, `font-variant`, `font-weight` in any order
$styleVariantWeight = array_slice($components, 0, $sizeIndex);
$styleVariantWeight = \array_slice($components, 0, $sizeIndex);
$stylePattern = "/^(italic|oblique)$/";
$variantPattern = "/^(small-caps)$/";
$weightPattern = "/^(bold|bolder|lighter|100|200|300|400|500|600|700|800|900)$/";

if (count($styleVariantWeight) > 3) {
if (\count($styleVariantWeight) > 3) {
return [];
}

Expand All @@ -2720,10 +2720,10 @@ protected function _set_font(string $value): array
}

// Optional slash + `line-height` followed by mandatory `font-family`
$lineFamily = array_slice($components, $sizeIndex + 1);
$lineFamily = \array_slice($components, $sizeIndex + 1);
$hasLineHeight = $lineFamily !== [] && $lineFamily[0] === "/";
$lineHeight = $hasLineHeight ? array_slice($lineFamily, 1, 1) : [];
$fontFamily = $hasLineHeight ? array_slice($lineFamily, 2) : $lineFamily;
$lineHeight = $hasLineHeight ? \array_slice($lineFamily, 1, 1) : [];
$fontFamily = $hasLineHeight ? \array_slice($lineFamily, 2) : $lineFamily;
$lineHeightPattern = "/^(normal|$number(?:$unit)?)$/";

// Missing `font-family` or `line-height` after slash
Expand Down Expand Up @@ -2760,7 +2760,7 @@ protected function _compute_text_align(string $val)
}
}

if (!in_array($alignment, self::TEXT_ALIGN_KEYWORDS, true)) {
if (!\in_array($alignment, self::TEXT_ALIGN_KEYWORDS, true)) {
return null;
}

Expand Down Expand Up @@ -3064,7 +3064,7 @@ protected function parse_border_side(string $value, array $styles = self::BORDER
$color = null;

foreach ($components as $val) {
if ($style === null && in_array($val, $styles, true)) {
if ($style === null && \in_array($val, $styles, true)) {
$style = $val;
} elseif ($color === null && $this->is_color_value($val)) {
$color = $val;
Expand Down Expand Up @@ -3271,7 +3271,7 @@ protected function _compute_outline_color(string $val)

protected function _compute_outline_style(string $val)
{
return in_array($val, self::OUTLINE_STYLES, true) ? $val : null;
return \in_array($val, self::OUTLINE_STYLES, true) ? $val : null;
}

protected function _compute_outline_width(string $val)
Expand All @@ -3297,7 +3297,7 @@ protected function _compute_border_spacing(string $val)
{
$parts = preg_split("/\s+/", $val);

if (count($parts) > 2) {
if (\count($parts) > 2) {
return null;
}

Expand Down Expand Up @@ -3368,9 +3368,9 @@ protected function _set_list_style(string $value): array
continue;
}

if (in_array($val, $types, true)) {
if (\in_array($val, $types, true)) {
$props["list_style_type"] = $val;
} elseif (in_array($val, $positions, true)) {
} elseif (\in_array($val, $positions, true)) {
$props["list_style_position"] = $val;
}
}
Expand All @@ -3388,7 +3388,7 @@ protected function _compute_size(string $val)
}

$parts = $this->parse_property_value($val);
$count = count($parts);
$count = \count($parts);

if ($count === 0 || $count > 3) {
return null;
Expand All @@ -3414,11 +3414,11 @@ protected function _compute_size(string $val)

if ($size !== null) {
// Standard paper size
[$l1, $l2] = array_slice(CPDF::$PAPER_SIZES[$size], 2, 2);
[$l1, $l2] = \array_slice(CPDF::$PAPER_SIZES[$size], 2, 2);
} elseif ($lengths === []) {
// Orientation only, use default paper size
$dims = $this->_stylesheet->get_dompdf()->getPaperSize();
[$l1, $l2] = array_slice($dims, 2, 2);
[$l1, $l2] = \array_slice($dims, 2, 2);
} else {
// Custom paper size
$l1 = $this->compute_length_positive($lengths[0]);
Expand Down Expand Up @@ -3481,7 +3481,7 @@ protected function _get_transform($computed)

foreach ($functions as $name => $pattern) {
if (preg_match("/$name\s*$pattern/i", $t, $matches)) {
$values = array_slice($matches, 1);
$values = \array_slice($matches, 1);

switch ($name) {
// <angle> units
Expand All @@ -3492,9 +3492,9 @@ protected function _get_transform($computed)

foreach ($values as $i => $value) {
if (strpos($value, "rad")) {
$values[$i] = rad2deg(floatval($value));
$values[$i] = rad2deg((float) $value);
} else {
$values[$i] = floatval($value);
$values[$i] = (float) $value;
}
}

Expand Down Expand Up @@ -3569,7 +3569,7 @@ protected function _get_transform($computed)
* @param string $computed
* @return array
*
* @link https://www.w3.org/TR/css-transforms-1/#transform-origin
* @link https://www.w3.org/TR/css-transforms-1/#transform-origin-property
*/
protected function _get_transform_origin($computed)
{
Expand All @@ -3578,9 +3578,9 @@ protected function _get_transform_origin($computed)
$values = preg_split("/\s+/", $computed);

$values = array_map(function ($value) {
if (in_array($value, ["top", "left"])) {
if (\in_array($value, ["top", "left"], true)) {
return 0;
} else if (in_array($value, ["bottom", "right"])) {
} else if (\in_array($value, ["bottom", "right"], true)) {
return "100%";
} else {
return $value;
Expand Down

0 comments on commit d88d496

Please sign in to comment.