Checkpoint and Heap Recovery in Ruby

Ruby callcc captures the current continuation, which you can later call to restore the control, but not the data. I would like to capture the current continuation along with the current memory image.

It seems to me that grabbing a heap does not have to be very difficult; I can rely on ObjectSpace::each_object and ObjectSpace::dump_all , or Marshal.dump , or just Object.clone . However, I do not see a direct way to restore the heap. Ideally, I would like to cross the map object_id -> object , restoring the old image of the object for each object_id (and re-adding object_id if the corresponding object was GC'd). No wonder Api doesn't have a Ruby level that allows me to do this. I am wondering if there are any low level hooks for Ruby GC that I can use.

Any help is appreciated, including suggestions for alternative approaches.

+8
garbage-collection ruby callcc
source share
2 answers

To answer my own question, Process.fork can be used for a more or less effective checkpoint heap and recovery effect. Whenever I have to check a bunch, I develop a new process and let the child continue. Now the parent process contains a control heap:

 def checkpoint if Process.fork.nil? then # if child, then resume execution immediately return else # if parent, wait for the child to exit. Process.wait end return # Parent now resumes execution from state it was in before forking. end 

When the state needs to be restored, the child process simply ends:

 def restore Process.exit end 

I am currently using this solution and still have not encountered any problems. I will edit this answer if I find it in the future.

+1
source share

How about pushing each one onto the stack? Then you can continue to pop up the stack to get the data.

https://github.com/bvsatyaram/Ruby-Data-Structures

0
source share

All Articles