Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2008-03-08
23:50 • Closed ticket [1881766fff]: Mutability plus 6 other changes artifact: 2891dc2baa user: ferrieux
2008-01-30
07:22 • Ticket [1881766fff]: 4 changes artifact: 9c139e23fd user: ferrieux
07:22 • Add attachment mutability851rc0.patch to ticket [1881766fffffffff|18817... artifact: 99cdba33b9 user: ferrieux
07:18 • Ticket [1881766fff] Mutability status still Open with 4 other changes artifact: 381bb740ba user: ferrieux
07:18 • Ticket [1881766fff]: 4 changes artifact: e9861bafc8 user: ferrieux
07:16 • Add attachment mutability850.patch to ticket [1881766fffffffff|1881766f... artifact: c76b5ac950 user: ferrieux
07:16 • Ticket [1881766fff] Mutability status still Open with 4 other changes artifact: 1b31f8bc5f user: ferrieux
2008-01-29
19:12 • Ticket [1881766fff]: 4 changes artifact: fd303ccdd8 user: ferrieux
19:11 • Ticket [1881766fff]: 4 changes artifact: a3c32aa224 user: ferrieux
12:11 • New ticket [1881766fff]. artifact: 56306d3c8d user: ferrieux

Mutability

Closed1881766fffffffffffffffffffffffffffffffffCreated 19 years agoUpdated 19 years ago
Descriptionplain2008-01-29 12:11:41
This experimental patch, which could make it to a TIP eventually, adds optional "mutability" to container values (Lists, but the same could be done to Dicts).

This means that when explicitly requested by the programmer (by the command prefix "mutant"), in-place operations may occur regardless of the sharedness of the object. Hence several other values may be simultaneously updated.

The purpose is to allow passing a deeply nested substructure and modifying it in-place, without knowledge of its surroundings.

Here are a few examples illustrating usage:

  # create a mutable
  set m [mutable [list 1 2 3]]
  # let another var hold it
  set n $m

  # mutate it
  mutant lappend m 4
  puts $m --> 1 2 3 4
  puts $n --> 1 2 3 4

  # use it like an immutable in all unaware code (no "mutant")
  lappend n 5 ;# this forks an immutable snapshot
  puts $n --> 1 2 3 4 5
  puts $m --> 1 2 3 4 (mutable value untouched)

  # now a deeper substructure
  set m [mutable [list 1 2 3]]
  set n [mutable [list 4 5 6]]
  set p [mutable [list 7 8 9]]
  mutant lappend m $n
  mutant lappend n $p
  puts $m --> 1 2 3 {4 5 6 {7 8 9}}
  mutant lset p 1 foo
  puts $m --> 1 2 3 {4 5 6 {7 foo 9}}

  # the cases that are not yet forbidden, but shoud shortly be:
  set l [list 1 2 3 [mutable [list 4 5 6]]] ;# contagion violation
  set m [mutable [list 1 2 3]];mutant lappend m $m; # cycle
ferrieuxplain2008-01-29 19:11:42
File Added - 264123: mutability850.patch
ferrieuxplain2008-01-29 19:12:49
File Added - 264124: mutability851rc0.patch

Logged In: YES 
user_id=496139
Originator: YES

File Added: mutability851rc0.patch
ferrieuxplain2008-01-30 07:16:31
File Added - 264230: mutability850.patch

Logged In: YES 
user_id=496139
Originator: YES

File Added: mutability850.patch
ferrieuxplain2008-01-30 07:18:15
File Deleted - 264124:
ferrieuxplain2008-01-30 07:18:16
File Deleted - 264123:
ferrieuxplain2008-01-30 07:22:17
File Added - 264232: mutability851rc0.patch

Logged In: YES 
user_id=496139
Originator: YES

Update:
  - properly per-interp and mutexed => thread- and slave- safe
  - debug info under the control of ::tcl_traceMutable
  - Mass Invalidator list is made of Tcl_Obj to reuse allocator
  - test suite OK including mutability.test

Todo: contagion; acyclicity; Dicts
File Added: mutability851rc0.patch
ferrieuxplain2008-03-08 23:50:24
Logged In: YES 
user_id=496139
Originator: YES

Miguel found a conceptual show stopper: iterators.

Indeed, the caller and the callee of a mutability-unaware iterator (like [foreach], which could be made mutability-aware, but extensions are allowed to declare other similar unaware functions) can cooperate to render invalid pointers kept locally in the iterator's implementation (like a pointer into a mutable list's elements array).

The bottom line is that "cheating" with Tcl_IsShared is fundamentally problematic. Hence the only sound way of promoting in-place operations is to make it relatively easy to render objects truly unshared, through idioms like upvarring a global, *and* to implement optimized in-place modes wherever it is useful, like in [lrange] (see patch 1890831).

Consequently, I close this tracker entry.
Log in (anonymous is fine) to add a comment.

Attachments: