GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Deserialize hash-map values in place via try_emplace in thrift Encode… · meta/fbthrift@e99f1d2

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • thrift/lib/cpp2
    • op/detail
      • EncodeHelpers.h
    • protocol/detail
      • protocol_methods.h
      • test
        • DeserializeKnownLengthMapTest.cpp
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎thrift/lib/cpp2/op/detail/EncodeHelpers.h‎

Copy file name to clipboardExpand all lines: thrift/lib/cpp2/op/detail/EncodeHelpers.h
+40-1Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ inline constexpr bool sorted_unique_constructible_v =
163163
sorted_unique_constructible_<void, T>;
164164

165165
FOLLY_CREATE_MEMBER_INVOKER(emplace_hint_invoker, emplace_hint);
166+
FOLLY_CREATE_MEMBER_INVOKER(try_emplace_invoker, try_emplace);
166167

167168
template <typename T>
168169
using detect_key_compare = typename T::key_compare;
@@ -175,6 +176,18 @@ constexpr bool map_emplace_hint_is_invocable_v = std::is_invocable_v<
175176
typename T::key_type,
176177
typename T::mapped_type>;
177178

179+
template <typename T>
180+
constexpr bool map_try_emplace_is_invocable_v =
181+
std::is_invocable_v<try_emplace_invoker, T, typename T::key_type>;
182+
183+
// Unordered maps that do not propagate their allocator can deserialize the
184+
// mapped value in place via try_emplace rather than through a temporary.
185+
template <typename T>
186+
constexpr bool map_supports_in_place_deserialize_v =
187+
!folly::is_detected_v<detect_key_compare, T> &&
188+
!apache::thrift::detail::alloc_should_propagate_map<T> &&
189+
map_try_emplace_is_invocable_v<T>;
190+
178191
template <typename T>
179192
constexpr bool set_emplace_hint_is_invocable_v = std::is_invocable_v<
180193
emplace_hint_invoker,
@@ -217,7 +230,33 @@ void deserialize_known_length_map(
217230
template <typename Map, typename KeyDeserializer, typename MappedDeserializer>
218231
requires(
219232
!sorted_unique_constructible_v<Map> &&
220-
map_emplace_hint_is_invocable_v<Map>)
233+
map_emplace_hint_is_invocable_v<Map> &&
234+
map_supports_in_place_deserialize_v<Map>)
235+
void deserialize_known_length_map(
236+
Map& map,
237+
std::uint32_t mapSize,
238+
const KeyDeserializer& kr,
239+
const MappedDeserializer& mr) {
240+
folly::reserve_if_available(map, mapSize);
241+
for (auto i = mapSize; i--;) {
242+
typename Map::key_type key = apache::thrift::detail::default_map_key(map);
243+
kr(key);
244+
auto [it, inserted] = map.try_emplace(std::move(key));
245+
if (inserted) {
246+
mr(it->second);
247+
} else {
248+
typename Map::mapped_type value =
249+
apache::thrift::detail::default_map_value(map);
250+
mr(value);
251+
}
252+
}
253+
}
254+
255+
template <typename Map, typename KeyDeserializer, typename MappedDeserializer>
256+
requires(
257+
!sorted_unique_constructible_v<Map> &&
258+
map_emplace_hint_is_invocable_v<Map> &&
259+
!map_supports_in_place_deserialize_v<Map>)
221260
void deserialize_known_length_map(
222261
Map& map,
223262
std::uint32_t mapSize,
Collapse file

‎thrift/lib/cpp2/protocol/detail/protocol_methods.h‎

Copy file name to clipboardExpand all lines: thrift/lib/cpp2/protocol/detail/protocol_methods.h
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ using op::detail::deserialize_known_length_set;
8989
using op::detail::detect_key_compare;
9090
using op::detail::ListDecodeImpl;
9191
using op::detail::map_emplace_hint_is_invocable_v;
92+
// NOLINTNEXTLINE(facebook-hte-DetailCall)
93+
using op::detail::map_supports_in_place_deserialize_v;
94+
// NOLINTNEXTLINE(facebook-hte-DetailCall)
95+
using op::detail::map_try_emplace_is_invocable_v;
9296
using op::detail::set_emplace_hint_is_invocable_v;
9397
using op::detail::sorted_unique_constructible_v;
9498
using op::detail::writeMapValueBegin;
Collapse file

‎thrift/lib/cpp2/protocol/detail/test/DeserializeKnownLengthMapTest.cpp‎

Copy file name to clipboardExpand all lines: thrift/lib/cpp2/protocol/detail/test/DeserializeKnownLengthMapTest.cpp
+71-2Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,32 @@ class MinimalMap : private folly::F14FastMap<K, M, H, E, A> {
8383
}
8484
};
8585

86+
template <typename K, typename M, typename H, typename E, typename A>
87+
class HintOnlyMap : private folly::F14FastMap<K, M, H, E, A> {
88+
using Super = folly::F14FastMap<K, M, H, E, A>;
89+
90+
public:
91+
using typename Super::allocator_type;
92+
using typename Super::iterator;
93+
using typename Super::key_type;
94+
using typename Super::mapped_type;
95+
using typename Super::size_type;
96+
using typename Super::value_type;
97+
98+
HintOnlyMap() = default;
99+
100+
using Super::operator[];
101+
using Super::end;
102+
using Super::get_allocator;
103+
using Super::size;
104+
105+
// `HintOnlyMap` has `emplace_hint` method, but not `try_emplace`.
106+
template <class... Args>
107+
iterator emplace_hint(iterator hint, Args&&... args) {
108+
return Super::emplace_hint(hint, std::forward<Args>(args)...);
109+
}
110+
};
111+
86112
} // namespace
87113

88114
namespace folly::test {
@@ -151,13 +177,13 @@ TEST_F(DeserializeKnownLengthMapTest, EmplaceHintConstuctCount) {
151177
EXPECT_EQ(
152178
TrackedValue::counts(),
153179
(folly::test::Counts{/* copyConstruct */ 0,
154-
/* moveConstruct */ 1,
180+
/* moveConstruct */ 0,
155181
/* copyConvert */ 0,
156182
/* moveConvert */ 0,
157183
/* copyAssign */ 0,
158184
/* moveAssign */ 0,
159185
/* defaultConstruct */ 1,
160-
/* destroyed */ 1}));
186+
/* destroyed */ 0}));
161187
}
162188

163189
TEST_F(DeserializeKnownLengthMapTest, EmplaceHintConstuctCountVec) {
@@ -195,6 +221,49 @@ TEST_F(DeserializeKnownLengthMapTest, EmplaceHintConstuctCountVec) {
195221
/* destroyed */ 0}));
196222
}
197223

224+
TEST_F(
225+
DeserializeKnownLengthMapTest, EmplaceHintWithoutTryEmplaceConstuctCount) {
226+
using Allocator = std::allocator<std::pair<const TrackedKey, TrackedValue>>;
227+
using Map = HintOnlyMap<
228+
TrackedKey,
229+
TrackedValue,
230+
TransparentTrackedKeyHash,
231+
TransparentTrackedKeyEqual,
232+
Allocator>;
233+
234+
static_assert(!detail::pm::sorted_unique_constructible_v<Map>);
235+
static_assert(detail::pm::map_emplace_hint_is_invocable_v<Map>);
236+
static_assert(!detail::pm::map_try_emplace_is_invocable_v<Map>);
237+
static_assert(!detail::pm::map_supports_in_place_deserialize_v<Map>);
238+
static_assert(!detail::alloc_should_propagate_map<Map>);
239+
240+
Map map;
241+
detail::pm::deserialize_known_length_map(
242+
map, 1, deserializeTrackedKey, deserializeTrackedValue);
243+
244+
EXPECT_EQ(map.size(), 1);
245+
EXPECT_EQ(
246+
TrackedKey::counts(),
247+
(folly::test::Counts{/* copyConstruct */ 0,
248+
/* moveConstruct */ 1,
249+
/* copyConvert */ 0,
250+
/* moveConvert */ 0,
251+
/* copyAssign */ 0,
252+
/* moveAssign */ 0,
253+
/* defaultConstruct */ 1,
254+
/* destroyed */ 1}));
255+
EXPECT_EQ(
256+
TrackedValue::counts(),
257+
(folly::test::Counts{/* copyConstruct */ 0,
258+
/* moveConstruct */ 1,
259+
/* copyConvert */ 0,
260+
/* moveConvert */ 0,
261+
/* copyAssign */ 0,
262+
/* moveAssign */ 0,
263+
/* defaultConstruct */ 1,
264+
/* destroyed */ 1}));
265+
}
266+
198267
TEST_F(DeserializeKnownLengthMapTest, EmplaceConstuctCount) {
199268
using Allocator = std::allocator<std::pair<const TrackedKey, TrackedValue>>;
200269
using Map = MinimalMap<

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.