Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Tier 2 Optimizer Eliminate Type Version Guards #119258

Closed
Labels
interpreter-core(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancement
@saulshanabrook

Description

@saulshanabrook

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

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2026 Movatter.jp