Skip to content

Commit

Permalink
refactor: increase performance by ~7% thanks to Tokens::block*Cache
Browse files Browse the repository at this point in the history
… hit increased by ~12% (PHP-CS-Fixer#6176)
  • Loading branch information
keradus authored Jan 9, 2024
1 parent 8818ab7 commit fc00ff1
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions src/Tokenizer/Tokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,10 +295,18 @@ public function setSize($size): bool
*/
public function offsetUnset($index): void
{
$this->changed = true;
$this->namespaceDeclarations = null;
if (isset($this[$index])) {
if (isset($this->blockStartCache[$index])) {
unset($this->blockEndCache[$this->blockStartCache[$index]], $this->blockStartCache[$index]);
}
if (isset($this->blockEndCache[$index])) {
unset($this->blockStartCache[$this->blockEndCache[$index]], $this->blockEndCache[$index]);
}

$this->unregisterFoundToken($this[$index]);

$this->changed = true;
$this->namespaceDeclarations = null;
}

parent::offsetUnset($index);
Expand All @@ -314,16 +322,21 @@ public function offsetUnset($index): void
*/
public function offsetSet($index, $newval): void
{
$this->blockStartCache = [];
$this->blockEndCache = [];

if (!isset($this[$index]) || !$this[$index]->equals($newval)) {
$this->changed = true;
$this->namespaceDeclarations = null;

if (isset($this[$index])) {
if (isset($this->blockStartCache[$index])) {
unset($this->blockEndCache[$this->blockStartCache[$index]], $this->blockStartCache[$index]);
}
if (isset($this->blockEndCache[$index])) {
unset($this->blockStartCache[$this->blockEndCache[$index]], $this->blockEndCache[$index]);
}

$this->unregisterFoundToken($this[$index]);
}

$this->changed = true;
$this->namespaceDeclarations = null;

$this->registerFoundToken($newval);
}

Expand Down

0 comments on commit fc00ff1

Please sign in to comment.