1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-27 04:45:20 +01:00

Fix #1696 - decrease memory usage by keeping parser

This commit is contained in:
Matthew Brown 2019-05-27 13:14:50 -04:00
parent 4302596654
commit c5e682d312

View File

@ -53,6 +53,11 @@ class StatementsProvider
*/ */
private static $lexer; private static $lexer;
/**
* @var PhpParser\Parser|null
*/
private static $parser;
public function __construct( public function __construct(
FileProvider $file_provider, FileProvider $file_provider,
ParserCacheProvider $parser_cache_provider = null, ParserCacheProvider $parser_cache_provider = null,
@ -352,7 +357,13 @@ class StatementsProvider
self::$lexer = new PhpParser\Lexer([ 'usedAttributes' => $attributes ]); self::$lexer = new PhpParser\Lexer([ 'usedAttributes' => $attributes ]);
} }
$parser = (new PhpParser\ParserFactory())->create(PhpParser\ParserFactory::PREFER_PHP7, self::$lexer); if (!self::$parser) {
$attributes = [
'comments', 'startLine', 'startFilePos', 'endFilePos',
];
self::$parser = (new PhpParser\ParserFactory())->create(PhpParser\ParserFactory::PREFER_PHP7, self::$lexer);
}
$used_cached_statements = false; $used_cached_statements = false;
@ -361,7 +372,7 @@ class StatementsProvider
if ($existing_statements && $file_changes && $existing_file_contents) { if ($existing_statements && $file_changes && $existing_file_contents) {
$clashing_traverser = new \Psalm\Internal\Traverser\CustomTraverser; $clashing_traverser = new \Psalm\Internal\Traverser\CustomTraverser;
$offset_analyzer = new \Psalm\Internal\Visitor\PartialParserVisitor( $offset_analyzer = new \Psalm\Internal\Visitor\PartialParserVisitor(
$parser, self::$parser,
$error_handler, $error_handler,
$file_changes, $file_changes,
$existing_file_contents, $existing_file_contents,
@ -376,7 +387,7 @@ class StatementsProvider
} else { } else {
try { try {
/** @var array<int, \PhpParser\Node\Stmt> */ /** @var array<int, \PhpParser\Node\Stmt> */
$stmts = $parser->parse($file_contents, $error_handler) ?: []; $stmts = self::$parser->parse($file_contents, $error_handler) ?: [];
} catch (\Throwable $t) { } catch (\Throwable $t) {
$stmts = []; $stmts = [];
@ -386,7 +397,7 @@ class StatementsProvider
} else { } else {
try { try {
/** @var array<int, \PhpParser\Node\Stmt> */ /** @var array<int, \PhpParser\Node\Stmt> */
$stmts = $parser->parse($file_contents, $error_handler) ?: []; $stmts = self::$parser->parse($file_contents, $error_handler) ?: [];
} catch (\Throwable $t) { } catch (\Throwable $t) {
$stmts = []; $stmts = [];