Skip to content
Tcl Source Code View Ticket
Login

View Ticket

performance patch for "lsort -command"

Closed / Fixed218501ffffffffffffffffffffffffffffffffffCreated 26 years agoUpdated 26 years ago
Descriptionplain2000-10-26 01:01:23
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 -
welch Closed ticket [218501ffff]. [History diff]2000-10-26 08:23:18
dgp Open ticket [218501ffff]. [History diff]2001-04-17 05:37:28
dgp Closed ticket [218501ffff]. [History diff]2001-04-17 05:37:51
Log in (anonymous is fine) to add a comment.