From a2a43c9ad38492c9dfd69346bd7902adbdbca402 Mon Sep 17 00:00:00 2001 From: Arthur Grand Date: Fri, 23 Oct 2026 01:28:20 +0900 Subject: [PATCH] perf: release syntax trees once objects are described Every object description kept the full syntax tree of its file for the rest of the run, though after describing the object only Blueprint::getUsagePathAndLines() reads it, to locate a violation. The factory now empties stmts, and the violation lookup parses that one file again when a test fails. Co-Authored-By: Claude Opus 5.5 --- src/Blueprint.php | 5 ++++- src/Factories/ObjectDescriptionFactory.php | 4 ++++ tests/ObjectDescriptionFactory.php | 11 +++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 tests/ObjectDescriptionFactory.php diff --git a/src/Blueprint.php b/src/Blueprint.php index 5f2db2b..2d334e9 100644 --- a/src/Blueprint.php +++ b/src/Blueprint.php @@ -261,8 +261,11 @@ private function getUsagePathAndLines(Layer $layer, string $objectName, string $ return null; } + // Object descriptions don't keep their syntax tree, so the file is parsed again to locate the violation... + $stmts = ServiceContainer::$parser->parse((string) file_get_contents($dependOnObject->path)) ?? []; + $nodes = ServiceContainer::$nodeFinder->findInstanceOf( - $dependOnObject->stmts, + ServiceContainer::$nodeTraverser->traverse($stmts), $class, ); diff --git a/src/Factories/ObjectDescriptionFactory.php b/src/Factories/ObjectDescriptionFactory.php index bdb901e..4bdd427 100644 --- a/src/Factories/ObjectDescriptionFactory.php +++ b/src/Factories/ObjectDescriptionFactory.php @@ -54,6 +54,10 @@ public static function make(string $filename, bool $onlyUserDefinedUses = true): static fn (string $use): bool => (! $onlyUserDefinedUses || self::isUserDefined($use)) && ! self::isSameLayer($object, $use), ) )); + + // The syntax tree is no longer needed once the object is described, and keeping it for every + // parsed file would hold most of the memory of an arch run... + $object->stmts = []; } return $object; diff --git a/tests/ObjectDescriptionFactory.php b/tests/ObjectDescriptionFactory.php new file mode 100644 index 0000000..0c19987 --- /dev/null +++ b/tests/ObjectDescriptionFactory.php @@ -0,0 +1,11 @@ +name)->toBe('Tests\Fixtures\Misc\HasNativeFunctions') + ->and($object->uses)->not->toBeEmpty() + ->and($object->stmts)->toBe([]); +});