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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/Duration.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,14 @@ private function __construct(int $seconds, int $nanos = 0)
*/
public static function zero(): Duration
{
return new Duration(0);
/** @var Duration|null $zero */
static $zero;

if ($zero) {
return $zero;
}

return $zero = new Duration(0);
}

/**
Expand Down
27 changes: 24 additions & 3 deletions src/Instant.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,14 @@ public static function of(int $epochSecond, int $nanoAdjustment = 0): Instant

public static function epoch(): Instant
{
return new Instant(0, 0);
/** @var Instant|null $epoch */
static $epoch;

if ($epoch) {
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!

}

public static function now(?Clock $clock = null): Instant
Expand All @@ -96,7 +103,14 @@ public static function now(?Clock $clock = null): Instant
*/
public static function min(): Instant
{
return new Instant(PHP_INT_MIN, 0);
/** @var Instant|null $min */
static $min;

if ($min) {
return $min;
}

return $min = new Instant(PHP_INT_MIN, 0);
}

/**
Expand All @@ -106,7 +120,14 @@ public static function min(): Instant
*/
public static function max(): Instant
{
return new Instant(PHP_INT_MAX, 999_999_999);
/** @var Instant|null $max */
static $max;

if ($max) {
return $max;
}

return $max = new Instant(PHP_INT_MAX, 999_999_999);
}

public function plus(Duration $duration): Instant
Expand Down
18 changes: 16 additions & 2 deletions src/LocalDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,14 @@ public static function now(TimeZone $timeZone, ?Clock $clock = null): LocalDate
*/
public static function min(): LocalDate
{
return LocalDate::of(self::MIN_YEAR, 1, 1);
/** @var LocalDate|null $min */
static $min;

if ($min) {
return $min;
}

return $min = LocalDate::of(self::MIN_YEAR, 1, 1);
}

/**
Expand All @@ -231,7 +238,14 @@ public static function min(): LocalDate
*/
public static function max(): LocalDate
{
return LocalDate::of(self::MAX_YEAR, 12, 31);
/** @var LocalDate|null $max */
static $max;

if ($max) {
return $max;
}

return $max = LocalDate::of(self::MAX_YEAR, 12, 31);
}

/**
Expand Down
18 changes: 16 additions & 2 deletions src/LocalDateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,29 @@ public static function fromNativeDateTime(DateTimeInterface $dateTime): LocalDat
*/
public static function min(): LocalDateTime
{
return new LocalDateTime(LocalDate::min(), LocalTime::min());
/** @var LocalDateTime|null $min */
static $min;

if ($min) {
return $min;
}

return $min = new LocalDateTime(LocalDate::min(), LocalTime::min());
}

/**
* Returns the highest possible value for LocalDateTime.
*/
public static function max(): LocalDateTime
{
return new LocalDateTime(LocalDate::max(), LocalTime::max());
/** @var LocalDateTime|null $max */
static $max;

if ($max) {
return $max;
}

return $max = new LocalDateTime(LocalDate::max(), LocalTime::max());
}

/**
Expand Down
29 changes: 25 additions & 4 deletions src/LocalTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,28 +175,49 @@ public static function now(TimeZone $timeZone, ?Clock $clock = null): LocalTime

public static function midnight(): LocalTime
{
return new LocalTime(0, 0, 0, 0);
return self::min();
}

public static function noon(): LocalTime
{
return new LocalTime(12, 0, 0, 0);
/** @var LocalTime|null $noon */
static $noon;

if ($noon) {
return $noon;
}

return $noon = new LocalTime(12, 0, 0, 0);
}

/**
* Returns the smallest possible value for LocalTime.
*/
public static function min(): LocalTime
{
return new LocalTime(0, 0, 0, 0);
/** @var LocalTime|null $min */
static $min;

if ($min) {
return $min;
}

return $min = new LocalTime(0, 0, 0, 0);
}

/**
* Returns the highest possible value for LocalTime.
*/
public static function max(): LocalTime
{
return new LocalTime(23, 59, 59, 999_999_999);
/** @var LocalTime|null $max */
static $max;

if ($max) {
return $max;
}

return $max = new LocalTime(23, 59, 59, 999_999_999);
}

/**
Expand Down
9 changes: 8 additions & 1 deletion src/Period.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,14 @@ public static function ofDays(int $days): Period
*/
public static function zero(): Period
{
return new Period(0, 0, 0);
/** @var Period|null $zero */
static $zero;

if ($zero) {
return $zero;
}

return $zero = new Period(0, 0, 0);
}

/**
Expand Down
9 changes: 8 additions & 1 deletion src/TimeZoneOffset.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,14 @@ public static function ofTotalSeconds(int $totalSeconds): TimeZoneOffset

public static function utc(): TimeZoneOffset
{
return new TimeZoneOffset(0);
/** @var TimeZoneOffset|null $utc */
static $utc;

if ($utc) {
return $utc;
}

return $utc = new TimeZoneOffset(0);
}

