[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Saving Instantiations of CLOS Objects

Jerry Crane writes:

>Trying to find a method to save the slot values of a series of instantiated
>CLOS objects as a cohesive block vs. recording individual values and 
>re-instantiating all objects each time this program is needed.  Several of
>the slots are pointers to the same or different classes of objects.  Ideally,
>should be able to save to file and reload without total reprocessing.

I have written a binary load/dump package for PCL; it is very much
faster than the ascii approach, particularly when large amounts of
numeric data is involved.  It is faster yet, by a factor of 2 or 3, if
you implement the actual low-level binary i/o in C using the foreign
function interface.  It has the added property that any structure that
is eq in the original environment is eq in the restored environment.
Actually, I usually use a version that cheats on the eq-ness of list
tails, since that cuts down on recursion and recording of structure
and is seldom really useful, at least in my applications.  

It involves a kb-mixin class for giving dump-load behavior to classes,
and a kb class, instances of which contain sets of instances (of
kb-mixin subclasses) that are to be collected into a single file.
There are binary dump-load methods for the basic lisp data types, and
a subclass of kb-mixin that simply dumps all the slots of its
instances (you use (pcl::class-slots (class-of thing)) to get at the
slots).  Each new item that is dumped is indexed in a big hash table
on the way out, so subsequent occurrences of the same object simply
dump the index.  Loading starts by building an array the size of the
largest index, and keeping track of objects by index as they are read

Ideally, you probably want to sort a complex interconnected network
before dumping, to minimize forward references (references to objects
that haven't been instantiated yet), but I didn't bother.  I just used
allocate-instance and initialize-instance separately.

It is more than just a few pages, but it is well documented and there
is a Microsoft Word document that goes with it.  I could probably get
it released if anyone is interested.  I am told that the
make-load-form function coming out in the new version of ACL will
probably supercede this functionality, so I haven't worked on it for
quite a while.

					John Collins