Skip to content

date: Skip timelib_strtotime call in date() for "now" - #25702

Open
adapik wants to merge 1 commit into
php:masterfrom
adapik:date-now-skip-parse
Open

adapik wants to merge 1 commit into
php:masterfrom
adapik:date-now-skip-parse

Conversation

@adapik

@adapik adapik commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Follow-up to b4e9b18, php_date_initialize() already has a shortcut for "now": it builds the current time directly and returns. But it only takes that shortcut after timelib_strtotime() has already parsed the string, and the parsed result is thrown away. So new DateTime(), new DateTimeImmutable(), date_create() and date_create_immutable(), with no argument or with "now", spend time scanning a string and allocating a timelib_time that nobody uses.

This change checks for "now" before parsing and skips timelib_strtotime() in that case. Everything else is unchanged.

Benchmark

Release build, ns per call:

Call master patched Change
new DateTime() 135.7 92.6 −32%
date_create() 131.1 87.1 −34%

@ArtUkrainskiy

Copy link
Copy Markdown
Contributor

I measured this merged onto master 5ddcc35 on a Ryzen 9 5950X. GCC 15 and clang 21, flags of the official Docker image. Output matches master on 42k calls around "now" on GCC, clang and ASan. The ext/date tests give the same result as on master.

Default zone UTC, negative = fewer cycles (faster):

call master, CPU cycles per call (GCC 15) GCC 15 clang 21
new DateTime() 806 −35% −33%
date_create() 773 −37% −33%
date_create_immutable() 775 −38% −33%
new DateTime('now', new DateTimeZone('Europe/Madrid')) 1,071 −22% −22%
new DateTime('now', new DateTimeZone('+05:30')) 546 −44% −40%
(new DateTimeImmutable())->format('Y-m-d H:i:s') 1,893 −16% −19%
new DateTimeImmutable('2026-11-18 12:34:56') 2,280 0% ~ 0% ~
date_create('NOW') 1,440 −4% layout? +1%
date_create('garbage') 1,025 −4% +6% layout?
DateTimeImmutable::createFromFormat(...) 2,624 +4% layout? 0%
date('Y-m-d', $ts) 867 +2% −2%

~ is within the noise of master against master. layout? means the cycles changed and the instructions didn't.

With America/New_York as the default zone it's 21–26% for the calls without an argument. Other strings run the same instructions as master.

The title says date(), but date() doesn't reach php_date_initialize(). Since it becomes the commit subject, maybe "ext/date: Skip timelib_strtotime() for "now""? date_create_immutable() gets faster too and could go in NEWS and UPGRADING.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants