2 * Copyright 2013 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "folly/Range.h"
18 #include "folly/Benchmark.h"
19 #include "folly/Foreach.h"
25 namespace folly { namespace detail {
26 // declaration of functions in Range.cpp
27 size_t qfind_first_byte_of_memchr(const StringPiece& haystack,
28 const StringPiece& needles);
30 size_t qfind_first_byte_of_byteset(const StringPiece& haystack,
31 const StringPiece& needles);
33 size_t qfind_first_byte_of_nosse(const StringPiece& haystack,
34 const StringPiece& needles);
37 using namespace folly;
44 void initStr(int len) {
45 cout << "string length " << len << ':' << endl;
54 const size_t ffoDelimSize = 128;
55 vector<string> ffoDelim;
57 string generateString(int len) {
58 std::uniform_int_distribution<uint32_t> validChar(1, 255); // no null-char
61 ret.push_back(validChar(rnd));
66 void initDelims(int len) {
69 string s(len - 1, '\0'); // find_first_of won't finish until last char
73 for (int i = 0; i < ffoDelimSize; ++i) {
74 // most delimiter sets are pretty small, but occasionally there could
76 auto n = rnd() % 8 + 1;
80 auto s = generateString(n);
82 // ~half of tests will find a hit
83 s[rnd() % s.size()] = 'a'; // yes, this could mean 'a' is a duplicate
85 ffoDelim.push_back(s);
89 } // anonymous namespace
91 BENCHMARK(FindSingleCharMemchr, n) {
92 StringPiece haystack(str);
93 FOR_EACH_RANGE (i, 0, n) {
94 doNotOptimizeAway(haystack.find('b'));
96 doNotOptimizeAway(&x);
100 BENCHMARK_RELATIVE(FindSingleCharRange, n) {
102 StringPiece haystack(str);
103 folly::StringPiece needle(&c, &c + 1);
104 FOR_EACH_RANGE (i, 0, n) {
105 doNotOptimizeAway(haystack.find(needle));
106 char x = haystack[0];
107 doNotOptimizeAway(&x);
111 BENCHMARK_DRAW_LINE();
113 // it's useful to compare our custom implementations vs. the standard library
114 inline size_t qfind_first_byte_of_std(const StringPiece& haystack,
115 const StringPiece& needles) {
116 return qfind_first_of(haystack, needles, asciiCaseSensitive);
119 template <class Func>
120 void findFirstOfRange(StringPiece needles, Func func, size_t n) {
121 StringPiece haystack(str);
122 FOR_EACH_RANGE (i, 0, n) {
123 doNotOptimizeAway(func(haystack, needles));
124 char x = haystack[0];
125 doNotOptimizeAway(&x);
129 const string delims2 = "bc";
131 BENCHMARK(FindFirstOf2NeedlesBase, n) {
132 findFirstOfRange(delims2, detail::qfind_first_byte_of, n);
135 BENCHMARK_RELATIVE(FindFirstOf2NeedlesNoSSE, n) {
136 findFirstOfRange(delims2, detail::qfind_first_byte_of_nosse, n);
139 BENCHMARK_RELATIVE(FindFirstOf2NeedlesStd, n) {
140 findFirstOfRange(delims2, qfind_first_byte_of_std, n);
143 BENCHMARK_RELATIVE(FindFirstOf2NeedlesMemchr, n) {
144 findFirstOfRange(delims2, detail::qfind_first_byte_of_memchr, n);
147 BENCHMARK_RELATIVE(FindFirstOf2NeedlesByteSet, n) {
148 findFirstOfRange(delims2, detail::qfind_first_byte_of_byteset, n);
151 BENCHMARK_DRAW_LINE();
153 const string delims4 = "bcde";
155 BENCHMARK(FindFirstOf4NeedlesBase, n) {
156 findFirstOfRange(delims4, detail::qfind_first_byte_of, n);
159 BENCHMARK_RELATIVE(FindFirstOf4NeedlesNoSSE, n) {
160 findFirstOfRange(delims4, detail::qfind_first_byte_of_nosse, n);
163 BENCHMARK_RELATIVE(FindFirstOf4NeedlesStd, n) {
164 findFirstOfRange(delims4, qfind_first_byte_of_std, n);
167 BENCHMARK_RELATIVE(FindFirstOf4NeedlesMemchr, n) {
168 findFirstOfRange(delims4, detail::qfind_first_byte_of_memchr, n);
171 BENCHMARK_RELATIVE(FindFirstOf4NeedlesByteSet, n) {
172 findFirstOfRange(delims4, detail::qfind_first_byte_of_byteset, n);
175 BENCHMARK_DRAW_LINE();
177 const string delims8 = "0123456b";
179 BENCHMARK(FindFirstOf8NeedlesBase, n) {
180 findFirstOfRange(delims8, detail::qfind_first_byte_of, n);
183 BENCHMARK_RELATIVE(FindFirstOf8NeedlesNoSSE, n) {
184 findFirstOfRange(delims8, detail::qfind_first_byte_of_nosse, n);
187 BENCHMARK_RELATIVE(FindFirstOf8NeedlesStd, n) {
188 findFirstOfRange(delims8, qfind_first_byte_of_std, n);
191 BENCHMARK_RELATIVE(FindFirstOf8NeedlesMemchr, n) {
192 findFirstOfRange(delims8, detail::qfind_first_byte_of_memchr, n);
195 BENCHMARK_RELATIVE(FindFirstOf8NeedlesByteSet, n) {
196 findFirstOfRange(delims8, detail::qfind_first_byte_of_byteset, n);
199 BENCHMARK_DRAW_LINE();
201 const string delims16 = "0123456789bcdefg";
203 BENCHMARK(FindFirstOf16NeedlesBase, n) {
204 findFirstOfRange(delims16, detail::qfind_first_byte_of, n);
207 BENCHMARK_RELATIVE(FindFirstOf16NeedlesNoSSE, n) {
208 findFirstOfRange(delims16, detail::qfind_first_byte_of_nosse, n);
211 BENCHMARK_RELATIVE(FindFirstOf16NeedlesStd, n) {
212 findFirstOfRange(delims16, qfind_first_byte_of_std, n);
215 BENCHMARK_RELATIVE(FindFirstOf16NeedlesMemchr, n) {
216 findFirstOfRange(delims16, detail::qfind_first_byte_of_memchr, n);
219 BENCHMARK_RELATIVE(FindFirstOf16NeedlesByteSet, n) {
220 findFirstOfRange(delims16, detail::qfind_first_byte_of_byteset, n);
223 BENCHMARK_DRAW_LINE();
225 const string delims32 = "!bcdefghijklmnopqrstuvwxyz_012345";
227 BENCHMARK(FindFirstOf32NeedlesBase, n) {
228 findFirstOfRange(delims32, detail::qfind_first_byte_of, n);
231 BENCHMARK_RELATIVE(FindFirstOf32NeedlesNoSSE, n) {
232 findFirstOfRange(delims32, detail::qfind_first_byte_of_nosse, n);
235 BENCHMARK_RELATIVE(FindFirstOf32NeedlesStd, n) {
236 findFirstOfRange(delims32, qfind_first_byte_of_std, n);
239 BENCHMARK_RELATIVE(FindFirstOf32NeedlesMemchr, n) {
240 findFirstOfRange(delims32, detail::qfind_first_byte_of_memchr, n);
243 BENCHMARK_RELATIVE(FindFirstOf32NeedlesByteSet, n) {
244 findFirstOfRange(delims32, detail::qfind_first_byte_of_byteset, n);
247 BENCHMARK_DRAW_LINE();
249 const string delims64 = "!bcdefghijklmnopqrstuvwxyz_"
250 "ABCDEFGHIJKLMNOPQRSTUVWXYZ-0123456789$";
252 BENCHMARK(FindFirstOf64NeedlesBase, n) {
253 findFirstOfRange(delims64, detail::qfind_first_byte_of, n);
256 BENCHMARK_RELATIVE(FindFirstOf64NeedlesNoSSE, n) {
257 findFirstOfRange(delims64, detail::qfind_first_byte_of_nosse, n);
260 BENCHMARK_RELATIVE(FindFirstOf64NeedlesStd, n) {
261 findFirstOfRange(delims64, qfind_first_byte_of_std, n);
264 BENCHMARK_RELATIVE(FindFirstOf64NeedlesMemchr, n) {
265 findFirstOfRange(delims64, detail::qfind_first_byte_of_memchr, n);
268 BENCHMARK_RELATIVE(FindFirstOf64NeedlesByteSet, n) {
269 findFirstOfRange(delims64, detail::qfind_first_byte_of_byteset, n);
272 BENCHMARK_DRAW_LINE();
274 template <class Func>
275 void findFirstOfRandom(Func func, size_t iters) {
276 for (int i = 0; i < iters; ++i) {
277 auto test = i % ffoDelim.size();
278 auto p = func(ffoTestString, ffoDelim[test]);
279 doNotOptimizeAway(p);
283 BENCHMARK(FindFirstOfRandomBase, n) {
284 findFirstOfRandom(detail::qfind_first_byte_of, n);
287 BENCHMARK_RELATIVE(FindFirstOfRandomNoSSE, n) {
288 findFirstOfRandom(detail::qfind_first_byte_of_nosse, n);
291 BENCHMARK_RELATIVE(FindFirstOfRandomStd, n) {
292 findFirstOfRandom(qfind_first_byte_of_std, n);
295 BENCHMARK_RELATIVE(FindFirstOfRandomMemchr, n) {
296 findFirstOfRandom(detail::qfind_first_byte_of_memchr, n);
299 BENCHMARK_RELATIVE(FindFirstOfRandomByteSet, n) {
300 findFirstOfRandom(detail::qfind_first_byte_of_byteset, n);
303 BENCHMARK_DRAW_LINE();
305 BENCHMARK(FindFirstOfOffsetRange, n) {
306 StringPiece haystack(str);
307 folly::StringPiece needles("bc");
308 DCHECK_EQ(haystack.size() - 1, haystack.find_first_of(needles, 1)); // works!
309 FOR_EACH_RANGE (i, 0, n) {
310 size_t pos = i % 2; // not a constant to prevent optimization
311 doNotOptimizeAway(haystack.find_first_of(needles, pos));
312 char x = haystack[0];
313 doNotOptimizeAway(&x);
317 BENCHMARK_DRAW_LINE();
319 int main(int argc, char** argv) {
320 google::ParseCommandLineFlags(&argc, &argv, true);
322 for (int len : {1, 8, 10, 16, 32, 64, 128, 256, 10*1024, 1024*1024}) {