-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
[mypyc] feat: constant folding for DictExpr [1/1] #19864
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
base: master
Are you sure you want to change the base?
Conversation
for more information, see https://pre-commit.ci
…into dict-literals
for more information, see https://pre-commit.ci
…into dict-literals
|
|
||
| # 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. |
There was a problem hiding this comment.
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
for more information, see https://pre-commit.ci
…into dict-literals
for more information, see https://pre-commit.ci
…into dict-literals
unsupported operand type(s) for |: '_UnionGenericAlias' and 'types.GenericAlias'
JukkaL
left a comment
There was a problem hiding this 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?
|
Nothing specific I can readily share but based on my own use case I see it
being useful in any code that's making requests and sending consistent or
quasi-consistent headers along with them, or code that creates a dict by
starting from some base dict expr and modifying it with conditional logic
below
For me this is most meaningful for a high thruput internal API
…On Tue, Oct 14, 2025 at 8:29 AM Jukka Lehtosalo ***@***.***> wrote:
***@***.**** commented on this pull request.
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?
—
Reply to this email directly, view it on GitHub
<#19864 (review)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AQ3HIHR7CQ7Q2LVHXJQD44L3XTUCBAVCNFSM6AAAAACGXC4PYGVHI2DSMVQWIX3LMV43YUDVNRWFEZLROVSXG5CSMV3GSZLXHMZTGMZVGM2TGMRVG4>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
|
Also helps for functions that need their own little lookup tables that aren't worthy of being a module level constant. I have a bunch of these little lookup tables in [one repo](https://github.com/BobTheBuidler/yearn-treasury/) I maintain
example:
```python
def check_owner(x: LicensePlate) -> bool:
owner = {"honda": "tom", "ford": "tim", "vw": "tam"}[x.car]
return is_good(owner)
```
edit
On Tue, Oct 14, 2025 at 8:33 AM Bob Buidler ***@***.***>
wrote:
… Nothing specific I can readily share but based on my own use case I see it
being useful in any code that's making requests and sending consistent or
quasi-consistent headers along with them, or code that creates a dict by
starting from some base dict expr and modifying it with conditional logic
below
On Tue, Oct 14, 2025 at 8:29 AM Jukka Lehtosalo ***@***.***>
wrote:
> ***@***.**** commented on this pull request.
>
> 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?
>
> —
> Reply to this email directly, view it on GitHub
> <#19864 (review)>,
> or unsubscribe
> <https://github.com/notifications/unsubscribe-auth/AQ3HIHR7CQ7Q2LVHXJQD44L3XTUCBAVCNFSM6AAAAACGXC4PYGVHI2DSMVQWIX3LMV43YUDVNRWFEZLROVSXG5CSMV3GSZLXHMZTGMZVGM2TGMRVG4>
> .
> You are receiving this because you authored the thread.Message ID:
> ***@***.***>
>
|
for more information, see https://pre-commit.ci
It should be possible to "constant fold" a LOC such as:
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.