Skip to content

Very poor scaling with many glibc/libc datetime/time library functions in free-threading build #163178

Backlog

Description

@johng

When doing datetime or time operations which calls C libc, or glibc there is a very poor or even negative scaling on free threading builds.

For example we have

N = 500_000

def work():
    for _ in range(N):
        datetime.now(timezone.utc)

for nthreads in (1, 4):
    threads = [threading.Thread(target=work) for _ in range(nthreads)]
    t0 = time.perf_counter()
    for t in threads: t.start()
    for t in threads: t.join()
    elapsed = time.perf_counter() - t0
    print(f"{nthreads} thread(s): {nthreads * N:,} calls in {elapsed:.2f}s")

Output on this Mac, stock main:

free-threaded
1 thread(s): 500,000 calls in 0.05s
4 thread(s): 2,000,000 calls in 0.62s

GIL
1 thread(s): 500,000 calls in 0.05s
4 thread(s): 2,000,000 calls in 0.20s

We can see 4 threads on FT performs worse than the GIL build. For this particular example gmtime_r() is called for the initial conversion of the UNIX timestamp into the date and time fields.

For example replacing this with a custom implementation in C results in much better scaling. For this particular case it seems replacing the library looks straight forward if I'm not missing anything.

We instead get 4 thread(s): 2,000,000 calls in 0.04s.

However there are many other C libraries that are used for other datetime operations which seem to collapse with multi threads in FT build. Not all these operations are likely to be common on a hot path however adding a full list for completeness.

C function What it does Python APIs that call it (macOS and Linux) macOS libc glibc Python call measured macOS FT macOS GIL Linux FT Linux GIL
gmtime_r Seconds → UTC date and time time.gmtime(); datetime.now(tz), datetime.fromtimestamp(t, tz), utcnow(), utcfromtimestamp(); indirectly email.utils.formatdate(), logging with converter=time.gmtime 0.19–0.24x 0.18–0.21x time.gmtime(t) 0.42x 1.03x 1.90x 1.04x
localtime_r Seconds → local date and time (time-zone database) time.localtime(), time.ctime(), time.asctime() / time.strftime() with no time tuple; datetime.now(), datetime.fromtimestamp(t), astimezone(), naive .timestamp(); logging.Formatter timestamps 0.23–0.28x 0.20–0.26x time.localtime(t) 0.49x 0.98x 1.11x 1.06x
mktime Local date and time → seconds time.mktime() 2.3–2.4x (slow: 0.7M/s on 1 thread) 0.22–0.23x time.mktime(tuple) 2.39x 0.99x 0.57x 0.99x
wcsftime Format a date as text time.strftime(), and through it datetime/date/time.strftime() and logging's %(asctime)s 0.42x 3.9x time.strftime(fmt, tuple) 0.69x 1.01x 1.01x 1.01x
strftime Same, 8-bit strings time.strftime() only where wcsftime is missing (OpenBSD) 0.31–0.36x 3.7–3.8x not used on macOS/Linux — — — —
localeconv Locale number formatting (decimal point, grouping) locale.localeconv(), format(x, 'n') / f"{x:n}", locale.format_string(), locale.str(), locale.currency() 0.18x 0.17–0.18x format(1234.5, 'n') 2.55x 0.98x 2.84x 1.02x
setlocale (query) Read the current locale name locale.getlocale(), locale.setlocale(cat); locale.localeconv() (on macOS also sets LC_CTYPE every call) 3.8–3.9x 1.7–4.0x locale.setlocale(LC_CTYPE) 3.76x 1.02x 4.13x 0.97x
timegm UTC date and time → seconds Not used on macOS or Linux (only where struct tm lacks tm_zone) 0.16x 0.28–0.38x not used on macOS/Linux — — — —

Activity

  1. added
    performance
    extension-modules
    on Dec 5, 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-modulesperformancetopic-free-threading

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions