Motoko Base Library Changes

If you deep dive the performance stats here: Mops • Motoko Package Manager

… you find that in one particular(common) instance buffer is still superior to vector. If you know the size of your final array and use put to load the buffer only to dump it with toArray then buffer is more cycle and memory efficient than vector. This is a very common use case when you need to transform one collection to another and is used a bunch in query calls where paginating through things or doing any kind of transforms to dump things from classy backend data structure to shareable structures that you can return from actors.

Dig a little deeper and you see how much better Array is at this than Buffer. This scenario avoids the dreaded array.append and its warning.

It may be nice to have TempArray class with a stripped down api that does just that in base. It would be clearly marked temp so no one would use it for long term data storage and it would be the best way to do this kind of loop and dump logic. Or maybe even something more to the point like TempLowMemoryFixedLengthAccumulator (At least the ai would know what it was for😂.)

5 Likes