Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2015-05-09
08:59 • Closed ticket [9ec7bd5f32]: Some way to modify complex data structures efficient... artifact: 8a10ef6310 user: ferrieux
08:59 • Ticket [9ec7bd5f32]: 3 changes artifact: 37cacac0db user: ferrieux
04:40 • New ticket [9ec7bd5f32]. artifact: 5ba44d1c5b user: anonymous

Some way to modify complex data structures efficiently

Closed / Invalid9ec7bd5f329df1ff790266b3c318a837ac32895eCreated 12 years agoUpdated 12 years ago
Descriptionplain2015-05-09 04:40:48
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
ferrieuxplain2015-05-09 08:59:18
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.
ferrieux Closed ticket [9ec7bd5f32]. [History diff]2015-05-09 08:59:46
Log in (anonymous is fine) to add a comment.