Skip to content

gh-131738: optimize builtin any/all/tuple calls with a generator expression arg - #131737

Merged
iritkatriel merged 17 commits into
python:mainfrom
iritkatriel:tuple
Mar 28, 2025
Merged

gh-131738: optimize builtin any/all/tuple calls with a generator expression arg#131737
iritkatriel merged 17 commits into
python:mainfrom
iritkatriel:tuple

Conversation

@iritkatriel

@iritkatrieliritkatriel commented Mar 25, 2025

Copy link
Copy Markdown
Member

@iritkatrieliritkatriel changed the title optimize any/all/tuple calls with a generator expression arggh-131738: optimize builtin any/all/tuple calls with a generator expression argMar 25, 2025
@TeamSpen210

Copy link
Copy Markdown

I don't think LOAD_COMMON_CONST is safe right now, since users could have messed with the builtins module. Probably better to stash the funcs in interp->callable_cache like other builtins used for optimisation?

@brandtbucherbrandtbucher left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very cool! Any plans to do this for set or list?

dict/max/min/sum are trickier, but may be worth exploring too.

Comment threadLib/opcode.py
Comment threadPython/bytecodes.c Outdated
Comment threadPython/codegen.c Outdated
Comment threadPython/codegen.c
Comment threadPython/codegen.c Outdated
Comment threadPython/codegen.c Outdated
@bedevere-app

Copy link
Copy Markdown

When you're done making the requested changes, leave the comment: I have made the requested changes; please review again.

@brandtbucher

Copy link
Copy Markdown
Member

This should fix the JIT builds:

diff --git a/Tools/jit/_targets.py b/Tools/jit/_targets.py
index aa2b56abf44..73a4210eee0 100644
--- a/Tools/jit/_targets.py+++ b/Tools/jit/_targets.py@@ -522,7 +522,14 @@ def get_target(host: str) -> _COFF | _ELF | _MachO:
args = ["-fms-runtime-lib=dll"]
target = _COFF(host, args=args)
elif re.fullmatch(r"x86_64-.*-linux-gnu", host):
- args = ["-fno-pic", "-mcmodel=medium", "-mlarge-data-threshold=0"]+ args = [+ # Jump tables generate R_X86_64_32S relocations, which assume a+ # signed 32-bit address space:+ "-fno-jump-tables",+ "-fno-pic",+ "-mcmodel=medium",+ "-mlarge-data-threshold=0",+ ]
target = _ELF(host, args=args)
else:
raise ValueError(host)

Comment threadPython/bytecodes.c Outdated
@markshannon

Copy link
Copy Markdown
Member

The overall approach looks good.

Do you have stats for this PR?
If there is enough of an increase in the execution counts for CALL_INTRINSIC_1 then we should move CALL_INTRINSIC_1 INTRINSIC_LIST_TO_TUPLE into a new LIST_TO_TUPLE instruction.

@rhettinger

Copy link
Copy Markdown
Contributor

dict/max/min/sum are trickier, but may be worth exploring too.

The latter three would be nice wins.

Comment threadPython/bytecodes.c Outdated
@iritkatriel

Copy link
Copy Markdown
MemberAuthor

@iritkatriel

Copy link
Copy Markdown
MemberAuthor

Do you have stats for this PR? If there is enough of an increase in the execution counts for CALL_INTRINSIC_1 then we should move CALL_INTRINSIC_1 INTRINSIC_LIST_TO_TUPLE into a new LIST_TO_TUPLE instruction.

image

@iritkatriel

Copy link
Copy Markdown
MemberAuthor

I have made the requested changes; please review again.

@bedevere-app

Copy link
Copy Markdown

Thanks for making the requested changes!

@brandtbucher: please review the changes made to this pull request.

@brandtbucherbrandtbucher left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. You can revert the JIT change, it's not needed anymore.

(Also I'm running JIT benchmarks just because I'm curious, but no need to block on those results at all.)

Comment threadObjects/genobject.c
#include "pycore_warnings.h" // _PyErr_WarnUnawaitedCoroutine()


#include "opcode_ids.h" // RESUME, etc

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this needed?

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I had to remove this include from another header so it needs to be included when needed.

Comment threadTools/jit/_targets.py Outdated
@brandtbucher

Copy link
Copy Markdown
Member

(Also, I think you need to make regen-cases.)

iritkatrieland others added 2 commits March 28, 2025 10:07
Co-authored-by: Brandt Bucher <brandtbucher@gmail.com>
@iritkatriel
iritkatriel enabled auto-merge (squash) March 28, 2025 10:13
@iritkatriel
iritkatriel merged commit 2c8f329 into python:mainMar 28, 2025
diegorusso pushed a commit to diegorusso/cpython that referenced this pull request Apr 1, 2025
ntBre pushed a commit to astral-sh/ruff that referenced this pull request Jul 8, 2026
## Summary
Closes#12912 by only applying
the tuple generator behavior of the rule on Python 3.14+, where tuple
generator expression got optimized in (ref:
python/cpython#131737). Rendering the concerns
on that issue no longer relevant on those Python versions.
This was especially problematic for `C409` as only some behaviour is in
preview, not the rule itself, so you couldn't use
`tool.ruff.lint.explicit-preview-rules = true`
## Test Plan
New snapshot should pass on CI
---
Note: Code changed by Claude Sonnet 4.6. Reviewed by human, although I
don't know much about Rust (but this seems a simple enough change)
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants

@iritkatriel@TeamSpen210@brandtbucher@markshannon@rhettinger@Arian-bee