0

For example mov ax, [bp, - 2] Takes from stack. And mov ax, word [some_Label] Takes from heap. I know that stack is much faster than heap because of the 1D'ish. But using mov opcode as same method to take 2D'ish, doesn't it take the "speed from stack away"? [Srry for bad english :)].

Edit:
Ok, so the text above is ancient can't believe I was so dumb, and still am XD.

So the question above meant to ask that: what is the difference between memory opcodes, that use the stack to load the data from, and those opcodes that use the heap to load the data from.

If I remember correctly what I meant by 1D'ish and 2D'ish was, that stack operates up and down, meanwhile heap has Page offset and a cell offset. This however is not true and I was wrong.

Answer:
Heap is slower at allocation time, because you need to ususally to call the kernel to give you the requested amount of memory (or if the user uses a smart allocator to allocate the wanted memory, then probrably the allocator already has already allocated a big memory buffer to give the user the memory from (this again is somewhat slower than stack allocation but atleast little faster than straight kernel level memory allocation requests.)) meanwhile stack allocation is just a substraction of stack register offset (RSP in x86_64). Thus allocating stack memory is faster than heap. But using mov operator to move data in/out from a heap/stack memory is the same speed.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Dr.Gray
  • 11
  • 4
  • And thx for giving you're time to help me ;) – Dr.Gray Feb 22 '19 at 17:47
  • 1
    Potential duplicate of https://stackoverflow.com/questions/49595006/is-stack-memory-contiguous-physically-in-linux – Hadi Brais Feb 22 '19 at 18:51
  • 1
    Possible duplicate of [Is stack memory contiguous physically in Linux?](https://stackoverflow.com/questions/49595006/is-stack-memory-contiguous-physically-in-linux) – Urosh T. Feb 22 '19 at 19:52
  • Can you clarify what you mean by "1D'ish" and "2D'ish". I'm not familiar with those terms. – prl Feb 22 '19 at 21:02
  • 1
    *"I know that stack is much faster than heap"* ? no? It's the same computer memory. The real performance may show some differences because "stack" area is highly likely in cache most of the time, while one often puts lot more data into "heap" area, making it harder for cache, but there's no physical difference in the memory chip, or in the `mov` instructions in your example. Any performance difference will come from other things (caching, access pattern, code itself, etc..). – Ped7g Feb 25 '19 at 11:20
  • Please post answers separately, click "answer my own question" and post that last paragraph. (It's correct, BTW. There are also details like lazy allocation leading to a page fault in newly-allocated memory, but that's true whether it's stack or mmap/malloc, and can be lumped in with the cost of heap allocation. The difference is that freeing a large heap allocation will usually give it back to the OS, so allocating again will once again pay the penalty for a page fault. While stack memory is usually not reclaimed, so it's like malloc keeping it on a free-list instead of munmap) – Peter Cordes Apr 02 '22 at 00:29

0 Answers0