This is the mail archive of the libc-alpha@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH 2/2] Consolidate cmp benchtests: New implementation.


On Fri, Sep 06, 2013 at 03:49:51PM +0530, Siddhesh Poyarekar wrote:
> On Fri, Sep 06, 2013 at 10:58:38AM +0200, OndÅej BÃlka wrote:
> > Hi,
> > 
> > Same idea with cmp functions.
> > 
> > Again we would need to select more representative inputs as followup.
> > 
> > For strcasecmp we shuold also check inputs consisting of aA to stress
> > case where conversion is needed.
> > 
> > 
> > 	* benchtests/bench-cmp.h: New file.
> > 	* benchtests/bench-memcmp.c: Use benchtests/bench-cmp.h.
> > 	* benchtests/bench-strcasecmp.c: Likewise.
> > 	* benchtests/bench-strcmp.c: Likewise.
> > 	* benchtests/bench-strncasecmp.c: Likewise.
> > 	* benchtests/bench-strncmp.c: Likewise.
> > 
> > ---
> >  benchtests/bench-cmp.h         |  112 ++++++++++++++++++++
> >  benchtests/bench-memcmp.c      |  159 +---------------------------
> >  benchtests/bench-strcasecmp.c  |  158 +---------------------------
> >  benchtests/bench-strcmp.c      |  225 ++--------------------------------------
> >  benchtests/bench-strncasecmp.c |  189 +--------------------------------
> >  benchtests/bench-strncmp.c     |  221 +--------------------------------------
> >  6 files changed, 138 insertions(+), 926 deletions(-)
> >  create mode 100644 benchtests/bench-cmp.h
> > 
> > diff --git a/benchtests/bench-cmp.h b/benchtests/bench-cmp.h
> 
> Please call this bench-cmp.c since the contents are not merely
> declarations.
> 
v2 is here, split to removal of old and replacing by new ones.

Here is addition, it allows fixed and randomized addition,

Second addition is benchmarking implemenations for string directory.

I did not handle strcasecmp functions yet as they performance depends on
locale which we now do not test.

	* benchtests/bench-cmp.c: New File.
	* benchtests/bench-memcmp.c: Likewise.
	* benchtests/bench-strcasecmp.c: Likewise.
	* benchtests/bench-strcmp.c: Likewise.
	* benchtests/bench-strcasencmp.c: Likewise.
	* benchtests/bench-strncmp.c: Likewise.
	* string/memcmp.c (memcmp): Allow renaming by MEMCMP macro.
	* string/strcmp.c (strcmp): Allow renaming by STRCMP macro.

---
 benchtests/bench-cmp.c         | 168 +++++++++++++++++++++++++++++++++++++++++
 benchtests/bench-memcmp.c      |  27 +++++++
 benchtests/bench-strcasecmp.c  |  22 ++++++
 benchtests/bench-strcmp.c      |  27 +++++++
 benchtests/bench-strncasecmp.c |  22 ++++++
 benchtests/bench-strncmp.c     |  27 +++++++
 string/memcmp.c                |  10 ++-
 string/strcmp.c                |  10 ++-
 8 files changed, 307 insertions(+), 6 deletions(-)
 create mode 100644 benchtests/bench-cmp.c
 create mode 100644 benchtests/bench-memcmp.c
 create mode 100644 benchtests/bench-strcasecmp.c
 create mode 100644 benchtests/bench-strcmp.c
 create mode 100644 benchtests/bench-strncasecmp.c
 create mode 100644 benchtests/bench-strncmp.c

