-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
[mypyc] Implement dict clear primitive #9724
Conversation
The test failures are probably caused with a problem with |
Thank you @JukkaL for the clarification. This is the first time I'm working with the mypyc codebase, so I'm still trying to get familiar with how it works internally. I'll have a look at it (as well as the copy primitive, which has similar issues) and update both my PRs as soon as possible. |
34f6041
to
f9e50f9
Compare
Test are finally passing! |
So, I just discovered that this repo exists https://github.com/mypyc/mypyc-benchmarks so I'll post the results from that as well: Master:
PR:
|
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.
Just a few minor comments, otherwise looks good! I like the performance improvement.
mypyc/test-data/run-dicts.test
Outdated
|
||
d = {'a': 1, 'b': 2} | ||
d.clear() | ||
assert d == {} |
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.
It would be nice to also test this with a DefaultDict
instance (still using Dict[...]
as the declared type) to test the other code path.
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.
See my most recent comments in #9721 -- they also apply here.
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.
At least the lines that call clear()
need to be moved away from driver.py
, since this file doesn't get compiled.
mypyc/primitives/dict_ops.py
Outdated
arg_types=[dict_rprimitive], | ||
return_type=void_rtype, | ||
c_function_name='CPyDict_Clear', | ||
error_kind=ERR_NEVER) |
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.
Use ERR_MAGIC
here since an overridden method might raise an exception.
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'm a little confused about this one. If I change the error_kind
to ERR_MAGIC
, I get this error:
assert not op.is_void, "void op generating errors?"
Due to the fact that the return type is void_rtype
. However, what should I change this to? CPyDict_Clear
uses internally PyDict_Clear
which has a void
return type itself.
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.
Ah one option is t to use bit_rprimitive
as the return type (char
in the C function) and ERR_FALSE
as the error kind. Then unconditionally return 1 when there can be no error. 0 signnals an exception being raised.
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.
@JukkaL I have added case testDictMethods
to be used for testing copy
, clear
and any other dict methods. Let me know if it looks good to you.
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.
Looks good now! Thanks for the updates.
Description
Implements dict clear primitive for improved performance.
Related ticket: mypyc/mypyc#644
Test Plan
Ensure that clearing a dict works as expected, write tests and calculate performance improvements.
Generated IR
The following script was used:
Master branch:
PR:
Performance
The following script was used:
Master branch: 0.97x (In this test, I mostly noticed a slowdown compared to running the script as is with Python 3.8)
PR: 1.14x speedup