From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 31471 invoked by alias); 28 Nov 2008 14:04:41 -0000 Received: (qmail 30724 invoked by uid 48); 28 Nov 2008 14:03:13 -0000 Date: Fri, 28 Nov 2008 14:04:00 -0000 Subject: [Bug bootstrap/38302] New: inefficient use of strlen in for loop X-Bugzilla-Reason: CC Message-ID: Reply-To: gcc-bugzilla@gcc.gnu.org To: gcc-bugs@gcc.gnu.org From: "dcb314 at hotmail dot com" Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-bugs-owner@gcc.gnu.org X-SW-Source: 2008-11/txt/msg02445.txt.bz2 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