12/14/2023 0 Comments Stack vs heap yotuebSo I would say after 2k the advantage of space saving for stack starts to diminish. If the object is >2k, then the 2-ptr size also is <1%. There is an important point which is for small allocations you save the 2-ptr sized words if you allocate on the stack. Of course stack has more chances to be have better locality and heap allocations can also be more expensive when we use the freelist in gen0, but since we only go to the GC for memory every few kbytes, the effect of using the freelist shouldn’t be too bad (If I use 100 cycles as an approximation of reading the freelist and 1 cycle to clear 1 byte, 100/8192 = 1.2%). This is about how you make the decisions based on performance.Īt the allocation time, in the best case heap allocations can be as fast as stack allocations – they both advance a pointer and clear memory they hand out. You already know there are syntactic things that you can do with references that you can’t do with structs like you can’t inherit from a struct so I won’t focus on those here. This is meant to help you understand these implications better so you can think how they fit into your scenario. Often the right approach is to understand what things mean, then see what consequences they have in your specific context. It makes sense to understand the implications of allocating on the stack vs on the heap in order to make the right decisions because as with most perf advice, there’s no one rule fits all. I searched around and there are plenty of articles that talk about *what* gets allocated on the stack vs on the heap, but not how you should decide what to allocate on the stack vs heap. The question there was “when should I allocate something on the stack vs on the heap?”. A recent discussion prompted me to write this blog entry.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |