GeezerVison is showing github.com — Deserialize hash-map values in place via try_emplace in thrift Encode… · meta/fbthrift@e99f1d2
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: thrift/lib/cpp2/op/detail/EncodeHelpers.h +40-1Lines changed: 40 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -163,6 +163,7 @@ inline constexpr bool sorted_unique_constructible_v = | 163 | 163 | sorted_unique_constructible_<void, T>; | 164 | 164 |
| 165 | 165 | FOLLY_CREATE_MEMBER_INVOKER(emplace_hint_invoker, emplace_hint); | | 166 | +FOLLY_CREATE_MEMBER_INVOKER(try_emplace_invoker, try_emplace); | 166 | 167 |
| 167 | 168 | template <typename T> | 168 | 169 | using detect_key_compare = typename T::key_compare; | @@ -175,6 +176,18 @@ constexpr bool map_emplace_hint_is_invocable_v = std::is_invocable_v< | 175 | 176 | typename T::key_type, | 176 | 177 | typename T::mapped_type>; | 177 | 178 |
| | 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 | + | 178 | 191 | template <typename T> | 179 | 192 | constexpr bool set_emplace_hint_is_invocable_v = std::is_invocable_v< | 180 | 193 | emplace_hint_invoker, | @@ -217,7 +230,33 @@ void deserialize_known_length_map( | 217 | 230 | template <typename Map, typename KeyDeserializer, typename MappedDeserializer> | 218 | 231 | requires( | 219 | 232 | !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>) | 221 | 260 | void deserialize_known_length_map( | 222 | 261 | Map& map, | 223 | 262 | std::uint32_t mapSize, | |
Collapse file Copy file name to clipboardExpand all lines: thrift/lib/cpp2/protocol/detail/protocol_methods.h +4Lines changed: 4 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -89,6 +89,10 @@ using op::detail::deserialize_known_length_set; | 89 | 89 | using op::detail::detect_key_compare; | 90 | 90 | using op::detail::ListDecodeImpl; | 91 | 91 | 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; | 92 | 96 | using op::detail::set_emplace_hint_is_invocable_v; | 93 | 97 | using op::detail::sorted_unique_constructible_v; | 94 | 98 | using op::detail::writeMapValueBegin; | |
Collapse file 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 number | Diff line number | Diff line change |
|---|
@@ -83,6 +83,32 @@ class MinimalMap : private folly::F14FastMap<K, M, H, E, A> { | 83 | 83 | } | 84 | 84 | }; | 85 | 85 |
| | 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 | + | 86 | 112 | } // namespace | 87 | 113 |
| 88 | 114 | namespace folly::test { | @@ -151,13 +177,13 @@ TEST_F(DeserializeKnownLengthMapTest, EmplaceHintConstuctCount) { | 151 | 177 | EXPECT_EQ( | 152 | 178 | TrackedValue::counts(), | 153 | 179 | (folly::test::Counts{/* copyConstruct */ 0, | 154 | | - /* moveConstruct */ 1, | | 180 | + /* moveConstruct */ 0, | 155 | 181 | /* copyConvert */ 0, | 156 | 182 | /* moveConvert */ 0, | 157 | 183 | /* copyAssign */ 0, | 158 | 184 | /* moveAssign */ 0, | 159 | 185 | /* defaultConstruct */ 1, | 160 | | - /* destroyed */ 1})); | | 186 | + /* destroyed */ 0})); | 161 | 187 | } | 162 | 188 |
| 163 | 189 | TEST_F(DeserializeKnownLengthMapTest, EmplaceHintConstuctCountVec) { | @@ -195,6 +221,49 @@ TEST_F(DeserializeKnownLengthMapTest, EmplaceHintConstuctCountVec) { | 195 | 221 | /* destroyed */ 0})); | 196 | 222 | } | 197 | 223 |
| | 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 | + | 198 | 267 | TEST_F(DeserializeKnownLengthMapTest, EmplaceConstuctCount) { | 199 | 268 | using Allocator = std::allocator<std::pair<const TrackedKey, TrackedValue>>; | 200 | 269 | using Map = MinimalMap< | |
|