Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: be3634ed6458cc8b753c68262a49ea0e20cfef86
Ticket: 1993441fffffffffffffffffffffffffffffffff
Lightweight pair type 'cons'
User & Date: ferrieux on 2008-06-13 22:53:00
Changes

  1. assignee changed to: "nobody"
  2. cmimetype changed to: "text/plain"
  3. comment changed to:
    The following type introduces a new objtype 'cons', which is simply a two-element list (named by analogy with Lisp's fundamental data structures). The advantage over plain lists is the absence of heap-allocated space for the internal rep, since twoPtrValue is enough to hold the two pointers.
    
    The script-visible part is as follows:
    
     - [cons $a $b] creates a Cons of elements $a and $b
     - the string repr of [cons $a $b] is the same as [list $a $b]'s
    
    Important details behind the scenes:
    
     - [lindex] and [lsort -index] access Conses transparently *without shimmering*
    
     - all other list ops do shimmer as they should, giving them the intuitively expected semantics (By going through the string rep for now. A shortcut is imagineable).
    
    Timing results below.
    
    Summary: 
    
    - Cons is nearly 2x faster and 1.7x smaller-memory than List for building a large (10 million) list of pairs.
    
    - This list is typical of an [lsort -index] task
    
    - The [lsort -index] speed is then exactly the same
    
    Perspective:
    
    The cons can be a useful replacement for lists everywhere a small structure is needed. That is notably the case of the Maybe monad proposed by Neil Madden to solve the NULL issue:
    
        [cons 0 ""] -> Nothing
        [cons 1 $x] -> Some $x
    
    
    
    ***CONS***
    
    Alex@DELLALEX /src/cvs/tcl/win
    $ ./tclsh86.exe
    % proc g n {set l {};for {set i 0} {$i<$n} {incr i} {lappend l [cons $i $i]};return $l}
    % time {set big [g 10000000];set x 1}
    4288955 microseconds per iteration
    % time {set big [g 10000000];set x 1}
    3746134 microseconds per iteration
    % time {set big [g 10000000];set x 1}
    3697388 microseconds per iteration
    
    % time {set srt [lsort -index 0 -integer -decreasing $big];set x 1}
    2301849 microseconds per iteration
    % time {set srt [lsort -index 0 -integer -decreasing $big];set x 1}
    2683979 microseconds per iteration
    % exit
    (process VM ~474M)
    
    
    ***LIST***
    
    Alex@DELLALEX /src/cvs/tcl/win
    $ ./tclsh86.exe
    % proc f n {set l {};for {set i 0} {$i<$n} {incr i} {lappend l [list $i $i]};return $l}
    % time {set big [f 10000000];set x 1}
    7609976 microseconds per iteration
    % time {set big [f 10000000];set x 1}
    6912197 microseconds per iteration
    % time {set big [f 10000000];set x 1}
    7021424 microseconds per iteration
    
    % time {set srt [lsort -index 0 -integer -decreasing $big];set x 1}
    2304322 microseconds per iteration
    % time {set srt [lsort -index 0 -integer -decreasing $big];set x 1}
    2703814 microseconds per iteration
    % exit
    (process VM ~796M)
    
    
    
  4. foundin changed to: "None"
  5. is_private changed to: "0"
  6. priority changed to: "5 Medium"
  7. resolution changed to: "None"
  8. status changed to: "Open"
  9. submitter changed to: "ferrieux"
  10. subsystem changed to: "None"
  11. title changed to: "Lightweight pair type 'cons'"
  12. type changed to: "Patch"