public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug bootstrap/38302]  New: inefficient use of strlen in for loop
@ 2008-11-28 14:04 dcb314 at hotmail dot com
  2008-11-28 17:05 ` [Bug bootstrap/38302] " pinskia at gcc dot gnu dot org
  2008-11-30 11:34 ` rguenth at gcc dot gnu dot org
  0 siblings, 2 replies; 3+ messages in thread
From: dcb314 at hotmail dot com @ 2008-11-28 14:04 UTC (permalink / raw)
  To: gcc-bugs

I just had a look at the source code of the GNU C compiler
snapshot 20091121, version 4.4, looking for the "inefficient use of strlen in
a for loop" pattern.

For file gcc/gengtype.c

$ grep "for.*;.*strlen.*;" gcc/gengtype.c
      for (aindex = 0; aindex < strlen (rtx_format[i]); aindex++)
      for (nmindex = 0; nmindex < strlen (ftag); nmindex++)

I checked the source code and it seems to me that the strlens may
be moved outside the for loop to speed up the code
from quadratic to linear and simplify the code.

I tried out my idea and the compiler seemed to bootstrap ok.

Here is a patch

--- gcc/gengtype.c.sav  2008-11-28 11:47:25.000000000 +0000
+++ gcc/gengtype.c      2008-11-28 11:49:57.000000000 +0000
@@ -1062,8 +1062,10 @@
       const char *sname;
       type_p substruct;
       char *ftag;
+      const size_t dcb_len1 = strlen (rtx_format[i]);
+      size_t dcb_len2;

-      for (aindex = 0; aindex < strlen (rtx_format[i]); aindex++)
+      for (aindex = 0; aindex < dcb_len1; aindex++)
        {
          type_p t;
          const char *subname;
@@ -1192,7 +1194,8 @@
       substruct = new_structure (sname, 0, &lexer_line, subfields, NULL);

       ftag = xstrdup (rtx_name[i]);
-      for (nmindex = 0; nmindex < strlen (ftag); nmindex++)
+      dcb_len2 = strlen( ftag);
+      for (nmindex = 0; nmindex < dcb_len2; nmindex++)
        ftag[nmindex] = TOUPPER (ftag[nmindex]);

       flds = create_field (flds, substruct, "");


-- 
           Summary: inefficient use of strlen in for loop
           Product: gcc
           Version: 4.4.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: bootstrap
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: dcb314 at hotmail dot com
  GCC host triplet: suse_linux-x86_64


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38302


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2008-11-30 11:34 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-11-28 14:04 [Bug bootstrap/38302] New: inefficient use of strlen in for loop dcb314 at hotmail dot com
2008-11-28 17:05 ` [Bug bootstrap/38302] " pinskia at gcc dot gnu dot org
2008-11-30 11:34 ` rguenth at gcc dot gnu dot org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).