While it seems that user-defined C callbacks present an intractable optimization problem, it actually is possible to guess the state of the virtual machine from the Tcl source code. In Tcl, user-defined callbacks are required to use a set of C library routines to access the Tcl internals (ie. variable values). Our system adds hooks to this core Tcl library, which will call out to TC routines when triggered. This allows the TC runtime to maintain more efficient structures, and keep them updated if they change unexpectedly. For example, the TC runtime maintains a table of commands, which is directly indexed, in order to save on the hashing costs normally associated with Tcl variable access. When a C callback function attempts to rename a command, this is trapped and the table entry is updated.
However, this mechanism is still insufficient to allow for more aggressive optimizations, since you cannot determine what the dependencies are until they are triggered. Thus, it is impossible to implement optimizations that involve code motion, elimination of unused variable assignments, and so on.