Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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.


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.


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.


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)?




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: