Memo: basic operations between collection types (conversion, copy, etc.)

Hey all,

Following a mini-thread earlier in the chat with @sebl and @tonfilm about how to efficiently convert or copy collections from a type to another (Spread / SpreadBuilder / MutableArray / MutableArraySegments) I started to compile the most common recipes and tricks into this patch, laid out as a double-entry array.

Some operations might be basic but a good reference for beginners who struggle understanding/converting the different collection types, but there are a few of the hot nodes for block copying a subset of value into another collection efficiently, or converting a collection into another type without copying, etc.

I might update the patch as I do further benchmarking between methods to compare performance when I stumble upon one of these scenarios.

Feel free to correct me if you find any mistakes/irrelevances, or to add on if you have other tips and tricks for all patcher levels!

T

ExsS.snippets.Collections.conversions+operations_20250107.vl (97.5 KB)


Benchy.vl (120.2 KB)

with more iterations


Benchy.vl (130.1 KB)

Damn this little Benchy region is so handy! 😍

Thanks for joining the patch

The results are pretty consistent with the internal works I had understood:

  • FromSequence creates a whole new copy => extremely slow
  • Buffer exposes the internal array, but it might have allocated more resource than needed, so you might be manipulating more data than necesary => better but not optimal
  • AsArray resizes the internal array to what is actually used so you get just what ya need => best

Edit to the patch: took out “ToArray” from the MutableArrayBuilder > MutableArray options in my table as it actually returns MutableArray

ExsS.snippets.Collections.conversions+operations_20250107b.vl (129.0 KB)

another funny observation can happen if you wait some time and then run it again, or when you run it the first time


i guess this is some initialization or even GC Allocation or so?

another funny observation can happen if you wait some time and then run it again, or when you run it the first time

Yes I noticed the same! After you leave it alone for some time indeed.
I am suspecting something gets disposed / recreated after a while indeed? But this goes beyond my knowledge.

Here is another patch update, where I added quite a bit of benchies 🤓
Especially for array-to-array copies.
Spoiler: by very far, the fastest way is MutableArray > BlockCopy (or CopyTo) > MutableArray, as @tonfilm was suggesting indeed.
Any other types or node for copy is either worse… or way worse.

Super instructive day!
Thanks again @sebl for the contribution to this quest.

EDIT: patch reuploaded, BlockCopy was missing the count input
EDIT2: even more benchies added for conversions


ExsS.snippets.Collections.conversions+operations_20250107c.vl (240.6 KB)