• Re: Accessing Tcl dict from C API

    From Harald Oehlmann@21:1/5 to All on Tue Jan 16 16:43:51 2024
    Am 16.01.2024 um 16:17 schrieb Ralf Fassel:
    | Can I (efficiently) access nested dicts with the C API?

    I would say 'yes', since in nested dicts the values are themselves just
    dicts instead of plain... well... 'values'. Haven't tried that myself, though.

    I confirm that this works: nested dicts from C.
    There is no buildin for it, so it should be programmed manually:

    TCL_OBJ * subdict;

    if (TCL_OK == Tcl_DictObjGet(interp, dictPtr, keyPtr, subdict)
    && TCL_OK == Tcl_DictObjGet(interp, subdict, keyPtr, valuePtrPtr) )

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ralf Fassel@21:1/5 to All on Tue Jan 16 16:17:34 2024
    * Alexandru <[email protected]>
    | I have some C functions that perform fast operations on lists of numbers.
    | Until now, I transfer Tcl data in the shape of lists to those C functions.
    | In some cases the Tcl data is stored in dictionaries so I run some Tcl
    | loops over the Tcl dict, put the needed data into lists and finally
    | pass the lists to the C function.
    | Since performance is paramount, I'm thinking that the above method might not be the fastest.
    | If I would directly pass the Tcl dict to the C function, would that be faster?

    Not copying data sounds always faster to me...

    https://www.tcl.tk/man/tcl8.6/TclLib/DictObj.html

    lists the C interface to dicts. Basically you get the TclObjs for the
    keys and/or values in the dict. You would then call Tcl_GetIntFromObj()
    on the value ptr.

    | What if the Tcl dict is "nested" so there are multiple keys that lead to a value?

    You would need some information on the nesting depth, since the value
    has no "I'm a dict" label attached. You could check whether Tcl_GetIntFromObj(valueptr) is successful and if not, try to use it as multiple-level dict.

    | Can I (efficiently) access nested dicts with the C API?

    I would say 'yes', since in nested dicts the values are themselves just
    dicts instead of plain... well... 'values'. Haven't tried that myself, though.

    HTH
    R'

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ralf Fassel@21:1/5 to All on Tue Jan 16 18:14:13 2024
    * Harald Oehlmann <[email protected]>
    | Am 16.01.2024 um 16:17 schrieb Ralf Fassel:
    | > | Can I (efficiently) access nested dicts with the C API?
    | > I would say 'yes', since in nested dicts the values are themselves
    | > just
    | > dicts instead of plain... well... 'values'. Haven't tried that myself, though.

    | I confirm that this works: nested dicts from C.
    | There is no buildin for it, so it should be programmed manually:

    | TCL_OBJ * subdict;

    | if (TCL_OK == Tcl_DictObjGet(interp, dictPtr, keyPtr, subdict)

    I *think* that needs to be

    Tcl_DictObjGet(interp, dictPtr, keyPtr, &subdict)

    since Tcl_DictObjGet() takes a TclObj** as last parameter.

    R'

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)