-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
bpo-46702: Specialize UNPACK_SEQUENCE
#31240
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
Conversation
Is there interest in
Edit: I saw the comment on the issue -- makes sense. |
Would it make sense to specialize not for type, but for size in this case? Something like TARGET(UNPACK_SEQUENCE_TWO):
PyObject *seq = TOP();
PyObject **items;
if (PyTuple_CheckExact(seq) &&
PyTuple_GET_SIZE(seq) == 2) {
items = ((PyTupleObject *)seq)->ob_item;
} else if (PyList_CheckExact(seq) &&
PyList_GET_SIZE(seq) == 2) {
items = ((PyListObject *)seq)->ob_item;
} else {
goto general_unpack;
}
SET_TOP(Py_NewRef(items[1]));
PUSH(Py_NewRef(items[0]));
... p.s. Sorry about the merge conflict. |
Honestly, I can't remember the last time I unpacked a two-element list (or other non-tuple-iterable). Two-element tuples are just so deeply baked into the language that I have trouble seeing a payoff for adding the additional logic for handling everything else in the same instruction. Three-element unpackings seem similarly rare (I can only think of I don't know, maybe they appear somewhat frequently in the benchmarks... but in that case, we should probably add specializations for lists and tuples of length ten. ;) |
Actually, it looks like #31254 should give us a better idea of the overall impact, at least. I'm still skeptical that specializing these will move the needle significantly vs. the two-element tuple case, so I'd like to explore and propose the less common patterns separately once these basic specializations are already in. |
The latest stats seem to suggest that this approach is the way to go for now. Then I'll try following up with If I had to guess, I'd say that:
I'm much less inclined to specialize for each of these individual cases (at least right now). Perhaps the three-element case may be worth it at some point. |
99% hit rate, with 2/3rds of the hits going to
UNPACK_SEQUENCE_TWO_TUPLE
.1% improvement overall:
https://bugs.python.org/issue46702