Jump to content

Question About RAM Access

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
Negative_3

Negative_3

    Newbie

  • Members
  • Pip
  • 1 posts
Hi, in C the RAM addresses of variables and stuff are not the actual addresses, just the addresses within the program's section of the memory.

So if I wanted to see what is in address 0x123456 within my program's section of the memory, the program doesn't just grab spot 0x123456 in the RAM (or cache if it happens to be there at that moment), it has to translate the local address 0x123456 into the corresponding actual address in the hardware. Now supposedly the operating system does this.

Does this not slow everything down? I know it makes things a lot more stable, but this sounds like it would make programs 3 times slower than if all address were that of the real ones in hardware.

#2
G_Morgan

G_Morgan

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 537 posts
This is very hardware dependent. Page table lookup is usually inefficient and x86 goes through 2 such tables (as many as 4 for x86_64 IIRC). However we have a tool called the translation lookaside buffer which is basically a set of registers (i.e. extremely fast memory) which contains the 7 most recently used page table entries. By the principle of locality of reference we know that you will spend a lot of time within a few pages, when you access one byte you usually access the byte next to it and so on. Therefore by storing recent address translations you remove most of the overhead of page table lookup.

Within programs, all you are using is an offset to the code segment register. Since the base pointer is within a register this is also fast access.

In the general case you have an offset pointer in a register and a base pointer also in a register. Adding these is reasonably fast and gives you the logical address for page table lookup. Usually the appropriate page will be in the TLB so that's also register access. Therefore it's only 3 registers accesses in the general case and you have your physical address. Quite often the offset pointer will be in a memory location itself so that will take longer.