Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Native Collections

Discussion in 'Scripting' started by OlegHCP, Apr 10, 2019.

  1. OlegHCP

    OlegHCP

    Joined:
    Mar 7, 2017
    Posts:
    47
    Perhaps I'm missing something but size of the the Unity NativeArray structure is 104 bytes. Probably it would be better to add reference modifiers to existing Unity functions which receive native collections as argument (schedulers, extensions, etc.).
    Variables with size larger than one hundred bytes in memory stack don't look like good solution.
     
  2. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    I’m not sure I fully understand your issue. What is the problem that 104 bytes cause versus a struct with 100? Can you perhaps give a (non-abstract) example? Do you experience a problem as a result?

    -ch
     
    Joe-Censored likes this.
  3. OlegHCP

    OlegHCP

    Joined:
    Mar 7, 2017
    Posts:
    47
    "One hundred" isn't a technical threshold but just a figure of speech. I meant NativeArray has too big size for structure.
     
  4. ZAXIS

    ZAXIS

    Joined:
    Sep 30, 2016
    Posts:
    23
    the author means
    Array.Sort(ref array); 

    vs
    Array.Sort(array); 
     
  5. OlegHCP

    OlegHCP

    Joined:
    Mar 7, 2017
    Posts:
    47
    The author means using modifier "in" (not "ref"), so your code even will not change and will remain as Array.Sort(array);
     
  6. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    I still don't get it. What's the actual issue here? That the code pushes too many bytes on the stack during an invocation? When is that ever going to be an issue? Unless you recurse the hell out of this, I don't see an issue. Why do you even care how much memory a frame pushes on the stack, especially if we are in the <1k range? Sure, many Compilers will push a reference once the size of a struct goes beyond 4 bytes, but methinks this is primarily a relic from the time when we hat 16 bit processors (read: 68000 or 8086) with a severely restricted stack size. I could even argue that pushing 104 bytes on the stack/heap during an invocation is *more* efficient nowadays than allocating a struct in heap memory, referencing it, and then GCing it after returning (IIRC when compilers allocate local memory for a new invocation they add it to the Frame size, which is essentially the same as pushing it on the stack (disclaimer: depends on the processor and memory model)).

    Now, there's a good chance I missed the original point, so: can you give usa good, concrete example how this is a problem and what the benefit of using a reference would be in normal, day-today tasks?
     
    Last edited: Apr 11, 2019
  7. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,294
    NativeArray is an DOTS feature, so you should probably post this in the correct subforum.
     
  8. OlegHCP

    OlegHCP

    Joined:
    Mar 7, 2017
    Posts:
    47
    There is no difference where to write. Unity team doesn't read these threads and doesn't care at all. Nothing will change.
     
  9. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Unity devs often watch the forums for new features or features in preview. The linked correct forum for this has Unity devs commenting all over the place. They won't be looking for threads on that topic in unrelated forums though, that you can be certain of. If you're always posting in the wrong forum, that would explain why you don't think Unity devs ever read your threads (because in that case you're likely correct).
     
    Suddoha likes this.
  10. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,515
    This is not true. I have gotten three bugs fixed in Unity in the past 7 years by going through the bug filing steps, sometimes after first posting in a forum and getting told by a Unity rep to file it.

    What you report regarding array size seems like more of a heuristic that you feel strongly about, perhaps based on some solid experience. But keep in mind that engineers on compiler teams are generally Pretty Darn Smart(tm), and they use instruments such as profilers and cache analyzers to make decisions about architecture changes.

    If you're seeing a churk in your game, attach the profiler, and actually prove that it is related to a 104-byte struct vs a 100-byte struct and post your findings. But again, it should be in the correct group, as pointed out above.

    Good luck!
     
    Joe-Censored likes this.