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

Use rector to add property typehints #8887

Merged

Conversation

jack-worman
Copy link
Contributor

Used Rector's TypedPropertyFromAssignsRector rule.

@jack-worman jack-worman force-pushed the Use_rector_to_add_property_typehints branch 4 times, most recently from 7446ff2 to 8a0c8df Compare December 11, 2022 23:02
@muglug
Copy link
Collaborator

muglug commented Dec 11, 2022

Hey! Thanks for this PR. What impact does this have on performance? Every time PHP writes to a typed property it performs a type-check.

From what I've elsewhere, I think the performance difference might be too miniscule to matter, but I'm definitely curious.

@jack-worman jack-worman force-pushed the Use_rector_to_add_property_typehints branch from 8a0c8df to dd0d830 Compare December 11, 2022 23:25
@jack-worman
Copy link
Contributor Author

jack-worman commented Dec 11, 2022

@muglug Here is a benchmark:

<?php

declare(strict_types=1);

class PerformanceTest1 {
    public int $var1;
    /** @var int */
    public $var2;
}
$performanceTest1 = new PerformanceTest1();

function getDelta(array $startTime, array $endTime): array {
    $delta = [$endTime[0] - $startTime[0], $endTime[1] - $startTime[1]];
    if ($delta[1] < 0) {
        $delta[0] -= 1;
        $delta[1] += 1_000_000_000;
    }
    return $delta;
}

$startTime = hrtime();
for ($i = 0; $i < 1_000_000_000; ++$i) {
    $performanceTest1->var2 = $i;
}
$endTime = hrtime();
$delta1 = getDelta($startTime, $endTime);

$startTime = hrtime();
for ($i = 0; $i < 1_000_000_000; ++$i) {
    $performanceTest1->var1 = $i;
}
$endTime = hrtime();
$delta2 = getDelta($startTime, $endTime);

var_export($delta1);
echo "\n";
var_export($delta2);
echo "\n";
var_export(getDelta($delta1, $delta2));
echo "\n";

Takes about 1 billion writes to get to a difference over 1 second.

@weirdan
Copy link
Collaborator

weirdan commented Dec 12, 2022

From what I see, there's less than 0.5% slowdown:
image

@weirdan weirdan added the release:internal The PR will be included in 'Internal changes' section of the release notes label Dec 12, 2022
@orklah
Copy link
Collaborator

orklah commented Dec 12, 2022

Well, for 0.5, I'd say it's worth it. I'm surprised not all properties were changed though. Maybe rector needs to pass in multiple sweeps for tricky properties?

@jack-worman
Copy link
Contributor Author

@orklah Yea... rector also screwed up on a bunch of the array types and mangled the docblocks. I had to go through and revert a bunch of stuff.

If this PR goes through I would be happy to go work on the rest of the untyped properties.

@weirdan
Copy link
Collaborator

weirdan commented Dec 12, 2022

Note that changing the property type for non-internal non-private properties is a BC break.

Basically:

  • changing a private property type is ok
  • changing any property type on internal classes (Psalm\Internal namespace) is ok

@jack-worman
Copy link
Contributor Author

Note that changing the property type for non-internal non-private properties is a BC break.

I re-reviewed the PR and can confirm there is no BC

@weirdan
Copy link
Collaborator

weirdan commented Dec 13, 2022

Yes, this PR is fine. I mean, this is something to keep in mind for your potential future PRs.

@orklah orklah merged commit 794ca93 into vimeo:master Dec 13, 2022
@orklah
Copy link
Collaborator

orklah commented Dec 13, 2022

Thanks!

@jack-worman jack-worman deleted the Use_rector_to_add_property_typehints branch December 13, 2022 19:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release:internal The PR will be included in 'Internal changes' section of the release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants