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

a question about an efficiency tradeoff with flavors

   Date: Wed, 18 Jul 90 17:31 EDT
   From: Qobi@zermatt.lcs.mit.edu (Jeffrey Mark Siskind)

   (defflavor F (I) () (:readable-instance-variables I))

   (defun G (F) (H (I F)))


   (defmethod (G F) () (H I))

I think the second form should be faster.
:READABLE-INSTANCE-VARIABLES is essentially a shorthand for

(defmethod (I F) ()

So, in the first case you invoke two ordinary functions (G and H) and one
generic function (I) and access one instance variable.  In the second
version you invoke one ordinary function (H), one generic function (G), and
access one instance variable.

One wrinkle is that the :READABLE-INSTANCE-VARIABLES doesn't actually
define such a method, but sets a flag in the generic function object that
indicates that it is an instance variable accessor.  I presume that this
flag is checked during generic function dispatching, so it never actually
funcalls a method.  If this makes the I generic function call as fast as an
ordinary function call, and we assume full generic function calls take
twice as long as ordinary function calls, then the two versions are

I think the best answer is that they are so close that it shouldn't be a
consideration, and you should decide based on object-oriented design issues
rather than performance.  The first form fixes the implementation of G for
all flavors, while the second form allows it to be redefined for other
flavors.  On the other hand, the first form permits some flavors to
implement I as a true method rather than just an instance variable
accessor, while the second one will only access I as an instance variable
(a flavor that defines an I method would probably also need to redefine G
to use it).

A third possibility is

(defmethod (G F) () (H (I self)))

This is probably slower than both of your forms (assuming the optimization
I described above, this takes the time of four function calls and one i.v.
access, versus three and one for your forms), but it is better
object-oriented design in my opinion.  It permits a flavor to redefine or
augment either the I method alone or the whole G method.