Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: e184db058f221241b76778d64ed5a97f555a5fb5
Ticket: 507d9b9651a3c90339c8808b48cc55b08459c0e9
upvar shortcut for procs
User & Date: dah on 2016-11-26 12:43:19
Changes

  1. assignee changed to: "nobody"
  2. closer changed to: "nobody"
  3. cmimetype changed to: "text/plain"
  4. comment changed to:
    Proposed patch against trunk to short circuit variable linking for procs (and perhaps tclOO if the approach taken in the patch is acceptable). This adds support for the '*' syntax to formal parameters to declare that the formal will be a future link to some variable.
    
    The approach taken emits no bytecodes -- they're not necessary because the syntax allows Tcl to know at compile time how to handle the parameters. Why do at runtime, when it can be done at compile time? It should yield a speed boost to hot code using it in place of upvar calls and impact on other code should be neglible.
    
    There are issues with the patch, namely, errors need proper handling. But all tests passed (in 8.6.6). I consider this just a hack at this time until someone that knows better thinks otherwise.
    
    Included below is a bench.tcl script to demo how this would look (also in patch).
    
    # doNothing - 77107 microseconds per iteration
    # doHack - 82073 microseconds per iteration
    # doUpvar - 163975 microseconds per iteration
    
    proc doNothing {a b} {
        set a bar
        set b baz
    }
    
    proc doHack {*a *b} {
        set a bar
        set b baz
    }
    
    proc doUpvar {a b} {
        upvar $a la
        upvar $b lb
        set la bar
        set lb baz
    }
    
    proc bench {} {
        # Warmup the caches first
        set c {}
        set d {}
        for {set i 0} {$i < 100000} {incr i} {
            doUpvar c d
        }
    
        puts [time {
        for {set i 0} {$i < 100000} {incr i} {
            doNothing c d
        }
        }]
    
        set a {}
        set b {}
        puts [time {
        for {set i 0} {$i < 100000} {incr i} {
            doHack a b
        }
        }]
    
        puts [time {
        for {set i 0} {$i < 100000} {incr i} {
            doUpvar c d
        }
        }]
    }
    
  5. is_private changed to: "0"
  6. login: "dah"
  7. priority changed to: "5 Medium"
  8. private_contact changed to: "45acae529cab3ab98979b4963483a82f50bb42e5"
  9. resolution changed to: "None"
  10. severity changed to: "Minor"
  11. status changed to: "Open"
  12. submitter changed to: "dah"
  13. subsystem changed to: "07. Variables"
  14. title changed to: "upvar shortcut for procs"
  15. type changed to: "RFE"