|
2005-05-11
| ||
| 21:21 | • Closed ticket [1143805fff]: Impurity of empty list plus 7 other changes artifact: 47dabea6af user: msofer | |
| 19:21 | • Ticket [1143805fff]: 6 changes artifact: d4f06c7d91 user: dkf | |
| 19:21 | • Open ticket [1143805fff]. artifact: d5dddf82b3 user: dkf | |
| 17:57 | • Ticket [1143805fff]: 4 changes artifact: cb8f2bae1a user: lars_h | |
| 02:11 | • Ticket [1143805fff]: 7 changes artifact: 093ab17d71 user: kennykb | |
| 02:11 | • Closed ticket [1143805fff]. artifact: 584d44fa4e user: kennykb | |
|
2005-04-29
| ||
| 21:11 | • Ticket [1143805fff]: 1 change artifact: 85bce5c5eb user: dkf | |
|
2005-02-19
| ||
| 04:13 | • Ticket [1143805fff]: 4 changes artifact: ee37b57f78 user: dgp | |
|
2005-02-18
| ||
| 13:59 | • New ticket [1143805fff]. artifact: 9551919225 user: lars_h | |
Impurity of empty [list]
Closed / Fixed1143805fffffffffffffffffffffffffffffffffCreated 22 years agoUpdated 22 years ago
Descriptionplain2005-02-18 13:59:28
A comment on the Tcler's Wiki page on [concat] (http://wiki.tcl.tk/
1037) made me aware of the fact that the result of [list] is not
always a pure list; the result of [list] when called without
arguments is rather an empty string. This difference can be seen
on the script level as follows:
% set third [expr 1.0/3]
0.333333333333
% expr {$third*3}
1.0
% expr {[lindex [concat [list $third] [list x]] 0]*3}
1.0
% expr {[lindex [concat [list $third] [list]] 0]*3}
0.999999999999
In the second case, the second argument of [concat] is not a pure
list, and thus [concat] operates on its arguments in string mode,
forcing a loss of the internal representation of the third.
The bug is thus: The result of [list] is not always a pure list.
Now, if anyone wants to argue that this is not a bug, then I can't
really object; since everything is (or at least should be) a string,
and in particular an empty list is the same thing as an empty
string, Tcl_ListObjCmd is completely right when it simply leaves
the empty result in the interpreter. Judging from the way it is
written, this is even by design:
int
Tcl_ListObjCmd(dummy, interp, objc, objv)
ClientData dummy; /* Not used. */
Tcl_Interp *interp; /* Current interpreter. */
register int objc; /* Number of arguments. */
register Tcl_Obj *CONST objv[]; /* The argument objects. */
{
/*
* If there are no list elements, the result is an empty object.
* Otherwise set the interpreter's result object to be a list
object.
*/
if (objc > 1) {
Tcl_SetObjResult(interp, Tcl_NewListObj((objc-1),
&(objv[1])));
}
return TCL_OK;
}
OTOH, the distinction between arbitrary lists and pure lists is
something that many programmers take care to observe, because
of the efficiency improvements this can bring, and then it seems a
bit odd that all return values from [list] _except_ the empty one
should be pure. This can lead to strange effects in cases like
set extraOpts [list]
foreach opt {-foo -bar -baz} {
if {[needOption $opt]} then {lappend extraOpts $opt}
}
eval [list someCmd $someHugeDataStructure] $extraOpts
where the [eval] suddenly takes much more time when no extra
options are passed, since $extraOpts will then not be a pure list.
Also, I often find myself writing
set res [list]
rather than
set res {}
precisely because I know that this will be a list and want to "speed
things up" by initialising to an object with a list representation, and
I am not the only one who does this -- "set someVar [list]" is quite
common on the Wiki. It now turns out that this is in vain, since
what [list] returns actually doesn't have a list representation!
dgpplain2005-02-19 04:13:18
Logged In: YES user_id=80530 IMHO... There is a bug here, but not the one you report. The bug is that floating point values don't make the internal -> string -> internal round trip without losing precision; in violation of "everything is a string". That's a known bug in Tcl (not sure whether there's a SF Tracker ID for it), and the work on TIP 132 should eventually solve it. With that bug fixed, the difference in your two commands will disappear, and the "no script-level programmer should have to care about internal reps, 'pure' or otherwise" advice will actually become true. Far too many Tcl'ers are preoccupied with "pure" representations and other internals details. Tcl ought to "just work" without requiring Tcl'ers to become C hackers.
dkf Ticket [1143805fff]: 1 change
[History diff]2005-04-29 21:11:12
kennykb Closed ticket [1143805fff].
[History diff]2005-05-11 02:11:13
kennykbplain2005-05-11 02:11:14
Logged In: YES user_id=99768 TIP #132 fixes the user-visible manifestation; the purity of the empty list is irrelevant to the observed behaviour. Fixed in 8.5
lars_hplain2005-05-11 17:57:06
Logged In: YES
user_id=938835
How I wish the powers that be would spend more electronic ink on the
actual _issue_ than on the _symptoms_ of the issue.
The issue in this report is *not* that real values can be distorted by loss of
internal representation; that was merely a (AFAICT standard) method of
demonstrating that the issue exists; a way of exhibiting a symptom. I
could instead have used the (less standard, and thus more obscure)
alternative of losing a wide internal representation of 65536 (as in
RFE#866469) to demonstrate the issue, but I chose not to, because I felt
that that could cloud the issue. However in view of how this report has
been examined, it seems it wouldn't have mattered; the issue got clouded
by concerns for symptoms anyway.
The actual _issue_, that neither TIP#132 nor TIP#237 will address, is that
an impure return value from [list] can in some cases cause an essentially
constant time operation to effectively take linear time, thus (if one is
unlucky) increasing the exponent of just about any polynomial time
algorithm by one when implemented in Tcl. The response from dgp that
Tcl'ers should not be concerned with such matters is hopefully not a well
thought through position.
On the bright side, TIP#157 ({expand}) will hopefully lead to a drastic
decrease in the number of [eval] constructions that may be harmed by an
impure list result, so the issue should become less and less important
over time. I still miss an explanation of why Tcl_ListObjCmd should have a
special case for no arguments however. Is it just a case of overdoing
optimisation, or does anyone have an actual motivation?
And for the record, in case anyone wants a [list] equivalent that always
returns a pure list, they can do
interp alias {} list {} lreplace x 0 0
dkf Open ticket [1143805fff].
[History diff]2005-05-11 19:21:31
dkfplain2005-05-11 19:21:32
Logged In: YES user_id=79902 But that's all beside the point entirely. On one level, the "correct" way of fixing your problem would be to make [eval] go back to always having the horrible time behaviour. That'd make your complaint go away, but not in the way that you want. :^) The fact that "pure" lists have a few spots where they have more efficient processing (at virtually zero cost to everything else) is merely an optimization. Hmm, looking at Tcl_ConcatObj(), I see that it does not handle the newer concepts in shared empty objects perfectly. Assigning to Miguel for resolution...
msoferplain2005-05-11 21:21:14
Logged In: YES user_id=148712 We had here one real bug (EIAS violation, fixed in HEAD), and one performance bug. As with all optimisations, there is a tradeoff: either an empty list has a list internal rep (and is therefore costly to create and destroy), or it doesn't (and it causes slowdowns in [concat]). As a side comment: having [list] return a listObj will not always speed things up. If you then lappend to it, you have to get rid of the empty list rep and replace it with a larger one. In that sense, the fact that it was created in the first place leads to a pure loss. Closing this, moving the [concat] issue over to [Bug 1178805]. That will be addressed before 8.5a3 release.
Log in (anonymous is fine) to add a comment.