Go back to last state

2019-03-01 09:56发布

问题:

Is there a way to go back to last state of the runtime? In short, save the runtime and reload it. But the core image is too big (I'm making a small game :-), so I come up with an idea, save some important data, and start running at certain line (maybe also the stack info).

For example:

(defun save ()
  _do-some-magic-to-save-the-state-and-then-exit_)

(defvar data (list 'a 'b 'c)) ; important data
(format t "Hello ")
(save)
(format t "World!~%")

Next time, the software can start at the point where it stopped.

$ software
Hello $ software
Hello $ software --load saved_state
World!

But I don't know how to do this in Common Lisp at all... Could someone please give me some ideas?

回答1:

How this works depends on the Common Lisp implementation. Consult the manual:

  • Clozure Common Lisp -> Saving Applications
  • SBCL -> Saving a Core Image
  • LispWorks -> Save Image
  • CLISP -> Saving an Image

  • several other implementations ...



回答2:

You might want to try implementing Alex Warth's Worlds (chapter 4 of his thesis). This would allow you to "sprout" a new World, run your computation in it, and either periodically commit the changes in the child world to the parent world, or abort and roll back to the parent's state. This is like an infinite undo, but you can sprout multiple worlds/chains of undo.