Skip to content

Commit a4b3e95

Browse files
committed
perf(longobject): keep wide int helper local
1 parent 81713ed commit a4b3e95

2 files changed

Lines changed: 13 additions & 9 deletions

File tree

Include/internal/pycore_long.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -388,12 +388,6 @@ _PyLong_FitsInt64(const PyLongObject *v)
388388
return top <= max_top;
389389
}
390390

391-
static inline int
392-
_PyLong_CheckExactAndFitsInt64(PyObject *op)
393-
{
394-
return PyLong_CheckExact(op) && _PyLong_FitsInt64((const PyLongObject *)op);
395-
}
396-
397391
/* Extract an exact int to int64_t without raising.
398392
* Returns true and writes *out on success; returns false if out of range.
399393
* Never sets a Python exception. */

Objects/longobject.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3942,6 +3942,15 @@ _wide_op_result(int64_t v)
39423942
return PyStackRef_FromPyObjectStealMortal(result);
39433943
}
39443944

3945+
/* Exact int -> int64_t helper for the wide int fast path.
3946+
* Keeps the exact-type check local to this translation unit. */
3947+
static inline bool
3948+
_PyLong_CheckExactAndTryAsInt64(PyObject *op, int64_t *out)
3949+
{
3950+
return PyLong_CheckExact(op) &&
3951+
_PyLong_TryAsInt64Exact((PyLongObject *)op, out);
3952+
}
3953+
39453954
/* Wide variant: operands are exact ints in the full int64 range (may be
39463955
* non-compact). Returns PyStackRef_NULL (without raising) when an input is
39473956
* out of int64 range or the sum overflows int64. Returns PyStackRef_ERROR
@@ -3955,7 +3964,8 @@ _PyCompactLong_AddWide(PyLongObject *a, PyLongObject *b)
39553964
return medium_from_stwodigits(v);
39563965
}
39573966
int64_t va, vb;
3958-
if (!_PyLong_TryAsInt64Exact(a, &va) || !_PyLong_TryAsInt64Exact(b, &vb)) {
3967+
if (!_PyLong_CheckExactAndTryAsInt64((PyObject *)a, &va) ||
3968+
!_PyLong_CheckExactAndTryAsInt64((PyObject *)b, &vb)) {
39593969
return PyStackRef_NULL;
39603970
}
39613971
int64_t v;
@@ -3972,7 +3982,6 @@ long_add_method(PyObject *a, PyObject *b)
39723982
return (PyObject*)long_add((PyLongObject*)a, (PyLongObject*)b);
39733983
}
39743984

3975-
39763985
static PyLongObject *
39773986
long_sub(PyLongObject *a, PyLongObject *b)
39783987
{
@@ -4032,7 +4041,8 @@ _PyCompactLong_SubtractWide(PyLongObject *a, PyLongObject *b)
40324041
return medium_from_stwodigits(v);
40334042
}
40344043
int64_t va, vb;
4035-
if (!_PyLong_TryAsInt64Exact(a, &va) || !_PyLong_TryAsInt64Exact(b, &vb)) {
4044+
if (!_PyLong_CheckExactAndTryAsInt64((PyObject *)a, &va) ||
4045+
!_PyLong_CheckExactAndTryAsInt64((PyObject *)b, &vb)) {
40364046
return PyStackRef_NULL;
40374047
}
40384048
int64_t v;

0 commit comments

Comments
 (0)