-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
RFC: Ways to enable allocation-free buffer operations #4244
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
Comments
All of these issues are still open. |
It also allocates an intermediate slice object for One way to handle this issue is to pre-create the memoryview objects, as is done here: https://github.com/micropython/micropython/blob/master/drivers/display/lcd160cr.py#L70-L71
It might seem convenient to add offset/size, but that doesn't scale well because there are many read/write functions/methods which would need such addition. Instead I would prefer to find a way to make it so a memoryview doesn't need to take any heap memory, similar to parts of the discussion in #2552. That's then something which can be easily composed in general ways. For example, with the proposal in #4020 to extend writeto to take multiple buffers like |
Added Unexpected Maker TinyS2 board definition
It is difficult to perform I2C reads of variable length data in an interrupt context. I tried pre-allocating a buffer, creating a memoryview, and issuing
Alas mv[x:y] instantiates a new memoryview causing an allocation. Consequently with a preallocated buffer you are forced to fill the entire buffer if allocation is to be avoided. Depending on the remote hardware this may be impossible.
In practice you might want to read into a buffer which has already accumulated some data (eg a circular buffer). The length of data to be read may also change at runtime. An option would be for readfrom_into to take two additional parameters: offset and size. Reading would start at offset and end when size bytes were received or the buffer became full. Defaults (0 and -1) could be used to provide existing behaviour.
This seems rather widespread. Code which reads into, and writes from, preallocated buffers appears to offer the promise of allocation-free operation but falls at the last hurdle of actually delivering it.
Or is there any way to ensure memoryview objects don't have this side-effect? The following fails:
The text was updated successfully, but these errors were encountered: