From d335b11706c9f276d159ad70e9372ff1fe4833c3 Mon Sep 17 00:00:00 2001 From: Chris Eibl <138194463+chris-eibl@users.noreply.github.com> Date: Thu, 13 Mar 2025 20:13:29 +0100 Subject: [PATCH 1/4] use the PYLONG_FROM_*INT macros in more PyLong_From* functions --- Objects/longobject.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/Objects/longobject.c b/Objects/longobject.c index 51ecbd9e0ee0bd..d6aa7c1bdcd1d9 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -6741,17 +6741,21 @@ PyUnstable_Long_CompactValue(const PyLongObject* op) { } -PyObject* PyLong_FromInt32(int32_t value) -{ return PyLong_FromNativeBytes(&value, sizeof(value), -1); } +PyObject* PyLong_FromInt32(int32_t value) { + PYLONG_FROM_INT(uint32_t, int32_t, value); +} -PyObject* PyLong_FromUInt32(uint32_t value) -{ return PyLong_FromUnsignedNativeBytes(&value, sizeof(value), -1); } +PyObject* PyLong_FromUInt32(uint32_t value) { + PYLONG_FROM_UINT(uint32_t, value); +} -PyObject* PyLong_FromInt64(int64_t value) -{ return PyLong_FromNativeBytes(&value, sizeof(value), -1); } +PyObject* PyLong_FromInt64(int64_t value) { + PYLONG_FROM_INT(uint64_t, int64_t, value); +} -PyObject* PyLong_FromUInt64(uint64_t value) -{ return PyLong_FromUnsignedNativeBytes(&value, sizeof(value), -1); } +PyObject* PyLong_FromUInt64(uint64_t value) { + PYLONG_FROM_UINT(uint64_t, value); +} #define LONG_TO_INT(obj, value, type_name) \ do { \ From 166ee3915bbc49fd4bb6507556d3c995d3aba12c Mon Sep 17 00:00:00 2001 From: Chris Eibl <138194463+chris-eibl@users.noreply.github.com> Date: Thu, 13 Mar 2025 20:23:34 +0100 Subject: [PATCH 2/4] blurb it --- .../2025-03-13-20-23-02.gh-issue-129149.z42wkm.rst | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2025-03-13-20-23-02.gh-issue-129149.z42wkm.rst diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-03-13-20-23-02.gh-issue-129149.z42wkm.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-03-13-20-23-02.gh-issue-129149.z42wkm.rst new file mode 100644 index 00000000000000..707dcdf395c81a --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2025-03-13-20-23-02.gh-issue-129149.z42wkm.rst @@ -0,0 +1,3 @@ +Add fast path for medium-size integers in :c:func:`PyLong_FromInt32`, +:c:func:`PyLong_FromUInt32`, :c:func:`PyLong_FromInt64` and +:c:func:`PyLong_FromUInt64`. Patch by Chris Eibl. From 3305d9bba18e025b8b8a00b335e3d88d9a420396 Mon Sep 17 00:00:00 2001 From: Chris Eibl <138194463+chris-eibl@users.noreply.github.com> Date: Thu, 13 Mar 2025 21:24:04 +0100 Subject: [PATCH 3/4] small ints have a fast path, too --- .../2025-03-13-20-23-02.gh-issue-129149.z42wkm.rst | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-03-13-20-23-02.gh-issue-129149.z42wkm.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-03-13-20-23-02.gh-issue-129149.z42wkm.rst index 707dcdf395c81a..9da77312cbb22d 100644 --- a/Misc/NEWS.d/next/Core_and_Builtins/2025-03-13-20-23-02.gh-issue-129149.z42wkm.rst +++ b/Misc/NEWS.d/next/Core_and_Builtins/2025-03-13-20-23-02.gh-issue-129149.z42wkm.rst @@ -1,3 +1,4 @@ -Add fast path for medium-size integers in :c:func:`PyLong_FromInt32`, -:c:func:`PyLong_FromUInt32`, :c:func:`PyLong_FromInt64` and +Add fast path for small and medium-size integers in +:c:func:`PyLong_FromInt32`, :c:func:`PyLong_FromUInt32`, +:c:func:`PyLong_FromInt64` and :c:func:`PyLong_FromUInt64`. Patch by Chris Eibl. From 9947a4ecca8cbcf31d7f8ce6e5771ddd0eb30b89 Mon Sep 17 00:00:00 2001 From: Chris Eibl <138194463+chris-eibl@users.noreply.github.com> Date: Thu, 13 Mar 2025 21:56:33 +0100 Subject: [PATCH 4/4] PEP-7 --- Objects/longobject.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/Objects/longobject.c b/Objects/longobject.c index d6aa7c1bdcd1d9..1c2f7f389366cf 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -6741,19 +6741,23 @@ PyUnstable_Long_CompactValue(const PyLongObject* op) { } -PyObject* PyLong_FromInt32(int32_t value) { +PyObject* PyLong_FromInt32(int32_t value) +{ PYLONG_FROM_INT(uint32_t, int32_t, value); } -PyObject* PyLong_FromUInt32(uint32_t value) { +PyObject* PyLong_FromUInt32(uint32_t value) +{ PYLONG_FROM_UINT(uint32_t, value); } -PyObject* PyLong_FromInt64(int64_t value) { +PyObject* PyLong_FromInt64(int64_t value) +{ PYLONG_FROM_INT(uint64_t, int64_t, value); } -PyObject* PyLong_FromUInt64(uint64_t value) { +PyObject* PyLong_FromUInt64(uint64_t value) +{ PYLONG_FROM_UINT(uint64_t, value); }