Stream: SIG-Embedded

Topic: Avoiding dynamic memory for string/list


view this post on Zulip Christof Petig (Aug 06 2024 at 21:55):

While trying to design a good API for string/list without memory allocation, I looked at caller provided buffers and lazy value lowering and was unable to design something which felt easy to use.

E.g. heapless::String always required an upper bound which ideally would be defined in the WIT, thus I ended up with bounded lists and strings which by using return value optimization already avoid all allocations without requiring CPB or LVL.

So my question to SIG embedded: Do these bounded containers already solve many problems with the component model for microcontrollers or am I too optimistic about it?

Based on discussions in wasi-http/#8, we should consider a return-value optimization that avoids calling realloc by instead allowing the core wasm caller to supply a (buffer, length) i32 pair as a ...
This isn't a fully-fleshed out idea, but it's coming up in #369 and so I thought it might be nice to split out here. I'm tentatively excited about it, though (in that "this is probably what we shou...
This extends #181 with the ability to embed bounded strings or lists directly within other structures while maintaining the variable length property. Some environments require avoiding all memory a...

view this post on Zulip Ralph (Aug 07 2024 at 07:59):

Christof Petig said:

So my question to SIG embedded: Do these bounded containers already solve many problems with the component model for microcontrollers or am I too optimistic about it?

you're too optimistic, but then, so am I. :-)

view this post on Zulip Thomas Trenner (Aug 07 2024 at 11:28):

It might help on some use cases. It depends on the setup. In a realtime capable system, one should not malloc/free at all (or at least minimize it). So, yes, there it might help.

On the other hand: We did some internal measurements with an artificial example, showing that malloc in wamr can perform faster (up to factor 2) than a native malloc (which is not surprising with the simple linear memory and not using the "system's" malloc in wasm). So, regarding "speed" this seems to be a lesser problem. More problematic is mem fragmentation, hence using only (statically) pre-allocated memory might help, if you can set it up properly.
(I recall, that there are languages which do not "have" a malloc in their programming model, hence Strings have a maximum size (like e.g. 16k). This, of course, leads to the problem, that a user (=the wasm app writer), who wants to read/parse a json/yml/xml file, might not be able to use some OSS parsers... or the user itself must adapt the OSS parser)

What do you think @Chris Woods @Stephen Berard @Dominik Tacke @Wang Xin ?
[edited typos]


Last updated: Dec 23 2024 at 13:07 UTC