[ORC] Add specialized SPSSerializationTraits for ArrayRef<char>.

Deserializing from an SPSSequence<char> to an an ArrayRef<char> will point the
ArrayRef<char> at the input buffer.
This commit is contained in:
Lang Hames 2021-09-02 20:45:46 +10:00
parent 5ab7bfa4fd
commit f38cfdabd1
2 changed files with 45 additions and 0 deletions

View File

@ -296,6 +296,31 @@ public:
static constexpr bool available = true;
};
/// Specialized SPSSequence<char> -> ArrayRef<char> serialization.
///
/// On deserialize, points directly into the input buffer.
template <> class SPSSerializationTraits<SPSSequence<char>, ArrayRef<char>> {
public:
static size_t size(const ArrayRef<char> &A) {
return SPSArgList<uint64_t>::size(static_cast<uint64_t>(A.size())) +
A.size();
}
static bool serialize(SPSOutputBuffer &OB, const ArrayRef<char> &A) {
if (!SPSArgList<uint64_t>::serialize(OB, static_cast<uint64_t>(A.size())))
return false;
return OB.write(A.data(), A.size());
}
static bool deserialize(SPSInputBuffer &IB, ArrayRef<char> &A) {
uint64_t Size;
if (!SPSArgList<uint64_t>::deserialize(IB, Size))
return false;
A = {IB.data(), Size};
return IB.skip(Size);
}
};
/// 'Trivial' sequence serialization: Sequence is serialized as a uint64_t size
/// followed by a for-earch loop over the elements of the sequence to serialize
/// each of them.

View File

@ -168,3 +168,23 @@ TEST(SimplePackedSerialization, StringMap) {
StringMap<int32_t> M({{"A", 1}, {"B", 2}});
spsSerializationRoundTrip<SPSSequence<SPSTuple<SPSString, int32_t>>>(M);
}
TEST(SimplePackedSerializationTest, ArrayRef) {
constexpr unsigned BufferSize = 6 + 8; // "hello\0" + sizeof(uint64_t)
ArrayRef<char> HelloOut = "hello";
char Buffer[BufferSize];
memset(Buffer, 0, BufferSize);
SPSOutputBuffer OB(Buffer, BufferSize);
EXPECT_TRUE(SPSArgList<SPSSequence<char>>::serialize(OB, HelloOut));
ArrayRef<char> HelloIn;
SPSInputBuffer IB(Buffer, BufferSize);
EXPECT_TRUE(SPSArgList<SPSSequence<char>>::deserialize(IB, HelloIn));
// Output should be copied to buffer.
EXPECT_NE(HelloOut.data(), Buffer);
// Input should reference buffer.
EXPECT_LT(HelloIn.data() - Buffer, BufferSize);
}