On Tuesday, 23 March 2021 at 12:06:18 UTC,
oleg.o....@gmail.com wrote:
> Folk, help me please with some questions about tcloo:
> - Is a performance of tcloo methods the same as ordinary proc?
Probably a little slower, as it takes more work to manage the stack frames. There's more going on under the covers (to enable calling superclass implementations via [next], etc.) and that's not free. But it depends on what you're doing; there are scenarios where it's possible to make back the performance due to TclOO having a variable resolver that you can (slightly) program.
> - Is a performance of tcloo methods the same as proc that exported from a namespace?
The additional cost of an exported procedure is very very small, so I expect my answer above to still hold.
> - Can i call tcloo methods and access object variables from C as easy as an ordinary proc and variables?
Calling methods from C is a bit messy; the supported interface is via Tcl_Eval() or Tcl_EvalObjv(). Directly calling a method is categorically not supported; they depend on having a context handle that can't be minted by user code (because it hooks into the non-recursive execution engine and uses trickery to get fast memory management).
Variables are just variables in the object's namespace.
If you're thinking of doing a lot of this, try doing a hybrid class, where you have non-exported methods in C that implement the bits you can't do any other way, and exported methods in Tcl to tie things together into a nice interface. That's one of the easiest ways to implement an object that participates in a coroutine in a complex way: writing NRE-aware C code isn't particularly easy.
> - Can i implement some tcloo object in C as easy as an ordinary proc?
Pretty close. Tcl_NewMethod() is conceptually much like Tcl_CreateObjCommand(), but the type signature of main implementation function is different (there's an additional parameter, the context, which describes what the current object is and what the current method call chain is) and the way you pass the implementation function in is a bit different too. Inside the implementation function, you mostly have to be careful to not assume the number of arguments used to “name” your call; that's non-constant (unavoidably). There's an API function to tell you how many arguments are your name (expected to be 2 or 1, for [$obj methName ...] vs [next ...]).
There's multiple options for how you attach C structures to an object, some of which properly associate with a particular method and others go more directly on the object (and can be shared between cooperating C methods). The distinction has to be made because the state of a method is not the state of an object; classes can be inherited from, and that adds a lot of complexity if you're not careful.
Donal.