Artifacts Associated With Ticket 218501ffffffffff
Ticket change [f008278499] (rid 109104) by nobody on 2000-10-26 01:01:23:
- assignee initialized to: "nobody"
- cmimetype initialized to: "text/plain"
- comment initialized to:
OriginalBugID: 726 Bug Version: 8.1b2 SubmitDate: '1998-10-13' LastModified: '1999-04-05' Severity: LOW Status: Released Submitter: admin ChangedBy: stanton OS: All OSVersion: NA Machine: NA FixedDate: '2019-02-01' FixedInVersion: 8.1b3 ClosedDate: '2000-10-25' We were puzzled by how slow lsort was with a -command parameter, so I the code and saw that the -command command was being executed in a string- based manner. And there was a bug in the evaluation of the command. This caused a test to fail when I implemented the object version of this. The following script was tested with Tcl8.1a2 on a 200 MHz Pentium II running RedHat 4.2 Linux compiled with gcc: catch {unset word_list} catch {unset sorted} catch {unset sorted2} catch {unset sorted3} catch {unset sorted4} set file [open /usr/dict/words] set data [read $file] close $file set word_list [split $data \n] proc compare {a b} { return [string compare a b] } time { set sorted [lsort -decreasing -command compare $word_list] set sorted2 [lsort -decreasing -command compare $word_list] set sorted3 [lsort -increasing -command compare $sorted] set sorted4 [lsort -increasing -command compare $sorted] } We patched the lsort routine in tcl8.1a2, and the patch is included last in this message. Here is the timing for the patch running the above script: catch {unset word_list} catch {unset sorted} catch {unset sorted2} catch {unset sorted3} catch {unset sorted4} set file [open /usr/dict/words] set data [read $file] close $file set word_list [split $data \n] proc compare {a b} { return [string compare a b] } time { set sorted [lsort -decreasing -command compare $word_list] set sorted2 [lsort -decreasing -command compare $word_list] set sorted3 [lsort -increasing -command compare $sorted] set sorted4 [lsort -increasing -command compare $sorted] } tcl8.1a2: 60752182 microseconds with patch: 17078423 microseconds or a 4x speedup. Here is the patch: Index: tests/cmdIL.test =================================================================== RCS file: /cvs/cvsfiles/devo/tcl/tests/cmdIL.test,v retrieving revision 1.5.2.1 retrieving revision 1.5.2.2 diff -r1.5.2.1 -r1.5.2.2 167c167 < lsort -command {cmp {this argument is very very long in order to make the dstring overflow its statically allocated space}} {{this first element is also long in order to help expand the dstring} {the second element, last but not least, is quite long also, in order to make absolutely sure that space is allocated dynamically for the dstring}} --- > lsort -command cmp {this argument is very very long in order to make the dstring overflow its statically allocated space}} {{this first element is also long in order to help expand the dstring} {the second element, last but not least, is quite long also, in order to make absolutely sure that space is allocated dynamically for the dstring} Index: generic/tclCmdIL.c =================================================================== RCS file: /cvs/cvsfiles/devo/tcl/generic/tclCmdIL.c,v retrieving revision 1.10.2.5 retrieving revision 1.10.2.6 diff -r1.10.2.5 -r1.10.2.6 55c55 < Tcl_DString compareCmd;/* The Tcl comparison command when sortMode --- > Tcl_Obj *compareCmdPtr; /* The Tcl comparison command when sortMode 2553,2554c2553,2554 < Tcl_DStringInit(&sortInfo.compareCmd); < Tcl_DStringAppend(&sortInfo.compareCmd, Tcl_GetString(cmdPtr), -1); --- > sortInfo.compareCmdPtr = cmdPtr; > Tcl_IncrRefCount(cmdPtr); 2588c2588,2589 < Tcl_DStringFree(&sortInfo.compareCmd); --- > Tcl_DecrRefCount(sortInfo.compareCmdPtr); > sortInfo.compareCmdPtr = NULL; 2844c2845 < int oldLength; --- > Tcl_Obj *objv[3] = {infoPtr->compareCmdPtr, objPtr1, objPtr2}; 2846,2849c2847,2848 < /* < * Generate and evaluate a command to determine which string comes < * first. < */ --- > infoPtr->resultCode = Tcl_EvalObjv(infoPtr->interp, 3, objv, "", > -1, 0); 2851,2858d2849 < oldLength = Tcl_DStringLength(&infoPtr->compareCmd); < Tcl_DStringAppendElement(&infoPtr->compareCmd, < Tcl_GetString(objPtr1)); < Tcl_DStringAppendElement(&infoPtr->compareCmd, < Tcl_GetString(objPtr2)); < infoPtr->resultCode = Tcl_Eval(infoPtr->interp, < Tcl_DStringValue(&infoPtr->compareCmd)); < Tcl_DStringTrunc(&infoPtr->compareCmd, oldLength); 2859a2851,2860 > Tcl_Obj *errorPtr = Tcl_NewListObj(3, objv); > int length; > char *compareCmd = Tcl_GetStringFromObj(errorPtr, &length); > Tcl_AddErrorInfo(infoPtr->interp, > "\n invoked from within\n\""); > Tcl_AddObjErrorInfo(infoPtr->interp, > compareCmd, length); > Tcl_AddErrorInfo(infoPtr->interp, > "\""); > Tcl_DecrRefCount(errorPtr); Syd Polk This slightly changes the semantics of lsort -command. If you have a command in the form: lsort $list -command [generate random command] this patch will break it. It will work in the normal cases, like lsort $list -command mysort lsort $list -command sorter up lsort $list -command sorter down and it passes the test suite. I applied the patch and can not confirm the problem listed above. It appeared to work fine. (A script that would break would have been useful!) Also, the patch certainly improves the performace - it makes sorting with the -command option about 5 times faster. 12/22/1998 18:47 - rjohnson - - foundin initialized to: "obsolete: 8.1b2"
- priority initialized to: "5 Medium"
- resolution initialized to: "None"
- status initialized to: "Open"
- submitter initialized to: "nobody"
- subsystem initialized to: "69. Other"
- title initialized to: "performance patch for "lsort -command""
- type initialized to: "RFE"
Ticket change [6e6b3f296e] (rid 100143) by welch on 2000-10-26 08:23:18:
- closedate changed to: "2000-10-26T01:23:18"
- closer changed to: "welch"
- priority changed to: "2"
- resolution changed to: "Fixed"
- status changed to: "Closed"
Ticket change [97a4e743f6] (rid 110474) by dgp on 2001-04-17 05:37:28:
- foundin changed to: "None"
- status changed to: "Open"
- subsystem changed to: "None"
- title changed to: "performance patch for "lsort -command""
Ticket change [41c21809be] (rid 116243) by dgp on 2001-04-17 05:37:51:
- closedate changed to: "2001-04-16T22:37:51"
- closer changed to: "dgp"
- status changed to: "Closed"
- subsystem changed to: "17. Commands I-L"
- title changed to: "performance patch for "lsort -command""