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)

301 Upvotes

156 comments sorted by

View all comments

Show parent comment

52

u/StickiStickman Jul 13 '23

Why? That's like saying it's sad that farmers don't still have to till the field manually with a hoe

51

u/cdb_11 Jul 13 '23

It's more like saying that farmers only learn how to operate modern tools without any understanding how farming actually works, in turn leading to a misuse of those tools and loss of quality and efficiency. Like you know, harvesting at the wrong time or something like that. Which would be indeed sad.

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?

4

u/fakehalo Jul 13 '23

My original background was C (decades ago) and at more primitive level than your average C developer as I was focusing on memory corruption exploitation/software auditing at that time, along with the software I'd write in C was almost always focused on speed and having as a small of a memory footprint as possible.

I still get a solid plan when I'm writing server-side stuff that has the potential to become a bottleneck, but for almost anything client-side all of that goes out the window. You might as well assume I'm a guy who doesn't know how anything works because I apply almost zero of my knowledge when I'm working in that domain, as it has almost no impact.

13

u/empire314 Jul 13 '23

A farmer does not need to know inner workings if a plant cell composition.

Sure, for most programmers it is useful to know something about memory, not all do. But this piece goes way deeper than what is in any way useful for almost anyone.

7

u/Randolpho Jul 13 '23

It's more like saying that farmers only learn how to operate modern tools without any understanding how farming actually works,

Eh… a better metaphor would be farmers not knowing how the biology and chemistry of the plants they grow works.

They know to plant seedlings at certain times, etc, but couldn’t explain how the cells of those seedlings work.

Farmers who do understand how all that works might be better theoretical farmers, they might push the boundaries of crop yield, or develop disease resistant strains of crops for other farmers to use, but the vast majority of farmers just need to sow, harvest, and distribute.

Of course the metaphor breaks down when you realize that the overwhelming majority of farms are corporate run with unskilled labor doing the work… “farmers” don’t actually exist the way we imagine they do these days.

2

u/siemenology Jul 13 '23

If this was an article detailing how memory works as a general principle, I'd agree with you. But there's about 10 pages of how memory works in general, and 100 pages of nitty-gritty details, many of which apply to a particular architecture or OS.

It's like teaching farmers the finer points of running the mass spectrometer used in labs to test nutrient levels. Farmer's need to know where to go for lab tests, and how to interpret the results and what they mean, but most of them don't need to spend a ton of time learning how the lab tests actually work. And the bit they do need to know can be summed up briefly without the fine details, ie "soil tests aren't as useful for nitrate levels due to their volatility, have tissue analysis done and adjust nitrogen fertilization based on those results" instead of going into the chemistry of nitrates in soil vs plant tissue.

3

u/wndrbr3d Jul 13 '23

This 100%.

Just like I'm not disappointed people don't have to rub sticks together to make fire.

1

u/AttackOfTheThumbs Jul 13 '23

Tilling is killing.

2

u/diegoasecas Jul 13 '23

permaculture ruined more minds than crack cocaine

1

u/StickiStickman Jul 14 '23

The plow is now