bench-strcasestr.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /* Measure strcasestr functions.
  2. Copyright (C) 2013-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #define MIN_PAGE_SIZE 131072
  16. #define TEST_MAIN
  17. #define TEST_NAME "strcasestr"
  18. #include "bench-string.h"
  19. #include "json-lib.h"
  20. static const char input[] =
  21. "This manual is written with the assumption that you are at least "
  22. "somewhat familiar with the C programming language and basic programming "
  23. "concepts. Specifically, familiarity with ISO standard C (*note ISO "
  24. "C::), rather than “traditional” pre-ISO C dialects, is assumed.\n"
  25. " The GNU C Library includes several “header files”, each of which "
  26. "provides definitions and declarations for a group of related facilities; "
  27. "this information is used by the C compiler when processing your program. "
  28. "For example, the header file ‘stdio.h’ declares facilities for "
  29. "performing input and output, and the header file ‘string.h’ declares "
  30. "string processing utilities. The organization of this manual generally "
  31. "follows the same division as the header files.\n"
  32. " If you are reading this manual for the first time, you should read "
  33. "all of the introductory material and skim the remaining chapters. There "
  34. "are a _lot_ of functions in the GNU C Library and it’s not realistic to "
  35. "expect that you will be able to remember exactly _how_ to use each and "
  36. "every one of them. It’s more important to become generally familiar "
  37. "with the kinds of facilities that the library provides, so that when you "
  38. "are writing your programs you can recognize _when_ to make use of "
  39. "library functions, and _where_ in this manual you can find more specific "
  40. "information about them.\n";
  41. #define STRCASESTR simple_strcasestr
  42. #define NO_ALIAS
  43. #define __strncasecmp strncasecmp
  44. #define __strnlen strnlen
  45. #include "../string/strcasestr.c"
  46. typedef char *(*proto_t) (const char *, const char *);
  47. IMPL (simple_strcasestr, 0)
  48. IMPL (strcasestr, 1)
  49. static void
  50. do_one_test (json_ctx_t *json_ctx, impl_t *impl, const char *s1,
  51. const char *s2, char *exp_result)
  52. {
  53. size_t i, iters = INNER_LOOP_ITERS_SMALL / 8;
  54. timing_t start, stop, cur;
  55. char *res;
  56. TIMING_NOW (start);
  57. for (i = 0; i < iters; ++i)
  58. res = CALL (impl, s1, s2);
  59. TIMING_NOW (stop);
  60. TIMING_DIFF (cur, start, stop);
  61. json_element_double (json_ctx, (double) cur / (double) iters);
  62. if (res != exp_result)
  63. {
  64. error (0, 0, "Wrong result in function %s %s %s", impl->name,
  65. (res == NULL) ? "(null)" : res,
  66. (exp_result == NULL) ? "(null)" : exp_result);
  67. ret = 1;
  68. }
  69. }
  70. static void
  71. do_test (json_ctx_t *json_ctx, size_t align1, size_t align2, size_t len1,
  72. size_t len2, int fail)
  73. {
  74. char *s1 = (char *) (buf1 + align1);
  75. char *s2 = (char *) (buf2 + align2);
  76. size_t size = sizeof (input) - 1;
  77. size_t pos = (len1 + len2) % size;
  78. char *ss2 = s2;
  79. for (size_t l = len2; l > 0; l = l > size ? l - size : 0)
  80. {
  81. size_t t = l > size ? size : l;
  82. if (pos + t <= size)
  83. ss2 = mempcpy (ss2, input + pos, t);
  84. else
  85. {
  86. ss2 = mempcpy (ss2, input + pos, size - pos);
  87. ss2 = mempcpy (ss2, input, t - (size - pos));
  88. }
  89. }
  90. s2[len2] = '\0';
  91. char *ss1 = s1;
  92. for (size_t l = len1; l > 0; l = l > size ? l - size : 0)
  93. {
  94. size_t t = l > size ? size : l;
  95. memcpy (ss1, input, t);
  96. ss1 += t;
  97. }
  98. if (!fail)
  99. memcpy (s1 + len1 - len2, s2, len2);
  100. s1[len1] = '\0';
  101. /* Remove any accidental matches except for the last if !fail. */
  102. for (ss1 = simple_strcasestr (s1, s2);
  103. ss1 != NULL;
  104. ss1 = simple_strcasestr (ss1 + 1, s2))
  105. if (fail || ss1 != s1 + len1 - len2)
  106. ++ss1[len2 / 2];
  107. json_element_object_begin (json_ctx);
  108. json_attr_uint (json_ctx, "len_haystack", len1);
  109. json_attr_uint (json_ctx, "len_needle", len2);
  110. json_attr_uint (json_ctx, "align_haystack", align1);
  111. json_attr_uint (json_ctx, "align_needle", align2);
  112. json_attr_uint (json_ctx, "fail", fail);
  113. json_array_begin (json_ctx, "timings");
  114. FOR_EACH_IMPL (impl, 0)
  115. do_one_test (json_ctx, impl, s1, s2, fail ? NULL : s1 + len1 - len2);
  116. json_array_end (json_ctx);
  117. json_element_object_end (json_ctx);
  118. }
  119. /* Test needles which exhibit worst-case performance for naive quadradic
  120. implementations. */
  121. static void
  122. test_hard_needle (json_ctx_t *json_ctx, size_t ne_len, size_t hs_len)
  123. {
  124. char *ne = (char *) buf1;
  125. char *hs = (char *) buf2;
  126. /* Hard needle for strstr algorithm using skip table. This results in many
  127. memcmp calls comparing most of the needle. */
  128. {
  129. memset (ne, 'a', ne_len);
  130. ne[ne_len] = '\0';
  131. ne[ne_len - 14] = 'b';
  132. memset (hs, 'a', hs_len);
  133. for (size_t i = ne_len; i <= hs_len; i += ne_len)
  134. {
  135. hs[i - 5] = 'b';
  136. hs[i - 62] = 'b';
  137. }
  138. json_element_object_begin (json_ctx);
  139. json_attr_uint (json_ctx, "len_haystack", hs_len);
  140. json_attr_uint (json_ctx, "len_needle", ne_len);
  141. json_attr_uint (json_ctx, "align_haystack", 0);
  142. json_attr_uint (json_ctx, "align_needle", 0);
  143. json_attr_uint (json_ctx, "fail", 1);
  144. json_attr_string (json_ctx, "desc", "Difficult skiptable(0)");
  145. json_array_begin (json_ctx, "timings");
  146. FOR_EACH_IMPL (impl, 0)
  147. do_one_test (json_ctx, impl, hs, ne, NULL);
  148. json_array_end (json_ctx);
  149. json_element_object_end (json_ctx);
  150. }
  151. /* 2nd hard needle for strstr algorithm using skip table. This results in
  152. many memcmp calls comparing most of the needle. */
  153. {
  154. memset (ne, 'a', ne_len);
  155. ne[ne_len] = '\0';
  156. ne[ne_len - 6] = 'b';
  157. memset (hs, 'a', hs_len);
  158. for (size_t i = ne_len; i <= hs_len; i += ne_len)
  159. {
  160. hs[i - 5] = 'b';
  161. hs[i - 6] = 'b';
  162. }
  163. json_element_object_begin (json_ctx);
  164. json_attr_uint (json_ctx, "len_haystack", hs_len);
  165. json_attr_uint (json_ctx, "len_needle", ne_len);
  166. json_attr_uint (json_ctx, "align_haystack", 0);
  167. json_attr_uint (json_ctx, "align_needle", 0);
  168. json_attr_uint (json_ctx, "fail", 1);
  169. json_attr_string (json_ctx, "desc", "Difficult skiptable(1)");
  170. json_array_begin (json_ctx, "timings");
  171. FOR_EACH_IMPL (impl, 0)
  172. do_one_test (json_ctx, impl, hs, ne, NULL);
  173. json_array_end (json_ctx);
  174. json_element_object_end (json_ctx);
  175. }
  176. /* Hard needle for Two-way algorithm - the random input causes a large number
  177. of branch mispredictions which significantly reduces performance on modern
  178. micro architectures. */
  179. {
  180. for (int i = 0; i < hs_len; i++)
  181. hs[i] = (rand () & 255) > 155 ? 'a' : 'b';
  182. hs[hs_len] = 0;
  183. memset (ne, 'a', ne_len);
  184. ne[ne_len - 2] = 'b';
  185. ne[0] = 'b';
  186. ne[ne_len] = 0;
  187. json_element_object_begin (json_ctx);
  188. json_attr_uint (json_ctx, "len_haystack", hs_len);
  189. json_attr_uint (json_ctx, "len_needle", ne_len);
  190. json_attr_uint (json_ctx, "align_haystack", 0);
  191. json_attr_uint (json_ctx, "align_needle", 0);
  192. json_attr_uint (json_ctx, "fail", 1);
  193. json_attr_string (json_ctx, "desc", "Difficult 2-way");
  194. json_array_begin (json_ctx, "timings");
  195. FOR_EACH_IMPL (impl, 0)
  196. do_one_test (json_ctx, impl, hs, ne, NULL);
  197. json_array_end (json_ctx);
  198. json_element_object_end (json_ctx);
  199. }
  200. /* Hard needle for standard algorithm testing first few characters of
  201. * needle. */
  202. {
  203. for (int i = 0; i < hs_len; i++)
  204. hs[i] = (rand () & 255) >= 128 ? 'a' : 'b';
  205. hs[hs_len] = 0;
  206. for (int i = 0; i < ne_len; i++)
  207. {
  208. if (i % 3 == 0)
  209. ne[i] = 'a';
  210. else if (i % 3 == 1)
  211. ne[i] = 'b';
  212. else
  213. ne[i] = 'c';
  214. }
  215. ne[ne_len] = 0;
  216. json_element_object_begin (json_ctx);
  217. json_attr_uint (json_ctx, "len_haystack", hs_len);
  218. json_attr_uint (json_ctx, "len_needle", ne_len);
  219. json_attr_uint (json_ctx, "align_haystack", 0);
  220. json_attr_uint (json_ctx, "align_needle", 0);
  221. json_attr_uint (json_ctx, "fail", 1);
  222. json_attr_string (json_ctx, "desc", "Difficult testing first 2");
  223. json_array_begin (json_ctx, "timings");
  224. FOR_EACH_IMPL (impl, 0)
  225. do_one_test (json_ctx, impl, hs, ne, NULL);
  226. json_array_end (json_ctx);
  227. json_element_object_end (json_ctx);
  228. }
  229. }
  230. static int
  231. test_main (void)
  232. {
  233. json_ctx_t json_ctx;
  234. test_init ();
  235. json_init (&json_ctx, 0, stdout);
  236. json_document_begin (&json_ctx);
  237. json_attr_string (&json_ctx, "timing_type", TIMING_TYPE);
  238. json_attr_object_begin (&json_ctx, "functions");
  239. json_attr_object_begin (&json_ctx, TEST_NAME);
  240. json_attr_string (&json_ctx, "bench-variant", "");
  241. json_array_begin (&json_ctx, "ifuncs");
  242. FOR_EACH_IMPL (impl, 0)
  243. json_element_string (&json_ctx, impl->name);
  244. json_array_end (&json_ctx);
  245. json_array_begin (&json_ctx, "results");
  246. for (size_t hlen = 8; hlen <= 256;)
  247. for (size_t klen = 1; klen <= 16; klen++)
  248. {
  249. do_test (&json_ctx, 1, 3, hlen, klen, 0);
  250. do_test (&json_ctx, 0, 9, hlen, klen, 1);
  251. do_test (&json_ctx, 1, 3, hlen + 1, klen, 0);
  252. do_test (&json_ctx, 0, 9, hlen + 1, klen, 1);
  253. do_test (&json_ctx, getpagesize () - 15, 9, hlen, klen, 1);
  254. if (hlen < 64)
  255. {
  256. hlen += 8;
  257. }
  258. else
  259. {
  260. hlen += 32;
  261. }
  262. }
  263. for (size_t hlen = 256; hlen <= 65536; hlen *= 2)
  264. for (size_t klen = 4; klen <= 256; klen *= 2)
  265. {
  266. do_test (&json_ctx, 1, 11, hlen, klen, 0);
  267. do_test (&json_ctx, 14, 5, hlen, klen, 1);
  268. do_test (&json_ctx, 1, 11, hlen + 1, klen + 1, 0);
  269. do_test (&json_ctx, 14, 5, hlen + 1, klen + 1, 1);
  270. do_test (&json_ctx, 1, 11, hlen + 1, klen, 0);
  271. do_test (&json_ctx, 14, 5, hlen + 1, klen, 1);
  272. do_test (&json_ctx, getpagesize () - 15, 5, hlen + 1, klen, 1);
  273. }
  274. test_hard_needle (&json_ctx, 64, 65536);
  275. test_hard_needle (&json_ctx, 256, 65536);
  276. test_hard_needle (&json_ctx, 1024, 65536);
  277. json_array_end (&json_ctx);
  278. json_attr_object_end (&json_ctx);
  279. json_attr_object_end (&json_ctx);
  280. json_document_end (&json_ctx);
  281. return ret;
  282. }
  283. #include <support/test-driver.c>