Skip to content
Tcl Source Code View Ticket
Login

View Ticket

Minor optimsations for Tcl_DStringAppend usage.

Closed1246897fffffffffffffffffffffffffffffffffCreated 21 years agoUpdated 21 years ago
Descriptionplain2005-07-28 16:11:31
Minor optimisation: there are a few calls to 
Tcl_DStringAppend with fixed strings, but give a length 
of -1. Replacing the -1 with the actual length (as is done 
in other places) would save a strlen.
eg.


tclConfig.c:141:    Tcl_DStringAppend 
(&cmdName, "::",          -1);
tclConfig.c:161:    Tcl_DStringAppend 
(&cmdName, "::pkgconfig", -1);
tclInterp.c:433:    Tcl_DStringAppend(&script, "lsearch -
exact", -1);
tclInterp.c:437:    Tcl_DStringAppend(&script, " [file join 
[info library] encoding]", -1);
tclInterp.c:450:    Tcl_DStringAppend(&script, "file join 
[info library] encoding", -1);
tclUtil.c:1856:Tcl_DStringAppend(dsPtr, " {", -1);
tclUtil.c:1858:Tcl_DStringAppend(dsPtr, "{", -1);
tclUtil.c:1885:    Tcl_DStringAppend(dsPtr, "}", -1);

There are also some where the string being copied is a 
Tcl_DString, so we could use the corresponding 
Tcl_DStringLength instead of -1; again saving a strlen.

eg.

tclLoad.c:349:Tcl_DStringAppend(&initName, 
Tcl_DStringValue(&pkgName), -1);
tclLoad.c:351:Tcl_DStringAppend
(&safeInitName, Tcl_DStringValue(&pkgName), -1);
tclLoad.c:353:        Tcl_DStringAppend(&unloadName, 
Tcl_DStringValue(&pkgName), -1);
tclLoad.c:355:        Tcl_DStringAppend
(&safeUnloadName, Tcl_DStringValue(&pkgName), -1);
tclNamesp.c:823:Tcl_DStringAppend(&buffer1, 
Tcl_DStringValue(&buffer2), -1);
tclNamesp.c:826:Tcl_DStringAppend(&buffer2, 
Tcl_DStringValue(&buffer1), -1);

(which makes me wonder if the Right Thing is done if 
any of the above contain embedded NULs in them, as 
the strlen will give the 'incorrect' length.).


Final point, wouldn't the copy loop in Tcl_DStringAppend 
be quicker as a memcpy and not as a character by 
character copy in the for loop. I've no empirical evidence, 
just the (perhaps incorrect!) assumption that the C 
library's memcpy is quicker than the corresponding for 
loop. 

Cheers!
dkf Ticket [1246897fff]: 2 changes [History diff]2005-07-28 23:25:57
dgpplain2005-07-29 04:32:13
Logged In: YES 
user_id=80530


there's no patch here?
dgpplain2005-07-29 05:04:06
Logged In: YES 
user_id=80530


The docs aren't explicit about it
(perhaps because they are so old),
but I don't believe the Tcl_DString*
routines were ever intended to
support embedded NUL bytes within
the dynamic strings.

The comment on the "length" field
of the Tcl_DString struct (in tcl.h)
reads:

  Number of non-NULL characters in the string

That said, it's clear that callers of
Tcl_DStringAppend() and
Tcl_DStringSetLength() could leave
NUL bytes within the "length" of the
string.

Wise or not, the Tcl_DString* routines
are fairly low level and have a long
history of exposing that capability.
If it gets too troubling, just make use
of Tcl_Obj's for your dynamically
sized string values.
dkfplain2005-07-29 15:56:11
Logged In: YES 
user_id=79902

The encoding subsystem relies on the fact that DStrings can
hold NUL bytes.

The DString system serves multiple purposes, some of which
are obsolete these days. It is both a way of having safely
managed buffers and a mechanism for doing some string
manipulations that was heavily used back in Tcl 7.* (and
before?) but which has been largely superceded for this
latter purpose these days by the Tcl_Obj system.

I'm fairly strongly inclined to leave things as they are
unless the code in question is on some performance critical
path: easier maintenance beats shaving a few nanoseconds
from a location where it will get swamped by some syscall...
dgpplain2005-08-13 09:12:27
Logged In: YES 
user_id=80530


The fixed string arguments should
continue to be paired with -1 arguments,
for ease of maintenance when the fixed
string might be changed.

When copying DStrings, I agree we'd be
better off passing in the DStringLength
rather than -1.

The memcpy seems reasonable to me.

I'd happily accept a patch along those lines.
Separate patches for both 8.5 and 8.4 lines
would be even better.

Thanks.
dgp Ticket [1246897fff]: 2 changes [History diff]2005-08-13 09:12:28
sf-robotplain2005-08-27 09:20:35
Logged In: YES 
user_id=1312539

This Tracker item was closed automatically by the system. It was
previously set to a Pending status, and the original submitter
did not respond within 14 days (the time period specified by
the administrator of this Tracker).
Log in (anonymous is fine) to add a comment.