Skip to content

[wip] [mypyc] feat: quasi-constant folding for DictExpr and TupleExpr #19542

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 12 commits into
base: master
Choose a base branch
from

Conversation

BobTheBuidler
Copy link
Contributor

@BobTheBuidler BobTheBuidler commented Jul 30, 2025

This is WIP, I just needed the runners to run. This PR strives to accomplish all of the following:

We should be able to constant fold an expression like (str, int) for use cases such as isinstance(obj, (str, int)) . Currently, the tuple is constructed each time. It should be feasible to do this with any tuple comprised of literals, builtin types, and/or final values of any kind.

It should also be possible to "constant fold" a LOC such as:

requests.get(some_url, headers={"k0": "v0", "k1": "v1"})

We can't simply create a dictionary constant since dicts are mutable and we don't know what's going to happen in the called function, but we can hold a template dict in memory and PyDict_Copy it when it's time for use. This skips the hashing and any resizing during construction, and simply takes the hash table from the existing dict instead, which will be (possibly much) quicker than constructing from scratch each time.

Sign up for free to 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.

1 participant