From 6e47adf9e4a595593403534a68699b45f6937f4d Mon Sep 17 00:00:00 2001 From: Greg Price Date: Thu, 1 Aug 2019 15:45:57 -0700 Subject: [PATCH 1/2] bpo-37812: Make the small-int alloc optimization unconditional. This optimization was added in 1993 (commit 842d2ccdc), and we've been running with it ever since. I think at this point it's fair to say that it's no longer experimental. That lets us simplify the code by removing a number of `#ifdef`s. In particular this will help us simplify the usage of CHECK_SMALL_INT. --- .../2019-08-10-14-15-48.bpo-37812.jkh0Wp.rst | 6 ++++++ Objects/longobject.c | 11 ++--------- 2 files changed, 8 insertions(+), 9 deletions(-) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2019-08-10-14-15-48.bpo-37812.jkh0Wp.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2019-08-10-14-15-48.bpo-37812.jkh0Wp.rst b/Misc/NEWS.d/next/Core and Builtins/2019-08-10-14-15-48.bpo-37812.jkh0Wp.rst new file mode 100644 index 00000000000000..a3b2238f4e2ae2 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2019-08-10-14-15-48.bpo-37812.jkh0Wp.rst @@ -0,0 +1,6 @@ +The "small int" optimization is now unconditionally enabled. This +optimization was introduced in Python 1.0.1, and means that :class:`int` +values from -5 to 256 are pre-initialized rather than constructed on demand. +The option was always enabled unless certain never-documented preprocessor +macros were defined to 0 at build time; now it is always enabled, +simplifying the logic. Patch by Greg Price. diff --git a/Objects/longobject.c b/Objects/longobject.c index 3978f5c4a16730..930810e0d93131 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -34,7 +34,6 @@ _Py_IDENTIFIER(big); PyObject *_PyLong_Zero = NULL; PyObject *_PyLong_One = NULL; -#if NSMALLNEGINTS + NSMALLPOSINTS > 0 /* Small integers are preallocated in this array so that they can be shared. The integers that are preallocated are those in the range @@ -77,10 +76,6 @@ maybe_small_long(PyLongObject *v) } return v; } -#else -#define CHECK_SMALL_INT(ival) -#define maybe_small_long(val) (val) -#endif /* If a freshly-allocated int is already shared, it must be a small integer, so negating it must go to PyLong_FromLong */ @@ -5808,7 +5803,6 @@ PyLong_GetInfo(void) int _PyLong_Init(void) { -#if NSMALLNEGINTS + NSMALLPOSINTS > 0 int ival, size; PyLongObject *v = small_ints; @@ -5836,7 +5830,7 @@ _PyLong_Init(void) Py_SIZE(v) = size; v->ob_digit[0] = (digit)abs(ival); } -#endif + _PyLong_Zero = PyLong_FromLong(0); if (_PyLong_Zero == NULL) return 0; @@ -5862,12 +5856,11 @@ PyLong_Fini(void) reinitializations will fail. */ Py_CLEAR(_PyLong_One); Py_CLEAR(_PyLong_Zero); -#if NSMALLNEGINTS + NSMALLPOSINTS > 0 + int i; PyLongObject *v = small_ints; for (i = 0; i < NSMALLNEGINTS + NSMALLPOSINTS; i++, v++) { _Py_DEC_REFTOTAL; _Py_ForgetReference((PyObject*)v); } -#endif } From ee5bcc784041b1b7a85a4a5d2bea763ef3b9ea37 Mon Sep 17 00:00:00 2001 From: Greg Price Date: Sat, 10 Aug 2019 17:00:06 -0700 Subject: [PATCH 2/2] tweak NEWS wording --- .../2019-08-10-14-15-48.bpo-37812.jkh0Wp.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Misc/NEWS.d/next/Core and Builtins/2019-08-10-14-15-48.bpo-37812.jkh0Wp.rst b/Misc/NEWS.d/next/Core and Builtins/2019-08-10-14-15-48.bpo-37812.jkh0Wp.rst index a3b2238f4e2ae2..8a9def3ebccf6f 100644 --- a/Misc/NEWS.d/next/Core and Builtins/2019-08-10-14-15-48.bpo-37812.jkh0Wp.rst +++ b/Misc/NEWS.d/next/Core and Builtins/2019-08-10-14-15-48.bpo-37812.jkh0Wp.rst @@ -1,6 +1,6 @@ The "small int" optimization is now unconditionally enabled. This optimization was introduced in Python 1.0.1, and means that :class:`int` -values from -5 to 256 are pre-initialized rather than constructed on demand. -The option was always enabled unless certain never-documented preprocessor -macros were defined to 0 at build time; now it is always enabled, -simplifying the logic. Patch by Greg Price. +values from -5 through 256 are pre-initialized rather than constructed on +demand. The option was always enabled unless certain never-documented +preprocessor macros were defined to 0 at build time; now it is always +enabled, simplifying the logic. Patch by Greg Price.