Commit df1ffef3 authored by Tom Jackson's avatar Tom Jackson Committed by Jordan DeLong

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
parent 76d60885
......@@ -2053,7 +2053,7 @@ template <typename E, class T, class A, class S>
inline
bool operator==(const basic_fbstring<E, T, A, S>& lhs,
const basic_fbstring<E, T, A, S>& rhs) {
return lhs.compare(rhs) == 0; }
return lhs.size() == rhs.size() && lhs.compare(rhs) == 0; }
template <typename E, class T, class A, class S>
inline
......
/*
* 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 <class String>
void randomBinaryString(String* toFill, unsigned int maxSize = 1000) {
assert(toFill);
toFill->resize(random(0, maxSize));
FOR_EACH (i, *toFill) {
*i = random('0', '1');
}
}
template <class String, class Integral>
void Num2String(String& str, Integral n) {
str.resize(30, '\0');
......
/*
* 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<STRING> 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 {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment