Skip to content

Commit

Permalink
minor #6250 Tokens - optimize cache clear (SpacePossum)
Browse files Browse the repository at this point in the history
This PR was merged into the master branch.

Discussion
----------

Tokens - optimize cache clear

replaces #6176

Commits
-------

5800ffc Tokens - optimize cache clear
  • Loading branch information
SpacePossum committed Jan 23, 2022
2 parents ac0d8b0 + 5800ffc commit b0a9fca
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Tokenizer/Tokens.php
Expand Up @@ -340,10 +340,15 @@ public function clearEmptyTokens(): void

for ($count = $index; $index < $limit; ++$index) {
if (!$this->isEmptyAt($index)) {
$this[$count++] = $this[$index]; // @phpstan-ignore-line as we know that index exists
// use directly for speed, skip the register of token kinds found etc.
parent::offsetSet($count++, $this[$index]);
}
}

// we are moving the tokens, we need to clear the indices Cache
$this->blockStartCache = [];
$this->blockEndCache = [];

$this->setSize($count);
}

Expand Down
66 changes: 66 additions & 0 deletions tests/Tokenizer/TokensTest.php
Expand Up @@ -16,6 +16,7 @@

use PhpCsFixer\Tests\Test\Assert\AssertTokensTrait;
use PhpCsFixer\Tests\TestCase;
use PhpCsFixer\Tokenizer\CT;
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;

Expand Down Expand Up @@ -1531,6 +1532,71 @@ public function provideInsertSlicesCases(): iterable
];
}

public function testBlockEdgeCachingOffsetSet(): void
{
$tokens = $this->getBlockEdgeCachingTestTokens();

$endIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_ARRAY_SQUARE_BRACE, 5);
static::assertSame(9, $endIndex);

$tokens->offsetSet(5, new Token('('));
$tokens->offsetSet(9, new Token('('));

$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid param $startIndex - not a proper block "start".');

$tokens->findBlockEnd(Tokens::BLOCK_TYPE_ARRAY_SQUARE_BRACE, 5);
}

public function testBlockEdgeCachingClearAt(): void
{
$tokens = $this->getBlockEdgeCachingTestTokens();

$endIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_ARRAY_SQUARE_BRACE, 5);
static::assertSame(9, $endIndex);

$tokens->clearAt(7); // note: offsetUnset doesn't work here
$endIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_ARRAY_SQUARE_BRACE, 5);
static::assertSame(9, $endIndex);

$tokens->clearEmptyTokens();
$endIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_ARRAY_SQUARE_BRACE, 5);
static::assertSame(8, $endIndex);
}

public function testBlockEdgeCachingInsertSlices(): void
{
$tokens = $this->getBlockEdgeCachingTestTokens();

$endIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_ARRAY_SQUARE_BRACE, 5);
static::assertSame(9, $endIndex);

$tokens->insertSlices([6 => [new Token([T_COMMENT, '/* A */'])], new Token([T_COMMENT, '/* B */'])]);

$endIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_ARRAY_SQUARE_BRACE, 5);
static::assertSame(11, $endIndex);
}

private function getBlockEdgeCachingTestTokens(): Tokens
{
Tokens::clearCache();

return Tokens::fromArray([
new Token([T_OPEN_TAG, '<?php ']),
new Token([T_VARIABLE, '$a']),
new Token([T_WHITESPACE, ' ']),
new Token('='),
new Token([T_WHITESPACE, ' ']),
new Token([CT::T_ARRAY_SQUARE_BRACE_OPEN, '[']),
new Token([T_WHITESPACE, ' ']),
new Token([T_COMMENT, '/* foo */']),
new Token([T_WHITESPACE, ' ']),
new Token([CT::T_ARRAY_SQUARE_BRACE_CLOSE, ']']),
new Token(';'),
new Token([T_WHITESPACE, "\n"]),
]);
}

private static function assertFindBlockEnd(int $expectedIndex, string $source, int $type, int $searchIndex): void
{
Tokens::clearCache();
Expand Down

0 comments on commit b0a9fca

Please sign in to comment.