peterhinch / micropython-mqtt

A 'resilient' asynchronous MQTT driver. Recovers from WiFi and broker outages.
MIT License
549 stars 116 forks source link

Optimize memory usage #76

Closed bobveringa closed 2 years ago

bobveringa commented 2 years ago

Optimize the memory usage of .as_read() and .as_write() functions. This is done primarily be using memory view to avoid making copies during slicing of the byte array. But also by changing the way memory is allocated when reading from the socket to prevent memory reallocation. This reallocation could lead to fragmentation of memory, preventing optimal use of it.

Related to issue #75

bobveringa commented 2 years ago

@peterhinch I forgot to add a small side note with this PR. If you are using C module and use the mp_obj_new_bytearray_by_ref any changes you make to the bytearray will be transmitted as the byte array is no longer copied.

I suspect that the number of users that this impacts is incredibly small. And the solution is just to make a copy yourself before transmitting.