Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-45116: Add the Py_ALWAYS_INLINE macro #28390

Merged
merged 3 commits into from
Sep 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions Doc/c-api/intro.rst
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,25 @@ complete listing.

.. versionadded:: 3.3

.. c:macro:: Py_ALWAYS_INLINE

Ask the compiler to always inline a static inline function. The compiler can
ignore it and decides to not inline the function.

It can be used to inline performance critical static inline functions when
building Python in debug mode with function inlining disabled. For example,
MSC disables function inlining when building in debug mode.

Marking blindly a static inline function with Py_ALWAYS_INLINE can result in
worse performances (due to increased code size for example). The compiler is
usually smarter than the developer for the cost/benefit analysis.

It must be specified before the function return type. Usage::

static inline Py_ALWAYS_INLINE int random(void) { return 4; }

.. versionadded:: 3.11

.. c:macro:: Py_CHARMASK(c)

Argument must be a character or an integer in the range [-128, 127] or [0,
Expand Down
22 changes: 22 additions & 0 deletions Include/pyport.h
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,28 @@ extern "C" {
#define _Py_HOT_FUNCTION
#endif

// Ask the compiler to always inline a static inline function. The compiler can
// ignore it and decides to not inline the function.
//
// It can be used to inline performance critical static inline functions when
// building Python in debug mode with function inlining disabled. For example,
// MSC disables function inlining when building in debug mode.
//
// Marking blindly a static inline function with Py_ALWAYS_INLINE can result in
// worse performances (due to increased code size for example). The compiler is
// usually smarter than the developer for the cost/benefit analysis.
//
// It must be specified before the function return type. Usage:
//
// static inline Py_ALWAYS_INLINE int random(void) { return 4; }
#if defined(__GNUC__) || defined(__clang__) || defined(__INTEL_COMPILER)
# define Py_ALWAYS_INLINE __attribute__((always_inline))
#elif defined(_MSC_VER)
# define Py_ALWAYS_INLINE __forceinline
#else
# define Py_ALWAYS_INLINE
#endif

// Py_NO_INLINE
// Disable inlining on a function. For example, it reduces the C stack
// consumption: useful on LTO+PGO builds which heavily inline code (see
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Add the :c:macro:`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. Patch by Victor Stinner.