espressif / esp-matter

Espressif's SDK for Matter
Apache License 2.0
706 stars 161 forks source link

Strategy to reduce memory consumption further (CON-1414) #1152

Open jonsmirl opened 2 weeks ago

jonsmirl commented 2 weeks ago

In the current design all of the attributes are doing individual mallocs. That seems fine until you take into account the malloc granularity is 16b I think and often only 4b is used. There are other ways possible to allocate this memory. Since attribute buffers are fixed size you could use a single malloc per endpoint and assign the buffers from it. Then as more attributes are created do a realloc if necessary. This scheme works to eliminate all of the tiny mallocs. You'd need to do some experimentation to see if switching strategies is worth it.

My devices now have 42 endpoints and those endpoints trigger many thousands of tiny allocations. If 2,000 tiny allocations each lose 12 bytes, that is 24KB of lost memory. Of course I switched over to PSRAM long ago so I am not bothered by this.

shubhamdp commented 1 week ago

@jonsmirl Thanks for the suggestion, will check and get back.