this post was submitted on 08 Apr 2025
260 points (99.6% liked)
games
20845 readers
218 users here now
Tabletop, DnD, board games, and minecraft. Also Animal Crossing.
-
3rd International Volunteer Brigade (Hexbear gaming discord)
Rules
- No racism, sexism, ableism, homophobia, or transphobia. Don't care if it's ironic don't post comments or content like that here.
- Mark spoilers
- No bad mouthing sonic games here :no-copyright:
- No gamers allowed :soviet-huff:
- No squabbling or petty arguments here. Remember to disengage and respect others choice to do so when an argument gets too much
founded 4 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
The compiler will see that
var3
is just two numbers added together and replace it with 7, which saves having to do an addition every time you run through that code, and is therefore faster.var1
andvar2
may be removed from the output as well; shorter code runs faster since you can fit more in the cache. In fact, sincevar3
is just a number, you can replace every place that it's used with a 7 as well; if you have some functions:... then the compiler will look at all that, delete the lot, and just use
1.4f
wherever theappleWeight()
function was called. Comment is gone, the decision making is gone, it's impossible to go backwards any more.I'm not a professional programmer and just a hobbyist, but if you also had a set function that changes jackApples to an input integer, what happens at compilation?
That disables a whole pile of the potential optimisations, of course. You could define
jackApples
as a "static variable" (as opposed to making it eg. a field in a class or struct):The most obvious consequence of this is that
jackApples
now has an address in memory, which you could find out with&jackApples
. Executable programs are arranged into a sequence of blocks when they're compiled, which have some historical names based on what they used to be for:text
section, which contains all of the executable code, and which might be made read-only by the OS.data
section, which contains variables that have a known value at startupbss
section, which contains variables that we know will exist but don't have a value. Might be zero'd out by the OS, might contain unknown leftover values.Because it's statically allocated,
jackApples
will be in thedata
section; if you opened up the executable with a hex editor, you'd see a 3 there.getTheNumberOfApples()
will be optimised by the compiler to return the contents of the memory address plus 4. That still counts as a very simple and short function, and it's quite likely that the compiler would inline it and remove the initial function. The actual process of calling a function is to:That takes a while, and worse - modern CPUs will try to "pipeline" all the instructions that they know are coming so that it all runs faster. Jumping to a function might break that pipeline, causing a "stall", which slows things down enormously. Much better to inline short functions - the fact that the value is "number in memory address plus four" might be optimised away a little wherever it's used, too.