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

CachedParser: use LRU cache #2879

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion conf/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -1857,7 +1857,7 @@ services:
class: PHPStan\Parser\CachedParser
arguments:
originalParser: @pathRoutingParser
cachedNodesByStringCountMax: %cache.nodesByStringCountMax%
cacheCapacity: %cache.nodesByStringCountMax%
autowired: false

phpParserDecorator:
Expand Down
65 changes: 14 additions & 51 deletions src/Parser/CachedParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@

use PhpParser\Node;
use PHPStan\File\FileReader;
use function array_slice;

class CachedParser implements Parser
{

/** @var array<string, Node\Stmt[]>*/
private array $cachedNodesByString = [];

private int $cachedNodesByStringCount = 0;
/** @var ParserCache<Node\Stmt[]>*/
private ParserCache $cache;

/** @var array<string, true> */
private array $parsedByString = [];

public function __construct(
private Parser $originalParser,
private int $cachedNodesByStringCountMax,
int $cacheCapacity,
)
{
$this->cache = $cacheCapacity === 0
? new UnlimitedCache()
: new LRUCache($cacheCapacity);
}

/**
Expand All @@ -30,68 +30,31 @@ public function __construct(
*/
public function parseFile(string $file): array
{
if ($this->cachedNodesByStringCountMax !== 0 && $this->cachedNodesByStringCount >= $this->cachedNodesByStringCountMax) {
$this->cachedNodesByString = array_slice(
$this->cachedNodesByString,
1,
null,
true,
);

--$this->cachedNodesByStringCount;
}

$sourceCode = FileReader::read($file);
if (!isset($this->cachedNodesByString[$sourceCode]) || isset($this->parsedByString[$sourceCode])) {
$this->cachedNodesByString[$sourceCode] = $this->originalParser->parseFile($file);
$this->cachedNodesByStringCount++;
if (!$this->cache->has($sourceCode) || isset($this->parsedByString[$sourceCode])) {
$this->cache->put($sourceCode, $this->originalParser->parseFile($file));
unset($this->parsedByString[$sourceCode]);
}

return $this->cachedNodesByString[$sourceCode];
return $this->cache->get($sourceCode);
}

/**
* @return Node\Stmt[]
*/
public function parseString(string $sourceCode): array
{
if ($this->cachedNodesByStringCountMax !== 0 && $this->cachedNodesByStringCount >= $this->cachedNodesByStringCountMax) {
$this->cachedNodesByString = array_slice(
$this->cachedNodesByString,
1,
null,
true,
);

--$this->cachedNodesByStringCount;
}

if (!isset($this->cachedNodesByString[$sourceCode])) {
$this->cachedNodesByString[$sourceCode] = $this->originalParser->parseString($sourceCode);
$this->cachedNodesByStringCount++;
if (!$this->cache->has($sourceCode)) {
$this->cache->put($sourceCode, $this->originalParser->parseString($sourceCode));
$this->parsedByString[$sourceCode] = true;
}

return $this->cachedNodesByString[$sourceCode];
return $this->cache->get($sourceCode);
}

public function getCachedNodesByStringCount(): int
{
return $this->cachedNodesByStringCount;
}

public function getCachedNodesByStringCountMax(): int
{
return $this->cachedNodesByStringCountMax;
}

/**
* @return array<string, Node[]>
*/
public function getCachedNodesByString(): array
public function getCachedItemsCount(): int
{
return $this->cachedNodesByString;
return $this->cache->getCount();
}

}
72 changes: 72 additions & 0 deletions src/Parser/LRUCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php declare(strict_types = 1);

namespace PHPStan\Parser;

use LogicException;
use function array_key_first;
use function array_keys;
use function count;
use function sprintf;

/**
* @template TValue
* @implements ParserCache<TValue>
*/
class LRUCache implements ParserCache
{

/** @var array<string, TValue> */
private array $cache;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it might make sense to try SplFixedArray here

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm our capacity is too small for it beeing useful

https://stackoverflow.com/a/11831076


public function __construct(private int $capacity)
{
$this->cache = [];
}

/**
* @return TValue
*/
public function get(string $key)
{
if (!isset($this->cache[$key])) {
throw new LogicException(sprintf('Key %s was not found in the cache, use ->has() first', $key));
}

$value = $this->cache[$key];
unset($this->cache[$key]);
$this->cache[$key] = $value;

return $this->cache[$key];
}

/**
* @param TValue $value
*/
public function put(string $key, $value): void
{
if (count($this->cache) >= $this->capacity) {
unset($this->cache[array_key_first($this->cache)]);
}

$this->cache[$key] = $value;
}

public function has(string $key): bool
{
return isset($this->cache[$key]);
}

public function getCount(): int
{
return count($this->cache);
}

/**
* @return list<array-key>
*/
public function getKeys(): array
{
return array_keys($this->cache);
}

}
25 changes: 25 additions & 0 deletions src/Parser/ParserCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php declare(strict_types = 1);

namespace PHPStan\Parser;

/**
* @template TValue
*/
interface ParserCache
{

/**
* @return TValue
*/
public function get(string $key);

/**
* @param TValue $value
*/
public function put(string $key, $value): void;

public function has(string $key): bool;

public function getCount(): int;

}
54 changes: 54 additions & 0 deletions src/Parser/UnlimitedCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php declare(strict_types = 1);

namespace PHPStan\Parser;

use LogicException;
use function count;
use function sprintf;

/**
* @template TValue
* @implements ParserCache<TValue>
*/
class UnlimitedCache implements ParserCache
{

/** @var array<string, TValue> */
private array $cache;

public function __construct()
{
$this->cache = [];
}

/**
* @return TValue
*/
public function get(string $key)
{
if (!isset($this->cache[$key])) {
throw new LogicException(sprintf('Key %s was not found in the cache, use ->has() first', $key));
}

return $this->cache[$key];
}

/**
* @param TValue $value
*/
public function put(string $key, $value): void
{
$this->cache[$key] = $value;
}

public function has(string $key): bool
{
return isset($this->cache[$key]);
}

public function getCount(): int
{
return count($this->cache);
}

}
19 changes: 7 additions & 12 deletions tests/PHPStan/Parser/CachedParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,14 @@ public function testParseFileClearCache(
$cachedNodesByStringCountMax
);

$this->assertEquals(
$cachedNodesByStringCountMax,
$parser->getCachedNodesByStringCountMax()
);

// Add strings to cache
for ($i = 0; $i <= $cachedNodesByStringCountMax; $i++) {
$parser->parseString('string' . $i);
}

$this->assertEquals(
$this->assertSame(
$cachedNodesByStringCountExpected,
$parser->getCachedNodesByStringCount()
);

$this->assertCount(
$cachedNodesByStringCountExpected,
$parser->getCachedNodesByString()
$parser->getCachedItemsCount()
);
}

Expand All @@ -58,6 +48,11 @@ public function dataParseFileClearCache(): \Generator
'cachedNodesByStringCountMax' => 51,
'cachedNodesByStringCountExpected' => 51,
];

yield 'unlimited' => [
'cachedNodesByStringCountMax' => 0,
'cachedNodesByStringCountExpected' => 1,
];
}

private function getParserMock(): Parser&MockObject
Expand Down
35 changes: 35 additions & 0 deletions tests/PHPStan/Parser/LRUCacheTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php declare(strict_types = 1);

namespace PHPStan\Parser;

use PHPUnit\Framework\TestCase;

class LRUCacheTest extends TestCase
{

public function testBasics(): void
{
$cache = new LRUCache(3);

$cache->put('1', '1');
$cache->put('2', '2');
$cache->put('3', '3');
self::assertSame([1, 2, 3], $cache->getKeys());

$cache->get('2');
self::assertSame([1, 3, 2], $cache->getKeys());

$cache->put('4', '4');
self::assertSame([3, 2, 4], $cache->getKeys());

$cache->get('2');
self::assertSame([3, 4, 2], $cache->getKeys());

$cache->get('2');
self::assertSame([3, 4, 2], $cache->getKeys());

$cache->put('5', '5');
self::assertSame([4, 2, 5], $cache->getKeys());
}

}
Loading