Skip to content

gh-88473: Implement fast path in date.today() for date types - #130980

Merged
pganssle merged 8 commits into
python:mainfrom
StanFromIreland:date_speedup
Sep 16, 2025
Merged

gh-88473: Implement fast path in date.today() for date types#130980
pganssle merged 8 commits into
python:mainfrom
StanFromIreland:date_speedup

Conversation

@StanFromIreland

@StanFromIrelandStanFromIreland commented Mar 8, 2025

Copy link
Copy Markdown
Member

Other suggested implementations are not as backward compatible.

$ ./python -m timeit -s 'from datetime import date' 'date.today()'
1000000 loops, best of 5: 271 nsec per loop
$ python3.14 -m timeit -s 'from datetime import date' 'date.today()'
200000 loops, best of 5: 1.36 usec per loop

~5x faster for date types

@picnixz

Copy link
Copy Markdown
Member

Are the benchmarks realised on a PGO build or a debug build? or maybe PGO+LTO? or just release build?

@StanFromIreland

Copy link
Copy Markdown
MemberAuthor

Both are just plain builds. python3.14 is from the morning but I doubt the few commits matter.

Comment threadModules/_datetimemodule.c
Comment threadModules/_datetimemodule.c
Comment threadModules/_datetimemodule.c Outdated
Comment threadModules/_datetimemodule.c Outdated
Comment threadModules/_datetimemodule.c Outdated
Comment threadModules/_datetimemodule.c Outdated
Comment threadMisc/NEWS.d/next/Library/2025-03-08-17-07-00.gh-issue-88473.qg23g8.rst Outdated
@picnixz

Copy link
Copy Markdown
Member

By plain builds, do you mean with or withou --with-pydebug? I assume without so it's probably release builds so it should be fine. But please the configure flags

@StanFromIreland

Copy link
Copy Markdown
MemberAuthor

@pganssle can you take a look at this?

Comment threadModules/_datetimemodule.c Outdated

@picnixzpicnixz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

On my part, it looks good but I'll let a datetime expert have the final decision.

Comment threadModules/_datetimemodule.c
Comment threadModules/_datetimemodule.c Outdated
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
@StanFromIreland

Copy link
Copy Markdown
MemberAuthor

Friendly ping @pganssle Any comments on this?

@StanFromIreland

Copy link
Copy Markdown
MemberAuthor

Friendly ping @pganssle :-)

@pgansslepganssle left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is an acceptable way to do it, but IIRC we already pushed some of this kind of dispatching logic in new_date_subclass_ex, so I think you can replace the entire contents of date_today with this:

staticPyObject*date_today(PyObject*cls, PyObject*Py_UNUSED(dummy))
{
/* Use C implementation to boost performance for date type */structtmtm;
time_tt;
time(&t);
if (_PyTime_localtime(t, &tm) !=0) {
returnNULL;
}
returnnew_date_subclass_ex(tm.tm_year+1900,
tm.tm_mon+1,
tm.tm_mday,
(PyObject*)cls);
}

And you'll get the same speedup and as a bonus datetime.today() will also get the same speedup.

@bedevere-app

Copy link
Copy Markdown

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

# Conflicts:
#	Modules/_datetimemodule.c
@StanFromIreland

Copy link
Copy Markdown
MemberAuthor

And you'll get the same speedup and as a bonus datetime.today() will also get the same speedup.

It will be incorrect though? Since datetime.today has hours, minutes etc.

@pganssle

Copy link
Copy Markdown
Member

It will be incorrect though? Since datetime.today has hours, minutes etc.

Hah, oops, right, right. Should have run the test suite and not just the benchmark.

OK, let's go with this as it is and if we want to speed up datetime.today we can add an implementation for datetime.datetime that is just effectively return datetime.now(tz=None).

@pganssle
pganssle merged commit fc3e22a into python:mainSep 16, 2025
48 of 49 checks passed
Sign up for freeto 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.

3 participants

@StanFromIreland@picnixz@pganssle