Skip to content

performance regression in PyType_GetModuleByDef on free-threaded build #148907

@kumaraditya303

Description

@kumaraditya303

Bug report

On the free-threaded build, thread_local_read in Tools/ftscalingbench/ftscalingbench.py regressed sharply between 3.15.0a8 and main. git bisect between v3.15.0a8 and 04fd103713a identifies 8923ca4 as the first bad commit.

Measurements (10 threads, Apple Silicon, free-threaded)

Python thread_local_read
3.14.4t 3.6× faster
3.15.0a8 3.7× faster
parent of 8923ca4 3.7× faster
8923ca4 11.9× slower
main @ 04fd103 10.8× slower

Root cause

The commit changed PyType_GetModuleByDef (Objects/typeobject.c:5892) which added a incref/decref which causes significant ref counting contention.

// before: genuinely borrowed, no refcount ops
return borrow_module_by_token(type, def);

// after: Py_NewRef inside PyType_GetModuleByToken, then immediate Py_XDECREF
PyObject *mod = PyType_GetModuleByToken(type, def);
Py_XDECREF(mod);
return mod;

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions