Skip to content

Conversation

@BobTheBuidler
Copy link
Contributor

@BobTheBuidler BobTheBuidler commented Sep 17, 2025

It should 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.

This PR does that.


# Supported Python literal types. All tuple / frozenset items must have supported
# Supported Python literal types. All tuple / frozenset / dict items must have supported
# literal types as well, but we can't represent the type precisely.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

is there a reason we can't represent the type precisely? I think with a HashableLiteralValue defined before LiteralValue, we can do it pretty cleanly

@BobTheBuidler BobTheBuidler changed the title [mypyc] feat: constant folding for DictExpr [mypyc] feat: constant folding for DictExpr [1/1] Oct 1, 2025
Copy link
Collaborator

@JukkaL JukkaL left a comment

Choose a reason for hiding this comment

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

I verified that this makes a simple micro-benchmark about 20% faster. However, this is quite a big and complex PR, and I'm not sure if the use case is common enough to support the "complexity budget". Can you show some real-world use cases where this would help?

@BobTheBuidler
Copy link
Contributor Author

BobTheBuidler commented Oct 14, 2025 via email

@BobTheBuidler
Copy link
Contributor Author

BobTheBuidler commented Oct 14, 2025 via email

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.

2 participants