bpo-45116: Add the Py_ALWAYS_INLINE macro (GH-28390) · python/cpython@6b41355 · GitHub
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

3 files changed

Lines changed: 44 additions & 0 deletions

File tree

Doc/c-api/intro.rst

Lines changed: 19 additions & 0 deletions

Include/pyport.h

Lines changed: 22 additions & 0 deletions
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
Lines changed: 3 additions & 0 deletions

0 commit comments

Comments
 (0)