Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
c6734ee7
Commit
c6734ee7
authored
Sep 12, 2019
by
Sergey Fedoseev
Committed by
Gregory P. Smith
Sep 12, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bpo-37802: Slightly improve perfomance of PyLong_FromUnsigned*() (GH-15192)
parent
8d88e8c6
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
45 additions
and
77 deletions
+45
-77
Misc/NEWS.d/next/Core and Builtins/2019-08-09-18-28-57.bpo-37802.pKxcAW.rst
...ore and Builtins/2019-08-09-18-28-57.bpo-37802.pKxcAW.rst
+3
-0
Objects/longobject.c
Objects/longobject.c
+42
-77
No files found.
Misc/NEWS.d/next/Core and Builtins/2019-08-09-18-28-57.bpo-37802.pKxcAW.rst
0 → 100644
View file @
c6734ee7
Slightly improve performance of :c:func:`PyLong_FromUnsignedLong`,
:c:func:`PyLong_FromUnsignedLongLong` and :c:func:`PyLong_FromSize_t`.
Patch by Sergey Fedoseev.
Objects/longobject.c
View file @
c6734ee7
...
@@ -43,6 +43,7 @@ PyObject *_PyLong_One = NULL;
...
@@ -43,6 +43,7 @@ PyObject *_PyLong_One = NULL;
static
PyLongObject
small_ints
[
NSMALLNEGINTS
+
NSMALLPOSINTS
];
static
PyLongObject
small_ints
[
NSMALLNEGINTS
+
NSMALLPOSINTS
];
#define IS_SMALL_INT(ival) (-NSMALLNEGINTS <= (ival) && (ival) < NSMALLPOSINTS)
#define IS_SMALL_INT(ival) (-NSMALLNEGINTS <= (ival) && (ival) < NSMALLPOSINTS)
#define IS_SMALL_UINT(ival) ((ival) < NSMALLPOSINTS)
#ifdef COUNT_ALLOCS
#ifdef COUNT_ALLOCS
Py_ssize_t
_Py_quick_int_allocs
,
_Py_quick_neg_int_allocs
;
Py_ssize_t
_Py_quick_int_allocs
,
_Py_quick_neg_int_allocs
;
...
@@ -78,6 +79,7 @@ maybe_small_long(PyLongObject *v)
...
@@ -78,6 +79,7 @@ maybe_small_long(PyLongObject *v)
}
}
#else
#else
#define IS_SMALL_INT(ival) 0
#define IS_SMALL_INT(ival) 0
#define IS_SMALL_UINT(ival) 0
#define get_small_int(ival) (Py_UNREACHABLE(), NULL)
#define get_small_int(ival) (Py_UNREACHABLE(), NULL)
#define maybe_small_long(val) (val)
#define maybe_small_long(val) (val)
#endif
#endif
...
@@ -378,32 +380,52 @@ PyLong_FromLong(long ival)
...
@@ -378,32 +380,52 @@ PyLong_FromLong(long ival)
return
(
PyObject
*
)
v
;
return
(
PyObject
*
)
v
;
}
}
#define PYLONG_FROM_UINT(INT_TYPE, ival) \
do { \
if (IS_SMALL_UINT(ival)) { \
return get_small_int((ival)); \
} \
/* Count the number of Python digits. */
\
Py_ssize_t ndigits = 0; \
INT_TYPE t = (ival); \
while (t) { \
++ndigits; \
t >>= PyLong_SHIFT; \
} \
PyLongObject *v = _PyLong_New(ndigits); \
if (v == NULL) { \
return NULL; \
} \
digit *p = v->ob_digit; \
while ((ival)) { \
*p++ = (digit)((ival) & PyLong_MASK); \
(ival) >>= PyLong_SHIFT; \
} \
return (PyObject *)v; \
} while(0)
/* Create a new int object from a C unsigned long int */
/* Create a new int object from a C unsigned long int */
PyObject
*
PyObject
*
PyLong_FromUnsignedLong
(
unsigned
long
ival
)
PyLong_FromUnsignedLong
(
unsigned
long
ival
)
{
{
PyLongObject
*
v
;
PYLONG_FROM_UINT
(
unsigned
long
,
ival
);
unsigned
long
t
;
}
int
ndigits
=
0
;
if
(
ival
<
PyLong_BASE
)
/* Create a new int object from a C unsigned long long int. */
return
PyLong_FromLong
(
ival
);
/* Count the number of Python digits. */
PyObject
*
t
=
ival
;
PyLong_FromUnsignedLongLong
(
unsigned
long
long
ival
)
while
(
t
)
{
{
++
ndigits
;
PYLONG_FROM_UINT
(
unsigned
long
long
,
ival
);
t
>>=
PyLong_SHIFT
;
}
}
v
=
_PyLong_New
(
ndigits
);
/* Create a new int object from a C size_t. */
if
(
v
!=
NULL
)
{
digit
*
p
=
v
->
ob_digit
;
PyObject
*
while
(
ival
)
{
PyLong_FromSize_t
(
size_t
ival
)
*
p
++
=
(
digit
)(
ival
&
PyLong_MASK
);
{
ival
>>=
PyLong_SHIFT
;
PYLONG_FROM_UINT
(
size_t
,
ival
);
}
}
return
(
PyObject
*
)
v
;
}
}
/* Create a new int object from a C double */
/* Create a new int object from a C double */
...
@@ -1186,34 +1208,6 @@ PyLong_FromLongLong(long long ival)
...
@@ -1186,34 +1208,6 @@ PyLong_FromLongLong(long long ival)
return
(
PyObject
*
)
v
;
return
(
PyObject
*
)
v
;
}
}
/* Create a new int object from a C unsigned long long int. */
PyObject
*
PyLong_FromUnsignedLongLong
(
unsigned
long
long
ival
)
{
PyLongObject
*
v
;
unsigned
long
long
t
;
int
ndigits
=
0
;
if
(
ival
<
PyLong_BASE
)
return
PyLong_FromLong
((
long
)
ival
);
/* Count the number of Python digits. */
t
=
ival
;
while
(
t
)
{
++
ndigits
;
t
>>=
PyLong_SHIFT
;
}
v
=
_PyLong_New
(
ndigits
);
if
(
v
!=
NULL
)
{
digit
*
p
=
v
->
ob_digit
;
while
(
ival
)
{
*
p
++
=
(
digit
)(
ival
&
PyLong_MASK
);
ival
>>=
PyLong_SHIFT
;
}
}
return
(
PyObject
*
)
v
;
}
/* Create a new int object from a C Py_ssize_t. */
/* Create a new int object from a C Py_ssize_t. */
PyObject
*
PyObject
*
...
@@ -1257,35 +1251,6 @@ PyLong_FromSsize_t(Py_ssize_t ival)
...
@@ -1257,35 +1251,6 @@ PyLong_FromSsize_t(Py_ssize_t ival)
return
(
PyObject
*
)
v
;
return
(
PyObject
*
)
v
;
}
}
/* Create a new int object from a C size_t. */
PyObject
*
PyLong_FromSize_t
(
size_t
ival
)
{
PyLongObject
*
v
;
size_t
t
;
int
ndigits
=
0
;
if
(
ival
<
PyLong_BASE
)
return
PyLong_FromLong
((
long
)
ival
);
/* Count the number of Python digits. */
t
=
ival
;
while
(
t
)
{
++
ndigits
;
t
>>=
PyLong_SHIFT
;
}
v
=
_PyLong_New
(
ndigits
);
if
(
v
!=
NULL
)
{
digit
*
p
=
v
->
ob_digit
;
Py_SIZE
(
v
)
=
ndigits
;
while
(
ival
)
{
*
p
++
=
(
digit
)(
ival
&
PyLong_MASK
);
ival
>>=
PyLong_SHIFT
;
}
}
return
(
PyObject
*
)
v
;
}
/* Get a C long long int from an int object or any object that has an
/* Get a C long long int from an int object or any object that has an
__int__ method. Return -1 and set an error if overflow occurs. */
__int__ method. Return -1 and set an error if overflow occurs. */
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment