2 * Copyright 2014 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/Benchmark.h>
18 #include <folly/Conv.h>
19 #include <folly/Foreach.h>
20 #include <boost/lexical_cast.hpp>
21 #include <gtest/gtest.h>
26 using namespace folly;
37 TEST(Conv, Integral2Integral) {
38 // Same size, different signs
39 s64 = numeric_limits<uint8_t>::max();
40 EXPECT_EQ(to<uint8_t>(s64), s64);
42 s64 = numeric_limits<int8_t>::max();
43 EXPECT_EQ(to<int8_t>(s64), s64);
46 TEST(Conv, Floating2Floating) {
48 double d1 = to<double>(f1);
52 auto f2 = to<float>(d2);
53 EXPECT_EQ(double(f2), d2);
55 double invalidFloat = std::numeric_limits<double>::max();
56 EXPECT_ANY_THROW(to<float>(invalidFloat));
57 invalidFloat = -std::numeric_limits<double>::max();
58 EXPECT_ANY_THROW(to<float>(invalidFloat));
61 auto shouldWork = to<float>(std::numeric_limits<double>::min());
62 // The value of `shouldWork' is an implementation defined choice
63 // between the following two alternatives.
64 EXPECT_TRUE(shouldWork == std::numeric_limits<float>::min() ||
71 template <class String>
72 void testIntegral2String() {
75 template <class String, class Int, class... Ints>
76 void testIntegral2String() {
77 typedef typename make_unsigned<Int>::type Uint;
78 typedef typename make_signed<Int>::type Sint;
81 EXPECT_EQ(to<String>(value), "123");
83 EXPECT_EQ(to<String>(svalue), "123");
85 EXPECT_EQ(to<String>(svalue), "-123");
87 value = numeric_limits<Uint>::min();
88 EXPECT_EQ(to<Uint>(to<String>(value)), value);
89 value = numeric_limits<Uint>::max();
90 EXPECT_EQ(to<Uint>(to<String>(value)), value);
92 svalue = numeric_limits<Sint>::min();
93 EXPECT_EQ(to<Sint>(to<String>(svalue)), svalue);
94 value = numeric_limits<Sint>::max();
95 EXPECT_EQ(to<Sint>(to<String>(svalue)), svalue);
97 testIntegral2String<String, Ints...>();
100 #if FOLLY_HAVE_INT128_T
101 template <class String>
102 void test128Bit2String() {
103 typedef unsigned __int128 Uint;
104 typedef __int128 Sint;
106 EXPECT_EQ(detail::digitsEnough<unsigned __int128>(), 39);
109 EXPECT_EQ(to<String>(value), "123");
111 EXPECT_EQ(to<String>(svalue), "123");
113 EXPECT_EQ(to<String>(svalue), "-123");
115 value = __int128(1) << 64;
116 EXPECT_EQ(to<String>(value), "18446744073709551616");
118 svalue = -(__int128(1) << 64);
119 EXPECT_EQ(to<String>(svalue), "-18446744073709551616");
122 EXPECT_EQ(to<String>(value), "0");
125 EXPECT_EQ(to<String>(svalue), "0");
127 // TODO: the following do not compile to<__int128> ...
130 value = numeric_limits<Uint>::min();
131 EXPECT_EQ(to<Uint>(to<String>(value)), value);
132 value = numeric_limits<Uint>::max();
133 EXPECT_EQ(to<Uint>(to<String>(value)), value);
135 svalue = numeric_limits<Sint>::min();
136 EXPECT_EQ(to<Sint>(to<String>(svalue)), svalue);
137 value = numeric_limits<Sint>::max();
138 EXPECT_EQ(to<Sint>(to<String>(svalue)), svalue);
144 TEST(Conv, Integral2String) {
145 testIntegral2String<std::string, char, short, int, long>();
146 testIntegral2String<fbstring, char, short, int, long>();
148 #if FOLLY_HAVE_INT128_T
149 test128Bit2String<std::string>();
150 test128Bit2String<fbstring>();
154 template <class String>
155 void testString2Integral() {
158 template <class String, class Int, class... Ints>
159 void testString2Integral() {
160 typedef typename make_unsigned<Int>::type Uint;
161 typedef typename make_signed<Int>::type Sint;
163 // Unsigned numbers small enough to fit in a signed type
164 static const String strings[] = {
171 "0000000000000000000000000042"
173 static const Uint values[] = {
182 FOR_EACH_RANGE (i, 0, sizeof(strings) / sizeof(*strings)) {
183 EXPECT_EQ(to<Uint>(strings[i]), values[i]);
184 EXPECT_EQ(to<Sint>(strings[i]), values[i]);
187 // Unsigned numbers that won't fit in the signed variation
188 static const String uStrings[] = {
193 static const Uint uValues[] = {
198 FOR_EACH_RANGE (i, 0, sizeof(uStrings)/sizeof(*uStrings)) {
199 EXPECT_EQ(to<Uint>(uStrings[i]), uValues[i]);
200 if (sizeof(Int) == 1) {
201 EXPECT_THROW(to<Sint>(uStrings[i]), std::range_error);
205 if (sizeof(Int) >= 4) {
206 static const String strings2[] = {
212 static const Uint values2[] = {
218 FOR_EACH_RANGE (i, 0, sizeof(strings2)/sizeof(*strings2)) {
219 EXPECT_EQ(to<Uint>(strings2[i]), values2[i]);
220 EXPECT_EQ(to<Sint>(strings2[i]), values2[i]);
223 static const String uStrings2[] = {
229 static const Uint uValues2[] = {
235 FOR_EACH_RANGE (i, 0, sizeof(uStrings2)/sizeof(uStrings2)) {
236 EXPECT_EQ(to<Uint>(uStrings2[i]), uValues2[i]);
237 if (sizeof(Int) == 4) {
238 EXPECT_THROW(to<Sint>(uStrings2[i]), std::range_error);
243 if (sizeof(Int) >= 8) {
244 static_assert(sizeof(Int) <= 8, "Now that would be interesting");
245 static const String strings3[] = {
249 "100000000000000000",
250 "9223372036854775807",
252 static const Uint values3[] = {
255 (Uint)25687346509278435ULL,
256 (Uint)100000000000000000ULL,
257 (Uint)9223372036854775807ULL,
259 FOR_EACH_RANGE (i, 0, sizeof(strings3)/sizeof(*strings3)) {
260 EXPECT_EQ(to<Uint>(strings3[i]), values3[i]);
261 EXPECT_EQ(to<Sint>(strings3[i]), values3[i]);
264 static const String uStrings3[] = {
265 "9223372036854775808",
266 "9987435987394857987",
267 "17873648761234698740",
268 "18446744073709551615",
270 static const Uint uValues3[] = {
271 (Uint)9223372036854775808ULL,
272 (Uint)9987435987394857987ULL,
273 (Uint)17873648761234698740ULL,
274 (Uint)18446744073709551615ULL,
276 FOR_EACH_RANGE (i, 0, sizeof(uStrings3)/sizeof(*uStrings3)) {
277 EXPECT_EQ(to<Uint>(uStrings3[i]), uValues3[i]);
278 if (sizeof(Int) == 8) {
279 EXPECT_THROW(to<Sint>(uStrings3[i]), std::range_error);
284 // Minimum possible negative values, and negative sign overflow
285 static const String strings4[] = {
289 "-9223372036854775808",
291 static const String strings5[] = {
295 "-9223372036854775809",
297 static const Sint values4[] = {
301 (Sint)(-9223372036854775807LL - 1),
303 FOR_EACH_RANGE (i, 0, sizeof(strings4)/sizeof(*strings4)) {
304 if (sizeof(Int) > std::pow(2, i)) {
305 EXPECT_EQ(values4[i], to<Sint>(strings4[i]));
306 EXPECT_EQ(values4[i] - 1, to<Sint>(strings5[i]));
307 } else if (sizeof(Int) == std::pow(2, i)) {
308 EXPECT_EQ(values4[i], to<Sint>(strings4[i]));
309 EXPECT_THROW(to<Sint>(strings5[i]), std::range_error);
311 EXPECT_THROW(to<Sint>(strings4[i]), std::range_error);
312 EXPECT_THROW(to<Sint>(strings5[i]), std::range_error);
316 // Bogus string values
317 static const String bogusStrings[] = {
324 "- 42", // whitespace is not allowed between the sign and the value
326 "12345678901234567890123456789",
328 for (const auto& str : bogusStrings) {
329 EXPECT_THROW(to<Sint>(str), std::range_error);
330 EXPECT_THROW(to<Uint>(str), std::range_error);
333 // A leading '+' character is only allowed when converting to signed types.
334 String posSign("+42");
335 EXPECT_EQ(42, to<Sint>(posSign));
336 EXPECT_THROW(to<Uint>(posSign), std::range_error);
338 testString2Integral<String, Ints...>();
341 TEST(Conv, String2Integral) {
342 testString2Integral<const char*, signed char, short, int, long, long long>();
343 testString2Integral<std::string, signed char, short, int, long, long long>();
344 testString2Integral<fbstring, signed char, short, int, long, long long>();
346 // Testing the behavior of the StringPiece* API
347 // StringPiece* normally parses as much valid data as it can,
348 // and advances the StringPiece to the end of the valid data.
349 char buf1[] = "100foo";
350 StringPiece sp1(buf1);
351 EXPECT_EQ(100, to<uint8_t>(&sp1));
352 EXPECT_EQ(buf1 + 3, sp1.begin());
353 // However, if the next character would cause an overflow it throws a
354 // range_error rather than consuming only as much as it can without
356 char buf2[] = "1002";
357 StringPiece sp2(buf2);
358 EXPECT_THROW(to<uint8_t>(&sp2), std::range_error);
359 EXPECT_EQ(buf2, sp2.begin());
362 TEST(Conv, StringPiece2Integral) {
363 string s = " +123 hello world ";
365 EXPECT_EQ(to<int>(&sp), 123);
366 EXPECT_EQ(sp, " hello world ");
369 TEST(Conv, StringPieceAppend) {
372 StringPiece sp(s, 0, 3);
373 string result = to<string>(s, sp);
374 EXPECT_EQ(result, "foobarfoo");
377 StringPiece sp1(s, 0, 3);
378 StringPiece sp2(s, 3, 3);
379 string result = to<string>(sp1, sp2);
380 EXPECT_EQ(result, s);
384 TEST(Conv, BadStringToIntegral) {
385 // Note that leading spaces (e.g. " 1") are valid.
386 vector<string> v = { "a", "", " ", "\n", " a0", "abcdef", "1Z", "!#" };
388 EXPECT_THROW(to<int>(s), std::range_error) << "s=" << s;
392 template <class String>
393 void testIdenticalTo() {
394 String s("Yukkuri shiteitte ne!!!");
396 String result = to<String>(s);
397 EXPECT_EQ(result, s);
400 template <class String>
401 void testVariadicTo() {
404 toAppend("Lorem ipsum ", 1234, String(" dolor amet "), 567.89, '!', &s);
405 EXPECT_EQ(s, "Lorem ipsum 1234 dolor amet 567.89!");
408 EXPECT_TRUE(s.empty());
410 s = to<String>("Lorem ipsum ", nullptr, 1234, " dolor amet ", 567.89, '.');
411 EXPECT_EQ(s, "Lorem ipsum 1234 dolor amet 567.89.");
414 template <class String>
415 void testIdenticalToDelim() {
416 String s("Yukkuri shiteitte ne!!!");
418 String charDelim = toDelim<String>('$', s);
419 EXPECT_EQ(charDelim, s);
421 String strDelim = toDelim<String>(String(">_<"), s);
422 EXPECT_EQ(strDelim, s);
425 template <class String>
426 void testVariadicToDelim() {
428 toAppendDelim(":", &s);
430 ":", "Lorem ipsum ", 1234, String(" dolor amet "), 567.89, '!', &s);
431 EXPECT_EQ(s, "Lorem ipsum :1234: dolor amet :567.89:!");
433 s = toDelim<String>(':');
434 EXPECT_TRUE(s.empty());
437 ":", "Lorem ipsum ", nullptr, 1234, " dolor amet ", 567.89, '.');
438 EXPECT_EQ(s, "Lorem ipsum ::1234: dolor amet :567.89:.");
441 TEST(Conv, NullString) {
442 string s1 = to<string>((char *) nullptr);
443 EXPECT_TRUE(s1.empty());
444 fbstring s2 = to<fbstring>((char *) nullptr);
445 EXPECT_TRUE(s2.empty());
448 TEST(Conv, VariadicTo) {
449 testIdenticalTo<string>();
450 testIdenticalTo<fbstring>();
451 testVariadicTo<string>();
452 testVariadicTo<fbstring>();
455 TEST(Conv, VariadicToDelim) {
456 testIdenticalToDelim<string>();
457 testIdenticalToDelim<fbstring>();
458 testVariadicToDelim<string>();
459 testVariadicToDelim<fbstring>();
462 template <class String>
463 void testDoubleToString() {
464 EXPECT_EQ(to<string>(0.0), "0");
465 EXPECT_EQ(to<string>(0.5), "0.5");
466 EXPECT_EQ(to<string>(10.25), "10.25");
467 EXPECT_EQ(to<string>(1.123e10), "11230000000");
470 TEST(Conv, DoubleToString) {
471 testDoubleToString<string>();
472 testDoubleToString<fbstring>();
475 TEST(Conv, FBStringToString) {
477 string ret = to<string>(foo);
478 EXPECT_EQ(ret, "foo");
479 string ret2 = to<string>(foo, 2);
480 EXPECT_EQ(ret2, "foo2");
483 TEST(Conv, StringPieceToDouble) {
484 string s = "2134123.125 zorro";
486 EXPECT_EQ(to<double>(&pc), 2134123.125);
487 EXPECT_EQ(pc, " zorro");
489 EXPECT_THROW(to<double>(StringPiece(s)), std::range_error);
490 EXPECT_EQ(to<double>(StringPiece(s.data(), pc.data())), 2134123.125);
492 // Test NaN conversion
494 to<double>("not a number");
496 } catch (const std::range_error &) {
499 EXPECT_TRUE(std::isnan(to<double>("NaN")));
500 EXPECT_EQ(to<double>("inf"), numeric_limits<double>::infinity());
501 EXPECT_EQ(to<double>("infinity"), numeric_limits<double>::infinity());
502 EXPECT_THROW(to<double>("infinitX"), std::range_error);
503 EXPECT_EQ(to<double>("-inf"), -numeric_limits<double>::infinity());
504 EXPECT_EQ(to<double>("-infinity"), -numeric_limits<double>::infinity());
505 EXPECT_THROW(to<double>("-infinitX"), std::range_error);
508 TEST(Conv, EmptyStringToInt) {
515 } catch (const std::range_error &) {
519 TEST(Conv, CorruptedStringToInt) {
521 StringPiece pc(s.data(), s.data() + 1); // Only "-"
526 } catch (const std::range_error &) {
530 TEST(Conv, EmptyStringToDouble) {
537 } catch (const std::range_error &) {
541 TEST(Conv, IntToDouble) {
542 auto d = to<double>(42);
544 /* This seems not work in ubuntu11.10, gcc 4.6.1
546 auto f = to<float>(957837589847);
548 } catch (std::range_error& e) {
549 //LOG(INFO) << e.what();
554 TEST(Conv, DoubleToInt) {
555 auto i = to<int>(42.0);
558 auto i = to<int>(42.1);
560 } catch (std::range_error& e) {
561 //LOG(INFO) << e.what();
565 TEST(Conv, EnumToInt) {
566 enum A { x = 42, y = 420, z = 65 };
569 auto j = to<char>(x);
572 auto i = to<char>(y);
573 LOG(ERROR) << static_cast<unsigned int>(i);
575 } catch (std::range_error& e) {
576 //LOG(INFO) << e.what();
580 TEST(Conv, EnumToString) {
582 enum A { x = 4, y = 420, z = 65 };
583 EXPECT_EQ("foo.4", to<string>("foo.", x));
584 EXPECT_EQ("foo.420", to<string>("foo.", y));
585 EXPECT_EQ("foo.65", to<string>("foo.", z));
588 TEST(Conv, IntToEnum) {
589 enum A { x = 42, y = 420 };
595 auto i = to<A>(5000000000L);
597 } catch (std::range_error& e) {
598 //LOG(INFO) << e.what();
602 TEST(Conv, UnsignedEnum) {
603 enum E : uint32_t { x = 3000000000U };
604 auto u = to<uint32_t>(x);
605 EXPECT_EQ(u, 3000000000U);
606 auto s = to<string>(x);
607 EXPECT_EQ("3000000000", s);
608 auto e = to<E>(3000000000U);
611 auto i = to<int32_t>(x);
612 LOG(ERROR) << to<uint32_t>(x);
614 } catch (std::range_error& e) {
618 #if defined(__clang__) || __GNUC_PREREQ(4, 7)
619 // to<enum class> and to(enum class) only supported in gcc 4.7 onwards
621 TEST(Conv, UnsignedEnumClass) {
622 enum class E : uint32_t { x = 3000000000U };
623 auto u = to<uint32_t>(E::x);
625 EXPECT_EQ(u, 3000000000U);
626 auto s = to<string>(E::x);
627 EXPECT_EQ("3000000000", s);
628 auto e = to<E>(3000000000U);
631 auto i = to<int32_t>(E::x);
632 LOG(ERROR) << to<uint32_t>(E::x);
634 } catch (std::range_error& e) {
638 // Multi-argument to<string> uses toAppend, a different code path than
640 TEST(Conv, EnumClassToString) {
641 enum class A { x = 4, y = 420, z = 65 };
642 EXPECT_EQ("foo.4", to<string>("foo.", A::x));
643 EXPECT_EQ("foo.420", to<string>("foo.", A::y));
644 EXPECT_EQ("foo.65", to<string>("foo.", A::z));
647 #endif // gcc 4.7 onwards
649 template<typename Src>
650 void testStr2Bool() {
651 EXPECT_FALSE(to<bool>(Src("0")));
652 EXPECT_FALSE(to<bool>(Src(" 000 ")));
654 EXPECT_FALSE(to<bool>(Src("n")));
655 EXPECT_FALSE(to<bool>(Src("no")));
656 EXPECT_FALSE(to<bool>(Src("false")));
657 EXPECT_FALSE(to<bool>(Src("False")));
658 EXPECT_FALSE(to<bool>(Src(" fAlSe" )));
659 EXPECT_FALSE(to<bool>(Src("F")));
660 EXPECT_FALSE(to<bool>(Src("off")));
662 EXPECT_TRUE(to<bool>(Src("1")));
663 EXPECT_TRUE(to<bool>(Src(" 001 ")));
664 EXPECT_TRUE(to<bool>(Src("y")));
665 EXPECT_TRUE(to<bool>(Src("yes")));
666 EXPECT_TRUE(to<bool>(Src("\nyEs\t")));
667 EXPECT_TRUE(to<bool>(Src("true")));
668 EXPECT_TRUE(to<bool>(Src("True")));
669 EXPECT_TRUE(to<bool>(Src("T")));
670 EXPECT_TRUE(to<bool>(Src("on")));
672 EXPECT_THROW(to<bool>(Src("")), std::range_error);
673 EXPECT_THROW(to<bool>(Src("2")), std::range_error);
674 EXPECT_THROW(to<bool>(Src("11")), std::range_error);
675 EXPECT_THROW(to<bool>(Src("19")), std::range_error);
676 EXPECT_THROW(to<bool>(Src("o")), std::range_error);
677 EXPECT_THROW(to<bool>(Src("fal")), std::range_error);
678 EXPECT_THROW(to<bool>(Src("tru")), std::range_error);
679 EXPECT_THROW(to<bool>(Src("ye")), std::range_error);
680 EXPECT_THROW(to<bool>(Src("yes foo")), std::range_error);
681 EXPECT_THROW(to<bool>(Src("bar no")), std::range_error);
682 EXPECT_THROW(to<bool>(Src("one")), std::range_error);
683 EXPECT_THROW(to<bool>(Src("true_")), std::range_error);
684 EXPECT_THROW(to<bool>(Src("bogus_token_that_is_too_long")),
688 TEST(Conv, StringToBool) {
689 // testStr2Bool<const char *>();
690 testStr2Bool<std::string>();
692 // Test with strings that are not NUL terminated.
693 const char buf[] = "01234";
694 EXPECT_FALSE(to<bool>(StringPiece(buf, buf + 1))); // "0"
695 EXPECT_TRUE(to<bool>(StringPiece(buf + 1, buf + 2))); // "1"
696 const char buf2[] = "one two three";
697 EXPECT_TRUE(to<bool>(StringPiece(buf2, buf2 + 2))); // "on"
698 const char buf3[] = "false";
699 EXPECT_THROW(to<bool>(StringPiece(buf3, buf3 + 3)), // "fal"
702 // Test the StringPiece* API
703 const char buf4[] = "001foo";
704 StringPiece sp4(buf4);
705 EXPECT_TRUE(to<bool>(&sp4));
706 EXPECT_EQ(buf4 + 3, sp4.begin());
707 const char buf5[] = "0012";
708 StringPiece sp5(buf5);
709 EXPECT_THROW(to<bool>(&sp5), std::range_error);
710 EXPECT_EQ(buf5, sp5.begin());
713 TEST(Conv, NewUint64ToString) {
716 #define THE_GREAT_EXPECTATIONS(n, len) \
718 EXPECT_EQ((len), uint64ToBufferUnsafe((n), buf)); \
720 auto s = string(#n); \
721 s = s.substr(0, s.size() - 2); \
725 THE_GREAT_EXPECTATIONS(0UL, 1);
726 THE_GREAT_EXPECTATIONS(1UL, 1);
727 THE_GREAT_EXPECTATIONS(12UL, 2);
728 THE_GREAT_EXPECTATIONS(123UL, 3);
729 THE_GREAT_EXPECTATIONS(1234UL, 4);
730 THE_GREAT_EXPECTATIONS(12345UL, 5);
731 THE_GREAT_EXPECTATIONS(123456UL, 6);
732 THE_GREAT_EXPECTATIONS(1234567UL, 7);
733 THE_GREAT_EXPECTATIONS(12345678UL, 8);
734 THE_GREAT_EXPECTATIONS(123456789UL, 9);
735 THE_GREAT_EXPECTATIONS(1234567890UL, 10);
736 THE_GREAT_EXPECTATIONS(12345678901UL, 11);
737 THE_GREAT_EXPECTATIONS(123456789012UL, 12);
738 THE_GREAT_EXPECTATIONS(1234567890123UL, 13);
739 THE_GREAT_EXPECTATIONS(12345678901234UL, 14);
740 THE_GREAT_EXPECTATIONS(123456789012345UL, 15);
741 THE_GREAT_EXPECTATIONS(1234567890123456UL, 16);
742 THE_GREAT_EXPECTATIONS(12345678901234567UL, 17);
743 THE_GREAT_EXPECTATIONS(123456789012345678UL, 18);
744 THE_GREAT_EXPECTATIONS(1234567890123456789UL, 19);
745 THE_GREAT_EXPECTATIONS(18446744073709551614UL, 20);
746 THE_GREAT_EXPECTATIONS(18446744073709551615UL, 20);
748 #undef THE_GREAT_EXPECTATIONS
751 ////////////////////////////////////////////////////////////////////////////////
752 // Benchmarks for ASCII to int conversion
753 ////////////////////////////////////////////////////////////////////////////////
754 // @author: Rajat Goel (rajat)
756 static int64_t handwrittenAtoi(const char* start, const char* end) {
758 bool positive = true;
762 throw std::runtime_error("empty string");
765 while (start < end && isspace(*start)) {
777 while (start < end && *start >= '0' && *start <= '9') {
778 auto const newRetVal = retVal * 10 + (*start++ - '0');
779 if (newRetVal < retVal) {
780 throw std::runtime_error("overflow");
786 throw std::runtime_error("extra chars at the end");
789 return positive ? retVal : -retVal;
792 static StringPiece pc1 = "1234567890123456789";
794 void handwrittenAtoiMeasure(unsigned int n, unsigned int digits) {
795 auto p = pc1.subpiece(pc1.size() - digits, digits);
796 FOR_EACH_RANGE (i, 0, n) {
797 doNotOptimizeAway(handwrittenAtoi(p.begin(), p.end()));
801 void follyAtoiMeasure(unsigned int n, unsigned int digits) {
802 auto p = pc1.subpiece(pc1.size() - digits, digits);
803 FOR_EACH_RANGE (i, 0, n) {
804 doNotOptimizeAway(folly::to<int64_t>(p.begin(), p.end()));
808 void clibAtoiMeasure(unsigned int n, unsigned int digits) {
809 auto p = pc1.subpiece(pc1.size() - digits, digits);
810 assert(*p.end() == 0);
811 static_assert(sizeof(long) == 8, "64-bit long assumed");
812 FOR_EACH_RANGE (i, 0, n) {
813 doNotOptimizeAway(atol(p.begin()));
817 void clibStrtoulMeasure(unsigned int n, unsigned int digits) {
818 auto p = pc1.subpiece(pc1.size() - digits, digits);
819 assert(*p.end() == 0);
821 FOR_EACH_RANGE (i, 0, n) {
822 doNotOptimizeAway(strtoul(p.begin(), &endptr, 10));
826 void lexicalCastMeasure(unsigned int n, unsigned int digits) {
827 auto p = pc1.subpiece(pc1.size() - digits, digits);
828 assert(*p.end() == 0);
829 FOR_EACH_RANGE (i, 0, n) {
830 doNotOptimizeAway(boost::lexical_cast<uint64_t>(p.begin()));
834 // Benchmarks for unsigned to string conversion, raw
836 unsigned u64ToAsciiTable(uint64_t value, char* dst) {
837 static const char digits[201] =
838 "00010203040506070809"
839 "10111213141516171819"
840 "20212223242526272829"
841 "30313233343536373839"
842 "40414243444546474849"
843 "50515253545556575859"
844 "60616263646566676869"
845 "70717273747576777879"
846 "80818283848586878889"
847 "90919293949596979899";
849 uint32_t const length = digits10(value);
850 uint32_t next = length - 1;
851 while (value >= 100) {
852 auto const i = (value % 100) * 2;
854 dst[next] = digits[i + 1];
855 dst[next - 1] = digits[i];
858 // Handle last 1-2 digits
860 dst[next] = '0' + uint32_t(value);
862 auto i = uint32_t(value) * 2;
863 dst[next] = digits[i + 1];
864 dst[next - 1] = digits[i];
869 void u64ToAsciiTableBM(unsigned int n, uint64_t value) {
870 // This is too fast, need to do 10 times per iteration
872 FOR_EACH_RANGE (i, 0, n) {
873 doNotOptimizeAway(u64ToAsciiTable(value + n, buf));
877 unsigned u64ToAsciiClassic(uint64_t value, char* dst) {
879 char* next = (char*)dst;
882 *next++ = '0' + (value % 10);
884 } while (value != 0);
885 unsigned length = next - start;
889 while (next > start) {
899 void u64ToAsciiClassicBM(unsigned int n, uint64_t value) {
900 // This is too fast, need to do 10 times per iteration
902 FOR_EACH_RANGE (i, 0, n) {
903 doNotOptimizeAway(u64ToAsciiClassic(value + n, buf));
907 void u64ToAsciiFollyBM(unsigned int n, uint64_t value) {
908 // This is too fast, need to do 10 times per iteration
910 FOR_EACH_RANGE (i, 0, n) {
911 doNotOptimizeAway(uint64ToBufferUnsafe(value + n, buf));
915 // Benchmark uitoa with string append
917 void u2aAppendClassicBM(unsigned int n, uint64_t value) {
919 FOR_EACH_RANGE (i, 0, n) {
920 // auto buf = &s.back() + 1;
922 s.append(buffer, u64ToAsciiClassic(value, buffer));
923 doNotOptimizeAway(s.size());
927 void u2aAppendFollyBM(unsigned int n, uint64_t value) {
929 FOR_EACH_RANGE (i, 0, n) {
930 // auto buf = &s.back() + 1;
932 s.append(buffer, uint64ToBufferUnsafe(value, buffer));
933 doNotOptimizeAway(s.size());
937 template <class String>
938 struct StringIdenticalToBM {
939 StringIdenticalToBM() {}
940 void operator()(unsigned int n, size_t len) const {
942 BENCHMARK_SUSPEND { s.append(len, '0'); }
943 FOR_EACH_RANGE (i, 0, n) {
944 String result = to<String>(s);
945 doNotOptimizeAway(result.size());
950 template <class String>
951 struct StringVariadicToBM {
952 StringVariadicToBM() {}
953 void operator()(unsigned int n, size_t len) const {
955 BENCHMARK_SUSPEND { s.append(len, '0'); }
956 FOR_EACH_RANGE (i, 0, n) {
957 String result = to<String>(s, nullptr);
958 doNotOptimizeAway(result.size());
963 static size_t bigInt = 11424545345345;
964 static size_t smallInt = 104;
965 static char someString[] = "this is some nice string";
966 static char otherString[] = "this is a long string, so it's not so nice";
967 static char reallyShort[] = "meh";
968 static std::string stdString = "std::strings are very nice";
969 static float fValue = 1.2355;
970 static double dValue = 345345345.435;
972 BENCHMARK(preallocateTestNoFloat, n) {
973 for (int i=0; i < n; ++i) {
974 auto val1 = to<std::string>(bigInt, someString, stdString, otherString);
975 auto val3 = to<std::string>(reallyShort, smallInt);
976 auto val2 = to<std::string>(bigInt, stdString);
977 auto val4 = to<std::string>(bigInt, stdString, dValue, otherString);
978 auto val5 = to<std::string>(bigInt, someString, reallyShort);
982 BENCHMARK(preallocateTestFloat, n) {
983 for (int i=0; i < n; ++i) {
984 auto val1 = to<std::string>(stdString, ',', fValue, dValue);
985 auto val2 = to<std::string>(stdString, ',', dValue);
988 BENCHMARK_DRAW_LINE();
990 static const StringIdenticalToBM<std::string> stringIdenticalToBM;
991 static const StringVariadicToBM<std::string> stringVariadicToBM;
992 static const StringIdenticalToBM<fbstring> fbstringIdenticalToBM;
993 static const StringVariadicToBM<fbstring> fbstringVariadicToBM;
995 #define DEFINE_BENCHMARK_GROUP(n) \
996 BENCHMARK_PARAM(u64ToAsciiClassicBM, n); \
997 BENCHMARK_RELATIVE_PARAM(u64ToAsciiTableBM, n); \
998 BENCHMARK_RELATIVE_PARAM(u64ToAsciiFollyBM, n); \
999 BENCHMARK_DRAW_LINE();
1001 DEFINE_BENCHMARK_GROUP(1);
1002 DEFINE_BENCHMARK_GROUP(12);
1003 DEFINE_BENCHMARK_GROUP(123);
1004 DEFINE_BENCHMARK_GROUP(1234);
1005 DEFINE_BENCHMARK_GROUP(12345);
1006 DEFINE_BENCHMARK_GROUP(123456);
1007 DEFINE_BENCHMARK_GROUP(1234567);
1008 DEFINE_BENCHMARK_GROUP(12345678);
1009 DEFINE_BENCHMARK_GROUP(123456789);
1010 DEFINE_BENCHMARK_GROUP(1234567890);
1011 DEFINE_BENCHMARK_GROUP(12345678901);
1012 DEFINE_BENCHMARK_GROUP(123456789012);
1013 DEFINE_BENCHMARK_GROUP(1234567890123);
1014 DEFINE_BENCHMARK_GROUP(12345678901234);
1015 DEFINE_BENCHMARK_GROUP(123456789012345);
1016 DEFINE_BENCHMARK_GROUP(1234567890123456);
1017 DEFINE_BENCHMARK_GROUP(12345678901234567);
1018 DEFINE_BENCHMARK_GROUP(123456789012345678);
1019 DEFINE_BENCHMARK_GROUP(1234567890123456789);
1020 DEFINE_BENCHMARK_GROUP(12345678901234567890U);
1022 #undef DEFINE_BENCHMARK_GROUP
1024 #define DEFINE_BENCHMARK_GROUP(n) \
1025 BENCHMARK_PARAM(clibAtoiMeasure, n); \
1026 BENCHMARK_RELATIVE_PARAM(lexicalCastMeasure, n); \
1027 BENCHMARK_RELATIVE_PARAM(handwrittenAtoiMeasure, n); \
1028 BENCHMARK_RELATIVE_PARAM(follyAtoiMeasure, n); \
1029 BENCHMARK_DRAW_LINE();
1031 DEFINE_BENCHMARK_GROUP(1);
1032 DEFINE_BENCHMARK_GROUP(2);
1033 DEFINE_BENCHMARK_GROUP(3);
1034 DEFINE_BENCHMARK_GROUP(4);
1035 DEFINE_BENCHMARK_GROUP(5);
1036 DEFINE_BENCHMARK_GROUP(6);
1037 DEFINE_BENCHMARK_GROUP(7);
1038 DEFINE_BENCHMARK_GROUP(8);
1039 DEFINE_BENCHMARK_GROUP(9);
1040 DEFINE_BENCHMARK_GROUP(10);
1041 DEFINE_BENCHMARK_GROUP(11);
1042 DEFINE_BENCHMARK_GROUP(12);
1043 DEFINE_BENCHMARK_GROUP(13);
1044 DEFINE_BENCHMARK_GROUP(14);
1045 DEFINE_BENCHMARK_GROUP(15);
1046 DEFINE_BENCHMARK_GROUP(16);
1047 DEFINE_BENCHMARK_GROUP(17);
1048 DEFINE_BENCHMARK_GROUP(18);
1049 DEFINE_BENCHMARK_GROUP(19);
1051 #undef DEFINE_BENCHMARK_GROUP
1053 #define DEFINE_BENCHMARK_GROUP(T, n) \
1054 BENCHMARK_PARAM(T ## VariadicToBM, n); \
1055 BENCHMARK_RELATIVE_PARAM(T ## IdenticalToBM, n); \
1056 BENCHMARK_DRAW_LINE();
1058 DEFINE_BENCHMARK_GROUP(string, 32);
1059 DEFINE_BENCHMARK_GROUP(string, 1024);
1060 DEFINE_BENCHMARK_GROUP(string, 32768);
1061 DEFINE_BENCHMARK_GROUP(fbstring, 32);
1062 DEFINE_BENCHMARK_GROUP(fbstring, 1024);
1063 DEFINE_BENCHMARK_GROUP(fbstring, 32768);
1065 #undef DEFINE_BENCHMARK_GROUP
1067 int main(int argc, char** argv) {
1068 testing::InitGoogleTest(&argc, argv);
1069 gflags::ParseCommandLineFlags(&argc, &argv, true);
1070 auto ret = RUN_ALL_TESTS();
1071 if (!ret && FLAGS_benchmark) {
1072 folly::runBenchmarks();