News for hackers. Stuff that matters.
Re:I Like and Use Global Variables by Cthulhu_ on Monday February 10, 2025 @04:22AM (#42998433)
I've seen code for emulators that is very long functions and a lot of globals; I'd also argue they're OK if your program flow is very stable / predictable and synchronous, and especially older consoles and game engines have predictable phases. Getting information from your "tick" to your "render" stage is a lot easier if you have global state.
Re:I Like and Use Global Variables by pjc50 on Monday February 10, 2025 @04:56AM (#42998689)
Emulators are often emulating "hardware globals", things like machine registers or GPU state where in the thing being emulated there really is only one instance.
Re:I Like and Use Global Variables by flohofwoe on Monday February 10, 2025 @08:30AM (#43000011)
Unless it's an arcade machine like Bombjack which has two Z80s and three AY sound chips ;)

It's still possible to wrap all that as nested state in a single global, and it works just fine:

https://github.com/floooh/chips-test/blob/613dbfe907104f4110...

That's a single mutable global with all application state, but inside that global are nested structs and the code working on those structs takes essentially a 'this' pointer. This approach works well since it enforces a strictly hierarchical code and data structure with a single root-struct (and also a single root-source-file) at the top.

Re:I Like and Use Global Variables by Joker_vD on Monday February 10, 2025 @05:38AM (#42998902)
A useless microbenchmark question: what is faster, accessing a global variable (which has to go through the GOT nowadays) or dereferencing a field in a structure that was passed to you by a pointer (a straightforward register+offset load)?