Skip to content

Commit d0e15f9

Browse files
authored
[orc-rt] Refactor WrapperFunction to simplify Serializer classes. (#161763)
Serializers only need to provide two methods now, rather than four. The first method should return an argument serializer / deserializer, the second a result value serializer / deserializer. The interfaces for these are now more uniform (deserialize now returns a tuple, rather than taking its output location(s) by reference). The intent is to simplify Serializer helper code. NFCI.
1 parent 40fce32 commit d0e15f9

File tree

2 files changed

+19
-29
lines changed

2 files changed

+19
-29
lines changed

orc-rt/include/orc-rt/SPSWrapperFunction.h

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,27 @@
2020
namespace orc_rt {
2121
namespace detail {
2222

23-
template <typename... SPSArgTs> struct WFSPSSerializer {
23+
template <typename... SPSArgTs> struct WFSPSHelper {
2424
template <typename... ArgTs>
25-
std::optional<WrapperFunctionBuffer> operator()(const ArgTs &...Args) {
25+
std::optional<WrapperFunctionBuffer> serialize(const ArgTs &...Args) {
2626
auto R =
2727
WrapperFunctionBuffer::allocate(SPSArgList<SPSArgTs...>::size(Args...));
2828
SPSOutputBuffer OB(R.data(), R.size());
2929
if (!SPSArgList<SPSArgTs...>::serialize(OB, Args...))
3030
return std::nullopt;
3131
return std::move(R);
3232
}
33-
};
3433

35-
template <typename... SPSArgTs> struct WFSPSDeserializer {
36-
template <typename... ArgTs>
37-
bool operator()(WrapperFunctionBuffer &ArgBytes, ArgTs &...Args) {
34+
template <typename ArgTuple>
35+
std::optional<ArgTuple> deserialize(WrapperFunctionBuffer ArgBytes) {
3836
assert(!ArgBytes.getOutOfBandError() &&
3937
"Should not attempt to deserialize out-of-band error");
4038
SPSInputBuffer IB(ArgBytes.data(), ArgBytes.size());
41-
return SPSArgList<SPSArgTs...>::deserialize(IB, Args...);
39+
ArgTuple Args;
40+
if (!SPSSerializationTraits<SPSTuple<SPSArgTs...>, ArgTuple>::deserialize(
41+
IB, Args))
42+
return std::nullopt;
43+
return Args;
4244
}
4345
};
4446

@@ -48,19 +50,8 @@ template <typename SPSSig> struct WrapperFunctionSPSSerializer;
4850

4951
template <typename SPSRetT, typename... SPSArgTs>
5052
struct WrapperFunctionSPSSerializer<SPSRetT(SPSArgTs...)> {
51-
static detail::WFSPSSerializer<SPSArgTs...> argumentSerializer() noexcept {
52-
return {};
53-
}
54-
static detail::WFSPSDeserializer<SPSArgTs...>
55-
argumentDeserializer() noexcept {
56-
return {};
57-
}
58-
static detail::WFSPSSerializer<SPSRetT> resultSerializer() noexcept {
59-
return {};
60-
}
61-
static detail::WFSPSDeserializer<SPSRetT> resultDeserializer() noexcept {
62-
return {};
63-
}
53+
static detail::WFSPSHelper<SPSArgTs...> arguments() noexcept { return {}; }
54+
static detail::WFSPSHelper<SPSRetT> result() noexcept { return {}; }
6455
};
6556

6657
/// Provides call and handle utilities to simplify writing and invocation of

orc-rt/include/orc-rt/WrapperFunction.h

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ class StructuredYield<std::tuple<RetT>, Serializer>
139139
public:
140140
using StructuredYieldBase<Serializer>::StructuredYieldBase;
141141
void operator()(RetT &&R) {
142-
if (auto ResultBytes = this->S.resultSerializer()(std::forward<RetT>(R)))
142+
if (auto ResultBytes = this->S.result().serialize(std::forward<RetT>(R)))
143143
this->Return(this->Session, this->CallCtx, ResultBytes->release());
144144
else
145145
this->Return(this->Session, this->CallCtx,
@@ -166,9 +166,9 @@ template <typename T, typename Serializer>
166166
struct ResultDeserializer<std::tuple<Expected<T>>, Serializer> {
167167
static Expected<T> deserialize(WrapperFunctionBuffer ResultBytes,
168168
Serializer &S) {
169-
T Val;
170-
if (S.resultDeserializer()(ResultBytes, Val))
171-
return std::move(Val);
169+
if (auto Val = S.result().template deserialize<std::tuple<T>>(
170+
std::move(ResultBytes)))
171+
return std::move(std::get<0>(*Val));
172172
else
173173
return make_error<StringError>("Could not deserialize result");
174174
}
@@ -205,7 +205,7 @@ struct WrapperFunction {
205205
"Result-handler should have exactly one argument");
206206
typedef typename ResultHandlerTraits::args_tuple_type ResultTupleType;
207207

208-
if (auto ArgBytes = S.argumentSerializer()(std::forward<ArgTs>(Args)...)) {
208+
if (auto ArgBytes = S.arguments().serialize(std::forward<ArgTs>(Args)...)) {
209209
C(
210210
[RH = std::move(RH),
211211
S = std::move(S)](orc_rt_SessionRef Session,
@@ -241,13 +241,12 @@ struct WrapperFunction {
241241
if (ArgBytes.getOutOfBandError())
242242
return Return(Session, CallCtx, ArgBytes.release());
243243

244-
ArgTuple Args;
245-
if (std::apply(bind_front(S.argumentDeserializer(), std::move(ArgBytes)),
246-
Args))
244+
if (auto Args =
245+
S.arguments().template deserialize<ArgTuple>(std::move(ArgBytes)))
247246
std::apply(bind_front(std::forward<Handler>(H),
248247
detail::StructuredYield<RetTupleType, Serializer>(
249248
Session, CallCtx, Return, std::move(S))),
250-
std::move(Args));
249+
std::move(*Args));
251250
else
252251
Return(Session, CallCtx,
253252
WrapperFunctionBuffer::createOutOfBandError(

0 commit comments

Comments
 (0)