After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 106973 - g_string_new usage
g_string_new usage
Status: RESOLVED FIXED
Product: glib
Classification: Platform
Component: general
unspecified
Other All
: Normal trivial
: ---
Assigned To: gtkdev
gtkdev
Depends on:
Blocks:
 
 
Reported: 2003-02-24 21:23 UTC by Morten Welinder
Modified: 2011-02-18 15:57 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
patch (746 bytes, patch)
2003-02-26 00:30 UTC, Matthias Clasen
none Details | Review
s/g_string_new ("")/g_string_new (NULL)/ (8.00 KB, patch)
2003-02-26 15:23 UTC, Morten Welinder
none Details | Review

Description Morten Welinder 2003-02-24 21:23:07 UTC
Use g_string_new (NULL) and save a strlen and a g_string_append.


glib/glib/gmarkup.c:  str = g_string_new ("");
glib/glib/gmarkup.c:    context->partial_chunk = g_string_new ("");
glib/glib/gmarkup.c:  str = g_string_new ("");
glib/glib/gmessages.c:  gstring = g_string_new ("");
glib/glib/gscanner.c:     gstring = g_string_new ("");
glib/glib/gscanner.c:     gstring = g_string_new ("");
glib/glib/gscanner.c:     gstring = g_string_new ("");
glib/glib/gscanner.c:         gstring = g_string_new ("");
glib/glib/gscanner.c:             gstring = g_string_new ("");
glib/glib/gshell.c:  retval = g_string_new ("");
glib/glib/gshell.c:    *token = g_string_new ("");
glib/glib/gspawn-win32-helper.c:      debugstring = g_string_new ("");
glib/glib/gspawn-win32-helper.c:      debugstring = g_string_new ("");
glib/glib/gspawn-win32-helper.c:      debugstring = g_string_new ("");
glib/glib/gspawn-win32-helper.c:      debugstring = g_string_new ("");
glib/glib/gspawn-win32.c:      outstr = g_string_new ("");
glib/glib/gspawn-win32.c:      errstr = g_string_new ("");
glib/glib/gspawn.c:      outstr = g_string_new ("");
glib/glib/gspawn.c:      errstr = g_string_new ("");
glib/gobject/gvaluetransform.c:      GString *gstring = g_string_new ("");
glib/tests/string-test.c:  string2 = g_string_new ("");
glib/tests/testglib.c:  string2 = g_string_new ("");
Comment 1 Havoc Pennington 2003-02-24 22:24:01 UTC
if this is an issue why not save apps having to think about it 
with a check like "if (s == NULL || *s == '\0')"
in g_string_new()?
Comment 2 Morten Welinder 2003-02-25 00:20:21 UTC
That's silly.  Just run the perl one-liner to fix them all instead of
slowing down the common path.

This showed up because gnumeric was spending too much time in strlen,
mostly because of this kind of silliness.  Millions of calls add up
over time.
Comment 3 Havoc Pennington 2003-02-25 00:24:44 UTC
Is it a one-time perl script or will app authors continue to do this? 
Seems likely they'll habitually do it.

Doesn't seem like "*s == '\0'" is likely to show up in profiles, 
and if you pass in NULL for s you don't even hit the *s == '\0', 
you only hit it if you were going to run strlen anyway
Comment 4 Morten Welinder 2003-02-25 03:25:12 UTC
Application authors can write slow code any which way they please.
I am more concerned by libraries.

Yes, it'll have to be revisited from time to time with no great haste.
(After all, the "" versions is functionally fine.)  That's not much
different from

* memory and file descriptor leaks
* incorrect isdigit/tolower/... usage
* printf ("...%d...", ..., getpid (), ...)
* oink = *((guint32*)(random_ptr))
* char *oink = "text";

and other systematic mistakes people get themselves into all the time.

(And while I am at it, why does this function end up calling
strlen *twice*?  It should really cache the length and use
g_string_append_len.)
Comment 5 Matthias Clasen 2003-02-26 00:30:51 UTC
Created attachment 14621 [details] [review]
patch
Comment 6 Matthias Clasen 2003-02-26 00:33:54 UTC
Here is a proposal for a g_string_new which goes straight to
g_string_sized_new for NULL or "". The chapest replacement for
g_string_new ("") is probably g_string_sized_new (0).
Comment 7 Morten Welinder 2003-02-26 15:23:16 UTC
Created attachment 14637 [details] [review]
s/g_string_new ("")/g_string_new (NULL)/
Comment 8 Morten Welinder 2003-02-26 15:24:39 UTC
That patch came from this script:

perl -pi -e 's/g_string_new \(""\)/g_string_new (NULL)/g' */*.c
Comment 9 Owen Taylor 2003-03-28 19:10:32 UTC
The patch to g_string_new() looks good to me. I'd go
ahead and commit it to both glib-2-2 and HEAD.

The patch to change g_string_new("") to g_string_new(NULL)
looks fine to me too ... while I very much doubt it will
be timable, there is some overhead to a string constant,
even if it is "".

(Actually, I'd probably omit the changes to the two test
cases, it makes them less clear for no benefit.)
Comment 10 Matthias Clasen 2003-03-30 22:02:45 UTC
Committed to both branches, omitting the test changes.