this post was submitted on 08 Apr 2025
267 points (99.6% liked)
Games
21348 readers
118 users here now
Tabletop, DnD, board games, and minecraft. Also Animal Crossing.
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 gamers allowed

- No squabbling or petty arguments here. Remember to disengage and respect others choice to do so when an argument gets too much
- Anti-Edelgard von Hresvelg trolling will result in an immediate ban from c/games and submitted to the site administrators for review.

- Can't read Colon Syntax Emoji? :skill-issue:
founded 6 years ago
MODERATORS
The compiler will see that
var3is 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.var1andvar2may be removed from the output as well; shorter code runs faster since you can fit more in the cache. In fact, sincevar3is 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.4fwherever 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
jackApplesas a "static variable" (as opposed to making it eg. a field in a class or struct):The most obvious consequence of this is that
jackApplesnow 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:textsection, which contains all of the executable code, and which might be made read-only by the OS.datasection, which contains variables that have a known value at startupbsssection, 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,
jackAppleswill be in thedatasection; 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.