diff --git a/benchtests/bench-cmp.c b/benchtests/bench-cmp.c
new file mode 100644
index 0000000..9fb4293
--- /dev/null
+++ b/benchtests/bench-cmp.c
@@ -0,0 +1,168 @@
+/* Measure memcmp, strcmp, strcasecmp, ... functions.
+   Copyright (C) 2013 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
+
+#define MIN_PAGE_SIZE (1<<22)
+#define TEST_MAIN
+#include "bench-string.h"
+#define CACHE_LINE_SIZE 64
+
+#ifdef WIDE
+# define CHAR wchar_t
+#else
+# define CHAR char
+#endif
+
+#if defined AS_STRNCMP || defined AS_STRNCASECMP || defined AS_MEMCMP
+typedef char *(*proto_t) (const char *, const char *, size_t);
+#else
+typedef char *(*proto_t) (const char *, const char *);
+#endif
+
+IMPL (FN_NAME, 1);
+
+static void
+do_one_test (impl_t *impl, size_t aligned, size_t len)
+{
+  size_t i, iters = INNER_LOOP_ITERS;
+  timing_t start, stop, cur;
+  static unsigned int r_seed = 42;
+
+  CHAR *srcs[INNER_LOOP_ITERS], *dsts[INNER_LOOP_ITERS], found[INNER_LOOP_ITERS];
+
+  for (i = 0; i < iters; ++i)
+    {
+      srcs[i] = ((CHAR *) buf1) + 
+                rand_r (&r_seed) % (MIN_PAGE_SIZE / sizeof (CHAR) / 2);
+      dsts[i] = ((CHAR *) buf1) + 
+                rand_r (&r_seed) % (MIN_PAGE_SIZE / sizeof (CHAR) / 2);
+
+      found[i] = (rand_r (&r_seed) % 2) ? 'a' : 'b';
+      if (aligned != -1) /* Align pointers. */
+        srcs[i] = (CHAR *) (((uintptr_t) srcs[i]) & ~(CACHE_LINE_SIZE -1)) + aligned;
+      dsts[i] = (CHAR *) (((uintptr_t) srcs[i]) & ~(CACHE_LINE_SIZE -1));
+
+    }
+
+  TIMING_NOW (start);
+  for (i = 0; i < iters; ++i)
+    {
+      CHAR orig1 = srcs[i][len - 1], orig2 = srcs[i][len];
+      srcs[i][len - 1] = found[i];
+      srcs[i][len] = 0;
+
+#if defined AS_STRNCMP || defined AS_STRNCASECMP || defined AS_MEMCMP
+      CALL (impl, srcs[i], dsts[i], len);
+#else
+      CALL (impl, srcs[i], dsts[i]);
+#endif
+
+      srcs[i][len - 1] = orig1;
+      srcs[i][len] = orig2;
+    }
+  TIMING_NOW (stop);
+
+  TIMING_DIFF (cur, start, stop);
+
+  TIMING_PRINT_MEAN ((double) cur, (double) iters);
+}
+
+static void
+do_test (size_t aligned, size_t len)
+{
+  printf ("%7zd, ",len);
+  if (aligned == -1)
+    printf ("rnd,    ");
+  else
+    printf("%3zd,    ",aligned);
+
+  FOR_EACH_IMPL (impl, 0)
+    do_one_test (impl, aligned, len);
+
+  putchar ('\n');
+}
+
+int
+test_main (void)
+{
+  size_t i;
+
+  test_init ();
+
+  for (i = 0; i < MIN_PAGE_SIZE; i++)
+    {
+      buf1[i] = 'a';
+      buf2[i] = 'a';
+    }
+
+  printf ("  Length, alignement");
+  FOR_EACH_IMPL (impl, 0)
+    printf (" %10s", impl->name);
+  putchar ('\n');
+
+  for (i = 1; i < 64; ++i)
+    {
+      do_test (0, i);
+      do_test (-1, i);
+    }
+
+  for (i = 1; i < 18; ++i)
+    {
+      do_test (0, 1 << i);
+      do_test (-1, 1 << i);
+    }
+
+
+  for (i = 0; i < CACHE_LINE_SIZE; ++i)
+    {
+      do_test (i, 16);
+    }
+
+  for (i = 0; i < CACHE_LINE_SIZE; ++i)
+    {
+      do_test (i, 32);
+    }
+
+  for (i = 0; i < CACHE_LINE_SIZE; ++i)
+    {
+      do_test (i, 64);
+    }
+
+#if defined AS_STRCASECMP || defined AS_STRNCASECMP
+  puts ("Compare aaaa vs AAAA.");
+  for (i = 0; i < MIN_PAGE_SIZE; i++)
+    {
+      buf1[i] = 'A';
+    }
+
+  for (i = 1; i < 64; ++i)
+    {
+      do_test (0, i);
+      do_test (-1, i);
+    }
+
+  for (i = 1; i < 18; ++i)
+    {
+      do_test (0, 1 << i);
+      do_test (-1, 1 << i);
+    }
+#endif
+
+  return ret;
+}
+
+#include "../test-skeleton.c"
diff --git a/benchtests/bench-memcmp.c b/benchtests/bench-memcmp.c
new file mode 100644
index 0000000..97e6da2
--- /dev/null
+++ b/benchtests/bench-memcmp.c
@@ -0,0 +1,27 @@
+/* Measure memcmp functions.
+   Copyright (C) 2013 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
+
+#define FN_NAME memcmp
+#define TEST_NAME "memcmp"
+#define AS_STRCHR
+#include "bench-cmp.c"
+
+#define MEMCMP memcmp_string
+#include "string/memcmp.c"
+
+IMPL (memcmp_string, 1)
diff --git a/benchtests/bench-strcasecmp.c b/benchtests/bench-strcasecmp.c
new file mode 100644
index 0000000..ffd9da42
--- /dev/null
+++ b/benchtests/bench-strcasecmp.c
@@ -0,0 +1,22 @@
+/* Measure strcasecmp functions.
+   Copyright (C) 2013 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
+
+#define FN_NAME strcasecmp
+#define TEST_NAME "strcasecmp"
+#define AS_MEMCHR
+#include "bench-cmp.c"
diff --git a/benchtests/bench-strcmp.c b/benchtests/bench-strcmp.c
new file mode 100644
index 0000000..18c200b
--- /dev/null
+++ b/benchtests/bench-strcmp.c
@@ -0,0 +1,27 @@
+/* Measure strcmp functions.
+   Copyright (C) 2013 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
+
+#define FN_NAME strcmp
+#define TEST_NAME "strcmp"
+#define AS_STRCHR
+#include "bench-cmp.c"
+
+#define STRCMP strcmp_string
+#include "string/strcmp.c"
+
+IMPL (strcmp_string, 1)
diff --git a/benchtests/bench-strncasecmp.c b/benchtests/bench-strncasecmp.c
new file mode 100644
index 0000000..5301917
--- /dev/null
+++ b/benchtests/bench-strncasecmp.c
@@ -0,0 +1,22 @@
+/* Measure strncasecmp functions.
+   Copyright (C) 2013 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
+
+#define FN_NAME strncasecmp
+#define TEST_NAME "strncasecmp"
+#define AS_MEMCHR
+#include "bench-cmp.c"
diff --git a/benchtests/bench-strncmp.c b/benchtests/bench-strncmp.c
new file mode 100644
index 0000000..064dc08
--- /dev/null
+++ b/benchtests/bench-strncmp.c
@@ -0,0 +1,27 @@
+/* Measure strncmp functions.
+   Copyright (C) 2013 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
+
+#define FN_NAME strncmp
+#define TEST_NAME "strncmp"
+#define AS_STRCHR
+#include "bench-cmp.c"
+
+#define STRNCMP strncmp_string
+#include "string/strncmp.c"
+
+IMPL (strncmp_string, 1)
diff --git a/string/memcmp.c b/string/memcmp.c
index dd76145..258b4e7 100644
--- a/string/memcmp.c
+++ b/string/memcmp.c
@@ -29,6 +29,10 @@
 
 #undef memcmp
 
+#ifndef MEMCMP
+# define MEMCMP memcmp
+#endif
+
 #ifdef _LIBC
 
 # include <memcopy.h>
@@ -304,7 +308,7 @@ memcmp_not_common_alignment (srcp1, srcp2, len)
 }
 
 int
-memcmp (s1, s2, len)
+MEMCMP (s1, s2, len)
      const __ptr_t s1;
      const __ptr_t s2;
      size_t len;
@@ -363,8 +367,8 @@ memcmp (s1, s2, len)
 
   return 0;
 }
-libc_hidden_builtin_def(memcmp)
+libc_hidden_builtin_def(MEMCMP)
 #ifdef weak_alias
 # undef bcmp
-weak_alias (memcmp, bcmp)
+weak_alias (MEMCMP, bcmp)
 #endif
diff --git a/string/strcmp.c b/string/strcmp.c
index a464563..b605fbe 100644
--- a/string/strcmp.c
+++ b/string/strcmp.c
@@ -18,13 +18,17 @@
 #include <string.h>
 #include <memcopy.h>
 
-#undef strcmp
+#undef STRCMP
+
+#ifndef STRCMP
+# define STRCMP strcmp
+#endif
 
 /* Compare S1 and S2, returning less than, equal to or
    greater than zero if S1 is lexicographically less than,
    equal to or greater than S2.  */
 int
-strcmp (p1, p2)
+STRCMP (p1, p2)
      const char *p1;
      const char *p2;
 {
@@ -43,4 +47,4 @@ strcmp (p1, p2)
 
   return c1 - c2;
 }
-libc_hidden_builtin_def (strcmp)
+libc_hidden_builtin_def (STRCMP)
-- 
1.8.3.2


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]