Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 936036b030dc5f97eb469f2bfe7df74fe5dcfe3e
Ticket: 3017879fffffffffffffffffffffffffffffffff
proc emptyness is cached too aggressively
User & Date: a_kovalenko on 2010-06-18 04:24:07
Changes

  1. assignee changed to: "msofer"
  2. cmimetype changed to: "text/plain"
  3. comment changed to:
    There is one known, long-standing feature of the TCL bytecode
    compiler: if a procedure is defined with empty body and all-matching
    parameter list {args}, TCL doesn't compile any real procedure call for
    its callers. In the output of tcl::unsupported::disassemble, it's visible as an absence of invokeStk1 instruction where an empty proc is called.
    
    This optimization may be regarded as a special case of inlining, like
    inlining an empty proc body and an all-accepting argument
    check. Inlining doesn't play well with redefinitions and traces, but
    for all years I look at that, TCL developers counted only
    _transparent_ optimizations as acceptable. Various `bytecode epoch'
    counters triggering bytecode recompilation create an illusion that
    there is no byte-compiler at all, it's just a code runs faster.
    
    # ==== Example 1, all ok ======
    proc do-nothing {args} {}
    proc talk {} {
      puts "Going to do nothing.."
      do-nothing 1 2 3 4
      puts "Done nothing successfuly."
    }
    # Here we may take a look at 
      puts [tcl::unsupported::disassemble proc talk]
    # no invokeStk1, just a {push1 ""; pop} pair.
    
    #.......later......
    proc do-nothing {args} {
      puts "You have to do _something_.."
    }
    
    puts [tcl::unsupported::disassemble proc talk]
    # And now we see invokeStk1, do-nothing is really called 
    talk
    # and does something.
    
    Unfortunately, redefining an _imported_ target procedure 
    is not noticed in importing namespaces:
    
    namespace eval foo {namespace export *}
    proc foo::do-nothing {args} {}
    proc talk ....................
    proc foo::do-nothing {args} {puts "doing something.."}
    talk ;# says nothing. 
    # Imported command retained the information somewhere,
    # that its target is an empty proc.
    namespace import -force foo::*
    # Reimporting with -force actually destroys target command
    # before reimport, and triggers byte-recompilation of `talk'
    # Reimporting without -force is regarded as a no-op,
    # as long as target is the same (and as an error otherwise).
    
    I hope for a solution propagating command redefinition down to each
    importing namespace, triggering bytecode recompilation there (it's
    currently possible on the script level, traversing all namespaces and
    their imports and reimporting a redefined command with -force).
    Or, if it turns out to be too complicated, I'd prefer to see this kind
    of bytecode optimization abandoned.
    
    Redefinition of an imported target is a convenient way to provide
    e.g. debugging facilities (zero-cost while disabled, triggering
    recompilation when turned on, low-cost afterwards). It turned out to
    be a fine technique to manage built-in command interception (consider
    an imported ::puts that points either to renamed original `puts' or to
    a redefined, redirecting version). But for the latter, bytecode not
    invalidated when appropriate may cause more severe semantical problems
    than simply not running a procedure that used to be empty.
    
    Several things on the same subject:
    
    1. TCL 8.5.8 release and CVS head expose similar behavior with
       imported proc redefinitons; the problem affects both.
    
    2. If a command in a foreign namespace is resolved via 
       namespace path and _not_ imported, the problem vanishes:
       all bytecode calling it is invalidated as it's redefined
       from "proc name args {}" to something other, and back.
    
    3. Execution traces (enter, leave) on an empty proc aren't invoked
       (even if no namespace import is involved, i.e. in the same (root) 
       namespace). However, I'm not sure that recompiling callers
       when a trace is added is a good thing.
    
    4. If we forget about redefining renamed compilable built-ins, the
       only transition that is semantically problematic is the one from an
       empty proc to a non-empty one. A code generated for non-empty proc
       (pushes + invokeStk1) remains valid for any other proc or command
       definition, including the empty one (if it's ever redefined back).
    
    5. There is a missing feature in namespace imports introspection:
       [namespace origin] spec creates an illusion that all imports of a
       command are like independent links to the same target, and only the
       target is `special' among them. In fact, imports of the same command
       may form a complicated dependency graph, and for now its structure 
       can't be introspected on script level in any non-intrusive way.
       (of course it's a reason for a separate report, maybe a TIP).
    
    
  4. foundin changed to: "obsolete: 8.6b1.1"
  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: "a_kovalenko"
  10. subsystem changed to: "47. Bytecode Compiler"
  11. title changed to: "proc emptyness is cached too aggressively"
  12. type changed to: "Bug"