These operations on the stack are very fast. The stack can only be modified by adding or subtracting elements from the end, analogous to a Vector with only the two mutating operations push! and pop!. The stack is a simple data structure with a beginning and end, similar to a Vector in Julia. Inside RAM, data is kept on either the stack or the heap. I found a post What scientists must know about hardware to write fast code by BioJulia Developer Jakob Nybo Nissen: I have had the same questions as the OP for some time. Thanks to and for their lucid explanations. There is error handling when using the heap so you can take action (ALLOCATE will fail with a STAT= specifier being set). If it runs out, there’s no recovery and your program will crash if you’re lucky (and will initiate WW3 if you are not). There’s no error handling when using the stack. The stack doesn’t have holes but it can run out quicker because it is generally provisioned smaller than the heap by the operating system. This creates “holes”, leading to inefficiency: some memory requests cannot be satisfied because no hole is big enough (even though the total hole space is more than enough). On the heap, recycling is not predictable: any item could be the next to be recycled. It’s called a stack because we always recycle the last thing we ordered (it shadows procedure entry and termination). But some Fortran variables are meant to survive completion of procedures (they have the SAVE attribute), so they cannot be put on the stack. Stack use ties lifetime of variables to lifetime of procedures, a nice abstraction. Only one procedure is executing at one time (ignore multithreading for now). In addition, there is the lifetime issue: the stack area is recycled at the time of completion of the procedure. The stack area of memory is private to the procedure, the heap is shared by all procedures (currently executing or not). Shared resources are slower than private resources because of the overhead of managing the sharing. These are the major reasons why people say the “stack” is faster than the “heap”, even though they all come from the same place physically. Stack variables do not need to be “freed”, which is also a slow process. Stack variables do not require allocating on the heap, which is managed by a memory allocator, which is a slow process. Stack variables cannot be accessed from another thread, so the compiler can assume they aren’t going to change. Even then, stack variables are often used enough that they are kept in the CPU cache. “Register spill” is when the compiler has to put variables on the stack because it ran out of registers. “Stack variables” are generally used often enough that the compiler doesn’t even put them on the stack, it just keeps them in a register. The heap is allocated dynamically as the program runs, so the compiler doesn’t know where the variables are coming from, reducing optimization opportunities. The difference is that the compiler generally knows where to find variables that are on the stack when it compiles the code. The stack and the heap come from the same place: the computer’s RAM.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |