Skip to content

Specialized subscript guards should require non-negative compact ints #153486

Open

Description

@KRRT7

The specialized int subscript fast paths currently assume a non-negative compact int, but that precondition is not expressed directly in the guard.

Make the guard explicit for the affected cases:

  • list[i]
  • tuple[i]
  • str[i]
  • list[i] = value

This aligns the guard with the existing specialization contract and removes redundant checks from the specialized bodies, without changing the fallback path for negative indices.

Linked PRs

Activity

  1. added a commit that references this issue on Jul 10, 2026
  2. markshannon commented on Jul 10, 2026

    @markshannon
    Member

    BINARY_OP_SUBSCR_LIST_INT doesn't require that the index is non-negative, so this will slowdown l[-1]
    Negative indices are quite common, especially -1.

    Perhaps, it would make more sense to handle negative indices for all these cases, like BINARY_OP_SUBSCR_LIST_INT already does:

    index  += index < 0 ? len : 0;

    before deopting if the index is still negative.

  3. KRRT7 commented on Jul 10, 2026

    @KRRT7
    Author

    so this will slowdown l[-1]

    yeah, I think i noticed the perf regression in the xml_etree_generate

    Perhaps, it would make more sense to handle negative indices for all these cases

    I'll look into it, thank you.

  4. added 3 commits that reference this issue on Jul 10, 2026
  5. added
    type-featureA feature request or enhancement
    performancePerformance or resource usage
    interpreter-core(Objects, Python, Grammar, and Parser dirs)
    on Jul 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions