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

Reply via email to