Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2002-06-29
04:25 • Closed ticket [575194ffff]: Wasteful code in compile plus 7 other changes artifact: e337a7af7e user: msofer
04:01 • Ticket [575194ffff]: 5 changes artifact: 7221f74cf8 user: andreas_kupries
2002-06-28
20:56 • New ticket [575194ffff]. artifact: 46c8ff16b8 user: pspjuth

Wasteful code in compile

Closed / Fixed575194ffffffffffffffffffffffffffffffffffCreated 24 years agoUpdated 24 years ago
Descriptionplain2002-06-28 20:56:25
In compile code (e.g. tclCompCmds.c) the combo
TclEmitPush and TclRegisterLiteral is rather common.
Typically:

TclEmitPush(TclRegisterLiteral(envPtr,
        valueTokenPtr[1].start, valueTokenPtr[1].size,
        /*onHeap*/ 0), envPtr);

However, TclEmitPush is a macro which uses its first
argument eight times and passes it at least three times,
which makes the above statement rather wasteful.

This is normally not any danger since TclRegisterLiteral
can be called repeatedly without anything changing, if
onHeap is 0 which is the normal case.
With onHeap = 1, it becomes disaster since the
successive calls deallocates the string. A naughty
trap for an innocent coder who likes to copy code
nearby :-)

So, either a big warning sign should be placed on
TclEmitPush and every use of the combo should be
rewritten so the literal index is stored in a var first:

objIndex = TclRegisterLiteral(envPtr,
        valueTokenPtr[1].start, valueTokenPtr[1].size,
        /*onHeap*/ 0);
TclEmitPush(objIndex, envPtr);

or, maybe TclEmitPush can be recoded to something
better, like a function or e.g.:

#define TclEmitPush(objIndex, envPtr) \
    { register int __objIndex__ = (objIndex); \
    if ((__objIndex__) <= 255) { \
TclEmitInstInt1(INST_PUSH1, (__objIndex__),\
(envPtr)); \
    } else { \
TclEmitInstInt4(INST_PUSH4, (__objIndex__),\
(envPtr)); \
    } \
    }
andreas_kupriesplain2002-06-29 04:01:18
Logged In: YES 
user_id=75003

Recoding things to use RegisterLiteral only once should also 
speed up the compiler, as it doesn't have to call the function 
multiple times. Expensive calls I guess.

Prio up.
msoferplain2002-06-29 04:25:54
Logged In: YES 
user_id=148712

Thanks for catching that; fixed.
Log in (anonymous is fine) to add a comment.