Mārtiņš Možeiko
2452 posts / 2 projects
Casey's break down of the keywork "static"
Edited by Mārtiņš Možeiko on

Inlining is about extra optimizations compiler can potentially do. Often that allows to remove large pieces of unused code, or eliminate common subexpressions. As a result less code is generated - meaning more bytes fit into CPU code cache and fewer bytes to execute, meaning code runs faster.

For example: https://godbolt.org/z/6T1KebExb

You can see in right side of code where inlining is forbidden (or just in general not possible), the compiler generates so much more code. Versus Left side compiler where everything is inlined to simple one instruction mov eax, 450 - way more efficient code.

If you're asking about how code cache works - that is the same as data cache. Every time CPU wants to read next bytes of instruction to execute it must be in L1 cache. If it isn't then it will stall waiting for fetching it from main memory. That's why it prefetches them ahead of time - same way how data prefetching works. Nothing different there.

65 posts
Casey's break down of the keywork "static"
Edited by da447m on

Thanks for explanation. Basically that inline calculated the whole loop at compile time and replaced with the result? What if that's not possible to know at compile time? Does inline even do any good in that case?

That's why it prefetches them ahead of time - same way how data prefetching works. Nothing different there.

In the case of data, it is conceptually more clear how to try to optimize for cache, e.g. not bloating structs with a lot of stuff that can be processed separated, etc.

How to reason over doing the same with functions? I'm aware this is a hard question and kinda unrelated to hmh. Is this even a thing in Data Oriented Design? Or "hey try to make your functions compact" is really the best we can do?

Is Casey's code using some sort of data oriented design that is noticeable? Or is he for now simply getting logic done first?

Mārtiņš Možeiko
2452 posts / 2 projects
Casey's break down of the keywork "static"