Skip to content

file-diff printing is slow #7899

@staabm

Description

@staabm

Bug Report

Subject Details
Rector version rectorphp/rector-src@dc57cff

Minimal PHP Code Causing Issue

when running rector with

<?php // rector-test.php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Set\ValueObject\SetList;

return static function (RectorConfig $rectorConfig): void {
    $rectorConfig->disableParallel();

    $rectorConfig->sets([
        SetList::TYPE_DECLARATION,
    ]);

    $rectorConfig->skip([
        Rector\TypeDeclaration\Rector\ClassMethod\FalseReturnClassMethodToNullableRector::class => '*'
    ]);

    $rectorConfig->paths([
        __DIR__ . '/../phpstan-src/src/Analyser/MutatingScope.php',
    ]);
};

with MutatingScope.php content of https://github.com/phpstan/phpstan-src/blob/ea3d08e01c8bc116ae509b9f690f7847be90546b/src/Analyser/MutatingScope.php

PHP 8.1.18 (cli) (built: Apr 12 2023 12:31:46) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.18, Copyright (c) Zend Technologies
    with blackfire v1.86.3~mac-x64-non_zts81, https://blackfire.io, by Blackfire

running on a recent macos with a m1 pro.

you get a pretty small result:

1 file with changes
===================

1) ../phpstan-src/src/Analyser/MutatingScope.php:2164

    ---------- begin diff ----------
@@ @@
 	/**
 	 * @param Node\Expr\PropertyFetch|Node\Expr\StaticPropertyFetch $propertyFetch
 	 */
-	private function hasPropertyNativeType($propertyFetch): bool
+	private function hasPropertyNativeType(\PhpParser\Node\Expr\PropertyFetch|\PhpParser\Node\Expr\StaticPropertyFetch $propertyFetch): bool
 	{
 		$propertyReflection = $this->propertyReflectionFinder->findPropertyReflectionFromNode($propertyFetch, $this);
 		if ($propertyReflection === null) {
@@ @@
 		foreach ($expressionTypes as $exprString => $expressionType) {
 			$staticExpression = $nodeFinder->findFirst(
 				[$expressionType->getExpr()],
-				static fn ($node) => $node instanceof Expr\StaticCall || $node instanceof Expr\StaticPropertyFetch,
+				static fn ($node): bool => $node instanceof Expr\StaticCall || $node instanceof Expr\StaticPropertyFetch,
 			);
 			if ($staticExpression !== null) {
 				continue;
@@ @@
 		}

 		$classReflectionName = $classMemberReflection->getDeclaringClass()->getName();
-		$canAccessClassMember = static function (ClassReflection $classReflection) use ($classMemberReflection, $classReflectionName) {
+		$canAccessClassMember = static function (ClassReflection $classReflection) use ($classMemberReflection, $classReflectionName): bool {
 			if ($classMemberReflection->isPrivate()) {
 				return $classReflection->getName() === $classReflectionName;
 			}
    ----------- end diff -----------

Applied rules:
 * AddArrowFunctionReturnTypeRector
 * AddMethodCallBasedStrictParamTypeRector (https://github.com/symplify/phpstan-rules/blob/master/docs/rules_overview.md#checktypehintcallertyperule)
 * AddClosureReturnTypeRector


 [OK] 1 file would have changed (dry-run) by Rector

but looking at the blackfire profile of blackfire run --ignore-exit-status php bin/rector.php -c rector-test.php --dry-run -vvv --debug we can see the biggest part of the time is required to render the diff after rector was already finished processing

grafik

grafik

I find it kind of suprising and did not yet found a way to make the diff printing faster, but want to leave it here so maybe others also can have a look.

the relevant code is here https://github.com/sebastianbergmann/diff/blob/7e53eb5309d8748956d3aaf8a9e20cf211260817/src/MemoryEfficientLongestCommonSubsequenceCalculator.php#L68-L87

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions