-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
py/objtype: Add support for __set_name__. (list version) #16806
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
Code size report:
|
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #16806 +/- ##
==========================================
- Coverage 98.44% 98.42% -0.02%
==========================================
Files 171 171
Lines 22192 22226 +34
==========================================
+ Hits 21847 21877 +30
- Misses 345 349 +4 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
5d73521
to
c6bd944
Compare
I've done some benchmarking using a new suite of internalbench class creation benchmarks (see PR #16825) and have compared the benchmark times from the base branch to this branch. There's a lot of noise in this (I'll see if I can run these on real hardware at some point), but overall this patch makes processing classes take about 32% longer:
There were also three other benchmark tests that got concerningly slower:
None of the other tests in their families seemed to be affected, but my tests for #15503 and #16816 both showed |
Signed-off-by: Anson Mansfield <amansfield@mantaro.com>
Signed-off-by: Anson Mansfield <amansfield@mantaro.com>
7a169aa
to
0dc3ccb
Compare
Ended up switching out |
d710e5a
to
b69bfbd
Compare
Also, since it's only binding rather than calling
|
Signed-off-by: Anson Mansfield <amansfield@mantaro.com>
Signed-off-by: Anson Mansfield <amansfield@mantaro.com>
Captures all __set_name__ calls with their per-attr args into a linked list during the special accessors check, to iterate safely later. Signed-off-by: Anson Mansfield <amansfield@mantaro.com>
Signed-off-by: Anson Mansfield <amansfield@mantaro.com>
Signed-off-by: Anson Mansfield <amansfield@mantaro.com>
Spurious coverage change for 8179697, along with three different spurious failures on that same I've also manually tested the path gated behind |
Would be good to see the change in benchmark results with the latest version here. |
Summary
This modifies #15503 to add the additional logic required to eliminate the modify-while-iterating hazard in the original implementation.
Testing
This feature was developed test-first as a cpydiff. All automated tests pass, including the original cpydiff that this patch converts into a mandatory test.
Trade-offs and Alternatives
Performing
__set_name__
in a hazard-free way requires about double the code complexity, including the need to allocate temporary storage to capture a frozen list of__set_name__
members into that can be iterated safely while calls into user code potentially mutate the class.The way the list is frozen currently involves generating a list of the names that the code is able to successfully do a
__set_name__
method lookup on, and then looping over the list looking up and calling those names. Potentially, it would be possible to skip that second name lookup if we store both the name and the method binding that was retrieved in the first step. This alternate approach would need three times the memory in its list allocation. It would also change one very slight detail about the observable behavior, for whether__set_name__
still gets called even if a descriptor was removed by another descriptor before we reached it in the iteration order (currently: no, cpython: yes).Alternatively, we could do a complete flat memcpy on
locals_dict
and iterate that copy. This seems to be the approach that CPython takes in their implementation (see python/cpython#72983). (Copying bulk memory is cheap there, though, and conditionals expensive, while on microcontrollers it's usually the opposite situation.)