lsort -command is too slow
Closed / Duplicate217802ffffffffffffffffffffffffffffffffffCreated 26 years agoUpdated 26 years ago
Descriptionplain2000-10-26 00:43:55
OriginalBugID: 1007 RFE
Version: 8.1b2
SubmitDate: '1998-12-22'
LastModified: '1998-12-22'
Severity: LOW
Status: Closed
Submitter: welch
ChangedBy: rjohnson
RelatedBugIDs: 726
OS: Windows NT
OSVersion: 4.0
Machine: X86
FixedDate: '1998-12-22'
ClosedDate: '2000-10-25'
Name:
Syd Polk
CVS:
tclCmdIL.c 1998/11/11 04:08:15 1.1.2.4
DesiredBehavior:
I am submitting a patch to change lsort -command to use a Tcl_Obj-based command instead of a string based command. This is substantially faster.
Patch:
Index: tclCmdIL.c
===================================================================
RCS file: /cvsroot/tcl/generic/tclCmdIL.c,v
retrieving revision 1.1.2.4
diff -c -r1.1.2.4 tclCmdIL.c
*** tclCmdIL.c1998/11/11 04:08:151.1.2.4
--- tclCmdIL.c1998/12/18 19:41:47
***************
*** 46,52 ****
int isIncreasing;/* Nonzero means sort in increasing order. */
int sortMode;/* The sort mode. One of SORTMODE_*
* values defined below */
! Tcl_DString compareCmd;/* The Tcl comparison command when sortMode
* is SORTMODE_COMMAND. Pre-initialized to
* hold base of command.*/
int index;/* If the -index option was specified, this
--- 46,52 ----
int isIncreasing;/* Nonzero means sort in increasing order. */
int sortMode;/* The sort mode. One of SORTMODE_*
* values defined below */
! Tcl_Obj *compareCmdPtr; /* The Tcl comparison command when sortMode
* is SORTMODE_COMMAND. Pre-initialized to
* hold base of command.*/
int index;/* If the -index option was specified, this
***************
*** 2514,2521 ****
}
}
if (sortInfo.sortMode == SORTMODE_COMMAND) {
! Tcl_DStringInit(&sortInfo.compareCmd);
! Tcl_DStringAppend(&sortInfo.compareCmd, Tcl_GetString(cmdPtr), -1);
}
sortInfo.resultCode = Tcl_ListObjGetElements(interp, objv[objc-1],
--- 2514,2535 ----
}
}
if (sortInfo.sortMode == SORTMODE_COMMAND) {
!
! /*
! * The existing command is a list. We want to flatten it, append
! * two dummy arguments on the end, and replace these arguments
! * later.
! */
!
! Tcl_Obj *newCommandPtr = Tcl_DuplicateObj(cmdPtr);
!
! if (Tcl_ListObjAppendElement(interp, newCommandPtr, Tcl_NewObj())
! != TCL_OK) {
! return TCL_ERROR;
! }
! Tcl_ListObjAppendElement(interp, newCommandPtr, Tcl_NewObj());
! sortInfo.compareCmdPtr = newCommandPtr;
! Tcl_IncrRefCount(newCommandPtr);
}
sortInfo.resultCode = Tcl_ListObjGetElements(interp, objv[objc-1],
***************
*** 2549,2555 ****
done:
if (sortInfo.sortMode == SORTMODE_COMMAND) {
! Tcl_DStringFree(&sortInfo.compareCmd);
}
return sortInfo.resultCode;
}
--- 2563,2570 ----
done:
if (sortInfo.sortMode == SORTMODE_COMMAND) {
! Tcl_DecrRefCount(sortInfo.compareCmdPtr);
! sortInfo.compareCmdPtr = NULL;
}
return sortInfo.resultCode;
}
***************
*** 2805,2826 ****
order = -1;
}
} else {
! int oldLength;
!
/*
! * Generate and evaluate a command to determine which string comes
! * first.
*/
- 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);
if (infoPtr->resultCode != TCL_OK) {
Tcl_AddErrorInfo(infoPtr->interp,
"\n (-compare command)");
return order;
--- 2820,2845 ----
order = -1;
}
} else {
! Tcl_Obj **objv, *paramObjv[2] = {objPtr1, objPtr2};
! int objc;
!
/*
! * We made space for the two things to compare. Replace them
! * and evaluate the result.
*/
+
+ Tcl_ListObjLength(infoPtr->interp, infoPtr->compareCmdPtr, &objc);
+ Tcl_ListObjReplace(infoPtr->interp, infoPtr->compareCmdPtr, objc - 2,
+ 2, 2, paramObjv);
+ Tcl_ListObjGetElements(infoPtr->interp, infoPtr->compareCmdPtr,
+ &objc, &objv);
+
+ infoPtr->resultCode = Tcl_EvalObjv(infoPtr->interp, objc, objv, 0);
if (infoPtr->resultCode != TCL_OK) {
+ Tcl_Obj *errorPtr = Tcl_NewListObj(3, objv);
+ int length;
+ char *compareCmd = Tcl_GetStringFromObj(errorPtr, &length);
Tcl_AddErrorInfo(infoPtr->interp,
"\n (-compare command)");
return order;
PatchFiles:
generic/tclCmdIL.c
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 [217802ffff].
[History diff]2000-10-26 07:49:18
welch Ticket [217802ffff]: 3 changes
[History diff]2000-10-26 07:49:19
dgp Ticket [217802ffff]: 3 changes
[History diff]2001-04-13 23:04:16
Log in (anonymous is fine) to add a comment.