Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Lib/test/test_opcache.py
Original file line number Diff line number Diff line change
Expand Up @@ -2094,6 +2094,18 @@ def for_iter_tuple():
self.assert_specialized(for_iter_tuple, "FOR_ITER_TUPLE")
self.assert_no_opcode(for_iter_tuple, "FOR_ITER")

b = b"\x00\x7f\x80\xff" * 3
result = []
def for_iter_bytes():
result.clear()
for i in b:
result.append(i)

for_iter_bytes()
self.assertEqual(result, list(b))
self.assert_specialized(for_iter_bytes, "FOR_ITER_VIRTUAL")
self.assert_no_opcode(for_iter_bytes, "FOR_ITER")

s = "abcdefghij"
def for_iter_str():
for i in s:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Speed up iteration over exact :class:`bytes` objects by inlining item
retrieval in the bytecode interpreter.
31 changes: 27 additions & 4 deletions Modules/_testinternalcapi/test_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 21 additions & 1 deletion Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -3883,7 +3883,27 @@ dummy_func(
replaced op(_FOR_ITER_VIRTUAL, (iter, null_or_index -- iter, null_or_index, next)) {
PyObject *iter_o = PyStackRef_AsPyObjectBorrow(iter);
Py_ssize_t index = PyStackRef_UntagInt(null_or_index);
_PyObjectIndexPair next_index = Py_TYPE(iter_o)->_tp_iteritem(iter_o, index);
_PyObjectIndexPair next_index;
if (PyBytes_CheckExact(iter_o)) {
if ((size_t)index >= (size_t)PyBytes_GET_SIZE(iter_o)) {
next_index = (_PyObjectIndexPair) {
.object = NULL,
.index = index,
};
}
else {
unsigned char value = (unsigned char)
((PyBytesObject *)iter_o)->ob_sval[index];
next_index = (_PyObjectIndexPair) {
.object = (PyObject *)&_PyLong_SMALL_INTS[
_PY_NSMALLNEGINTS + value],
.index = index + 1,
};
}
}
else {
next_index = Py_TYPE(iter_o)->_tp_iteritem(iter_o, index);
}
PyObject *next_o = next_index.object;
index = next_index.index;
if (next_o == NULL) {
Expand Down
31 changes: 27 additions & 4 deletions Python/generated_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading