Skip to content

Commit 80e61b3

Browse files
swolchokfacebook-github-bot
authored andcommitted
Frozen: fix performance of FixedSizeString view comparison
Summary: I observed clang generating a byte-by-byte comparison for FixedSizeString, despite the length being known to be 8 and therefore comparable with a simple integer comparison. Reviewed By: iahs Differential Revision: D53557089 fbshipit-source-id: 9b6a5bfd06a3130567f1ed22e7532a2178f44e07
1 parent 3756d50 commit 80e61b3

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

thrift/lib/cpp2/frozen/FrozenFixedSizeString-inl.h

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,20 @@ struct FixedSizeStringLayout : public LayoutBase {
6565
os << folly::demangle(type.name());
6666
}
6767

68-
using View = folly::ByteRange;
69-
View view(ViewPosition self) const {
70-
return folly::ByteRange{self.start, T::kFixedSize};
71-
}
68+
struct View : public folly::ByteRange {
69+
public:
70+
using folly::ByteRange::ByteRange;
71+
72+
View(folly::ByteRange bytes) : folly::ByteRange(bytes) {}
73+
74+
bool operator==(View rhs) {
75+
return memcmp(this->data(), rhs.data(), T::kFixedSize) == 0;
76+
}
77+
78+
using folly::ByteRange::toString;
79+
};
80+
81+
View view(ViewPosition self) const { return View(self.start, T::kFixedSize); }
7282

7383
static size_t hash(const T& value) {
7484
return FixedSizeStringHash<T::kFixedSize, T>::hash(value);

thrift/lib/cpp2/frozen/test/FrozenBench.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,11 +190,19 @@ struct OwnedKey<folly::ByteRange> {
190190
using type = FixedSizeString<8>;
191191
};
192192

193+
template <>
194+
struct OwnedKey<
195+
typename detail::FixedSizeStringLayout<FixedSizeString<8>>::View> {
196+
using type = FixedSizeString<8>;
197+
};
198+
193199
template <
194200
typename M,
195201
typename T,
196202
std::enable_if_t<
197-
!std::is_same<typename M::key_type, folly::ByteRange>::value,
203+
!std::is_same<
204+
typename M::key_type,
205+
detail::FixedSizeStringLayout<FixedSizeString<8>>::View>::value,
198206
bool> = true>
199207
typename M::const_iterator mapFind(const M& map, const T& key) {
200208
return map.find(key);
@@ -206,7 +214,9 @@ template <
206214
typename M,
207215
typename T,
208216
std::enable_if_t<
209-
std::is_same<typename M::key_type, folly::ByteRange>::value,
217+
std::is_same<
218+
typename M::key_type,
219+
detail::FixedSizeStringLayout<FixedSizeString<8>>::View>::value,
210220
bool> = true>
211221
typename M::const_iterator mapFind(const M& map, const T& key) {
212222
static_assert(std::is_same<T, FixedSizeString<8>>::value);

0 commit comments

Comments
 (0)