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

[performance] Use static variables inside every singleton constructors. #77

Merged
merged 1 commit into from
Sep 15, 2023

Conversation

gnutix
Copy link
Contributor

@gnutix gnutix commented Sep 14, 2023

Hello folks,

Here's another performance optimization when static constructors are called a lot. It was already done for some methods in the library, so I kept the same code style and applied it to all static constructors that take no arguments ("constants", in a way).

Let me know if you think some of these do not deserve it. I applied it everywhere without much thinking about it, because I don't think there's any downside of having that static variable, and I definitely know that some of these methods show up a lot in our benchmarks, like TimeZone(Offset)::utc() and Duration::zero(), min()/max(), etc, and then it's definitely beneficial.

@gnutix gnutix force-pushed the optimize-singleton-constructors branch from e2129ef to 0f129ec Compare September 14, 2023 12:51
@gnutix
Copy link
Contributor Author

gnutix commented Sep 14, 2023

I don't think it make sense to try and fix the code coverage for a static variable check, but if you think so, please let me know how and I can make the changes.

@gnutix gnutix changed the title Use static variables inside every singleton constructors. [performance] Use static variables inside every singleton constructors. Sep 14, 2023
@BenMorel
Copy link
Member

Hi,

I don't think DayOfWeek and Month will benefit much from this, as they call get() internally, which already caches instances.

Plus, in the next version (0.6), these classes will be converted to PHP 8.1 enums, so it won't be long before you get a much bigger improvement.

I agree with all the others, though!

I don't think it make sense to try and fix the code coverage for a static variable check, but if you think so, please let me know how and I can make the changes.

I still prefer to get it covered, so you can just add a test for each method that calls it twice and ensures that it returns the same instance!

@gnutix gnutix force-pushed the optimize-singleton-constructors branch from 0f129ec to 19fb90d Compare September 14, 2023 22:31
@gnutix gnutix force-pushed the optimize-singleton-constructors branch from 19fb90d to ca4b381 Compare September 14, 2023 22:33
@gnutix
Copy link
Contributor Author

gnutix commented Sep 14, 2023

Tests added, reverted DayOfWeek and Month as suggested.

PS: i find it unexpected that ECS is configured with PhpCsFixer\Fixer\PhpUnit\PhpUnitTestCaseStaticMethodCallsFixer so that it forces using $this-> to call methods that are actually static (like assertSame). 🤷‍♂️

@BenMorel
Copy link
Member

PS: i find it unexpected that ECS is configured with PhpCsFixer\Fixer\PhpUnit\PhpUnitTestCaseStaticMethodCallsFixer so that it forces using $this-> to call methods that are actually static (like assertSame). 🤷‍♂️

I tend to agree. @tigitz Do you want to change this?

@tigitz
Copy link
Contributor

tigitz commented Sep 15, 2023

Yeah it's a matter of preference as explained here: https://docs.phpunit.de/en/10.3/assertions.html

The idea I guess from this rule would be to at least force consistency throughout the codebase.

But if you don't mind having both usage then I can remove it. Let me know.

@BenMorel Taking the opportunity to friendly remind you about the dedicated PR about CI and codestyle that is still pending your review 😉

@BenMorel BenMorel merged commit 8dd2b2c into brick:master Sep 15, 2023
7 checks passed
@BenMorel
Copy link
Member

Thank you, @gnutix!

@tigitz:

The idea I guess from this rule would be to at least force consistency throughout the codebase.

Definitely! I was thinking about switching to self, not allowing both. After double checking, this is actually the style I already use in brick/math and brick/money at least.

Taking the opportunity to friendly remind you about the dedicated PR about CI and codestyle that is still pending your review 😉

I know, I'm sorry, I'm still a bit unsure about whether this is the way I want things, so I postponed indefinitely 😕 But the lack of coding standards on the other libraries really bothers me, so I'll have to take a decision at some point.

In the meantime, if you want to change the PHPUnit code style, a PR is welcome 🙂

return $epoch;
}

return $epoch = new Instant(0, 0);
Copy link
Contributor

Choose a reason for hiding this comment

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

        /** @var Instant|null $epoch */
        static $epoch;

        if (!isset($epoch)) {
            $epoch = new Instant(0, 0);
        }

        return $epoch;

I think this variant is simpler, more cache-way

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agree. That's how I spontaneously wrote the PR, then I changed it to match the current code style..

Copy link
Member

Choose a reason for hiding this comment

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

I don't mind if you want to change this.
If you do, please use if ($epoch === null) instead of isset(), which wouldn't alert on undefined variable!

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.

4 participants