Skip to content

Commit

Permalink
cut token::family logic extraction in one of hotpaths
Browse files Browse the repository at this point in the history
  • Loading branch information
keradus committed Dec 28, 2024
1 parent 32bd8ca commit a1c8543
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
21 changes: 21 additions & 0 deletions src/Tokenizer/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ final class Token
*/
private bool $isArray;

/**
* @var int|string
*
* @todo Replace the name with outcome of https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/issues/8333
*/
private $family;

/**
* @param array{int, string}|string $token token prototype
*/
Expand All @@ -66,10 +73,12 @@ public function __construct($token)
}

$this->isArray = true;
$this->family = $token[0];
$this->id = $token[0];
$this->content = $token[1];
} elseif (\is_string($token)) {
$this->isArray = false;
$this->family = $token;
$this->id = null;
$this->content = $token;
} else {
Expand Down Expand Up @@ -267,6 +276,18 @@ public function getId(): ?int
return $this->id;
}

/**
* @return int|string
*
* @internal
*
* @private
*/
public function getFamily()
{
return $this->family;
}

/**
* Get token's name.
*
Expand Down
11 changes: 4 additions & 7 deletions src/Tokenizer/Tokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,7 @@ public static function detectBlockType(Token $token): ?array
}
}

// inlined extractTokenKind() call on the hot path
$tokenKind = $token->isArray() ? $token->getId() : $token->getContent();
$tokenKind = $token->getFamily();

return $blockEdgeKinds[$tokenKind] ?? null;
}
Expand Down Expand Up @@ -1423,8 +1422,7 @@ private function changeCodeHash(string $codeHash): void
*/
private function registerFoundToken(Token $token): void
{
// inlined extractTokenKind() call on the hot path
$tokenKind = $token->isArray() ? $token->getId() : $token->getContent();
$tokenKind = $token->getFamily();

$this->foundTokenKinds[$tokenKind] ??= 0;
++$this->foundTokenKinds[$tokenKind];
Expand All @@ -1435,8 +1433,7 @@ private function registerFoundToken(Token $token): void
*/
private function unregisterFoundToken(Token $token): void
{
// inlined extractTokenKind() call on the hot path
$tokenKind = $token->isArray() ? $token->getId() : $token->getContent();
$tokenKind = $token->getFamily();

if (1 === $this->foundTokenKinds[$tokenKind]) {
unset($this->foundTokenKinds[$tokenKind]);
Expand All @@ -1453,7 +1450,7 @@ private function unregisterFoundToken(Token $token): void
private function extractTokenKind($token)
{
return $token instanceof Token
? ($token->isArray() ? $token->getId() : $token->getContent())
? $token->getFamily()
: (\is_array($token) ? $token[0] : $token);
}

Expand Down

0 comments on commit a1c8543

Please sign in to comment.