-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
py/obj: Add new type flag to indicate subscr accepts slice-on-stack. #17736
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?
py/obj: Add new type flag to indicate subscr accepts slice-on-stack. #17736
Conversation
Code size report:
|
8b65d69
to
d54de76
Compare
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #17736 +/- ##
=======================================
Coverage 98.41% 98.41%
=======================================
Files 171 171
Lines 22210 22211 +1
=======================================
+ Hits 21857 21858 +1
Misses 353 353 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
This makes sense and seems worth spending a flag bit on. |
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 like a good fix to me, agree with jeff that it's worth spending a flag bit on this given the optimisation potential.
One minor thing, the test name has a typo (optimse instead of optimise).
The recently merged 5e9189d now allows temporary slices to be allocated on the C stack, which is much better than allocating them on the GC heap. Unfortunately there are cases where the C-allocated slice can escape and be retained as an object, which leads to crashes (because that object points to the C stack which now has other values on it). The fix here is to add a new `MP_TYPE_FLAG_SUBSCR_ALLOWS_STACK_SLICE`. Native types should set this flag if their subscr method is guaranteed not to hold on to a reference of the slice object. Fixes issue micropython#17733 (see also micropython#17723). Signed-off-by: Damien George <damien@micropython.org>
d54de76
to
49f5f42
Compare
Ah, thanks! Now fixed. |
Summary
The recently merged PR #12518 allowed temporary slices to be allocated on the C stack, which is much better than allocating them on the GC heap.
Unfortunately there are cases where the C-allocated slice can escape and be retained as an object, which leads to crashes (because that object points to the C stack which now has other values on it).
See #17723 and #17733.
The fix here is to add a new
MP_TYPE_FLAG_SUBSCR_ALLOWS_STACK_SLICE
. Native types should set this flag if theirsubscr
method is guaranteed not to hold on to a reference of the slice object.Testing
The existing tests pass. A new test is added to test the bugs reported in #17723 and #17733.
Trade-offs and Alternatives
The function
mp_obj_is_native_type()
actually tests a type flag, so we replace the test of one flag with another flag. Thus the fix here should be no change in code size and no change in performance.We do use up another
MP_TYPE_FLAG_xxx
bit, but there are still 5 left.Eventually more native types (eg tuple, list) can have this flag set, but I'll leave that for a follow-up PR.