5 ms·
Serialization [1]. Say you want to take a snapshot of where your program is, run the code some more, and then later on restore execution back to that snapshot.
by byuu 7y ago
Serialization [1].
Say you want to take a snapshot of where your program is, run the code some more, and then later on restore execution back to that snapshot.
In the parlance of video games and emulators, this would mean save states. You could use these save states to implement speculative execution to eg remove input latency from games using a method called run-ahead [2].
With state machines, this is trivial: just record all the state machine variables. With coroutines / cooperative threading, it's not: the state is all stored on the stack frame.
My stackful coroutine library can do this by essentially memcpy'ing the stack frames themselves, but it comes with some seriously negative caveats: the biggest is you can't save this to disk, because on the next program run, ASLR, etc would invalidate the pointers in the stack frame. In fact, you can't even safely release and allocate new memory between a serialize and unserialize point, or the stack frames may have dangling pointers in them. In practice, this is acceptable and workable for features like real-time rewind and run-ahead.
Library support for C++ coroutine serialization would be trivial since they are stackless. Getting around the limitations I mentioned for stackful coroutines would be immensely more challenging.
[1] https://byuu.net/design/cooperative-serialization https://byuu.net/design/cooperative-serialization
[2] https://byuu.net/input/run-ahead https://byuu.net/input/run-ahead
- speedplane 7y ago> Getting around the limitations I mentioned for stackful coroutines would be immensely more challenging. Challenging but it's definitely been done before. The system below [1] (disclosure: I worked on it years ago), stores every memory write in a separate buffer on the assembly level. You need to modify the compiler for this, and it cuts performance roughly in half, but it allows you to step backwards in code and perform sophisticated performance analysis. [1] https://www.ghs.com/products/timemachine.html https://www.ghs.com/products/timemachine.html
- byuu 7y agoOh certainly, it's just beyond what I can do as a C library. I've also seen another approach that implemented a customized libc replacement to allow this to work. But that and modifying the compiler itself are really extreme steps. It's still doable, and it's definitely worth doing, but I understand the uses I've mentioned are rather niche, so it's unlikely to occur. For one, we still don't have built-in stackful coroutine support even in C++20.
- speedplane 7y ago> modifying the compiler itself are really extreme steps Yes, it's not for the average developer. Yet it should be incumbent on the tools builders. I'm actually surprised that most compilers/interpreters don't have a "step backwards" flag that you can turn on during compile/run-time. It would be a helpful feature to expert and novice developers alike, but hasn't really caught on outside of niche areas.
- aidenn0 7y agoCan't you just disable ASLR?