Skip to content

Commit

Permalink
Fix #1696 - decrease memory usage by keeping parser
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed May 27, 2019
1 parent 4302596 commit c5e682d
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/Psalm/Internal/Provider/StatementsProvider.php
Expand Up @@ -53,6 +53,11 @@ class StatementsProvider
*/
private static $lexer;

/**
* @var PhpParser\Parser|null
*/
private static $parser;

public function __construct(
FileProvider $file_provider,
ParserCacheProvider $parser_cache_provider = null,
Expand Down Expand Up @@ -352,7 +357,13 @@ public static function parseStatements(
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;

Expand All @@ -361,7 +372,7 @@ public static function parseStatements(
if ($existing_statements && $file_changes && $existing_file_contents) {
$clashing_traverser = new \Psalm\Internal\Traverser\CustomTraverser;
$offset_analyzer = new \Psalm\Internal\Visitor\PartialParserVisitor(
$parser,
self::$parser,
$error_handler,
$file_changes,
$existing_file_contents,
Expand All @@ -376,7 +387,7 @@ public static function parseStatements(
} else {
try {
/** @var array<int, \PhpParser\Node\Stmt> */
$stmts = $parser->parse($file_contents, $error_handler) ?: [];
$stmts = self::$parser->parse($file_contents, $error_handler) ?: [];
} catch (\Throwable $t) {
$stmts = [];

Expand All @@ -386,7 +397,7 @@ public static function parseStatements(
} else {
try {
/** @var array<int, \PhpParser\Node\Stmt> */
$stmts = $parser->parse($file_contents, $error_handler) ?: [];
$stmts = self::$parser->parse($file_contents, $error_handler) ?: [];
} catch (\Throwable $t) {
$stmts = [];

Expand Down

0 comments on commit c5e682d

Please sign in to comment.