Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 47c144da07e60980a0ed897e5acfbb858c15aed9
Ticket: 1517602fffffffffffffffffffffffffffffffff
long expressions and Tcl_Tokens
User & Date: dgp on 2006-07-05 15:52:45
Changes

  1. assignee changed to: "dgp"
  2. cmimetype changed to: "text/plain"
  3. comment changed to:
    
    The array of Tcl_Tokens that
    Tcl's parser generates has
    several design flaws when used
    to parse long expressions.
    
    consider the simple expression
    "1+1".  It parses into the
    Tcl_Token stream:
    
    subexpr 1+1 5
    operator + 0
    subexpr 1 1
    text 1 0
    subexpr 1 1
    text 1 0
    
    That's six Tcl_Tokens to
    represent a 3 character
    expression.  At 16 bytes
    each (more on 64-bit system),
    that's a considerable bloat
    of memory.  Apply that to
    
    expr [string repeat +1 10000]
    
    and you need 640Kbytes just
    to hold the final Tcl_Token array
    for a 10Kbyte expression.
    
    Worse, you need that 640KBytes
    as one contiguous block of memory,
    so even if you might have the RAM
    available, you can panic if its
    not available in a large enough block.
    
    A fairly straightforward simplification
    to an RPN style token stream would give:
    
    operator + 2
    text 1 0
    text 1 0
    
    The expr parser already adds TCL_TOKEN_WORD
    tokens to flag multi-token operands.  There's
    no apparent need for the TCL_TOKEN_SUB_EXPR
    to mark the start of operands.  The other
    value TCL_TOKEN_SUB_EXPR offers is to keep 
    the substring "1+1" distinct from the 
    substring "+" in the TCL_TOKEN_OPERATOR
    token or the substring "{foo}" distinct
    from the substring "foo".  The main utility
    of that, though, is to have the string rep
    of the whole subexpression handy for error
    messages or debug trace messages.  Makes
    more sense to me to have such messages take
    longer to generate, with the tradeoff that
    larger legal expressions are usable.
    
    One other issue where Tcl_Tokens don't
    serve expressions well.  In order to
    parse the boundaries of literal string
    representations of numbers, TclParseNumber()
    determines the numeric value.  This is
    simply discarded because Tcl_Tokens only
    store strings.  Later, when the expression
    is actually evaluated, the string form of
    the number has to be parsed all over again
    to get the numeric value.  A revision
    that permitted passing through a Tcl_Obj
    would be nice.  An additional Tcl_Token
    type might permit that.
    
    The problem with most of these ideas
    is that the current design for the
    Tcl_Token array representation of
    expressions is documented as part
    of the Tcl_ParseExpr() routine.  If
    we make any of these changes, we'll
    at least have to build a converter
    to the old layout into T_PE.
    
  4. foundin changed to: "None"
  5. priority changed to: "5 Medium"
  6. resolution changed to: "None"
  7. status changed to: "Open"
  8. submitter changed to: "dgp"
  9. subsystem changed to: "45. Parsing and Eval"
  10. title changed to: "long expressions and Tcl_Tokens"
  11. type changed to: "RFE"