/**
Expand Down
5 changes: 4 additions & 1 deletion tests/DurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ class DurationTest extends AbstractTestCase
{
public function testZero(): void
{
$this->assertDurationIs(0, 0, Duration::zero());
$zero = Duration::zero();

$this->assertDurationIs(0, 0, $zero);
$this->assertSame($zero, Duration::zero());
}

/**
Expand Down
15 changes: 12 additions & 3 deletions tests/InstantTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ public function providerOf(): array

public function testEpoch(): void
{
$this->assertInstantIs(0, 0, Instant::epoch());
$epoch = Instant::epoch();

$this->assertInstantIs(0, 0, $epoch);
$this->assertSame($epoch, Instant::epoch());
}

public function testNow(): void
Expand All @@ -63,12 +66,18 @@ public function testNow(): void

public function testMin(): void
{
$this->assertInstantIs(PHP_INT_MIN, 0, Instant::min());
$min = Instant::min();

$this->assertInstantIs(PHP_INT_MIN, 0, $min);
$this->assertSame($min, Instant::min());
}

public function testMax(): void
{
$this->assertInstantIs(PHP_INT_MAX, 999999999, Instant::max());
$max = Instant::max();

$this->assertInstantIs(PHP_INT_MAX, 999999999, $max);
$this->assertSame($max, Instant::max());
}

/**
Expand Down
10 changes: 8 additions & 2 deletions tests/LocalDateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,18 @@ public function providerNow(): array

public function testMin(): void
{
$this->assertLocalDateIs(Year::MIN_VALUE, 1, 1, LocalDate::min());
$min = LocalDate::min();

$this->assertLocalDateIs(Year::MIN_VALUE, 1, 1, $min);
$this->assertSame($min, LocalDate::min());
}

public function testMax(): void
{
$this->assertLocalDateIs(Year::MAX_VALUE, 12, 31, LocalDate::max());
$max = LocalDate::max();

$this->assertLocalDateIs(Year::MAX_VALUE, 12, 31, $max);
$this->assertSame($max, LocalDate::max());
}

/**
Expand Down
10 changes: 8 additions & 2 deletions tests/LocalDateTimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,18 @@ public function providerParseInvalidDateTimeThrowsException(): array

public function testMin(): void
{
$this->assertLocalDateTimeIs(Year::MIN_VALUE, 1, 1, 0, 0, 0, 0, LocalDateTime::min());
$min = LocalDateTime::min();

$this->assertLocalDateTimeIs(Year::MIN_VALUE, 1, 1, 0, 0, 0, 0, $min);
$this->assertSame($min, LocalDateTime::min());
}

public function testMax(): void
{
$this->assertLocalDateTimeIs(Year::MAX_VALUE, 12, 31, 23, 59, 59, 999999999, LocalDateTime::max());
$max = LocalDateTime::max();

$this->assertLocalDateTimeIs(Year::MAX_VALUE, 12, 31, 23, 59, 59, 999999999, $max);
$this->assertSame($max, LocalDateTime::max());
}

public function testMinMaxOf(): void
Expand Down
20 changes: 16 additions & 4 deletions tests/LocalTimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,22 +188,34 @@ public function providerNow(): array

public function testMidnight(): void
{
$this->assertLocalTimeIs(0, 0, 0, 0, LocalTime::midnight());
$midnight = LocalTime::midnight();

$this->assertLocalTimeIs(0, 0, 0, 0, $midnight);
$this->assertSame($midnight, LocalTime::midnight());
}

public function testNoon(): void
{
$this->assertLocalTimeIs(12, 0, 0, 0, LocalTime::noon());
$noon = LocalTime::noon();

$this->assertLocalTimeIs(12, 0, 0, 0, $noon);
$this->assertSame($noon, LocalTime::noon());
}

public function testMin(): void
{
$this->assertLocalTimeIs(0, 0, 0, 0, LocalTime::min());
$min = LocalTime::min();

$this->assertLocalTimeIs(0, 0, 0, 0, $min);
$this->assertSame($min, LocalTime::min());
}

public function testMax(): void
{
$this->assertLocalTimeIs(23, 59, 59, 999999999, LocalTime::max());
$max = LocalTime::max();

$this->assertLocalTimeIs(23, 59, 59, 999999999, $max);
$this->assertSame($max, LocalTime::max());
}

/**
Expand Down
5 changes: 4 additions & 1 deletion tests/PeriodTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ public function testOfDays(): void

public function testZero(): void
{
$this->assertPeriodIs(0, 0, 0, Period::zero());
$zero = Period::zero();

$this->assertPeriodIs(0, 0, 0, $zero);
$this->assertSame($zero, Period::zero());
}

/**
Expand Down
5 changes: 4 additions & 1 deletion tests/TimeZoneOffsetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,10 @@ public function providerOfInvalidTotalSecondsThrowsException(): iterable

public function testUtc(): void
{
$this->assertTimeZoneOffsetIs(0, TimeZoneOffset::utc());
$utc = TimeZoneOffset::utc();

$this->assertTimeZoneOffsetIs(0, $utc);
$this->assertSame($utc, TimeZoneOffset::utc());
}

/**
Expand Down
5 changes: 4 additions & 1 deletion tests/TimeZoneTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ public function providerParseInvalidStringThrowsException(): array

public function testUtc(): void
{
$this->assertTimeZoneOffsetIs(0, TimeZone::utc());
$utc = TimeZone::utc();

$this->assertTimeZoneOffsetIs(0, $utc);
$this->assertSame($utc, TimeZone::utc());
}

public function testIsEqualTo(): void
Expand Down
Loading