r/programming • • Jul 13 '23

What Every Programmer Should Know About Memory

https://people.freebsd.org/~lstewart/articles/cpumemory.pdf

This resource is starting to get a bit older. But it's such a good resource for learning about memory.

Hope you like it (it's a pdf)

296 Upvotes

156 comments sorted by

View all comments

Show parent comment

89

u/Brian Jul 13 '23

Is it?

How much misuse do you think a web developer will commit because they don't know the difference between NUMA and UMA? Or that RAM cells usually have 6 transistors? Or that RAM has a refresh cycle every 64 ms, or the difference between full and set-associative caches?

how farming actually works

But that's the thing - no farmer knows how farming actually works. They stop at a level of abstraction that makes sense for them: the detailed biology of how the bacteria in a cow's gut breaks down their grass is important, sure, but not usually something they have to care about themselves - they just care that that bacteria is there and doing its job.

At some level, your understanding of how something "actually works" is only as deep as the deepest abstraction you use, and no-one's abstractions go all the way down. Few programmers understand the quantum mechanics that let modern transistors work, for instance, and unless you're working in chip fab, it probably wouldn't help you if you did. Now, there are advantages to knowing a few abstractions lower than what you're building on: abstractions do tend to be leaky, and understanding the underlying mechanism can help. But that web programmer won't make any fundamental errors on par with a farmer harvesting at the wrong time just because they don't know that cache prefetching won't cross page boundaries, for the same reason the farmer won't because they don't know the the metabolic full cycle of gut-bacteria - it's too much below the abstractions they're working with to matter.

Which is not to say that other programmers won't benefit from knowing it - but only really if they're working at a lower level: an OS kernel say, or maybe detailed benchmarking where RAM refresh could make a difference. But I think most programmers only really need to know some basics (Roughly what virtual memory does, caches are faster than RAM - so pack stuff together). Even then, at higher levels of abstraction you can get away without it.

-6

u/cdb_11 Jul 13 '23

Sure, I agree, a farmer probably doesn't necessarily has to know it in that much detail. But I think the difference is that the farmer will understand the general implications of that, while most programmers just don't understand the implications of how memory works at all. I think everyone should be aware of the implications or just the fact that this matters a lot, even if it isn't of any use for you right now. Because then if you'd eventually like to stop writing slow programs, you can always go back and learn it.

20

u/Synaps4 Jul 13 '23

I think you're arguing for programmers to know about....2 pages worth of memory stuff, not the 114 presented here.

1

u/cdb_11 Jul 13 '23

I don't know if it's just 2 pages, but sure, just the gist of it is probably good enough for the general public. I think there is a miscommunication going on here and you want me to debate whether the headline is correct or not, while I was just talking about what the contents actually say and that it's kinda important to know if you care about performance.

-9

u/AppearanceHeavy6724 Jul 13 '23

I mostly disagree with you. You at least need to know about caches if engage in any computation intensive programming (which is not by any means low-level); you'd also benefit from knowing the memory-related stuff if say writing an interpreter.
Now even more subtle point is that if you know this kind of stuff you'd still be a better programmer, it is much like you study calculus in high school - most people would not need it anyway in their lifes, but it will help them to remember less complex yet very useful things such exponents and logarithms.

21

u/Brian Jul 13 '23

You at least need to know about caches if engage in any computation intensive programming (

I disagree. You don't need to know anything more than the basics that I mentioned (ie. they exist, are faster than RAM, and you should take advantage of that by helping with locality). You do not need to know the intricacies of full associative vs set-associative caches etc.

if you know this kind of stuff you'd still be a better programmer

I did mention that there's value in knowing a few abstractions down - but there's diminishing returns the deeper you go below what you're working on, and the majority of what is discussed here is deeper than will matter for most programmers.

-10

u/AppearanceHeavy6724 Jul 13 '23

Yeah well, to get full advantage of locality you at least need to know the cache line size and the fact one cache line can kick out the other one, if they have same tag. Otherwise you'll end up with code optimized for locality performing even worse the unoptimized one. (like here https://stackoverflow.com/questions/12264970/why-is-my-program-slow-when-looping-over-exactly-8192-elements)

3

u/NotUniqueOrSpecial Jul 13 '23

But those kinds of locality and memory optimizations largely apply at the level of native languages.

Tons (most?) programming these days is in higher level languages with fancy JITs, their own runtimes, etc., and those don't give you this level of control.

V8's gonna align your ArrayBuffer as it sees fit, and you can't do jack about that.

2

u/AppearanceHeavy6724 Jul 13 '23

You'd be surprised how these rules are still applicable to JITted languages too, and how often you need to implement computationally intensive code in JavaScript (say an emulator). Let alone Java and C# which are in fact almost low-level languages (you can have SIMD intrinsics in your code in C#).

Really, the same rule - do not traverse arrays in the wrong order is equally valid in javascript or even python too.

1

u/Full-Spectral Jul 14 '23

How much misuse do you think a web developer will commit because they don't know the difference between NUMA and UMA?

Thurman?