-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
py/objdeque: Expand implementation to be doubly-ended #10724
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
Conversation
9186ee4
to
c6ae8d1
Compare
Code size report:
|
c6ae8d1
to
2eff3f9
Compare
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #10724 +/- ##
==========================================
- Coverage 98.39% 98.39% -0.01%
==========================================
Files 161 161
Lines 21079 21140 +61
==========================================
+ Hits 20740 20800 +60
- Misses 339 340 +1 ☔ View full report in Codecov by Sentry. |
2eff3f9
to
c46f854
Compare
f2168dd
to
a235ede
Compare
Thanks for the contribution! This change does add a lot of code size:
It would be good to try and optimise that down. Then we need to make a decision on whether this is an important feature and worth the cost, and/or whether it should be guarded by new config options. |
c22e1bc
to
393926b
Compare
77a439c
to
300e053
Compare
Current code size diff:
|
I was thinking perhaps these new features should only be enabled on larger devices, but looking at: Line 1267 in 3229791
deque is already gated by ROM_LEVEL_AT_LEAST_EXTRA which feels appropriate to me.
|
Looking at this again, I did some code-size measurements with various parts in this PR disabled (on stm32 PYBv1.0 which is indicative of most bare-metal ports size):
I think iteration and subscript are less important/useful than the additional double-ended methods ( So I'd propose to add the options:
And have these default to |
Reducing the default config size by a third sounds reasonable for sure. Out of interest @LordFlashmeow did you have a particular use-case using iter and indexing? |
I’m not actively using them, but supporting Indexing isn’t strictly necessary, but I think it’s natural to have and brings the behavior more in-line with the reference implementation. |
Also, curious what's the use case for |
In case it helps with this discussion, I have an application for which I need to implement a fast moving average of data acquired from a sensor. Doing this with a performant deque would be ideal, and much cleaner to implement if the deque is iterable. If selecting just a subset of the added functionality LordFlashmeow has implemented is necessary due to space constraints, I would like to log my vote for making deque iterable as that would be very helpful. |
@SAK917 Have you considered a ringbuf in this application? It has the potential to be more efficient because the storage can be in a fixed-size pre-allocated object such as an array. |
Thanks for your suggestion @peterhinch, I appreciate you taking the time to respond and agree that a ring buffer / circular queue makes sense for my application. That said, my understanding of the MP deque is that it is implemented as a performant statically allocated ring buffer, and as far as that aspect it works well now. However, in addition to this PR bringing the MP deque closer to feature parity with CPython, if the deque was just iterable without the other new functionality, it would make a moving average much easier to implement:
And I do recognize that there is a moving average workaround for the current non-iterable deque implementation (psuedocode below) that is also more performant than calculating the sum every time the moving average is updated as the example above does:
But this is unintuitive and the iterable deque implementation above is considerably clearer and more concise, especially for novice / non-programmers that expect the MP deque to function similarly to the CPython implementation. |
This is an automated heads-up that we've just merged a Pull Request See #13763 A search suggests this PR might apply the STATIC macro to some C code. If it Although this is an automated message, feel free to @-reply to me directly if |
300e053
to
e0790b0
Compare
I have rebased this PR, fixed conflicts, and make Current size code change for this PR:
|
Add `pop()`, `appendleft()`, and `extend()` methods, support iteration and indexing, and initializing from an existing sequence. Iteration and indexing (subscription) have independent configuration flags to enable them. They are enabled by default at the same level that collections.deque is enabled (the extra features level). Also add tests for checking new behavior. Signed-off-by: Damien George <damien@micropython.org>
e0790b0
to
7dff38f
Compare
Now merged. Thanks @LordFlashmeow for the implementation. |
Thank you @LordFlashmeow, this is very helpful! |
Add
pop()
,appendleft()
, andextend()
methods, support iteration and indexing, and initializing from an existing sequence.Added tests for verifying new behavior.