www.gibmonks.com

  Previous section   Next section

Practical Programming in Tcl & Tk, Third Edition
By Brent B. Welch

Table of Contents
Chapter 51.  Tcl/Tk 8.0


The Tcl Compiler

The Tcl Compiler is an on-the-fly compiler that is virtually transparent to Tcl scripts. The compiler translates a script to byte codes the first time it evaluates it. If the script is evaluated again, such as in a loop or in a procedure body, then the byte codes are executed and the translation step is saved. If a procedure is redefined, then the compiler discards any translated byte codes for it.

The compiler uses a dual-ported object model instead of the simple string-based model used in earlier versions of Tcl. The dual-ported objects hold a string value and a native representation of that string such as an integer, double-precision floating point value, or compiler byte codes. This makes it possible to save translations between strings and the native representations. The object model is described in Chapter 44 starting at page 615.

The performance improvement for your application will depend on what features you use. Math expressions and list accesses have improved a lot. Overall you should expect a factor of 2 speedup, and I have heard reports of 10 and 20 times improvement in parts of some applications.

Compile-Time Errors

The compiler catches some sorts of errors earlier than the pure interpreted version. The first time a compiler runs a procedure, it translates the whole thing to byte codes first. If there are syntax errors at the end of the procedure it prevents any code in the procedure from running.

A similar problem occurs with list data. If a string is not a perfect list, then the list commands will fail when parsing it, even if they do not use the damaged part of the list. For example, lindex used to process only enough of a list to find the requested element. In Tcl 8.0 the whole list is converted into a native representation. Errors at the end of a list will prevent you from getting elements at the beginning. This is mainly an issue when you use list operations on arbitrary input data.

Binary String Support

Tcl now supports binary data. This means that an embedded NULL byte no longer terminates a value. Instead, Tcl keeps a byte count for its string objects. This is facilitated by the switch from simple strings to dual-ported objects.

The binary format and binary scan commands support conversions between binary data and strings. These are described on page 54. The unsupported0 command was improved and became the fcopy command, which is described on page 239.


      Previous section   Next section
    Top