From df1ffef33a788d14089e7fad2ee6cb84d192504b Mon Sep 17 00:00:00 2001 From: Tom Jackson Date: Wed, 13 Mar 2013 15:44:59 -0700 Subject: [PATCH] Short-circuit operator== based on size() Summary: We don't do this today, but it looks like std::string does. For longer, similar strings, this is a big win. Before: ```lang=text ============================================================================ ./folly/test/FBStringTestBenchmarks.cpp.h relative time/iter iters/s ============================================================================ BM_equality_string(65536) 5.13ms 194.87 BM_equality_fbstring(65536) 11.34ms 88.18 ============================================================================ ``` After: ```lang=text ============================================================================ ./folly/test/FBStringTestBenchmarks.cpp.h relative time/iter iters/s ============================================================================ BM_equality_string(65536) 5.01ms 199.74 BM_equality_fbstring(65536) 6.63ms 150.78 ============================================================================ ``` Test Plan: Benchmark, unit tests Reviewed By: tudorb@fb.com FB internal diff: D737482 --- folly/FBString.h | 2 +- folly/test/FBStringBenchmark.cpp | 11 ++++++++++- folly/test/FBStringTestBenchmarks.cpp.h | 19 ++++++++++++++++++- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/folly/FBString.h b/folly/FBString.h index 7c465870..e0c56250 100644 --- a/folly/FBString.h +++ b/folly/FBString.h @@ -2053,7 +2053,7 @@ template inline bool operator==(const basic_fbstring& lhs, const basic_fbstring& rhs) { - return lhs.compare(rhs) == 0; } + return lhs.size() == rhs.size() && lhs.compare(rhs) == 0; } template inline diff --git a/folly/test/FBStringBenchmark.cpp b/folly/test/FBStringBenchmark.cpp index 65fb6525..e9cd297b 100644 --- a/folly/test/FBStringBenchmark.cpp +++ b/folly/test/FBStringBenchmark.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2012 Facebook, Inc. + * Copyright 2013 Facebook, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -56,6 +56,15 @@ void randomString(String* toFill, unsigned int maxSize = 1000) { } } +template +void randomBinaryString(String* toFill, unsigned int maxSize = 1000) { + assert(toFill); + toFill->resize(random(0, maxSize)); + FOR_EACH (i, *toFill) { + *i = random('0', '1'); + } +} + template void Num2String(String& str, Integral n) { str.resize(30, '\0'); diff --git a/folly/test/FBStringTestBenchmarks.cpp.h b/folly/test/FBStringTestBenchmarks.cpp.h index 1e29ed29..9726fb7e 100644 --- a/folly/test/FBStringTestBenchmarks.cpp.h +++ b/folly/test/FBStringTestBenchmarks.cpp.h @@ -1,5 +1,5 @@ /* - * Copyright 2012 Facebook, Inc. + * Copyright 2013 Facebook, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -190,6 +190,23 @@ expect to get a call for an interview."; } BENCHMARK_PARAM(BENCHFUN(findUnsuccessful), 524288); +void BENCHFUN(equality)(int iters, int arg) { + std::vector haystack(arg); + + BENCHMARK_SUSPEND { + for (auto& hay : haystack) { + randomBinaryString(&hay, 1024); + } + } + + FOR_EACH_RANGE (i, 0, iters) { + STRING needle; + randomBinaryString(&needle, 1024); + doNotOptimizeAway(std::find(haystack.begin(), haystack.end(), needle)); + } +} +BENCHMARK_PARAM(BENCHFUN(equality), 65536); + void BENCHFUN(replace)(int iters, int arg) { STRING s; BENCHMARK_SUSPEND { -- 2.34.1