graphics - Common lisp typecase vs defgeneric runtime analysis -


i'm writing app common lisp uses opengl, , thing has grown i've realized have bit of choice make. have bunch of different classes need code render them , often, i'm considering following code structures doing this:

(defgeneric render (x)) (defmethod render ((x vanilla-foo))    (generic-foo-stuff)    (vanilla-specific-stuff)    (more-generic-foo-stuff)) (defmethod render ((x chocolate-foo))    (generic-foo-stuff)    (chocolate-specific-stuff)    (more-generic-foo-stuff)) 

and on, lots of these methods. other alternative use typecase statement:

(defun render (any-old-foo)    (generic-foo-stuff)    (typecase       (vanilla-foo           (vanilla-specific-stuff))       (chocolate-foo          (chocolate-specific-stuff))       ;;and on, lots of cases here     )     (more-generic-foo-stuff)) 

i suppose these both ugly in own way, assumption lisp using kind of hash table o(1) lookup under hood maps argument type passed generic function location of needed method, whereas second method require o(n) type comparisons work way through typecase statement. on other hand, depending how slow or fast hashing might be, might need thousands of foo flavors before first method faster.

is there performance reason should 1 on other? i'm open other suggestions, or references kind of document make clearer me going on under hood in each case. i'm curious other instances of sort of conflict may have encountered.

thanks!

at least can make code using generic functions less ugly factoring out generic stuff :before , :after methods:

(defgeneric render (x))  (defmethod render :before (x) ; has done first   (generic-foo-stuff))  (defmethod render :after (x) ;  has done last   (more-generic-foo-stuff))  (defmethod render ((x vanilla-foo)) ; vanilla-specific things here    (vanilla-specific-stuff))  (defmethod render ((x chocolate-foo))     (chocolate-specific-stuff)) 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -