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

feat: Add sprintf in the list of compiler optimized functions #8092

Merged
merged 2 commits into from
Jun 18, 2024

Conversation

stof
Copy link
Contributor

@stof stof commented Jun 18, 2024

A compiler optimization has been added for sprintf in PHP 8.4.

A compiler optimization has been added for `sprintf` in PHP 8.4.
@stof stof changed the title Add sprintf in the list of compiler optimized functions feat: Add sprintf in the list of compiler optimized functions Jun 18, 2024
@coveralls
Copy link

Coverage Status

coverage: 95.168%. remained the same
when pulling b7cc7bc on stof:patch-2
into fa3b1ed on PHP-CS-Fixer:master.

@coveralls
Copy link

Coverage Status

coverage: 95.168%. remained the same
when pulling 143e17a on stof:patch-2
into fa3b1ed on PHP-CS-Fixer:master.

@@ -378,6 +378,7 @@ private function getAllCompilerOptimizedFunctionsNormalized(): array
'is_string',
'ord',
'sizeof',
'sprintf',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about other functions like vsprintf?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the optimization has only been implemented for sprintf. There is no optimization for vsprintf for now

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have questioned this in php/php-src#14546 (comment)

Copy link
Contributor Author

@stof stof Jun 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My guess is that vsprintf is harder to transform into a concatenation because the compiler sees a single argument (the array of values) and has no easy way to destructure it at compile time.

Thus, to me, the use case for vsprintf compared to sprintf is likely to be the case where the array of values comes from a variable (using vsprintf with an array literal is just adding the overhead of creating an array and then destroying it, compared to sprintf). And the case of a variable would not be optimizable anyway as you cannot destructure it at compile time as it requires having the variable value.

Copy link
Member

@Wirone Wirone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quick questions before I scroll through all leading \ in ~200 files 😅.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we have test case for sprintf to see whether \ was added?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the test is not testing each of the compiler optimized functions explicitly, so I haven't added a new test for this function.

@@ -378,6 +378,7 @@ private function getAllCompilerOptimizedFunctionsNormalized(): array
'is_string',
'ord',
'sizeof',
'sprintf',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know there wasn't such a convention before, but wouldn't it make sense to add sprintf only if PHP >= 8.4? For previous versions it does not add value, so probably people running older PHP versions not necessarily want to apply potentially huge diff that does not improve performance for them?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not support this idea, as running PHP CS Fixer in different environments is even worse - if not detected from composer.json of course.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mvorisek I don't understand what you said. The suggested usage of Fixer is to run it on the PHP version you use for your app. So if the app is still on 7.4 for example, there's no point in applying \ to sprintf as it does not bring the performance boost, but can lead to sudden CI crashes when Fixer is updated.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean if we condition it on executable PHP version, I do not want different fixer output locally vs. CI when the minor PHP version does not match.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Wirone open-source package will need to support multiple versions of PHP. They don't have a single target PHP version. And they should be fully-qualifying calls to sprintf to benefit the subset of their users running on PHP >=8.4 even when their min target version is lower than 8.4 (qualifying the call does not break code on PHP 8.3. It simply does not unlock a performance optimization there)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mvorisek but there are rules that behave differently depending on PHP runtime, that's why I wanted to discuss it.

@stof yeah, for packages it's different, but still it'll affect codebases with pinned PHP versions. I don't want to enforce this change, just wanted to figure out the best approach.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mvorisek but there are rules that behave differently depending on PHP runtime, that's why I wanted to discuss it.

The desired behaviour is too have PHP CS Fixer stable across PHP runtime versions once the codebase is fixed for the highest supported version. This is quite working for now for ex. with attributes which are ignored in lower PHP versions.

If we really want sprintf to condition based on PHP version, we will have to add it for PHP >=8.4 version and allow both variants below such version if configured so.

If this is too complicated, we must either always fix it or not.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is no harm of applying \ on PHP7 etc, apply it regardless of runtime.

@stof
Copy link
Contributor Author

stof commented Jun 18, 2024

@Wirone if you want a smaller diff, check the first commit. I intentionally kept the run of PHP-CS-Fixer in a separate commit to make the review easier.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants