Skip to content
Tcl Source Code History Of Ticket 9ec7bd5f329df1ff
Login

History Of Ticket 9ec7bd5f329df1ff

Artifacts Associated With Ticket 9ec7bd5f329df1ff

  1. Ticket change [5ba44d1c5b] (rid 141713) by anonymous on 2015-05-09 04:40:48:

    1. assignee initialized to: "nobody"
    2. closer initialized to: "nobody"
    3. cmimetype initialized to: "text/plain"
    4. comment initialized to:
      It seems that TCL can only tackle efficiently complex data structure that are only composed of lists or only composed of dicts. But when there is a mix of data structures (lists and dicts) and it's necessary to modify an internal node, TCL makes a copy and modifies that copy, instead of modify directly the internal node.
      
      Here there is an example:
      
      
      set complex_datastructure [dict create a b c [list 1 2 3]]
      
      # We get the list inside the dict
      set subnode [dict get $complex_datastructure c]
      
      # We retrieve the internal representation and we observe that subnode is a list. The pointer reference is 0x98c1270.
      puts [tcl::unsupported::representation $subnode]
      
      # Return value: value is a list with a refcount of 3, object pointer at 0x98b6f08, internal representation 0x98c1270:(nil), no string representation
      
      # We change the subnode
      lset subnode 0 10
      
      # We get the internal representation again but the pointer reference is different: 0x98b7838!
      puts [tcl::unsupported::representation $subnode]
      
      # Return value: value is a list with a refcount of 2, object pointer at 0x98b7838, internal representation 0x98caa40:(nil), no string representation
      
      
      # The pointer of the original data structure subnode is the same as the begining. It didn't change: 0x98c1270
      puts [tcl::unsupported::representation [dict get $complex_datastructure c]]
      
      # Return: value is a list with a refcount of 2, object pointer at 0x98b6f08, internal representation 0x98c1270:(nil), no string representation
      
    5. foundin initialized to: "8.6"
    6. is_private initialized to: "0"
    7. login: "anonymous"
    8. priority initialized to: "5 Medium"
    9. private_contact initialized to: "1c4b6cdb98c9dfc4d1e74018f3f4efa00ad217e2"
    10. resolution initialized to: "None"
    11. severity initialized to: "Minor"
    12. status initialized to: "Open"
    13. submitter initialized to: "anonymous"
    14. subsystem initialized to: "- New Builtin Commands"
    15. title initialized to:
      Some way to modify complex data structures efficiently
      
    16. type initialized to: "Bug"
  2. Ticket change [37cacac0db] (rid 141714) by ferrieux on 2015-05-09 08:59:18:

    1. icomment:
      The reason why we modify the copy, is that there are still live references to the original instance (refcount>1). It turns out that there is no other sound way to do it, given the string semantics of the language (look for "Mutability patch" in comp.lang.tcl archives to witness a failed attempt to wander off).
      
       Most Tcl list-manipulation primitives are optimized to work in-place when refcount is 1. So, when it matters, arrange for refcount to be 1. Note this is not trivial. Two gotchas:
      
        - don't pass the value-to-be-modified-in-place as proc args, because it will add references from the (live) call stack, which you cannot nuke.
      
        - use the "K-free-K trick" (like the K combinator, without defining it): assuming the last reference is in the (local) var "x" holding it, pass the value 
      
           $x[unset x]
      
      to the list-manipulation primitive. E.g, for in-place truncation:
      
           set x [lrange $x[unset x] 0 1000]
      
      The case of pass-by-varname primitives is a bit different. Lset is one such beast. The copy-on-write principle applies all the same, except that there is no need to unset the var (nor is it an option, since we're passing a varname !), but you still need to make sure the var is the only remaining reference. When this is so, a [representation $x] yields a refcount of 2 (one for the var, and one for the stack reference, i.e. the argument passed to [representation]).
      
      In your case, $subnode has a refcount of 3 because one reference remains in the containing dict. This prevents lset from working in-place. Again, when it matters, explicit ref surgery is due. In your case, temporarily severe the subnode from its container:
      
           set subnode [dict get $complex_datastructure c]
           dict unset complex_datastructure c
           lset subnode 0 10
           dict set complex_datastructure c $subnode
      
      Note that, contrarily to the K-free-K, this idiom can be hidden in a utility function, since the important value is deep inside a container rather than passed as an argument.
      
      Summary: such tricks
      
            - are a necessary evil due to the value semantics in Tcl;
            - have a cost in terms of code complexity, and should be pulled only when it matters, ie for big values;
            - don't have a fixed or obvious indexing algorithm (what if the value is buried in a dict-of-lists-of-dict ?)
      
      
      For these reasons, ad hoc code like the above will always be the best approach, on a case-by-case basis, and no magical primitive in the core will ease that. Hence, closing, sorry.
      
    2. login: "ferrieux"
    3. mimetype: "text/plain"
  3. Ticket change [8a10ef6310] (rid 141715) by ferrieux on 2015-05-09 08:59:46:

    1. closedate changed to: "2457151.87484022"
    2. closer changed to: "ferrieux"
    3. login: "ferrieux"
    4. mimetype: "text/plain"
    5. resolution changed to: "Invalid"
    6. status changed to: "Closed"