File tree 4 files changed +13
-9
lines changed
Misc/NEWS.d/next/Core and Builtins
4 files changed +13
-9
lines changed Original file line number Diff line number Diff line change @@ -23,8 +23,9 @@ extern void _PyLong_FiniTypes(PyInterpreterState *interp);
23
23
#define _PyLong_SMALL_INTS _Py_SINGLETON(small_ints)
24
24
25
25
// _PyLong_GetZero() and _PyLong_GetOne() must always be available
26
- #if _PY_NSMALLPOSINTS < 2
27
- # error "_PY_NSMALLPOSINTS must be greater than 1"
26
+ // _PyLong_FromUnsignedChar must always be available
27
+ #if _PY_NSMALLPOSINTS < 257
28
+ # error "_PY_NSMALLPOSINTS must be greater than or equal to 257"
28
29
#endif
29
30
30
31
// Return a borrowed reference to the zero singleton.
@@ -37,6 +38,11 @@ static inline PyObject* _PyLong_GetZero(void)
37
38
static inline PyObject * _PyLong_GetOne (void )
38
39
{ return (PyObject * )& _PyLong_SMALL_INTS [_PY_NSMALLNEGINTS + 1 ]; }
39
40
41
+ static inline PyObject * _PyLong_FromUnsignedChar (unsigned char i )
42
+ {
43
+ return Py_NewRef ((PyObject * )& _PyLong_SMALL_INTS [_PY_NSMALLNEGINTS + i ]);
44
+ }
45
+
40
46
PyObject * _PyLong_Add (PyLongObject * left , PyLongObject * right );
41
47
PyObject * _PyLong_Multiply (PyLongObject * left , PyLongObject * right );
42
48
PyObject * _PyLong_Subtract (PyLongObject * left , PyLongObject * right );
Original file line number Diff line number Diff line change
1
+ Speed up iteration of :class: `bytes ` and :class: `bytearray ` by 30%. Patch by Kumar Aditya.
Original file line number Diff line number Diff line change 6
6
#include "pycore_bytes_methods.h"
7
7
#include "pycore_object.h" // _PyObject_GC_UNTRACK()
8
8
#include "pycore_strhex.h" // _Py_strhex_with_sep()
9
+ #include "pycore_long.h" // _PyLong_FromUnsignedChar()
9
10
#include "bytesobject.h"
10
11
11
12
/*[clinic input]
@@ -2428,7 +2429,6 @@ static PyObject *
2428
2429
bytearrayiter_next (bytesiterobject * it )
2429
2430
{
2430
2431
PyByteArrayObject * seq ;
2431
- PyObject * item ;
2432
2432
2433
2433
assert (it != NULL );
2434
2434
seq = it -> it_seq ;
@@ -2437,11 +2437,8 @@ bytearrayiter_next(bytesiterobject *it)
2437
2437
assert (PyByteArray_Check (seq ));
2438
2438
2439
2439
if (it -> it_index < PyByteArray_GET_SIZE (seq )) {
2440
- item = PyLong_FromLong (
2441
- (unsigned char )PyByteArray_AS_STRING (seq )[it -> it_index ]);
2442
- if (item != NULL )
2443
- ++ it -> it_index ;
2444
- return item ;
2440
+ return _PyLong_FromUnsignedChar (
2441
+ (unsigned char )PyByteArray_AS_STRING (seq )[it -> it_index ++ ]);
2445
2442
}
2446
2443
2447
2444
it -> it_seq = NULL ;
Original file line number Diff line number Diff line change @@ -3133,7 +3133,7 @@ striter_next(striterobject *it)
3133
3133
assert (PyBytes_Check (seq ));
3134
3134
3135
3135
if (it -> it_index < PyBytes_GET_SIZE (seq )) {
3136
- return PyLong_FromLong (
3136
+ return _PyLong_FromUnsignedChar (
3137
3137
(unsigned char )seq -> ob_sval [it -> it_index ++ ]);
3138
3138
}
3139
3139
You can’t perform that action at this time.
0 commit comments