gcc 3.2.1 optimizer degradation (strlen, -O2)

Leonid Pauzner uue@pauzner.dnttm.ru
Sat Jan 11 17:58:00 GMT 2003


Hi!

It turns out that the libc strlen() function (compiled with -O2)
became nearly 2 times slower when I switched from gcc 2.95.3 to gcc 3.2.1
on a Pentium machine.

Hand-optimised version improve performance of 3.2.1 compiled strlen code
up to the level of 2.95.3 (and with 2.95.3 both optimized and original
versions show the same performance).


This is DJGPP libc, strlen.c:
=============================
/* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
#include <string.h>

size_t
strlen(const char *str)
{
  const char *s;

  if (str == 0)
    return 0;
  for (s = str; *s; ++s);
  return s-str;
}
=============================

My version (strlen2.c)
======================
#include <string.h>

size_t
strlen(const char *str)
{
  register const char *s = str;

  if (s == 0)
    return 0;
  while (*s++);
  return s-1-str;
}
======================

Sample test case:
=================
#include <string.h>

int main ()
{
  const char* str =
    "jhrfhruiehfiuerhifuhreihfgihrighihrfghkrjehklhlqkmjewbfjkfgtyhjjhgrwae3k";
  int len = strlen(str);
  int l, res;
  int n = 100000;

  for (; n > 0; n--) {
    l = len;
    while (l != 0) {
      res = strlen(str+l);  /* res = len - l */
      l = len - res;        /* touch res */
      l--;
    }
  }
  return 0;
}




More information about the Gcc mailing list