Skip to content

Conversation

@andrey-legayev
Copy link
Contributor

Hi,

I've run xdebug profiler for Magento2 static content deploy and found huge amount of calls to in_array() inside Less processor.
I've tried to optimize it and got ~4% performance improvement.

Pls review my changes.

Andrey

$this->_oelements_len--;
}

$this->_oelements_assoc = array_fill_keys($this->_oelements, true);
Copy link
Member

Choose a reason for hiding this comment

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

Will need to review where and how _oelements_len and _oelements are used. If they are not used anywhere else, and (especially) not cached or filled in by caches anywhere, then we might be be able to instead make this the normal form of _oelements itself.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried to analyze algorithm to replace _oelements with associative array, but it's quite complicated and I've decided not to do that.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks. I've had a more in-depth review today, and I better understnad what you mean now.

I mistakenly thought that the array was generated elsewhere, perhaps by pushing into the array in a loop or in repsonse to other signals. But, that is not the case. It is generated right here a few lines up from preg_match_all. This means even if we did not keep a sequential list, we would still need to iterate the list (directly or via array_fill_keys). It is inevitable (without much more changes).


$this->object_id = $i++;
$this->parent_ids = array($this->object_id);
// string value is needed to make sure array_merge() processes keys as strings, not as numerics
Copy link
Member

Choose a reason for hiding this comment

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

Will need to look for where this is used. Might make sense to migrate to the + operator which can at times be faster to do this kind of operation for associative arrays.


// look for circular references
if( in_array($targetExtend->object_id, $extend->parent_ids,true) ){
if (isset($extend->parent_ids[$targetExtend->object_id])) {
Copy link
Member

Choose a reason for hiding this comment

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

Worth comparing to array_key_exists. Although counterintuitively, it seems isset() despite doing more work (exists + non-null), has historically been faster. Not sure if that's still the case in PHP 7.2+.

Choose a reason for hiding this comment

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

With PHP 7.4, array_key_exists is now faster: php/php-src#3360, though only when statically resolved: php/php-src@f1c0e67.

@Krinkle
Copy link
Member

Krinkle commented Sep 30, 2019

Thanks @andrey-legayev. Much appreciated.

I've filed https://phabricator.wikimedia.org/T234270 internally to track review of this.

This Less.php fork exists mainly to keep the ship afloat to host any fixes for incompatibilities or warnings we found during Wikipedia's migration to PHP 7.2. It doesn't currently have a long-term steward and is still marked as an "upstream" package, however that may need to change at some point - although we're short on resources in this area so that might take a while to get through. Thanks again!

@Krinkle Krinkle self-assigned this Mar 19, 2020
@Krinkle Krinkle merged commit 93a2fde into wikimedia:master Mar 19, 2020
@andrey-legayev
Copy link
Contributor Author

thanks for processing it!

@ihor-sviziev ihor-sviziev deleted the perf-fixes branch September 17, 2020 08:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

4 participants