Skip to content

Optimize len for string constants in optimizer #144140

Closed

Description

@y1yang0

Feature or enhancement

Proposal:

Hi team, I found an optimization opportunity: len() calls on string constants can be
optimized at compile time, similar to how tuple constants are handled.

Quick testing with python -m test showed _CALL_LEN hits:

  • str: 23 (6 optimizable with this patch)
  • list: 28
  • tuple: 4 (3 already optimized)

This patch extends the existing constant folding for len(tuple) to also handle
len("string"), replacing the function call with _SHUFFLE_3_LOAD_CONST_INLINE_BORROW
when the result is an immortal integer.

Has this already been discussed elsewhere?

No response given

Links to previous discussion of this feature:

No response

Linked PRs

Activity

  1. added a commit that references this issue on Jan 22, 2026
  2. added a commit that references this issue on Jan 24, 2026
  3. added a commit that references this issue on Feb 15, 2026
  4. added a commit that references this issue on May 18, 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 usagetopic-JITtype-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions