From 1878a74ad0e7c4be1d9f7a27850905e57054e03d Mon Sep 17 00:00:00 2001
From: Miguel Sofer <miguel.sofer@gmail.com>
Date: Sat, 22 Dec 2007 21:50:50 +0000
Subject: 	* generic/tclCmdIL.c (Tcl_LsortObjCmd, Tcl_LsearchObjCmd):
 avoid 	calling SelectObjFromSublist when there are no sublists.

---
 ChangeLog          |  5 +++++
 generic/tclCmdIL.c | 59 +++++++++++++++++++++++++++++++++---------------------
 2 files changed, 41 insertions(+), 23 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index a8feac2..cf502cd 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,10 @@
 2007-12-21  Miguel Sofer  <msofer@users.sf.net>
 
+	* generic/tclCmdIL.c (Tcl_LsortObjCmd, Tcl_LsearchObjCmd): avoid
+	calling SelectObjFromSublist when there are no sublists.
+
+2007-12-21  Miguel Sofer  <msofer@users.sf.net>
+
 	* generic/tclCmdIL.c (Tcl_LsortObjCmd): preallocate a listObj of
 	sufficient length for the sorted list instead of growing
 	it. Second commit replaces calls to Tcl_ListObjAppenElement with
diff --git a/generic/tclCmdIL.c b/generic/tclCmdIL.c
index a41b611b..df1e48d 100644
--- a/generic/tclCmdIL.c
+++ b/generic/tclCmdIL.c
@@ -16,7 +16,7 @@
  * See the file "license.terms" for information on usage and redistribution of
  * this file, and for a DISCLAIMER OF ALL WARRANTIES.
  *
- * RCS: @(#) $Id: tclCmdIL.c,v 1.131 2007/12/22 01:59:10 msofer Exp $
+ * RCS: @(#) $Id: tclCmdIL.c,v 1.132 2007/12/22 21:50:52 msofer Exp $
  */
 
 #include "tclInt.h"
@@ -2714,7 +2714,7 @@ Tcl_LsearchObjCmd(
     offset = 0;
     noCase = 0;
     sortInfo.compareCmdPtr = NULL;
-    sortInfo.isIncreasing = 0;
+    sortInfo.isIncreasing = 1;
     sortInfo.sortMode = 0;
     sortInfo.interp = interp;
     sortInfo.resultCode = TCL_OK;
@@ -2746,6 +2746,7 @@ Tcl_LsearchObjCmd(
 	    break;
 	case LSEARCH_DECREASING:	/* -decreasing */
 	    isIncreasing = 0;
+	    sortInfo.isIncreasing = 0;
 	    break;
 	case LSEARCH_DICTIONARY:	/* -dictionary */
 	    dataType = DICTIONARY;
@@ -2758,6 +2759,7 @@ Tcl_LsearchObjCmd(
 	    break;
 	case LSEARCH_INCREASING:	/* -increasing */
 	    isIncreasing = 1;
+	    sortInfo.isIncreasing = 1;
 	    break;
 	case LSEARCH_INLINE:		/* -inline */
 	    inlineReturn = 1;
@@ -3042,12 +3044,16 @@ Tcl_LsearchObjCmd(
 	upper = listc;
 	while (lower + 1 != upper && sortInfo.resultCode == TCL_OK) {
 	    i = (lower + upper)/2;
-	    itemPtr = SelectObjFromSublist(listv[i], &sortInfo);
-	    if (sortInfo.resultCode != TCL_OK) {
-		if (sortInfo.indexc > 1) {
-		    ckfree((char *) sortInfo.indexv);
+	    if (sortInfo.indexc != 0) {
+		itemPtr = SelectObjFromSublist(listv[i], &sortInfo);
+		if (sortInfo.resultCode != TCL_OK) {
+		    if (sortInfo.indexc > 1) {
+			ckfree((char *) sortInfo.indexv);
+		    }
+		    return sortInfo.resultCode;
 		}
-		return sortInfo.resultCode;
+	    } else {
+		itemPtr = listv[i];
 	    }
 	    switch ((enum datatypes) dataType) {
 	    case ASCII:
@@ -3136,16 +3142,21 @@ Tcl_LsearchObjCmd(
 	}
 	for (i = offset; i < listc; i++) {
 	    match = 0;
-	    itemPtr = SelectObjFromSublist(listv[i], &sortInfo);
-	    if (sortInfo.resultCode != TCL_OK) {
-		if (listPtr != NULL) {
-		    Tcl_DecrRefCount(listPtr);
-		}
-		if (sortInfo.indexc > 1) {
-		    ckfree((char *) sortInfo.indexv);
+	    if (sortInfo.indexc != 0) {	    
+		itemPtr = SelectObjFromSublist(listv[i], &sortInfo);
+		if (sortInfo.resultCode != TCL_OK) {
+		    if (listPtr != NULL) {
+			Tcl_DecrRefCount(listPtr);
+		    }
+		    if (sortInfo.indexc > 1) {
+			ckfree((char *) sortInfo.indexv);
+		    }
+		    return sortInfo.resultCode;
 		}
-		return sortInfo.resultCode;
+	    } else {
+		itemPtr = listv[i];
 	    }
+		
 	    switch ((enum modes) mode) {
 	    case SORTED:
 	    case EXACT:
@@ -3240,7 +3251,7 @@ Tcl_LsearchObjCmd(
 		 * Note that these appends are not expected to fail.
 		 */
 
-		if (returnSubindices) {
+		if (returnSubindices && (sortInfo.indexc != 0)) {
 		    itemPtr = SelectObjFromSublist(listv[i], &sortInfo);
 		} else {
 		    itemPtr = listv[i];
@@ -3823,13 +3834,15 @@ SortCompare(
 	return order;
     }
 
-    objPtr1 = SelectObjFromSublist(objPtr1, infoPtr);
-    if (infoPtr->resultCode != TCL_OK) {
-	return order;
-    }
-    objPtr2 = SelectObjFromSublist(objPtr2, infoPtr);
-    if (infoPtr->resultCode != TCL_OK) {
-	return order;
+    if (infoPtr->indexc != 0) {
+	objPtr1 = SelectObjFromSublist(objPtr1, infoPtr);
+	if (infoPtr->resultCode != TCL_OK) {
+	    return order;
+	}
+	objPtr2 = SelectObjFromSublist(objPtr2, infoPtr);
+	if (infoPtr->resultCode != TCL_OK) {
+	    return order;
+	}
     }
 
     if (infoPtr->sortMode == SORTMODE_ASCII) {
-- 
cgit v0.12

