TclOO Method Dispatch

APN Notes on what I have learnt about TclOO method dispatching - see [L1 ]

DKF: In terms of implementation, a few extra notes. There is an internal function called TclOOGetCallContext that returns a call-chain handle for a method, constructor or destructor, and which is called when you invoke the method. The call chain is then fixed for the life of the invocation of of the method; TclOOInvokeContext (the other major function in this area) then just walks the chain. TOGCC uses caches heavily, at all of the object, class and Tcl_Obj method name levels.


One thing I've discovered (probably relates more to variable access as opposed to dispatch) is that it is significantly cheaper to declare a variable in the class definition as opposed to using my variable in the method itself. For example,

class create C {
   constructor {val} {my variable v ; set v $val}
   method m {} { my variable v ; return $v }
   method n {} { return 5 }
}

class create D {
   variable v
   constructor {val} { set v $val }
   method m {} {return $v}
   method n {} { return 5 }
}

The numbers below show D.m is almost twice as fast as C.m, presumably because the variable resolution is all done in C at method dispatch time as opposed to needing a separate my variable call at script level.

(woof) 70 % C create c 8 ; D create d 8
::d
(woof) 71 % time {c m} 10000
7.6739 microseconds per iteration
(woof) 72 % time {d m} 10000
3.9759 microseconds per iteration
(woof) 73 % time {c n} 10000
3.643 microseconds per iteration
(woof) 74 % time {d n} 10000
3.583 microseconds per iteration

Significant in a real application? Who knows.