Skip to content

gh-162350: Add AC vectorcall for datetime.timedelta / datetime.timezone - #162500

Open
tanloong wants to merge 2 commits into
python:mainfrom
tanloong:datetime_vectorcall
Open

tanloong wants to merge 2 commits into
python:mainfrom
tanloong:datetime_vectorcall

Conversation

@tanloong

@tanloong tanloong commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Add @vectorcall to datetime.timedelta.__new__ and datetime.timezone.__new__. There is no change of exception text for bad arguments.

Benchmark:

+------------------------------+--------+----------------------+
| Benchmark                    | main   | patch                |
+==============================+========+======================+
| timedelta(days=1)            | 457 ns | 364 ns: 1.26x faster |
+------------------------------+--------+----------------------+
| timezone(timedelta(hours=1)) | 645 ns | 462 ns: 1.40x faster |
+------------------------------+--------+----------------------+
| Geometric mean               | (ref)  | 1.21x faster         |
+------------------------------+--------+----------------------+

Benchmark hidden because not significant (1): timedelta(1, 2, 3)
Details
import pyperf

SETUP = """
import datetime
from datetime import timedelta, timezone
"""

CASES = [
    ('timedelta(days=1)', 'timedelta(days=1)'),
    ('timedelta(1, 2, 3)', 'timedelta(1, 2, 3)'),
    ('timezone(timedelta(hours=1))', 'timezone(timedelta(hours=1))')
]

runner = pyperf.Runner()
for name, stmt in CASES:
    runner.timeit(name, stmt=stmt, setup=SETUP)

Comment thread Modules/_datetimemodule.c
0, /* tp_alloc */
delta_new, /* tp_new */
.tp_vectorcall = delta_vectorcall,
0, /* tp_free */

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 no longer tp_free, move it before tp_vectorcall.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Right. Moved tp_vectorcall to the end.

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.

3 participants