Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 8d549a012a5fecfc46c7164475e1620619edcfaf
Ticket: 218933ffffffffffffffffffffffffffffffffff
Optimisation of booleans
User & Date: nobody on 2000-10-26 01:08:07
Changes

  1. assignee changed to: "nobody"
  2. cmimetype changed to: "text/plain"
  3. comment changed to:
    OriginalBugID: 2815 RFE
    Version: 8.2
    SubmitDate: '1999-09-16'
    LastModified: '1999-12-09'
    Severity: LOW
    Status: Released
    Submitter: techsupp
    ChangedBy: hobbs
    OS: Other
    Machine: NA
    FixedDate: '1999-12-09'
    FixedInVersion: 8.3b1
    ClosedDate: '2000-10-25'
    
    
    Name:
    Peter Spjuth
    
    DesiredBehavior:
    When testing some things for performance, one result surprised me.
        The following construct:
          if {[string compare $str "apa"]}
        was significantly faster than:
          if {[string equal $str "apa"]}
        
        The reason is that "string equal" returns a boolean object, which
        goes through a rather costly conversion to an integer in the bytecode
        INST_TRY_CVT_TO_NUMERIC.
        
        I've gathered some statistics when running some of my scripts. They
        show that virtually every object that comes to TRY_CVT (those that 
        are not int or double) is a boolean without string rep, which makes
        an optimisation for that case really worth the trouble.
        A boolean object with no string representation can easily be
        converted to an integer, which is done in this patch.
        
        The same situation arises in INST_LNOT, and I've made the same
        change there.
        
        When studying the code in INST_TRY_CVT_TO_NUMERIC I also saw
        another thing. At the end, the string rep is discarded, and thus
        the object is copied first if it is shared. My measurments show
        that almost every object there is shared (no surprise, the typical
        object is referenced both by the interp's result, and by the stack),
        and it also shows that few objects have a string rep.
        So, if the purpose is to get an object without a string rep,
        why bother if there is none in the first place?
        
        The patch adds a check for that. I chose a placement for the check
        that lets the "shared" variable be correct, even though that is
        not the optimal place.
    
    Patch:
    Index: tclExecute.c
    ===================================================================
    RCS file: /cvsroot/tcl/generic/tclExecute.c,v
    retrieving revision 1.7
    diff -c -r1.7 tclExecute.c
    *** tclExecute.c1999/06/16 21:56:331.7
    --- tclExecute.c1999/09/16 19:39:46
    ***************
    *** 2309,2328 ****
      tPtr = valuePtr->typePtr;
      if ((tPtr != &tclIntType) && ((tPtr != &tclDoubleType)
      || (valuePtr->bytes != NULL))) {
    !     char *s = Tcl_GetStringFromObj(valuePtr, &length);
    !     if (TclLooksLikeInt(s, length)) {
    ! result = Tcl_GetLongFromObj((Tcl_Interp *) NULL,
    ! valuePtr, &i);
          } else {
    ! result = Tcl_GetDoubleFromObj((Tcl_Interp *) NULL,
    ! valuePtr, &d);
    !     }
    !     if (result != TCL_OK) {
    ! TRACE(("\"%.20s\" => ILLEGAL TYPE %s\n",
    !         s, (tPtr? tPtr->name : "null")));
    ! IllegalExprOperandType(interp, pc, valuePtr);
    ! Tcl_DecrRefCount(valuePtr);
    ! goto checkForCatch;
          }
          tPtr = valuePtr->typePtr;
      }
    --- 2309,2333 ----
      tPtr = valuePtr->typePtr;
      if ((tPtr != &tclIntType) && ((tPtr != &tclDoubleType)
      || (valuePtr->bytes != NULL))) {
    !     if ((tPtr == &tclBooleanType) 
    !     && (valuePtr->bytes == NULL)) {
    ! valuePtr->typePtr = &tclIntType;
          } else {
    ! char *s = Tcl_GetStringFromObj(valuePtr, &length);
    ! if (TclLooksLikeInt(s, length)) {
    !     result = Tcl_GetLongFromObj((Tcl_Interp *) NULL,
    !     valuePtr, &i);
    ! } else {
    !     result = Tcl_GetDoubleFromObj((Tcl_Interp *) NULL,
    !     valuePtr, &d);
    ! }
    ! if (result != TCL_OK) {
    !     TRACE(("\"%.20s\" => ILLEGAL TYPE %s\n",
    !     s, (tPtr? tPtr->name : "null")));
    !     IllegalExprOperandType(interp, pc, valuePtr);
    !     Tcl_DecrRefCount(valuePtr);
    !     goto checkForCatch;
    ! }
          }
          tPtr = valuePtr->typePtr;
      }
    ***************
    *** 2493,2510 ****
      converted = 0;
      if ((tPtr != &tclIntType) && ((tPtr != &tclDoubleType)
      || (valuePtr->bytes != NULL))) {
    !     s = Tcl_GetStringFromObj(valuePtr, &length);
    !     if (TclLooksLikeInt(s, length)) {
    ! result = Tcl_GetLongFromObj((Tcl_Interp *) NULL,
    ! valuePtr, &i);
    !     } else {
    ! result = Tcl_GetDoubleFromObj((Tcl_Interp *) NULL,
    ! valuePtr, &d);
    !     }
    !     if (result == TCL_OK) {
      converted = 1;
          }
    -     result = TCL_OK; /* reset the result variable */
          tPtr = valuePtr->typePtr;
      }
      
    --- 2498,2521 ----
      converted = 0;
      if ((tPtr != &tclIntType) && ((tPtr != &tclDoubleType)
      || (valuePtr->bytes != NULL))) {
    !     if ((tPtr == &tclBooleanType) 
    !     && (valuePtr->bytes == NULL)) {
    ! valuePtr->typePtr = &tclIntType;
      converted = 1;
    +     } else {
    + s = Tcl_GetStringFromObj(valuePtr, &length);
    + if (TclLooksLikeInt(s, length)) {
    +     result = Tcl_GetLongFromObj((Tcl_Interp *) NULL,
    +     valuePtr, &i);
    + } else {
    +     result = Tcl_GetDoubleFromObj((Tcl_Interp *) NULL,
    +     valuePtr, &d);
    + }
    + if (result == TCL_OK) {
    +     converted = 1;
    + }
    + result = TCL_OK; /* reset the result variable */
          }
          tPtr = valuePtr->typePtr;
      }
      
    ***************
    *** 2523,2540 ****
          shared = 0;
          if (Tcl_IsShared(valuePtr)) {
      shared = 1;
    ! if (tPtr == &tclIntType) {
    !     i = valuePtr->internalRep.longValue;
    !     objPtr = Tcl_NewLongObj(i);
    ! } else {
    !     d = valuePtr->internalRep.doubleValue;
    !     objPtr = Tcl_NewDoubleObj(d);
      }
    - Tcl_IncrRefCount(objPtr);
    - TclDecrRefCount(valuePtr);
    - valuePtr = objPtr;
    - stackPtr[stackTop] = valuePtr;
    - tPtr = valuePtr->typePtr;
          } else {
      Tcl_InvalidateStringRep(valuePtr);
          }
    --- 2534,2553 ----
          shared = 0;
          if (Tcl_IsShared(valuePtr)) {
      shared = 1;
    ! if (valuePtr->bytes != NULL) {
    !     if (tPtr == &tclIntType) {
    ! i = valuePtr->internalRep.longValue;
    ! objPtr = Tcl_NewLongObj(i);
    !     } else {
    ! d = valuePtr->internalRep.doubleValue;
    ! objPtr = Tcl_NewDoubleObj(d);
    !     }
    !     Tcl_IncrRefCount(objPtr);
    !     TclDecrRefCount(valuePtr);
    !     valuePtr = objPtr;
    !     stackPtr[stackTop] = valuePtr;
    !     tPtr = valuePtr->typePtr;
      }
          } else {
      Tcl_InvalidateStringRep(valuePtr);
          }
    
    PatchFiles:
    tclExecute.c
    
    
    
    Good patch to include, must be tested for proper coverage.
    I cheated for 8.2.1 by making [string equal] return an int
    after realizing that Booleans were a lesser type (irk). 
    -- 09/18/1999 hobbs
    This will be in 8.3b1.  It appears to give a *slight* speed
    advantage overall (definitely for bool types), but perhaps a
    better test would show a clearer difference.  Anyway, it does
    pass everything.
    
    I also changed [string equal] back to returning a bool, as
    this essentially removes the speed penalty, and I like the
    correctness of it. 
    -- 12/09/1999 hobbs
    
  4. foundin changed to: "obsolete: 8.2"
  5. priority changed to: "5 Medium"
  6. resolution changed to: "None"
  7. status changed to: "Open"
  8. submitter changed to: "nobody"
  9. subsystem changed to: "10. Objects"
  10. title changed to: "Optimisation of booleans"
  11. type changed to: "Bug"