Skip to content

datetime: cache module lookups (2x faster) #148085

Description

@maurycy

Feature or enhancement

Proposal:

gh-148072 got me looking for more places like this.

It's not that frequent: asyncio, sqlite etc. do it, but it turns out that datetime does not:

https://github.com/python/cpython/blob/dea4083aa95/Modules/_datetimemodule.c#L2081

https://github.com/python/cpython/blob/dea4083aa95/Modules/_datetimemodule.c#L1882

https://github.com/python/cpython/blob/dea4083aa95/Modules/_datetimemodule.c#L2063

Benchmark

I've got a branch ready with a very simple caching in datetime_state:

| Benchmark          | main-c50d6cd | datetime-cache-time-module-lookups-a49f862 |
|--------------------|:------------:|:------------------------------------------:|
| datetime.timetuple | 260 ns       | 126 ns: 2.07x faster                       |
| datetime.strftime  | 784 ns       | 509 ns: 1.54x faster                       |
| date.timetuple     | 258 ns       | 124 ns: 2.08x faster                       |
| Geometric mean     | (ref)        | 1.88x faster                               |

Where:

import pyperf, datetime

r = pyperf.Runner()
dt = datetime.datetime(2026, 4, 4, 12, 30, 45)
d = datetime.date(2026, 4, 4)

r.bench_func("datetime.timetuple", dt.timetuple)
r.bench_func("datetime.strftime", dt.strftime, "%Y-%m-%d")
r.bench_func("date.timetuple", d.timetuple)

Where:

2026-04-04T14:17:02.794335000+0200 maurycy@gimel /Users/maurycy/work/cpython-main (main c50d6cd) % ./python.exe -c "import sysconfig; print(sysconfig.get_config_var('CONFIG_ARGS'))"
'--enable-optimizations' '--with-lto'
[127] 2026-04-04T14:17:24.011667000+0200 maurycy@gimel /Users/maurycy/src/github.com/maurycy/cpython (datetime-cache-time-module-lookups a49f862) % ./python.exe -c "import sysconfig; print(sysconfig.get_config_var('CONFIG_ARGS'))"
'--enable-optimizations' '--with-lto'

This is macOS@M4.

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

This was already discussed:

#71587
#120224

However, from what I see, the problem with Py_Finalize() and Py_Initialize() was just a static variable, and since #119810 it's now per interprerter:

https://github.com/python/cpython/pull/119810/changes#diff-3ee250e3806e884518fd872e9148baf532de6ec54c1cdb4e7679fbb2869d9c47R114

Linked PRs

Activity

  1. changed the title [-]`datetime`: cache time module lookups[/-] [+]`datetime`: cache module lookups[/+] on Apr 4, 2026
  2. changed the title [-]`datetime`: cache module lookups[/-] [+]`datetime`: cache module lookups (2x faster)[/+] on Apr 4, 2026
  3. added a commit that references this issue on Aug 29, 2026
  4. added a commit that references this issue on Sep 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    extension-modulesC modules in the Modules dirperformancePerformance or resource usagetype-featureA feature request or enhancement

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions