Tcl Source Code

View Ticket
Login
Ticket UUID: 1529526
Title: Avoid some unnecesary strlen()s
Type: Patch Version: None
Submitter: afredd Created on: 2006-07-27 08:15:07
Subsystem: 16. Commands A-H Assigned To: dkf
Priority: 5 Medium Severity:
Status: Closed Last Modified: 2007-09-17 20:23:23
Resolution: Accepted Closed By: dkf
    Closed on: 2007-09-17 13:23:23
Description:
In the call to Tcl_SetStringObj in the STORE_ARY macro
in StoreStatData(), the string length can be
determined since they are all string literals.
So the attached patch saves unnecesary 12 strlen()s
per call to "file stat ...".
NB. The use of ("" fieldName) is simply to ensure that
if STORE_ARY is called with something other than
a string literal as the fieldName, then the compiler
will helpfully given an error. 

--
This same technique of determining the length of a
string literal using (sizeof(strLit) - 1) could be
useful for Tcl_NewStringObj & Tcl_DStringAppend which
are often called with a string literal and a length
of -1 in the core. So something like these macros
added to tclInt.h and then used in the core would
save some additional unnecesary strlen()s:


/* Macro to determine the length of a "string literal"
 * at compile time.
 * NB. The use of (strLit "") below ensures that the
 * compiler will report an error if anything other
 * than a string literal is given to the macro.
 */
#define StrLitLen(strLit) \
  ((int) (sizeof(strLit "") - 1))

#define TclNewStrLitObj(strLit) \
  Tcl_NewStringObj(strLit, strLitLen(strLit))

#define TclDStrLitAppend(dsPtr, strLit) \
  Tcl_DStringAppend(dsPtr, strLit, strLitLen(strLit))



So calls like:
  Tcl_NewStringObj("foo", -1)
would then be written as
  TclNewStrLitObj("foo")
and would be expanded as
  Tcl_NewStringObj("foo", 3)


(There are about 100 calls to Tcl_NewStringObj &
Tcl_DStringAppend which could use these macros to
avoid an unnecesary strlen)
User Comments: dkf added on 2007-09-17 20:23:23:
Logged In: YES 
user_id=79902
Originator: NO

Only error handling code now uses the slow version

dkf added on 2007-04-10 21:48:54:
Logged In: YES 
user_id=79902
Originator: NO

Adopted some of this via a new macro TclNewLiteralStringObj

afredd added on 2006-07-27 15:15:08:

File Added - 186420: tclCmdAH.c-diff

Attachments: