Commit fc10d0b8 authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook Github Bot

Apply clang-format to folly/**/*Range*.*

Summary: [Folly] Apply `clang-format` to `folly/**/*Range*.*`.

Reviewed By: meyering

Differential Revision: D5570553

fbshipit-source-id: 230aa8557c2d402d741aaa541f96f4634dd13d3b
parent e4c892c7
This diff is collapsed.
......@@ -24,8 +24,9 @@ namespace folly {
namespace detail {
size_t qfind_first_byte_of_bitset(const StringPieceLite haystack,
const StringPieceLite needles) {
size_t qfind_first_byte_of_bitset(
const StringPieceLite haystack,
const StringPieceLite needles) {
std::bitset<256> s;
for (auto needle : needles) {
s[(uint8_t)needle] = true;
......@@ -38,10 +39,11 @@ size_t qfind_first_byte_of_bitset(const StringPieceLite haystack,
return std::string::npos;
}
size_t qfind_first_byte_of_byteset(const StringPieceLite haystack,
const StringPieceLite needles) {
size_t qfind_first_byte_of_byteset(
const StringPieceLite haystack,
const StringPieceLite needles) {
SparseByteSet s;
for (auto needle: needles) {
for (auto needle : needles) {
s.add(uint8_t(needle));
}
for (size_t index = 0; index < haystack.size(); ++index) {
......@@ -51,7 +53,5 @@ size_t qfind_first_byte_of_byteset(const StringPieceLite haystack,
}
return std::string::npos;
}
}
}
......@@ -38,38 +38,60 @@ class StringPieceLite {
public:
StringPieceLite(const char* b, const char* e) : b_(b), e_(e) {}
template <typename Range>
/* implicit */ StringPieceLite(const Range& r) :
StringPieceLite(r.data(), r.data() + r.size()) {}
const char* data() const { return b_; }
const char* begin() const { return b_; }
const char* end() const { return e_; }
size_t size() const { return size_t(e_ - b_); }
bool empty() const { return size() == 0; }
const char& operator[](size_t i) const { DCHECK_GT(size(), i); return b_[i]; }
/* implicit */ StringPieceLite(const Range& r)
: StringPieceLite(r.data(), r.data() + r.size()) {}
const char* data() const {
return b_;
}
const char* begin() const {
return b_;
}
const char* end() const {
return e_;
}
size_t size() const {
return size_t(e_ - b_);
}
bool empty() const {
return size() == 0;
}
const char& operator[](size_t i) const {
DCHECK_GT(size(), i);
return b_[i];
}
template <typename Range>
explicit operator Range() const { return Range(begin(), end()); }
explicit operator Range() const {
return Range(begin(), end());
}
private:
const char* b_;
const char* e_;
};
inline size_t qfind_first_byte_of_std(const StringPieceLite haystack,
const StringPieceLite needles) {
auto ret = std::find_first_of(haystack.begin(), haystack.end(),
needles.begin(), needles.end(),
[](char a, char b) { return a == b; });
inline size_t qfind_first_byte_of_std(
const StringPieceLite haystack,
const StringPieceLite needles) {
auto ret = std::find_first_of(
haystack.begin(),
haystack.end(),
needles.begin(),
needles.end(),
[](char a, char b) { return a == b; });
return ret == haystack.end() ? std::string::npos : ret - haystack.begin();
}
size_t qfind_first_byte_of_bitset(
const StringPieceLite haystack,
const StringPieceLite needles);
size_t qfind_first_byte_of_bitset(const StringPieceLite haystack,
const StringPieceLite needles);
size_t qfind_first_byte_of_byteset(const StringPieceLite haystack,
const StringPieceLite needles);
size_t qfind_first_byte_of_byteset(
const StringPieceLite haystack,
const StringPieceLite needles);
inline size_t qfind_first_byte_of_nosse(const StringPieceLite haystack,
const StringPieceLite needles) {
inline size_t qfind_first_byte_of_nosse(
const StringPieceLite haystack,
const StringPieceLite needles) {
if (UNLIKELY(needles.empty() || haystack.empty())) {
return std::string::npos;
}
......@@ -77,13 +99,10 @@ inline size_t qfind_first_byte_of_nosse(const StringPieceLite haystack,
// This is not an exact science since it depends on the CPU, the size of
// needles, and the size of haystack.
if ((needles.size() >= 4 && haystack.size() <= 10) ||
(needles.size() >= 16 && haystack.size() <= 64) ||
needles.size() >= 32) {
(needles.size() >= 16 && haystack.size() <= 64) || needles.size() >= 32) {
return qfind_first_byte_of_byteset(haystack, needles);
}
return qfind_first_byte_of_std(haystack, needles);
}
}
}
......@@ -28,13 +28,14 @@
#if !FOLLY_SSE_PREREQ(4, 2)
namespace folly {
namespace detail {
size_t qfind_first_byte_of_sse42(const StringPieceLite haystack,
const StringPieceLite needles) {
size_t qfind_first_byte_of_sse42(
const StringPieceLite haystack,
const StringPieceLite needles) {
return qfind_first_byte_of_nosse(haystack, needles);
}
}
}
# else
#else
#include <cstdint>
#include <limits>
#include <string>
......@@ -49,15 +50,14 @@ size_t qfind_first_byte_of_sse42(const StringPieceLite haystack,
// a function with always_inline fails to build. The _mm_* functions are marked
// always_inline.
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67368
#if defined FOLLY_SANITIZE_ADDRESS && \
FOLLY_SANITIZE_ADDRESS == 1 && \
#if defined FOLLY_SANITIZE_ADDRESS && FOLLY_SANITIZE_ADDRESS == 1 && \
__GNUC_PREREQ(4, 9)
# define _mm_load_si128(p) (*(p))
# define _mm_loadu_si128(p) ((__m128i)__builtin_ia32_loaddqu((const char*)(p)))
# ifdef _mm_cmpestri
# undef _mm_cmpestri
# endif
# define _mm_cmpestri(a, b, c, d, e) \
#define _mm_load_si128(p) (*(p))
#define _mm_loadu_si128(p) ((__m128i)__builtin_ia32_loaddqu((const char*)(p)))
#ifdef _mm_cmpestri
#undef _mm_cmpestri
#endif
#define _mm_cmpestri(a, b, c, d, e) \
__builtin_ia32_pcmpestri128((__v16qi)(a), b, (__v16qi)(c), d, e)
#endif
......@@ -67,8 +67,9 @@ namespace detail {
// It's okay if pages are bigger than this (as powers of two), but they should
// not be smaller.
static constexpr size_t kMinPageSize = 4096;
static_assert(kMinPageSize >= 16,
"kMinPageSize must be at least SSE register size");
static_assert(
kMinPageSize >= 16,
"kMinPageSize must be at least SSE register size");
template <typename T>
static inline uintptr_t page_for(T* addr) {
......@@ -76,19 +77,20 @@ static inline uintptr_t page_for(T* addr) {
}
static inline size_t nextAlignedIndex(const char* arr) {
auto firstPossible = reinterpret_cast<uintptr_t>(arr) + 1;
return 1 + // add 1 because the index starts at 'arr'
((firstPossible + 15) & ~0xF) // round up to next multiple of 16
- firstPossible;
auto firstPossible = reinterpret_cast<uintptr_t>(arr) + 1;
return 1 + // add 1 because the index starts at 'arr'
((firstPossible + 15) & ~0xF) // round up to next multiple of 16
- firstPossible;
}
static size_t qfind_first_byte_of_needles16(const StringPieceLite haystack,
const StringPieceLite needles)
FOLLY_DISABLE_ADDRESS_SANITIZER;
static size_t qfind_first_byte_of_needles16(
const StringPieceLite haystack,
const StringPieceLite needles) FOLLY_DISABLE_ADDRESS_SANITIZER;
// helper method for case where needles.size() <= 16
size_t qfind_first_byte_of_needles16(const StringPieceLite haystack,
const StringPieceLite needles) {
size_t qfind_first_byte_of_needles16(
const StringPieceLite haystack,
const StringPieceLite needles) {
DCHECK_GT(haystack.size(), 0u);
DCHECK_GT(needles.size(), 0u);
DCHECK_LE(needles.size(), 16u);
......@@ -101,11 +103,10 @@ size_t qfind_first_byte_of_needles16(const StringPieceLite haystack,
return detail::qfind_first_byte_of_nosse(haystack, needles);
}
auto arr2 = _mm_loadu_si128(
reinterpret_cast<const __m128i*>(needles.data()));
auto arr2 = _mm_loadu_si128(reinterpret_cast<const __m128i*>(needles.data()));
// do an unaligned load for first block of haystack
auto arr1 = _mm_loadu_si128(
reinterpret_cast<const __m128i*>(haystack.data()));
auto arr1 =
_mm_loadu_si128(reinterpret_cast<const __m128i*>(haystack.data()));
auto index =
_mm_cmpestri(arr2, int(needles.size()), arr1, int(haystack.size()), 0);
if (index < 16) {
......@@ -114,7 +115,7 @@ size_t qfind_first_byte_of_needles16(const StringPieceLite haystack,
// Now, we can do aligned loads hereafter...
size_t i = nextAlignedIndex(haystack.data());
for (; i < haystack.size(); i+= 16) {
for (; i < haystack.size(); i += 16) {
arr1 =
_mm_load_si128(reinterpret_cast<const __m128i*>(haystack.data() + i));
index = _mm_cmpestri(
......@@ -127,27 +128,30 @@ size_t qfind_first_byte_of_needles16(const StringPieceLite haystack,
}
template <bool HAYSTACK_ALIGNED>
size_t scanHaystackBlock(const StringPieceLite haystack,
const StringPieceLite needles,
uint64_t idx)
// Turn off ASAN because the "arr2 = ..." assignment in the loop below reads
// up to 15 bytes beyond end of the buffer in #needles#. That is ok because
// ptr2 is always 16-byte aligned, so the read can never span a page boundary.
// Also, the extra data that may be read is never actually used.
FOLLY_DISABLE_ADDRESS_SANITIZER;
size_t scanHaystackBlock(
const StringPieceLite haystack,
const StringPieceLite needles,
uint64_t idx)
// Turn off ASAN because the "arr2 = ..." assignment in the loop below reads
// up to 15 bytes beyond end of the buffer in #needles#. That is ok because
// ptr2 is always 16-byte aligned, so the read can never span a page
// boundary. Also, the extra data that may be read is never actually used.
FOLLY_DISABLE_ADDRESS_SANITIZER;
// Scans a 16-byte block of haystack (starting at blockStartIdx) to find first
// needle. If HAYSTACK_ALIGNED, then haystack must be 16byte aligned.
// If !HAYSTACK_ALIGNED, then caller must ensure that it is safe to load the
// block.
template <bool HAYSTACK_ALIGNED>
size_t scanHaystackBlock(const StringPieceLite haystack,
const StringPieceLite needles,
uint64_t blockStartIdx) {
size_t scanHaystackBlock(
const StringPieceLite haystack,
const StringPieceLite needles,
uint64_t blockStartIdx) {
DCHECK_GT(needles.size(), 16u); // should handled by *needles16() method
DCHECK(blockStartIdx + 16 <= haystack.size() ||
(page_for(haystack.data() + blockStartIdx) ==
page_for(haystack.data() + blockStartIdx + 15)));
DCHECK(
blockStartIdx + 16 <= haystack.size() ||
(page_for(haystack.data() + blockStartIdx) ==
page_for(haystack.data() + blockStartIdx + 15)));
__m128i arr1;
if (HAYSTACK_ALIGNED) {
......@@ -159,15 +163,13 @@ size_t scanHaystackBlock(const StringPieceLite haystack,
}
// This load is safe because needles.size() >= 16
auto arr2 = _mm_loadu_si128(
reinterpret_cast<const __m128i*>(needles.data()));
auto arr2 = _mm_loadu_si128(reinterpret_cast<const __m128i*>(needles.data()));
auto b =
_mm_cmpestri(arr2, 16, arr1, int(haystack.size() - blockStartIdx), 0);
size_t j = nextAlignedIndex(needles.data());
for (; j < needles.size(); j += 16) {
arr2 = _mm_load_si128(
reinterpret_cast<const __m128i*>(needles.data() + j));
arr2 = _mm_load_si128(reinterpret_cast<const __m128i*>(needles.data() + j));
auto index = _mm_cmpestri(
arr2,
......@@ -184,11 +186,13 @@ size_t scanHaystackBlock(const StringPieceLite haystack,
return std::string::npos;
}
size_t qfind_first_byte_of_sse42(const StringPieceLite haystack,
const StringPieceLite needles);
size_t qfind_first_byte_of_sse42(
const StringPieceLite haystack,
const StringPieceLite needles);
size_t qfind_first_byte_of_sse42(const StringPieceLite haystack,
const StringPieceLite needles) {
size_t qfind_first_byte_of_sse42(
const StringPieceLite haystack,
const StringPieceLite needles) {
if (UNLIKELY(needles.empty() || haystack.empty())) {
return std::string::npos;
} else if (needles.size() <= 16) {
......
......@@ -24,9 +24,8 @@ namespace folly {
namespace detail {
size_t qfind_first_byte_of_sse42(const StringPieceLite haystack,
const StringPieceLite needles);
size_t qfind_first_byte_of_sse42(
const StringPieceLite haystack,
const StringPieceLite needles);
}
}
......@@ -80,9 +80,8 @@ void initFile(int len) {
}
}
string generateString(int len) {
std::uniform_int_distribution<uint32_t> validChar(1, 255); // no null-char
std::uniform_int_distribution<uint32_t> validChar(1, 255); // no null-char
string ret;
while (len--) {
ret.push_back(validChar(rnd));
......@@ -93,7 +92,7 @@ string generateString(int len) {
void initDelims(int len) {
ffoDelim.clear();
string s(len - 1, '\0'); // find_first_of won't finish until last char
string s(len - 1, '\0'); // find_first_of won't finish until last char
s.push_back('a');
ffoTestString = s;
......@@ -107,7 +106,7 @@ void initDelims(int len) {
auto s = generateString(n);
if (rnd() % 2) {
// ~half of tests will find a hit
s[rnd() % s.size()] = 'a'; // yes, this could mean 'a' is a duplicate
s[rnd() % s.size()] = 'a'; // yes, this could mean 'a' is a duplicate
}
ffoDelim.push_back(s);
}
......@@ -305,8 +304,9 @@ BENCHMARK_RELATIVE(FindFirstOf32NeedlesBitSet, n) {
BENCHMARK_DRAW_LINE();
const string delims64 = "!bcdefghijklmnopqrstuvwxyz_"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ-0123456789$";
const string delims64 =
"!bcdefghijklmnopqrstuvwxyz_"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ-0123456789$";
BENCHMARK(FindFirstOf64NeedlesBase, n) {
findFirstOfRange(delims64, detail::qfind_first_byte_of, n);
......@@ -400,7 +400,7 @@ BENCHMARK_DRAW_LINE();
int main(int argc, char** argv) {
gflags::ParseCommandLineFlags(&argc, &argv, true);
for (int len : {1, 8, 10, 16, 32, 64, 128, 256, 10*1024, 1024*1024}) {
for (int len : {1, 8, 10, 16, 32, 64, 128, 256, 10 * 1024, 1024 * 1024}) {
initStr(len);
initDelims(len);
initFile(len);
......
This diff is collapsed.
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