Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Declaring an array within a while loop, stack or heap? performance?

Discussion in 'Scripting' started by luniac, Jul 24, 2023.

  1. luniac

    luniac

    Joined:
    Jan 12, 2011
    Posts:
    596
    Code (CSharp):
    1. while(shapeArIndex < arShapeDetect.Length)
    2.         {
    3.             string[] currentShapeAr = ParseShapeDetectString(arShapeDetect[shapeArIndex]);
    if i have something like this, would it be better to declare the array outside of the loop for memory allocation reasons? or does it make no difference?

    if i allocate it outside the loop than i can REUSE the same array for all loops, but does the compiler optimize it such that its the same thing in the end?

    EDIT:
    actually my parse function creates an array anyway and returns it so.... idk lol does it matter then if i declare a new array currentShapeAr, and also declare an array in the Parse function that returns to be assigned to currentShapeAr?

    EDIT2:
    actually the number of elements is fixed at 3, so i can just create a 3 element array and pass it as parameter to the Parse function and assign the parsed string to the array directly, so at least i dont have to create 2 arrays every time.
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    Arrays are reference types. You're not allocating here you're just getting a reference to the return of
    ParseShapeDetectString(arShapeDetect[shapeArIndex])
    , unless said method is returning a new array.

    In any case use the profiler to check the differences.
     
    luniac likes this.
  3. luniac

    luniac

    Joined:
    Jan 12, 2011
    Posts:
    596

    Code (CSharp):
    1. ParseShapeDetectString(currentShapeAr, arShapeDetect[shapeArIndex]);
    yea i changed code so i just pass my array currentShapeAr declared and initialized outside the loop with 3 elements fixed.
    So the array is only created once outside loop within function scope, so itll get deallocated after while loop completes and function closes right?


    But i am confused by reference type, if its not allocated here where is it allocated then?

    EDIT:
    haha just realized had to pass the array into function using "ref" keyword otherwise assigning the parsed strings into the array only does it locally in the Parse function, this really screws with my head, it feel to me like im passing the actual array from outside into the Parse function, but somehow it just assigns the new strings locally to a locally created array thats the same as what i passed into the parameter? but outside it stays the same!? it just doesn't make sense to me.
     
    Last edited: Jul 24, 2023
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    You allocate when you make a new instance.

    Code (CSharp):
    1. ClassA a1 = new ClassA(); // allocates
    2.  
    3. ClassA a2 = a1; // does not allocate
    a2
    is just a reference to the same point in memory as
    a1
    instanced, but you're not allocating memory as you do so.

    So it depends on what
    ParseShapeDetectString
    is returning. Is it returning a newly instantiated array? Or is just returning a reference to an existing array in memory?
     
    luniac likes this.
  5. luniac

    luniac

    Joined:
    Jan 12, 2011
    Posts:
    596
    oh duhhh yea of course it allocates on new, brain fart moment.

    yea i changed up my code, before my Parse function initialized a new array and assigned it to Split("_"); and then returned it to be assigned to my outside array which was also initialized with same number of elements so it worked.

    Now i just declare an empty 3 elements array outside the loop, and
    Code (CSharp):
    1. ParseShapeDetectString(ref currentShapeAr, arShapeDetect[shapeArIndex]);
    so with "ref" keyword I can directly assign the passed array to Split("_");

    but where is the array initialized, on the stack?
    if i did a new array initialization inside the while loop, how would the compiler handle that?
     
  6. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    If you initialise a new array in a loop, it, as you would imagine, allocates for every iteration.

    Once again the profiler will inform you of all this.
     
    luniac likes this.
  7. AngryProgrammer

    AngryProgrammer

    Joined:
    Jun 4, 2019
    Posts:
    431
    https://learn.microsoft.com/en-us/dotnet/standard/design-guidelines/guidelines-for-collections
     
    luniac and spiney199 like this.