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

Re: Size-of instance



Ken Tilton wrote:
> To Digitool--how about a size-of function?

Here's a way to tell the size, in bytes, of a CLOS
instance. It should be as accurate as Bill's meter-consing
macro, and it has the additional advantage of being able
to operate on an instance after it's created. Bear in
mind, however, that it's not portable and it won't be
accurate for forwarded instances (instances whose class
definitions have changed). Perhaps someone can improve
this to deal better with forwarded instances. (For
forwarded instances, should it show the total of the
old and the new instances or just the new one???)

The minimum size of a CLOS instance is 16 bytes: 8 for the pointer
to the class wrapper and 8 for space for a forwarding pointer.

See the documentation for uvsize etc. in your MCL manual
for more info.

(defmethod sizeof ((me standard-object))
  (* 8 (uvsize me)))

#|
(defclass foo () (slot1 slot2))

(meter-consing (make-instance 'foo)) ; you may need to do this twice to
  ; get things initialized
-> Consed 24 bytes  

(meter-consing (setf f (make-instance 'foo)))
-> Consed 24 bytes

(sizeof f)
-> 24

|#


-- 
Shannon Spires
svspire@sandia.gov