Browse Source

minor #4048 Tokens - inlined extractTokenKind() call on the hot path (staabm)

This PR was merged into the 2.12 branch.

Discussion
----------

Tokens - inlined extractTokenKind() call on the hot path

as identified in https://github.com/FriendsOfPHP/PHP-CS-Fixer/issues/4026 we inline calls to `extractTokenKind()` which leads to a 5-7% speed increase.

most calls of this method happen thru the changed code as can be seen in the blackfire profile

![image](https://user-images.githubusercontent.com/120441/46915856-1a77fe80-cfb2-11e8-9e0c-531b88059ad4.png)

Commits
-------

27c78e39 Tokens - inlined extractTokenKind() call on the hot path
Dariusz Ruminski 6 years ago
parent
commit
49c3a9e383
1 changed files with 10 additions and 2 deletions
  1. 10 2
      src/Tokenizer/Tokens.php

+ 10 - 2
src/Tokenizer/Tokens.php

@@ -1402,7 +1402,11 @@ class Tokens extends \SplFixedArray
      */
     private function registerFoundToken($token)
     {
-        $tokenKind = $this->extractTokenKind($token);
+        // inlined extractTokenKind() call on the hot path
+        $tokenKind = $token instanceof Token
+            ? ($token->isArray() ? $token->getId() : $token->getContent())
+            : (\is_array($token) ? $token[0] : $token)
+        ;
 
         if (!isset($this->foundTokenKinds[$tokenKind])) {
             $this->foundTokenKinds[$tokenKind] = 0;
@@ -1418,7 +1422,11 @@ class Tokens extends \SplFixedArray
      */
     private function unregisterFoundToken($token)
     {
-        $tokenKind = $this->extractTokenKind($token);
+        // inlined extractTokenKind() call on the hot path
+        $tokenKind = $token instanceof Token
+            ? ($token->isArray() ? $token->getId() : $token->getContent())
+            : (\is_array($token) ? $token[0] : $token)
+        ;
 
         if (!isset($this->foundTokenKinds[$tokenKind])) {
             return;