Skip to content

Commit 3908980

Browse files
sadroeckfacebook-github-bot
authored andcommitted
Add NativeMap support
Summary: This diff introduces `NativeMap`, a tagged representation of various `folly::F14FastMap` based specializations of `Value`. In the `Protocol::Value` implementation, a map was always represented as `folly::F14FastMap<Value, Value>`, significantly increasing the memory use & reducing cache efficiency for anything other than the most complex types. The set of specializations was chosen according to the most-commonly used set specializations as found in the meta codebase(s): * `MapOf<primitive, primitive>`, for each primitive type, e.g. bool, i32, .. * `MapOf<primitive, Object>`, for each primitive type, e.g. bool, i32, .. * `MapOf<primitive, Value`, for each primitive type, e.g. bool, i32, .. Lastly the fallback resolves to the default `Protocol::Value::map`, i.e. `MapOf<Value, Value>`, allowing any generic map structure at the highest relative cost. Note: These specializations aren't set in stone & can be added/removed. Reviewed By: thedavekwon Differential Revision: D72358846 fbshipit-source-id: b018a3bed516906660d4467f13e1152be3c45b9e
1 parent 6ff8254 commit 3908980

File tree

4 files changed

+564
-25
lines changed

4 files changed

+564
-25
lines changed

third-party/thrift/src/thrift/lib/cpp2/protocol/NativeObject-inl.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,51 @@ NativeSet::Specialized<T>* NativeSet::if_type() noexcept {
116116
return std::get_if<Specialized<T>>(&kind_);
117117
}
118118

119+
// ---- NativeMap ---- //
120+
121+
template <typename... Args>
122+
NativeMap::NativeMap(MapOf<Args...>&& m) noexcept : kind_(std::move(m)) {}
123+
124+
template <typename T>
125+
bool NativeMap::is_type() const noexcept {
126+
static_assert(
127+
detail::is_map_v<T> && !std::is_same_v<T, NativeMap>,
128+
"NativeMap is always a map type");
129+
return std::holds_alternative<Specialized<T>>(kind_);
130+
}
131+
132+
template <typename T>
133+
const NativeMap::Specialized<T>& NativeMap::as_type() const {
134+
static_assert(
135+
detail::is_map_v<T> && !std::is_same_v<T, NativeMap>,
136+
"NativeMap is always a map type");
137+
return std::get<Specialized<T>>(kind_);
138+
}
139+
140+
template <typename T>
141+
NativeMap::Specialized<T>& NativeMap::as_type() {
142+
static_assert(
143+
detail::is_map_v<T> && !std::is_same_v<T, NativeMap>,
144+
"NativeMap is always a map type");
145+
return std::get<Specialized<T>>(kind_);
146+
}
147+
148+
template <typename T>
149+
const NativeMap::Specialized<T>* NativeMap::if_type() const noexcept {
150+
static_assert(
151+
detail::is_map_v<T> && !std::is_same_v<T, NativeMap>,
152+
"NativeMap is always a map type");
153+
return std::get_if<Specialized<T>>(&kind_);
154+
}
155+
156+
template <typename T>
157+
NativeMap::Specialized<T>* NativeMap::if_type() noexcept {
158+
static_assert(
159+
detail::is_map_v<T> && !std::is_same_v<T, NativeMap>,
160+
"NativeMap is always a map type");
161+
return std::get_if<Specialized<T>>(&kind_);
162+
}
163+
119164
// ---- Object ---- //
120165

121166
template <typename... Args>

0 commit comments

Comments
 (0)