Improve bench-strstr
[glibc.git] / benchtests / bench-strstr.c
blob31309b24029a96de7381e1050fd89e5d26642e5f
1 /* Measure strstr functions.
2 Copyright (C) 2013-2019 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #define MIN_PAGE_SIZE 131072
20 #define TEST_MAIN
21 #define TEST_NAME "strstr"
22 #include "bench-string.h"
24 static const char input[] =
25 "This manual is written with the assumption that you are at least "
26 "somewhat familiar with the C programming language and basic programming "
27 "concepts. Specifically, familiarity with ISO standard C (*note ISO "
28 "C::), rather than “traditional” pre-ISO C dialects, is assumed.\n"
30 " The GNU C Library includes several “header files”, each of which "
31 "provides definitions and declarations for a group of related facilities; "
32 "this information is used by the C compiler when processing your program. "
33 "For example, the header file ‘stdio.h’ declares facilities for "
34 "performing input and output, and the header file ‘string.h’ declares "
35 "string processing utilities. The organization of this manual generally "
36 "follows the same division as the header files.\n"
38 " If you are reading this manual for the first time, you should read "
39 "all of the introductory material and skim the remaining chapters. There "
40 "are a _lot_ of functions in the GNU C Library and it’s not realistic to "
41 "expect that you will be able to remember exactly _how_ to use each and "
42 "every one of them. It’s more important to become generally familiar "
43 "with the kinds of facilities that the library provides, so that when you "
44 "are writing your programs you can recognize _when_ to make use of "
45 "library functions, and _where_ in this manual you can find more specific "
46 "information about them.\n";
48 /* Simple yet efficient strstr - for needles < 32 bytes it is 2-4 times
49 faster than the optimized twoway_strstr. */
50 static char *
51 basic_strstr (const char *s1, const char *s2)
53 size_t i;
54 int c = s2[0];
56 if (c == 0)
57 return (char*)s1;
59 for ( ; s1[0] != '\0'; s1++)
61 if (s1[0] != c)
62 continue;
63 for (i = 1; s2[i] != 0; i++)
64 if (s1[i] != s2[i])
65 break;
66 if (s2[i] == '\0')
67 return (char*)s1;
70 return NULL;
73 #define RETURN_TYPE char *
74 #define AVAILABLE(h, h_l, j, n_l) \
75 (((j) + (n_l) <= (h_l)) \
76 || ((h_l) += __strnlen ((void*)((h) + (h_l)), (n_l) + 512), \
77 (j) + (n_l) <= (h_l)))
78 #define CHECK_EOL (1)
79 #define RET0_IF_0(a) if (!a) goto ret0
80 #define FASTSEARCH(S,C,N) (void*) strchr ((void*)(S), (C))
81 #define LONG_NEEDLE_THRESHOLD 32U
82 #define __strnlen strnlen
83 #include "string/str-two-way.h"
85 /* Optimized Two-way implementation from GLIBC 2.29. */
86 static char *
87 twoway_strstr (const char *haystack, const char *needle)
89 size_t needle_len; /* Length of NEEDLE. */
90 size_t haystack_len; /* Known minimum length of HAYSTACK. */
92 /* Handle empty NEEDLE special case. */
93 if (needle[0] == '\0')
94 return (char *) haystack;
96 /* Skip until we find the first matching char from NEEDLE. */
97 haystack = strchr (haystack, needle[0]);
98 if (haystack == NULL || needle[1] == '\0')
99 return (char *) haystack;
101 /* Ensure HAYSTACK length is at least as long as NEEDLE length.
102 Since a match may occur early on in a huge HAYSTACK, use strnlen
103 and read ahead a few cachelines for improved performance. */
104 needle_len = strlen (needle);
105 haystack_len = __strnlen (haystack, needle_len + 256);
106 if (haystack_len < needle_len)
107 return NULL;
109 /* Check whether we have a match. This improves performance since we avoid
110 the initialization overhead of the two-way algorithm. */
111 if (memcmp (haystack, needle, needle_len) == 0)
112 return (char *) haystack;
114 /* Perform the search. Abstract memory is considered to be an array
115 of 'unsigned char' values, not an array of 'char' values. See
116 ISO C 99 section 6.2.6.1. */
117 if (needle_len < LONG_NEEDLE_THRESHOLD)
118 return two_way_short_needle ((const unsigned char *) haystack,
119 haystack_len,
120 (const unsigned char *) needle, needle_len);
121 return two_way_long_needle ((const unsigned char *) haystack, haystack_len,
122 (const unsigned char *) needle, needle_len);
125 typedef char *(*proto_t) (const char *, const char *);
127 IMPL (strstr, 1)
128 IMPL (twoway_strstr, 0)
129 IMPL (basic_strstr, 0)
131 static void
132 do_one_test (impl_t *impl, const char *s1, const char *s2, char *exp_result)
134 size_t i, iters = INNER_LOOP_ITERS;
135 timing_t start, stop, cur;
136 char *res;
138 TIMING_NOW (start);
139 for (i = 0; i < iters; ++i)
140 res = CALL (impl, s1, s2);
141 TIMING_NOW (stop);
143 TIMING_DIFF (cur, start, stop);
145 TIMING_PRINT_MEAN ((double) cur, (double) iters);
147 if (res != exp_result)
149 error (0, 0, "Wrong result in function %s %s %s", impl->name,
150 res, exp_result);
151 ret = 1;
156 static void
157 do_test (size_t align1, size_t align2, size_t len1, size_t len2,
158 int fail)
160 char *s1 = (char *) (buf1 + align1);
161 char *s2 = (char *) (buf2 + align2);
163 size_t size = sizeof (input) - 1;
164 size_t pos = (len1 + len2) % size;
166 char *ss2 = s2;
167 for (size_t l = len2; l > 0; l = l > size ? l - size : 0)
169 size_t t = l > size ? size : l;
170 if (pos + t <= size)
171 ss2 = mempcpy (ss2, input + pos, t);
172 else
174 ss2 = mempcpy (ss2, input + pos, size - pos);
175 ss2 = mempcpy (ss2, input, t - (size - pos));
178 s2[len2] = '\0';
180 char *ss1 = s1;
181 for (size_t l = len1; l > 0; l = l > size ? l - size : 0)
183 size_t t = l > size ? size : l;
184 memcpy (ss1, input, t);
185 ss1 += t;
188 if (!fail)
189 memcpy (s1 + len1 - len2, s2, len2);
190 s1[len1] = '\0';
192 /* Remove any accidental matches except for the last if !fail. */
193 for (ss1 = basic_strstr (s1, s2); ss1; ss1 = basic_strstr (ss1 + 1, s2))
194 if (fail || ss1 != s1 + len1 - len2)
195 ++ss1[len2 / 2];
197 printf ("Length %4zd/%3zd, alignment %2zd/%2zd, %s:",
198 len1, len2, align1, align2, fail ? "fail " : "found");
200 FOR_EACH_IMPL (impl, 0)
201 do_one_test (impl, s1, s2, fail ? NULL : s1 + len1 - len2);
203 putchar ('\n');
206 static int
207 test_main (void)
209 test_init ();
211 printf ("%23s", "");
212 FOR_EACH_IMPL (impl, 0)
213 printf ("\t%s", impl->name);
214 putchar ('\n');
216 for (size_t hlen = 64; hlen <= 256; hlen += 32)
217 for (size_t klen = 1; klen <= 16; klen++)
219 do_test (1, 3, hlen, klen, 0);
220 do_test (0, 9, hlen, klen, 1);
223 for (size_t hlen = 256; hlen <= 65536; hlen *= 2)
224 for (size_t klen = 16; klen <= 256; klen *= 2)
226 do_test (1, 11, hlen, klen, 0);
227 do_test (14, 5, hlen, klen, 1);
230 return ret;
233 #include <support/test-driver.c>