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
Specialized match_keys
for exact dictionary type
#93714
Comments
What you mean is something like if(PyDIct_CheckExact(o)) {
PyDIct_GetItemWIthError(o, key)
}
else {
// The current code goes here
} right? (Whenever I see the word "specialize" now I immediately think of PEP 659, so I wanted to clear that up). |
Link to the current Line 912 in 4c496f1
I was thinking of having a second copy of the function that operates on dicts. So
and And no - I wasn't proposing anything like PEP 659 - just an if statement to pick a slightly faster implementation |
In theory this seems sound to me and I'm +1. But I'll wait to hear what @brandtbucher has to say. |
I actually discovered during a PyCon all-nighter (thanks to some nerd-sniping by @tonybaloney) that we can get a huge speedup for mapping patterns by moving this logic into the bytecode and:
As a bonus, with this scheme we get dictionary specialization for free, since the logic is being moved “into Python code”. My plan is to make this change in 3.12. I have a branch with the work, just haven’t gotten around to cleaning it up and opening a PR yet. I’d be okay merging a PR that does what you describe now since it’s always better to make things faster. But just know that there’s a pretty good chance it could get completely ripped out and replaced before 3.12 is released, haha. |
Unfortunately it’s documented that |
Indeed, that’s the main sticking point. Although I'm pretty confident that it won't be much of an issue because:
|
Feature or enhancement
I think it's probably worthwhile to write a specialized version of
match_keys
(for structural pattern matching of mappings) for exact dictionary typesPitch
The most common mapping type is almost certainly the built-in
dict
. Thematch_keys
function in ceval.c could be specialized to handle this type. Doing so would allow you to skip the dummyobject
, and replace theget
call withPyDict_GetItemWithError
. I don't think this should change the observable behaviour at all for exactdict
I'm in the process of reimplementing structural pattern matching on Cython, and this change gives ~2x optimization of some microbenchmarks.
If this change is deemed desirable I'm happy to implement it myself.
Previous discussion
Not aware of any
The text was updated successfully, but these errors were encountered: