Skip to content

Commit a175d64

Browse files
authored
GH-129149: Add fast path for medium-sized integers in PyLong_From* functions (#131211)
Add a fast path for medium-sized integers in `PyLong_FromInt{32,64}` and `PyLong_FromUInt{32,64}`.
1 parent 425f60b commit a175d64

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Add fast path for small and medium-size integers in
2+
:c:func:`PyLong_FromInt32`, :c:func:`PyLong_FromUInt32`,
3+
:c:func:`PyLong_FromInt64` and
4+
:c:func:`PyLong_FromUInt64`. Patch by Chris Eibl.

Objects/longobject.c

+12-4
Original file line numberDiff line numberDiff line change
@@ -6749,16 +6749,24 @@ PyUnstable_Long_CompactValue(const PyLongObject* op) {
67496749

67506750

67516751
PyObject* PyLong_FromInt32(int32_t value)
6752-
{ return PyLong_FromNativeBytes(&value, sizeof(value), -1); }
6752+
{
6753+
PYLONG_FROM_INT(uint32_t, int32_t, value);
6754+
}
67536755

67546756
PyObject* PyLong_FromUInt32(uint32_t value)
6755-
{ return PyLong_FromUnsignedNativeBytes(&value, sizeof(value), -1); }
6757+
{
6758+
PYLONG_FROM_UINT(uint32_t, value);
6759+
}
67566760

67576761
PyObject* PyLong_FromInt64(int64_t value)
6758-
{ return PyLong_FromNativeBytes(&value, sizeof(value), -1); }
6762+
{
6763+
PYLONG_FROM_INT(uint64_t, int64_t, value);
6764+
}
67596765

67606766
PyObject* PyLong_FromUInt64(uint64_t value)
6761-
{ return PyLong_FromUnsignedNativeBytes(&value, sizeof(value), -1); }
6767+
{
6768+
PYLONG_FROM_UINT(uint64_t, value);
6769+
}
67626770

67636771
#define LONG_TO_INT(obj, value, type_name) \
67646772
do { \

0 commit comments

Comments
 (0)