Forum OpenACS Development: Re: New Tcl API proc "package_exec_plsql" ...

Collapse
Posted by Jun Yamog on
Hi,

This package procs are nice.  Lars pointed me to it.

Regarding the ns_sets and lists.  Maybe Ben came to the same situation as me.  Initially started using list of lists for bcms when passing stuff from proc to proc.  It was nice and easy at first.  Then as time went by, I ended up making code to parse the list of lists.  Me and Deds talked a bit, he used  ns sets.  I then started to experiment with list of ns sets.  It turned out to be better, but it was cludgy at start.  Maybe because I wasn't used to ns sets.

Anyway I am not sure if my experience does apply... But hey Happy New Year! :)

What is the advantage of ns_set over arrays and array get/set?
Guan, AFAIK there is no advantage. There are only two good reasons to use an ns_set rather than a Tcl array:
  1. You're using an AOLserver API which uses ns_set.
  2. Maybe because you need to allow multiple key/value pairs all with the same key. ns_set supports this directly, Tcl arrays do not.
Many ns_set operations scale very poorly (O(n) with the number of items in the set), and the ns_set API is generally less powerful and more annoying to work with than the Tcl array API.

On the other hand, the ns_set C code is very simple and thus might be easier than Tcl arrays to embed in other C code.

For use from C code, using ns_set could also be easier than Tcl arrays in some cases, as ns_set has an explicit and complete API in both Tcl and C. Tcl arrays, on the other hand, don't really have any complete C API. Some Tcl array operations are really easy from C, but for others you'll need to code up your own (relatively simple) helper functions. E.g., I needed to write one for "array names", basically it's just a wrapper around Tcl_ArrayObjCmd. (Yes you can just call Tcl_Eval from C to execute the "array names" Tcl command directly, but this is a Bad Idea if you're doing this from within an inner loop; the performance hit can be very large.)

I'd say always use a Tcl array (or nsv when appropriate) by default, never use an ns_set unless you have a very specific and concrete reason why you should do so.

Actually, ns_set looks like a legacy data structure to me, I'd never choose to use it for anything new. Even if I really needed a set-like data structure that supports duplicate keys, I probably wouldn't use ns_set, I'd code my own using Tcl Arrays (and maybe Tcl lists too) underneath.