[Python-checkins] bpo-45116: Py_DEBUG ignores Py_ALWAYS_INLINE (GH-28419)
vstinner
webhook-mailer at python.org
Fri Sep 17 16:46:49 EDT 2021
https://github.com/python/cpython/commit/e4044e9f893350b4623677c048d33414a77edf55
commit: e4044e9f893350b4623677c048d33414a77edf55
branch: main
author: Victor Stinner <vstinner at python.org>
committer: vstinner <vstinner at python.org>
date: 2021-09-17T22:46:38+02:00
summary:
bpo-45116: Py_DEBUG ignores Py_ALWAYS_INLINE (GH-28419)
If the Py_DEBUG macro is defined, the Py_ALWAYS_INLINE macro does
nothing.
files:
M Doc/c-api/intro.rst
M Include/pyport.h
diff --git a/Doc/c-api/intro.rst b/Doc/c-api/intro.rst
index aac28b1e77561..3e7890cb76647 100644
--- a/Doc/c-api/intro.rst
+++ b/Doc/c-api/intro.rst
@@ -124,6 +124,9 @@ complete listing.
worse performances (due to increased code size for example). The compiler is
usually smarter than the developer for the cost/benefit analysis.
+ If Python is :ref:`built in debug mode <debug-build>` (if the ``Py_DEBUG``
+ macro is defined), the :c:macro:`Py_ALWAYS_INLINE` macro does nothing.
+
It must be specified before the function return type. Usage::
static inline Py_ALWAYS_INLINE int random(void) { return 4; }
diff --git a/Include/pyport.h b/Include/pyport.h
index af7fbe7fc7195..d27d0838f1b2c 100644
--- a/Include/pyport.h
+++ b/Include/pyport.h
@@ -568,10 +568,19 @@ extern "C" {
// worse performances (due to increased code size for example). The compiler is
// usually smarter than the developer for the cost/benefit analysis.
//
+// If Python is built in debug mode (if the Py_DEBUG macro is defined), the
+// Py_ALWAYS_INLINE macro does nothing.
+//
// 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)
+#if defined(Py_DEBUG)
+ // If Python is built in debug mode, usually compiler optimizations are
+ // disabled. In this case, Py_ALWAYS_INLINE can increase a lot the stack
+ // memory usage. For example, forcing inlining using gcc -O0 increases the
+ // stack usage from 6 KB to 15 KB per Python function call.
+# define Py_ALWAYS_INLINE
+#elif defined(__GNUC__) || defined(__clang__) || defined(__INTEL_COMPILER)
# define Py_ALWAYS_INLINE __attribute__((always_inline))
#elif defined(_MSC_VER)
# define Py_ALWAYS_INLINE __forceinline
More information about the Python-checkins
mailing list