Skip to content

Free-threading: performance regression in concurrent type slot updates #162752

Todo

Description

@hugovk

Bug report

Issue #158195 / PR #159972 "Do not hold the type lock during stop-the-world" (backported to 3.14) has caused a performance regression in concurrent type slot updates on the free-threaded build, biggest regression on Windows, medium on macOS and smaller on Ubuntu.

On Windows CI, test_opcache is now often taking more than 10 minutes and needing a slow re-run, contributing to one CI job going from ~28 mins to ~38 mins and becoming the bottleneck. In the last 30 main runs, test_opcache was re-run in 19 arm64 and 15 x64 Windows free-threading debug jobs.

Median test_opcache time on main GitHub Actions over 12 runs before and 12 after the PR was merged:

Job Before After
Windows free-threading, arm64, switch-case under 30 s 10 min (timeout)
Windows free-threading, x64, switch-case under 30 s 7.9 min
Windows free-threading, x64, tail-call under 30 s 3.6 min
macOS free-threading under 30 s 5.0 min
Ubuntu free-threading, x64 under 30 s 66 s
Ubuntu free-threading, arm under 30 s 42 s
TSan free-threading 51 s 78 s
All non-free-threading Windows jobs under 30 s under 30 s
  • Same pattern on 3.15 and 3.14.
  • Free-threading only: the default (GIL) builds are unaffected.
  • Tail-call too: the x64 tail-call job went from under 30 seconds to 3.6 minutes. It doesn't time out because those are release builds with more headroom.
  • Windows arm64 tail-call: not shown here, those jobs were only added recently.

The extra time is in TestRacesDoNotCrash.test_load_attr_getattribute_overridden and test_binary_subscr_getitem. Locally on macOS the whole file went from 1.1 s to 26.9 s (debug build).

Benchmark to repro

Here's a minimal benchmark derived from test_opcache:

"""Time threads assigning a special method on a class (a type slot update)."""
import sys
import threading
import time

THREADS = int(sys.argv[1]) if len(sys.argv) > 1 else 4
N = int(sys.argv[2]) if len(sys.argv) > 2 else 2000


class C:
    __getitem__ = lambda self, item: None


def write():
    for _ in range(N):
        C.__getitem__ = lambda self, item: None


threads = [threading.Thread(target=write) for _ in range(THREADS)]
start = time.perf_counter()
for t in threads:
    t.start()
for t in threads:
    t.join()
elapsed = time.perf_counter() - start
print(f"{THREADS} threads x {N} assignments: {elapsed:.2f}s "
      f"({elapsed / (THREADS * N) * 1e6:.0f} us each)")

Running a free-threaded (debug) build locally on macOS:

  • Before, 2ef3728: 4 threads x 2000 assignments: 0.01s (1 us each)
  • After, b89f1c9: 4 threads x 2000 assignments: 2.31s (289 us each)

CPython versions tested on:

CPython main branch, 3.16, 3.15, 3.14

Operating systems tested on:

Windows, macOS, Linux

Linked PRs

Activity

  1. added
    type-bug
    performance
    on Nov 27, 2026
  2. added
    interpreter-core
    on Nov 27, 2026
  3. self-assigned this
    on Nov 27, 2026
  4. nascheme commented on Nov 27, 2026

    @nascheme
    Member

    This slowdown is caused by the combination of two things: after GH-159972, we do the STW for every slot assignment, even if it's not needed. Second, GH-162764 means that threads waiting on the STW mutex can wait an extra 1 ms. We can (and should) fix both of these problems, I think.

  5. added 3 commits that reference this issue on Nov 29, 2026
  6. added 3 commits that reference this issue on Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

interpreter-coreperformancetopic-free-threadingtype-bug

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions