Skip to content

Commit

Permalink
ProcessExtends: Use map instead of list for selectors and parents
Browse files Browse the repository at this point in the history
* Convert Less\Tree\Extend#parent_ids from a list to a map.

* Maintain a copy of Less\Tree\Selector#_oelements list in map form.
  Keep the list as well for now because there is a logical order to this
  information, which callers outside ProcessExtends depend on.

These change bring an performance improvement.
About 4% speedup for Magento2.

Co-authored-by: Timo Tijhof <krinklemail@gmail.com>
  • Loading branch information
andrey-legayev and Krinkle authored Mar 19, 2020
1 parent 004eaea commit 93a2fde
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
11 changes: 8 additions & 3 deletions lib/Less/Tree/Extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,13 @@ public function __construct($selector, $option, $index){
break;
}

$this->object_id = $i++;
$this->parent_ids = array($this->object_id);
// This must use a string (instead of int) so that array_merge()
// preserves keys on arrays that use IDs in their keys.
$this->object_id = 'id_' . $i++;

$this->parent_ids = array(
$this->object_id => true
);
}

public function accept( $visitor ){
Expand Down Expand Up @@ -74,4 +79,4 @@ public function findSelfSelectors( $selectors ){
$this->selfSelectors = array(new Less_Tree_Selector($selfElements));
}

}
}
5 changes: 5 additions & 0 deletions lib/Less/Tree/Selector.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Less_Tree_Selector extends Less_Tree{
public $elements_len = 0;

public $_oelements;
public $_oelements_assoc;
public $_oelements_len;
public $cacheable = true;

Expand Down Expand Up @@ -83,6 +84,8 @@ public function match( $other ){
public function CacheElements(){

$this->_oelements = array();
$this->_oelements_assoc = array();

$css = '';

foreach($this->elements as $v){
Expand All @@ -108,6 +111,8 @@ public function CacheElements(){
array_shift($this->_oelements);
$this->_oelements_len--;
}

$this->_oelements_assoc = array_fill_keys($this->_oelements, true);
}
}

Expand Down
10 changes: 6 additions & 4 deletions lib/Less/Visitor/processExtends.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ private function doExtendChaining( $extendsList, $extendsListTarget, $iterationC
$extend = $extendsList[$extendIndex];
$targetExtend = $extendsListTarget[$targetExtendIndex];

// look for circular references
if( in_array($targetExtend->object_id, $extend->parent_ids,true) ){
// Optimisation: Explicit reference, <https://github.com/wikimedia/less.php/pull/14>
if( \array_key_exists($targetExtend->object_id, $extend->parent_ids) ){
// ignore circular references
continue;
}

Expand Down Expand Up @@ -269,7 +270,8 @@ private function HasMatches($extend, $haystackSelectorPath){
return true;
}

if( in_array($first_el, $hackstackSelector->_oelements) ){
// Optimisation: Explicit reference, <https://github.com/wikimedia/less.php/pull/14>
if( \array_key_exists($first_el, $hackstackSelector->_oelements_assoc) ){
return true;
}
}
Expand Down Expand Up @@ -466,4 +468,4 @@ protected function visitDirectiveOut(){
array_pop($this->allExtendsStack);
}

}
}
2 changes: 1 addition & 1 deletion test/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ function obj($mixed, $objects = array() ) {


$exclude_keys = array('originalRuleset','currentFileInfo','lookups','index','ruleset_id','type','_rulesets','_variables','allowImports','_css','cache_string','elements_len',
'_oelements','first_oelements','_oelements_len','cacheable', ); //'variable','combinator'
'_oelements','_oelements_assoc','first_oelements','_oelements_len','cacheable', ); //'variable','combinator'
//$exclude_keys = array();

$type = gettype($mixed);
Expand Down

0 comments on commit 93a2fde

Please sign in to comment.