[New-bugs-announce] [issue45094] Consider using __forceinline and __attribute__((always_inline)) for debug builds

STINNER Victor report at bugs.python.org
Fri Sep 3 09:26:40 EDT 2021

New submission from STINNER Victor <vstinner at python.org>:

I converted many C macros to static inline functions to reduce the risk of programming bugs (macros pitfalls), limit variable scopes to the function, have a more readable function, and benefit of the function name even when it's inlined in debuggers and profilers.

When Py_INCREF() macro was converted to a static inline function, using __attribute__((always_inline)) was considered, but the idea was rejected. See bpo-35059.

I'm now trying to convert the Py_TYPE() macro to a static inline function. The problem is that by default, MSC disables inlining and test_exceptions does crash with a stack overflow, since my PR 28128 increases the usage of the stack memory: see bpo-44348.

For the specific case of CPython built by MSC, we can increase the stack size, or change compiler optimizations to enable inlining. But the problem is wider than just CPython built by MSC in debug mode. Third party C extensions built by distutils may get the same issue. Building CPython on other platforms on debug mode with all compiler optimizations disabled (ex: gcc -O0) can also have the same issue.

I propose to reconsider the usage __forceinline (MSC) and __attribute__((always_inline)) (GCC, clang) on the most important static inline functions, like Py_INCREF() and Py_TYPE(), to avoid this issue.

components: Interpreter Core
messages: 400990
nosy: vstinner
priority: normal
severity: normal
status: open
title: Consider using __forceinline and __attribute__((always_inline)) for debug builds
versions: Python 3.11

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list