[php-src] PR #25738: ext/date: Look up the time zone offset in date_format() only when used
| From: | ArtUkrainskiy | Date: | Wed, 25 Nov 2026 13:12:40 +0000 |
| Subject: | [php-src] PR #25738: ext/date: Look up the time zone offset in date_format() only when used | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-40995@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/25738
Author: ArtUkrainskiy
date() and DateTimeInterface::format() build the time zone offset of a
local time before they read the format. That allocates a timelib_time_offset and an
abbreviation string. For a time zone identifier it also searches the transitions. Most formats never
use it. Only I O P p T Z c r do, and e for a zone given as an offset or
abbreviation.
Now the offset is built once, when the format first reaches one of these characters. A format
without them doesn't build it at all. gmdate() didn't build it before and
still doesn't.
AMD Ryzen 9 5950X, negative = fewer cycles (faster):
| call | master, CPU cycles per call (GCC 15.2) | GCC 15.2 | clang 21.1 |
|---|--:|--:|--:|
| format('Y'), UTC | 416 | −42% | −26% |
| format('Y'), Europe/Madrid | 746 | −68% | −63% |
| format('Y-m-d H:i:s'), UTC | 1,039 | −17% | −9% |
| format('Y-m-d H:i:s'), Europe/Madrid | 1,258 | −39% | −34% |
| format('Y-m-d H:i:s'), +05:30 | 1,080 | −20% | −18% |
| format('Y-m-d H:i:s'), EST | 985 | −12% | −8% |
| format('Y-m-d\TH:i:s.u'), America/New_York | 1,398 | −34% | −29% |
| date('Y-m-d'), UTC | 866 | −17% | −10% |
| date('Y-m-d H:i:s'), UTC | 1,277 | −12% | −5% |
| format('c'), UTC | 812 | +1% | +8% layout? |
| format('c'), Europe/Madrid | 1,125 | +2% | +5% layout? |
| format('c'), +05:30 | 832 | +6% layout? | +12% layout? |
| format('Y-m-d\TH:i:sP'), Europe/Madrid | 1,468 | −1% | +7% layout? |
| format('D, d M Y H:i:s O'), +05:30 | 1,390 | 0% ~ | +6% layout? |
| date(DATE_ATOM), UTC | 1,433 | −2% | +3% |
~ is within the noise of master against master. layout? means the cycles changed and the
instructions didn't.
On GCC that's about 500 cycles saved per call on Europe/Madrid and 150–185 on UTC, whatever
the format. json_encode() and serialize() of date objects gain too, since
they format each date with 'x-m-d H:i:s.u'. For 100 dates in Europe/Madrid
that's 18–23% fewer cycles.
Formats that use the offset, and gmdate(), run 0–1% more instructions than on master.
Their cycles move by −7% to +12%, and the slower rows change when I add -falign-functions=64
-falign-loops=32. gmdate('Y-m-d H:i:s') runs none of the new code and
still moves from +8% to −2% on clang. So I read these rows as code placement, but I haven't
found the exact cause.
The output is byte-identical to master on over 50,000 calls. They cover every format character,
escapes and random formats. The dates use time zone identifiers, offsets in minutes and seconds, and
abbreviations, with years from PHP_INT_MIN to PHP_INT_MAX. I checked that
on GCC, clang, ZTS and ASan + UBSan. The ext/date tests pass on all of them. On ASan,
ExtendDateTime.phpt fails on master too. The new test prints the offset characters
escaped and not, on every zone type and with gmdate(). I haven't measured on Intel
or tested on Windows.
#20758 replaces the slprintf() calls in the same function. This PR doesn't touch
them.
Full numbers and scripts: https://github.com/ArtUkrainskiy/php-src-bench/tree/main/reports/date-format-lazy-offset