Skip to content

Commit 6b41355

Browse files
authored
bpo-45116: Add the Py_ALWAYS_INLINE macro (GH-28390)
Add the Py_ALWAYS_INLINE macro to ask the compiler to always inline a static inline function. The compiler can ignore it and decides to not inline the function.
1 parent 064464f commit 6b41355

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

Doc/c-api/intro.rst

+19
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,25 @@ complete listing.
111111

112112
.. versionadded:: 3.3
113113

114+
.. c:macro:: Py_ALWAYS_INLINE
115+
116+
Ask the compiler to always inline a static inline function. The compiler can
117+
ignore it and decides to not inline the function.
118+
119+
It can be used to inline performance critical static inline functions when
120+
building Python in debug mode with function inlining disabled. For example,
121+
MSC disables function inlining when building in debug mode.
122+
123+
Marking blindly a static inline function with Py_ALWAYS_INLINE can result in
124+
worse performances (due to increased code size for example). The compiler is
125+
usually smarter than the developer for the cost/benefit analysis.
126+
127+
It must be specified before the function return type. Usage::
128+
129+
static inline Py_ALWAYS_INLINE int random(void) { return 4; }
130+
131+
.. versionadded:: 3.11
132+
114133
.. c:macro:: Py_CHARMASK(c)
115134
116135
Argument must be a character or an integer in the range [-128, 127] or [0,

Include/pyport.h

+22
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,28 @@ extern "C" {
557557
#define _Py_HOT_FUNCTION
558558
#endif
559559

560+
// Ask the compiler to always inline a static inline function. The compiler can
561+
// ignore it and decides to not inline the function.
562+
//
563+
// It can be used to inline performance critical static inline functions when
564+
// building Python in debug mode with function inlining disabled. For example,
565+
// MSC disables function inlining when building in debug mode.
566+
//
567+
// Marking blindly a static inline function with Py_ALWAYS_INLINE can result in
568+
// worse performances (due to increased code size for example). The compiler is
569+
// usually smarter than the developer for the cost/benefit analysis.
570+
//
571+
// It must be specified before the function return type. Usage:
572+
//
573+
// static inline Py_ALWAYS_INLINE int random(void) { return 4; }
574+
#if defined(__GNUC__) || defined(__clang__) || defined(__INTEL_COMPILER)
575+
# define Py_ALWAYS_INLINE __attribute__((always_inline))
576+
#elif defined(_MSC_VER)
577+
# define Py_ALWAYS_INLINE __forceinline
578+
#else
579+
# define Py_ALWAYS_INLINE
580+
#endif
581+
560582
// Py_NO_INLINE
561583
// Disable inlining on a function. For example, it reduces the C stack
562584
// consumption: useful on LTO+PGO builds which heavily inline code (see
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Add the :c:macro:`Py_ALWAYS_INLINE` macro to ask the compiler to always
2+
inline a static inline function. The compiler can ignore it and decides to
3+
not inline the function. Patch by Victor Stinner.

0 commit comments

Comments
 (0)