Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2011-09-16
02:06 • Ticket [3408408fff] Erratic sharing/duplication of values in expr status s... artifact: 0e3133c441 user: dgp
2011-09-15
23:28 • Ticket [3408408fff]: 4 changes artifact: 2710ce6ece user: dgp
2011-09-13
13:30 • Ticket [3408408fff]: 4 changes artifact: 773e94c107 user: ferrieux
06:22 • New ticket [3408408fff]. artifact: 432cd065c7 user: ferrieux

Erratic sharing/duplication of values in [expr]

Open3408408fffffffffffffffffffffffffffffffffCreated 15 years agoUpdated 15 years ago
Descriptionplain2011-09-13 06:22:11
To save memory by sharing oft-used values, a natural technique would be

 lappend storage [expr {$computed_bit?1:0}]

Unfortunately, the 1 and 0 values coming out of the [expr] are *not* extra references to the hugely shared literals, but fresh copies.
A mysterious workaround guessed out of the blue by Colin is:

 lappend storage [expr {$computed_bit==1}]

Funnily, the ternary operator *can* respect sharing with more complex values:

 set list1 [list yes yes yes]
 set list2 [list no no no]
 lappend storage [expr {$computed_bit?$LIST1:$LIST2}]

All three behaviors are suprising...
ferrieuxplain2011-09-13 13:30:18
15:19        dgp        ferrieux, based on a quck look I think your sharing goals are getting thwarted by the TRY_CVT_TO_NUMERIC shenanigans that take place on the way out of an [expr].
15:20        dgp        Another contributor may be whether your context has [expr] compiled early, or compiled only at run time.

16:11        dgp        ferrieux, a possible way forward on the expr literal sharing issue...
16:12        dgp        At the point in CompileExprTree() where the INST_TRY_CVT_TO_NUMERIC opcode is pushed...
16:13        dgp        ...because we've determined there's a possible need to convert a non-canonical number to its canonical form...
16:15        dgp        if there's some way to make the conversion right there at compile time to canonical form, register *that* as a literal and sieze the sharing advantage then avoid pushing the opcode...
16:15        dgp        ...well that's the concept.  I'd have to review how much we know at that point.
dgpplain2011-09-15 23:28:06
Partial improvement toward increased sharing
committed to trunk.
dgpplain2011-09-16 02:06:53
Unfortunately tackling the specific case in the
ticket is at least difficult.  It's long been the (unwise?)
design of [expr] to not embrace "everything is a string"
but impose on [expr] the burden of producing only
'normalized' string reps of numbers:

% expr 7.5000
7.5
% expr 0x1
1

It's precisely when a literal value 
is bubbling up from the leaf of an
expression tree to become the result
of the expression that we do not know
whether or not it's already in normalized
form.  The TRY_CVT_TO_NUMERIC 
instruction is pushed to enforce the guarantee
and the way it operates is to produce a new
unshared value.

Absent a revolution where we at long last let
[expr] embrace the EIAS philosophy, I don't 
see any quick and simple path to improve this.
And for now, I don't have the cycles to spare
for something more ambitious.  Sorry.
Log in (anonymous is fine) to add a comment.