r/AskComputerScience • u/bovine_scatology • Oct 30 '19
Why is data transferred between cache and RAM in blocks?
Why blocks?
28
u/vk6flab Oct 30 '19
When you get the mail from your letterbox do you go there and get one letter, go back inside, rinse and repeat?
Or, do you pick up all the letters in one go?
The same is true for getting stuff from RAM.
4
u/SafeForWorkLife Oct 30 '19
I like this explanation.
Mine is: Most likely you will be need more than one thing from that memory block.
1
u/javaHoosier Oct 31 '19
Is there a standard size for the block? How is that decided?
2
u/vk6flab Oct 31 '19
I don't know, but I doubt it.
It's likely dependent on the combination of CPU and cache size, perhaps taking into account what the CPU is doing.
Given the nature of hardware development, I suspect that it's dynamic.
2
1
u/Minymioke Oct 31 '19
How large are your hands? You can carry a lot, but it won't help to go with your hand full if you have to drop some to open a door, or if you also have to pick the newspaper.
1
u/kickopotomus Nov 01 '19
It is decided by the underlying architecture of the CPU. Cache line size for most modern desktop/server architectures (Intel, AMD, and ARM) is 64 bytes.
However, lower power embedded CPUs/SOCs generally have smaller caches and cache lines (if they have them at all).
1
u/javaHoosier Nov 01 '19
Thanks, most of my CS degree has been data structures, algorithms, and several classes over subfields of ai. I feel like I have a huge gap of knowledge when it comes to cpu architecture and OS concepts.
1
u/kickopotomus Nov 01 '19
Anytime! If you want to get a better understanding of what exactly is happening at the hardware level, nand2tetris offers a pretty good bottom-up approach.
3
Oct 31 '19
It's to reduce the amount of bookkeeping maintained by the cache.
The cache must track which parts of main memory (RAM) are stored within it. This bookkeeping (metadata, if you will) itself consumes memory within the cache itself.
To cache a single byte of main memory, the cache must know where the byte resides within main memory. This is the byte's address, which in 64-bit CPUs could be as large as 8 bytes (though in practice less). So, to cache a single byte requires 8 bytes of metadata, a huge inefficiency.
To reduce this overhead, the cache instead stores ranges of bytes which have contiguous addresses in main memory. This way, for each range, only the start address and the number of bytes need to be kept. To reduce the overhead further, each range is constrained to have the same number of bytes that is a power of 2, and the start address is constrained to be multiples of that power of 2. This way, only the start address, divided by that power of 2, needs to be maintained per range.
These ranges of main memory that start and end at each power of 2 are called cachelines. Assuming a cacheline size of 64 bytes (or 2^6 bytes) and an address size of 48 bits, the cache would require 42 bits of overhead to store 512 bits of main memory.
0
u/011101000011101101 Oct 31 '19
Because reading from disk is slow compared to ram. Once you're in there you might as well grab more stuff because you're probably going to need it.
1
u/ideletedmyredditacco Oct 31 '19
this question has nothing to do with reading from disk
1
u/011101000011101101 Oct 31 '19
I was thinking of migrating data from the page cache to ram, not transferring between the cpu chip caches to ram. However the same principle hold true that the ram is significantly slower than the chip caches
23
u/Try-Catch-Block Oct 30 '19
Spatial locality