- Home /
Resizing builtin arrays
I'm using arrays which must be accessed many times every frame, so I'm using builtin arrays. However I often need to resize these arrays, remove entries, add entries etc. I know you can do it with js arrays but they are much slower. As I see it I have two options:
// Using only builtin arrays function DeleteThingUsingOnlyBuiltin(index) { // check that index exists in the array, if not return null if (index >= thingCount) { return null; } thingCount--; var thingPositionTemp = bPos; thingPosition = new Vector3[thingCount]; for (var i=0;i<thingCount;i++) { var ii; if (i<index) {ii=i; } else {ii=i+1; } thingPosition[i] = thingPositionTemp[ii]; } }
// Using JS arrays function DeleteThingUsingJSArray( index : int ) { // check that index exists in the array, if not return null if (index >= thingCount) { return null; } thingCount--; var jsArray = new Array (thingPosition); jsArray.RemoveAt(index); thingPosition = jsArray.ToBuiltin(Vector3); }
The JS version is neater and easier to implement (especially as there are more attributes than this in the actual code), but is it going to slow me down significantly compared to the purely builtin version? I will still do all my array ACCESSING through builtin arrays.
Answer by Eric5h5 · Aug 05, 2010 at 11:46 PM
Using generic Lists is faster than dynamic arrays. Javascript has generics in Unity3, so it's possible to use them now if you have access to the beta. A static built-in array will always be faster since there's less overhead, so if you really need to get every last bit of speed possible at the expense of convenience, then stick with built-in arrays. But Lists are fast enough in many cases where Arrays are too slow.
Answer by jashan · Aug 05, 2010 at 12:33 PM
To be honest, as long as it's just once per frame and not for hundreds or thousands of objects, I wouldn't worry too much about performance. When you're on the iPhone, things look different, of course.
Personally, I avoid using arrays as much as possible and use proper generics classes (List) as those are much more convenient to use. Internally, those use arrays most of the time and handle the "adding stuff to the list / removing stuff from the list" efficiently for me.
Have you done any performance comparison between the different types of arrays? That will probably show you if it's worth the hassle at all. You can't use generics in JavaScript, so don't worry about those ...
It would be quite a bit more than once per frame unfortunately... and I script in UnityJS, so I'm guessing the Lists are out?
I did a bunch of stuff in builtin arrays, then converted to completely use JS arrays and it ground to a halt.
Assu$$anonymous$$g performance is critical, is option 1 in my question going to be faster than option 2? Even if it's $$anonymous$$imal, it will all add up.