Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork34.1k
Description
Feature or Enhancement:
Proposal:
Note: I made this issue at the PyCon sprints discussing with@Fidget-Spinner and in collaboration with@dpdani
The tier 2 optimizer should eliminate type version guards if it is safe to do.
It is safe if it has previously checked the type version guard at runtime and we haven't had an escape opt since then.
Note that this will needPy_Decref to be deferred before the 3.13 final release, to be usable, because otherwise there could be an escape anywhere we decrement the reference count.
Example
For example, if we have this code:
classA:attr=1defthing(a):returna.attr+a.attrfor_inrange(1000):thing(A())
then whenthing is executed it should only run the type guard once.
If we disassemble this code then we see it emits theLOAD_ATTR_NONDESCRIPTOR_WITH_VALUES bytecode:
>>>importdis>>>dis.dis(thing,adaptive=True)8RESUME_CHECK09LOAD_FAST0 (a)LOAD_ATTR_NONDESCRIPTOR_WITH_VALUES0 (attr)LOAD_FAST0 (a)LOAD_ATTR_NONDESCRIPTOR_WITH_VALUES0 (attr)BINARY_OP_ADD_INT0 (+)RETURN_VALUE
If we look at the definition ofLOAD_ATTR_NONDESCRIPTOR_WITH_VALUES, we see it uses_GUARD_TYPE_VERSION (inbytecodes.c:
macro(LOAD_ATTR_NONDESCRIPTOR_WITH_VALUES)=unused/1+_GUARD_TYPE_VERSION+_GUARD_DORV_VALUES_INST_ATTR_FROM_DICT+_GUARD_KEYS_VERSION+_LOAD_ATTR_NONDESCRIPTOR_WITH_VALUES;
So if this is executed one after the other, without any unknown function calls in the middle, it should remove the second_GUARD_TYPE_VERSION call.
Has this already been discussed elsewhere?
This is a minor feature, which does not need previous discussion elsewhere
Links to previous discussion of this feature:
No response