Tcl Source Code

View Ticket
Login
Ticket UUID: 3408408
Title: Erratic sharing/duplication of values in [expr]
Type: Bug Created on: 2011-09-13 06:22:11
Submitter: ferrieux Assigned to: dgp
Subsystem: 48. Number Handling Severity:
Priority: 5 Medium Last modified: 2011-09-16 02:06:53
Status: Open Closed by:
Resolution: Postponed Closed on:
Version: None
Description:
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...
User Comments:
ferrieux added on 2011-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.

dgp added on 2011-09-15 23:28:06:
Partial improvement toward increased sharing
committed to trunk.

dgp added on 2011-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.