please remove
This is ‘technically’ what a List does under the hood.
Really a ‘List’ is just a wrapper class around an array. As entries are added the array is resized in an anticipatory manner… usually by doubling the size (depends on the version, the mono used in unity doubles).
Slow down of List is that access is controlled by a method call rather than direct memory access. When accessing you get the overhead of that stack frame for the function, when adding it’s more overhead because it ensures the capacity and other things before adding which can be multiple method calls… and even a resizing if necessary.
Of course you can always create a List with a capacity set from the get go if you know how large the list will probably grow. Avoiding any resizes along the way.
A byte[ ] array would be far faster than a List… often when dealing with byte streams, an array is far preferred as it allows for controlled buffering.
I’m honestly not clear on what part of sockets your using that is even allowing these other dictionaries… data streams almost exclusively use arrays or streams. Unless you mean you’re serializing collections to be sent across some socket connection… in which case, YES, an array would be the fastest most certainly… arrays are fairly straightforward objects for serializing. A Dictionary is fairly complex to serialize.
Dictionary uses a hashtable. This means lookups are WAY faster since it’s nearly an O(1) operation as opposed to an O(n) operation in a List. In a List to lookup, it has to loop through every entry and test if it’s the one we’re looking for. If the entry we are looking up happens to be the last entry… you just looped the length of the whole collection.
If it’s a Dictionary, it generates the hash, finds the bucket in which that hash should be, and tests if that matches… if it doesn’t, it’s not in the collection. No need to loop over ALL entries. It only has to compare against the small handful of objects who happen to be in the same hash bucket. A collection of 100+ objects might only have 2 or 3 colliding hashes… if it was a list, that’s 100 comparisons, in the dict, it’s 2 or 3.
Furthermore, yes… mono is implemented slightly different from Microsoft’s .net, furthermore the mono used by unity is a bit outdated and not as optimized (hence the GC being meh). I’ve dug through the source of the mono implementation a lot, and there are things it does slightly slower than MS.net, and somethings it does better. For the most part… especially for collections (which are fairly standard algorithms in the industry), they perform near one another.
Use the collection that meets your needs.