Tcl Source Code

View Ticket
Login
Ticket UUID: 1101710
Title: Enhanced "if" bytecoding for [if $e ...]
Type: RFE Created on: 2005-01-13 15:30:56
Submitter: robert_seeger Assigned to: msofer
Subsystem: 47. Bytecode Compiler Severity:
Priority: 4 Last modified: 2006-01-09 22:31:02
Status: Closed Closed by: sf-robot
Resolution: Invalid Closed on: 2006-01-09 15:31:02
Version: None
Description:
When bytecompiling [if $e ...] vs [if { [expr $e] }
..], the second version is faster, due to the
bytecompiler for "if" not performing an optimization
that it most likely could. Specifically, it can be seen
in the information below that the "if" is just pushinhg
"if" with the arguements and "invokeStk1", whereas the
"expr" is adding the variable to the stack, then
calling "exprStk", "tryCvtToNumeric", and "jumpFalse1".

I believe the "if" command could benefit from doing the
same as the "expr" command in this circumstance.


---- showing the problem ----
#!/bin/sh
# This line continues for Tcl, but is a single line for
'sh' \
    exec tclsh "$0" ${1+"$@"}

set expr1 { 1 < 2 }

time { if $expr1 {} else {} } 10000
time { if {[expr $expr1]} {} else {} } 10000

load ~/cvs/bytecode/libbytecode.so

proc withBrace {e} { if { [expr $e] } { return 1} else
{ return 2 } }
proc withoutBrace {e} { if $e { return 1} else { return
2 } }
proc noVar {} { if { 1 < 2 } { return 1 } else { return
2 } }

puts "--- withBrace: [time {withBrace $expr1} 100000]"
puts "--- withoutBrace: [time {withoutBrace $expr1}
100000]"
puts "--- noVar: [time {noVar } 100000]"

puts "--- withBrace\n[bytecode::get withBrace]"
puts "--- withoutBrace\n[bytecode::get withoutBrace]"
puts "--- noVar\n[bytecode::get noVar]"

return

--- withBrace: 6 microseconds per iteration
--- withoutBrace: 9 microseconds per iteration
--- noVar: 3 microseconds per iteration

--- withBrace
Header {...} Procedure {
    ReferenceCount 1
    NumArgs 1
    CompiledLocals 1
    Slots {
        0 { Slot 0 Type {scalar arg} Name e}
    }
} CompiledLiterals {
    0 1
    1 2
} ExceptionRanges {} Commands {
    0 {Number 1 PC {0 13} Src {1 47}
       Code {"if { [expr $e] } { return 1} else {
return 2 } "}}
    1 {Number 2 PC {0 2} Src {7 13} Code {"expr $e"}}
    2 {Number 3 PC {6 8} Src {20 27} Code {"return 1"}}
    3 {Number 4 PC {11 13} Src {37 45} Code {"return 2 "}}
} ByteCodes {
    0  {Number  0 Name loadScalar1
        Operands {{Type OPERAND_UINT1 Value 0 Note {var
"e"}}}}
    2  {Number  2 Name exprStk         Operands {}}
    3  {Number  3 Name tryCvtToNumeric Operands {}}
    4  {Number  4 Name jumpFalse1
        Operands {{Type OPERAND_INT1 Value 7 Note {pc
11}}}}
    6  {Number  6 Name push1
        Operands {{Type OPERAND_UINT1 Value 0 Note {"1"}}}}
    8  {Number  8 Name done            Operands {}}
    9  {Number  9 Name jump1
        Operands {{Type OPERAND_INT1 Value 5 Note {pc
14}}}}
    11 {Number 11 Name push1
        Operands {{Type OPERAND_UINT1 Value 1 Note {"2"}}}}
    13 {Number 13 Name done            Operands {}}
    14 {Number 14 Name done            Operands {}}
}
--- withoutBrace
Header {...} Procedure {
    ReferenceCount 1 NumArgs 1 CompiledLocals 1 Slots {
        0 {Slot 0 Type {scalar arg} Name e}
    }
} CompiledLiterals {
    0 if
    1 { return 1}
    2 else
    3 { return 2 }
} ExceptionRanges {} Commands {
    0 {Number 1 PC {0 11} Src {1 36}
       Code {"if $e { return 1} else { return 2 } "}}
} ByteCodes {
    0  {Number  0 Name push1
        Operands {{Type OPERAND_UINT1 Value 0 Note
{"if"}}}}
    2  {Number  2 Name loadScalar1
        Operands {{Type OPERAND_UINT1 Value 0 Note {var
"e"}}}}
    4  {Number  4 Name push1
        Operands {{Type OPERAND_UINT1 Value 1 Note {"
return 1"}}}}
    6  {Number  6 Name push1
        Operands {{Type OPERAND_UINT1 Value 2 Note
{"else"}}}}
    8  {Number  8 Name push1
        Operands {{Type OPERAND_UINT1 Value 3 Note {"
return 2 "}}}}
    10 {Number 10 Name invokeStk1
        Operands {{Type OPERAND_UINT1 Value 5}}}
    12 {Number 12 Name done        Operands {}}
}
--- noVar
Header {...} Procedure {
    ReferenceCount 1
    NumArgs 0
    CompiledLocals 0
} CompiledLiterals {
    0 1
    1 2
} ExceptionRanges {} Commands {
    0 {Number 1 PC {0 14} Src {1 44}
       Code {"if { 1 < 2 } { return 1 } else { return 2
} "}}
    1 {Number 2 PC {7 9} Src {16 24} Code {"return 1 "}}
    2 {Number 3 PC {12 14} Src {34 42} Code {"return 2 "}}
} ByteCodes {
    0 {Number 0 Name push1
       Operands {{Type OPERAND_UINT1 Value 0 Note {"1"}}}}
    2 {Number 2 Name push1
       Operands {{Type OPERAND_UINT1 Value 1 Note {"2"}}}}
    4 {Number 4 Name lt Operands {}}
    5 {Number 5 Name jumpFalse1
       Operands {{Type OPERAND_INT1 Value 7 Note {pc 12}}}}
    7 {Number 7 Name push1
       Operands {{Type OPERAND_UINT1 Value 0 Note {"1"}}}}
    9 {Number 9 Name done Operands {}}
    10 {Number 10 Name jump1
        Operands {{Type OPERAND_INT1 Value 5 Note {pc
15}}}}
    12 {Number 12 Name push1
        Operands {{Type OPERAND_UINT1 Value 1 Note {"2"}}}}
    14 {Number 14 Name done Operands {}}
    15 {Number 15 Name done Operands {}}
}
User Comments:
dkf added on 2005-01-13 23:37:18:
Logged In: YES 
user_id=79902

Wouldn't it be wonderful if that was both possible and easy? :^(

 * [expr [rename expr expr2; subst 0]] compiles wrongly, but
for thoroughly evil reasons.
 * [if {$e} { foo } elseif [set e 1] { bar }] means that you
have to process expressions in a strange order even if you
ignore the breakage noted above.

robert_seeger added on 2005-01-13 23:57:19:
Logged In: YES 
user_id=1081558

Indeed, it appears as if the [if { [expr $e] } ...] version
is faster not because "if" doesn't bytecompile when it
should, but because "expr" does when it shouldn't :(

sf-robot added on 2006-01-09 22:31:02:
Logged In: YES 
user_id=1312539

This Tracker item was closed automatically by the system. It was
previously set to a Pending status, and the original submitter
did not respond within 14 days (the time period specified by
the administrator of this Tracker).