Skip to content

GH-132554: "Virtual" iterators #132555

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 17 commits into
base: main
Choose a base branch
from

Conversation

markshannon
Copy link
Member

@markshannon markshannon commented Apr 15, 2025

Just a draft PR until I have performance numbers.

@markshannon
Copy link
Member Author

Performance is good. Nothing amazing, but a small speedup.

Stats show no significant changes

@@ -233,6 +233,9 @@ extern intptr_t PyStackRef_UntagInt(_PyStackRef ref);

extern _PyStackRef PyStackRef_TagInt(intptr_t i);

/* Increments a tagged int, but does not check for overflow */
extern _PyStackRef PyStackRef_IncrementTaggedIntNoOverflow(_PyStackRef ref);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you need this declaration since the definition is here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the Py_STACKREF_DEBUG declaration. The definition below is the non-debug version.

Comment on lines 2930 to 2931
if (_PyInterpreterState_GET()->eval_frame)
goto failure;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (_PyInterpreterState_GET()->eval_frame)
goto failure;
if (_PyInterpreterState_GET()->eval_frame) {
goto failure;
}

PyStackRef_IncrementTaggedIntNoOverflow(_PyStackRef ref)
{
assert(ref.index != (uintptr_t)-1); // Overflow
return (_PyStackRef){ .index = ref.index + 2 };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a bit weird that "increment" does += 2. Maybe "Advance"?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It increases the value by one. The low bit is the tag bit.

Python/ceval.c Outdated
_PyForIter_NextWithIndex(PyObject *seq, _PyStackRef index)
{
assert(PyStackRef_IsTaggedInt(index));
assert(Py_TYPE(seq) == &PyTuple_Type || Py_TYPE(seq) == &PyList_Type);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not *_CheckExact?

Python/ceval.c Outdated
if ((size_t)i >= size) {
return PyStackRef_NULL;
}
PyObject *next_o = PySequence_Fast_GET_ITEM(seq, i);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assert (next_o != NULL); ?

Python/ceval.c Outdated
if ((size_t)i >= size) {
return PyStackRef_NULL;
}
PyObject *next_o = PySequence_Fast_GET_ITEM(seq, i);
Copy link
Member

@Fidget-Spinner Fidget-Spinner Apr 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't thread safe for FT. Check out how to do this in the function _PyList_GetItemRefNoLock.

Comment on lines +3433 to +3445
size_t size = PyList_GET_SIZE(seq);
if ((size_t)i >= size) {
return PyStackRef_NULL;
}
#ifdef Py_GIL_DISABLED
PyObject *item = _PyList_GetItemRef((PyListObject *)seq, i);
if (item == NULL) {
return PyStackRef_NULL;
}
return PyStackRef_FromPyObjectSteal(item);
#else
return PyStackRef_FromPyObjectNew(PyList_GET_ITEM(seq, i));
#endif
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_PyList_GetItemRef does the same thing as this entire block (check index and get the item). It also behaves differently on FT and default builds, so you can just use that here, and replace all this code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants