Skip to content
Tcl Source Code View Ticket
Login

View Ticket

Avoid some unnecesary strlen()s

Closed / Accepted1529526fffffffffffffffffffffffffffffffffCreated 20 years agoUpdated 19 years ago
Descriptionplain2006-07-27 08:15:07
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)
afreddplain2006-07-27 15:15:08
File Added - 186420: tclCmdAH.c-diff
dkfplain2007-04-10 21:48:54
Logged In: YES 
user_id=79902
Originator: NO

Adopted some of this via a new macro TclNewLiteralStringObj
dkfplain2007-09-17 20:23:23
Logged In: YES 
user_id=79902
Originator: NO

Only error handling code now uses the slow version
Log in (anonymous is fine) to add a comment.

Attachments: