Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tokens - optimize cache clear #6250

Merged
merged 1 commit into from Jan 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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