Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 7164a8c7b54145a7fdbb2c27ef624a82837386f3
Ticket: 1692378fffffffffffffffffffffffffffffffff
TIP 280: performance impact
User & Date: andreas_kupries on 2007-04-03 01:24:01
Changes

  1. assignee changed to: "dgp"
  2. icomment:
    Logged In: YES 
    user_id=75003
    Originator: NO
    
    Attached a test script containing the original p proc, and a q proc using '-exact --'. My results are:
    
    8.5a6
    p/match   7.96166 microseconds per iteration
    p/!match  2.14355 microseconds per iteration
    q/match   1.34635 microseconds per iteration
    q/!match  1.35886 microseconds per iteration
    
    8.4.15
    p/match   1.94189 microseconds per iteration
    p/!match  1.98629 microseconds per iteration
    q/match   2.01993 microseconds per iteration
    q/!match  2.07895 microseconds per iteration
    
    A slow down in the matching case for p, but not for q (-exact --).
    Current theory regarding this slowdown:
    
    In 'q', due to the use of '-exact --' the switch command is deeply byte-compiled, including the branches. This keeps it fast. However in 'p' the generated byte code falls back to the regular command implementation, which 'eval's the found branch. Due #280 this eval is now slower. The non-matching case is still as fast because no branch is found, i.e. there is no slow eval, it is skipped. Does that make sense ?
    
    To test the theory I disabled the code in lines 2933ff and 3002ff in tclCmdMZ.c ... The time for p/match went down to 8.4.15 levels. Refining this further I am coming to the conclusion that the main cause is the call to 'TclListLines' in line 2962. In other words, it is not, as theorized, the eval which is the problem, but the fact that we have to reparse the switch body argument to determine the line the found branch is starting on/at. And we do this every time the switch executed.
    
    Our options to handle this are:
    
    (1) Repeal all of #280.
    (2) Find a way to cache the result of the 'TclListLines' so that the parsing is done only once, and further executions of the same switch use the cached information.
    (3) Find a way to defer the 'TclListLines', i.e. instead of doing it immediately do it only when needed, i.e. when the information is asked for.
    
    No #1 is not very appealing, as we would loose the whole functionality.
    No #3 is conceptually the most elegant IMVHO, but implementation wise I already see problems. Because the EvalObjEx of the switch branch will count line numbers and for that it will need the line the branch is at. I.e. the deferal is not very far. It would work only if the EvalEx counts relative and creates another structure X = <base,relative-line>, where <base> is the defered computation, and X can be defered too. That can become very complex to manage.
    No #2 is conceptually not very elegant, i.e. it feels more hackish. compared to #3 its implementation should however be much easier. We 'only' need a way to associate the 'TclListLines' result with (a) the switch, or (b) the switch body list. Maybe a hashtable keyed by the address of the switch body object. Which is a literal, not moving around ... This is the same idea as used for the byte codes, see the 'lineBCPtr' hash table.
    
    Re-assigning to dgp for review of my comments and ideas. Don, feel free to assign to Donal as well for an opinion, given that switch is his. Well, at least the BC compiler part.
    
    
  3. login: "andreas_kupries"
  4. mimetype: "text/plain"
  5. username: "andreas_kupries"