Skip to content

Commit

Permalink
rename
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos committed May 22, 2024
1 parent cee2a3d commit e773a82
Show file tree
Hide file tree
Showing 9 changed files with 164 additions and 658 deletions.
89 changes: 2 additions & 87 deletions include/ylt/struct_pb.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023, Alibaba Group Holding Limited;
* Copyright (c) 2024, Alibaba Group Holding Limited;
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -14,89 +14,4 @@
* limitations under the License.
*/
#pragma once
#include <cassert>
#include <cstdint>
#include <cstring>
#include <vector>
#if defined __clang__
#define STRUCT_PB_INLINE __attribute__((always_inline)) inline
#elif defined _MSC_VER
#define STRUCT_PB_INLINE __forceinline
#else
#define STRUCT_PB_INLINE __attribute__((always_inline)) inline
#endif
#define STRUCT_PB_NODISCARD [[nodiscard]]
namespace struct_pb {

struct UnknownFields {
STRUCT_PB_NODISCARD std::size_t total_size() const {
std::size_t total = 0;
for (const auto& f : fields) {
total += f.len;
}
return total;
}
void serialize_to(char* data, std::size_t& pos, std::size_t size) const {
for (const auto& f : fields) {
assert(pos + f.len <= size);
std::memcpy(data + pos, f.data, f.len);
pos += f.len;
}
}
void add_field(const char* data, std::size_t start, std::size_t end) {
fields.push_back(Field{data + start, end - start});
}
struct Field {
const char* data;
std::size_t len;
};
std::vector<Field> fields;
};
/*
* Low-Level API for struct_pb user
* the following internal API will be generated by struct_pb protoc plugin
*/
namespace internal {
template <typename T>
STRUCT_PB_NODISCARD std::size_t get_needed_size(
const T& t, const UnknownFields& unknown_fields = {});
template <typename T>
void serialize_to(char* data, std::size_t size, const T& t,
const UnknownFields& unknown_fields = {});
template <typename T>
STRUCT_PB_NODISCARD bool deserialize_to(T& t, const char* data,
std::size_t size,
UnknownFields& unknown_fields);
template <typename T>
STRUCT_PB_NODISCARD bool deserialize_to(T& t, const char* data,
std::size_t size);
} // namespace internal

/*
* High-Level API for struct_pb user
* If you need more fine-grained operations, encapsulate the internal API
* yourself.
*/
template <typename Buffer = std::vector<char>, typename T>
STRUCT_PB_NODISCARD Buffer serialize(const T& t,
const UnknownFields& unknown_fields = {}) {
Buffer buffer;
auto size = struct_pb::internal::get_needed_size(t, unknown_fields);
buffer.resize(size);
struct_pb::internal::serialize_to(buffer.data(), buffer.size(), t,
unknown_fields);
return buffer;
}
template <typename T, typename Buffer>
STRUCT_PB_NODISCARD STRUCT_PB_INLINE bool deserialize_to(
T& t, UnknownFields& unknown_fields, const Buffer& buffer) {
return struct_pb::internal::deserialize_to(t, buffer.data(), buffer.size(),
unknown_fields);
}
template <typename T, typename Buffer>
STRUCT_PB_NODISCARD STRUCT_PB_INLINE bool deserialize_to(T& t,
const Buffer& buffer) {
return struct_pb::internal::deserialize_to(t, buffer.data(), buffer.size());
}

} // namespace struct_pb
#include "struct_pb/struct_pb_impl.hpp"
173 changes: 4 additions & 169 deletions include/ylt/struct_pb/struct_pb_impl.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023, Alibaba Group Holding Limited;
* Copyright (c) 2024, Alibaba Group Holding Limited;
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -14,171 +14,6 @@
* limitations under the License.
*/
#pragma once
#include <cassert>
#include <cstring>
#include <map>
#include <string>
#include <vector>

#include "ylt/struct_pb.hpp"

namespace struct_pb {

namespace internal {
STRUCT_PB_NODISCARD STRUCT_PB_INLINE uint32_t encode_zigzag(int32_t v) {
return (static_cast<uint32_t>(v) << 1U) ^
static_cast<uint32_t>(
-static_cast<int32_t>(static_cast<uint32_t>(v) >> 31U));
}
STRUCT_PB_NODISCARD STRUCT_PB_INLINE uint64_t encode_zigzag(int64_t v) {
return (static_cast<uint64_t>(v) << 1U) ^
static_cast<uint64_t>(
-static_cast<int64_t>(static_cast<uint64_t>(v) >> 63U));
}

STRUCT_PB_NODISCARD STRUCT_PB_INLINE int64_t decode_zigzag(uint64_t u) {
return static_cast<int64_t>((u >> 1U)) ^
static_cast<uint64_t>(-static_cast<int64_t>(u & 1U));
}
STRUCT_PB_NODISCARD STRUCT_PB_INLINE int64_t decode_zigzag(uint32_t u) {
return static_cast<int64_t>((u >> 1U)) ^
static_cast<uint64_t>(-static_cast<int64_t>(u & 1U));
}

STRUCT_PB_NODISCARD STRUCT_PB_INLINE std::size_t calculate_varint_size(
uint64_t v) {
std::size_t ret = 0;
do {
ret++;
v >>= 7;
} while (v != 0);
return ret;
}

[[nodiscard]] STRUCT_PB_INLINE bool decode_varint(const char* data,
std::size_t& pos_,
std::size_t size_,
uint64_t& v) {
// fix test failed on arm due to different char definition
const signed char* data_ = reinterpret_cast<const signed char*>(data);
// from https://github.com/facebook/folly/blob/main/folly/Varint.h
if (pos_ < size_ && (static_cast<uint64_t>(data_[pos_]) & 0x80U) == 0) {
v = static_cast<uint64_t>(data_[pos_]);
pos_++;
return true;
}
constexpr const int8_t max_varint_length = sizeof(uint64_t) * 8 / 7 + 1;
uint64_t val = 0;
if (size_ - pos_ >= max_varint_length) [[likely]] {
do {
// clang-format off
int64_t b = data_[pos_++];
val = ((uint64_t(b) & 0x7fU) ); if (b >= 0) { break; }
b = data_[pos_++]; val |= ((uint64_t(b) & 0x7fU) << 7U); if (b >= 0) { break; }
b = data_[pos_++]; val |= ((uint64_t(b) & 0x7fU) << 14U); if (b >= 0) { break; }
b = data_[pos_++]; val |= ((uint64_t(b) & 0x7fU) << 21U); if (b >= 0) { break; }
b = data_[pos_++]; val |= ((uint64_t(b) & 0x7fU) << 28U); if (b >= 0) { break; }
b = data_[pos_++]; val |= ((uint64_t(b) & 0x7fU) << 35U); if (b >= 0) { break; }
b = data_[pos_++]; val |= ((uint64_t(b) & 0x7fU) << 42U); if (b >= 0) { break; }
b = data_[pos_++]; val |= ((uint64_t(b) & 0x7fU) << 49U); if (b >= 0) { break; }
b = data_[pos_++]; val |= ((uint64_t(b) & 0x7fU) << 56U); if (b >= 0) { break; }
b = data_[pos_++]; val |= ((uint64_t(b) & 0x01U) << 63U); if (b >= 0) { break; }
// clang-format on
return false;
} while (false);
}
else {
unsigned int shift = 0;
while (pos_ != size_ && int64_t(data_[pos_]) < 0) {
val |= (uint64_t(data_[pos_++]) & 0x7fU) << shift;
shift += 7;
}
if (pos_ == size_) {
return false;
}
val |= uint64_t(data_[pos_++]) << shift;
}
v = val;
return true;
}
STRUCT_PB_INLINE void serialize_varint(char* const data, std::size_t& pos,
std::size_t size, uint64_t v) {
while (v >= 0x80) {
assert(pos < size);
data[pos++] = static_cast<uint8_t>(v | 0x80);
v >>= 7;
}
data[pos++] = static_cast<uint8_t>(v);
}
STRUCT_PB_NODISCARD STRUCT_PB_INLINE bool deserialize_varint(const char* data,
std::size_t& pos,
std::size_t size,
uint64_t& v) {
return decode_varint(data, pos, size, v);
}
STRUCT_PB_NODISCARD STRUCT_PB_INLINE bool read_tag(const char* data,
std::size_t& pos,
std::size_t size,
uint64_t& tag) {
return deserialize_varint(data, pos, size, tag);
}
inline bool deserialize_unknown(const char* data, std::size_t& pos,
std::size_t size, uint32_t tag,
UnknownFields& unknown_fields) {
uint32_t field_number = tag >> 3;
if (field_number == 0) {
return false;
}
auto offset = internal::calculate_varint_size(tag);
auto start = pos - offset;
uint32_t wire_type = tag & 0b0000'0111;
switch (wire_type) {
case 0: {
uint64_t t;
auto ok = internal::deserialize_varint(data, pos, size, t);
if (!ok) [[unlikely]] {
return false;
}
unknown_fields.add_field(data, start, pos);
break;
}
case 1: {
static_assert(sizeof(double) == 8);
if (pos + 8 > size) [[unlikely]] {
return false;
}
pos += 8;
unknown_fields.add_field(data, start, pos);
break;
}
case 2: {
uint64_t sz;
auto ok = internal::deserialize_varint(data, pos, size, sz);
if (!ok) [[unlikely]] {
return false;
}
if (pos + sz > size) [[unlikely]] {
return false;
}
pos += sz;
unknown_fields.add_field(data, start, pos);
break;
}
case 5: {
static_assert(sizeof(float) == 4);
if (pos + 4 > size) [[unlikely]] {
return false;
}
pos += 4;
unknown_fields.add_field(data, start, pos);
break;
}
default: {
assert(false && "error path");
}
}
return true;
}
} // namespace internal

} // namespace struct_pb
#include <iguana/pb_reader.hpp>
#include <iguana/pb_writer.hpp>
namespace struct_pb = iguana;
18 changes: 0 additions & 18 deletions include/ylt/struct_pb/struct_pb_reader.hpp

This file was deleted.

18 changes: 0 additions & 18 deletions include/ylt/struct_pb/struct_pb_writer.hpp

This file was deleted.

18 changes: 0 additions & 18 deletions include/ylt/struct_pb0.hpp

This file was deleted.

12 changes: 2 additions & 10 deletions src/struct_pack/benchmark/benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,8 @@

#ifdef HAVE_PROTOBUF
#include "protobuf_sample.hpp"
#ifdef HAVE_STRUCT_PB
#include "struct_pb_sample.hpp"
#endif
#endif
#include "struct_pb_sample0.hpp"
#include "struct_pb_sample.hpp"
#ifdef HAVE_FLATBUFFER
#include "flatbuffer_sample.hpp"
#endif
Expand Down Expand Up @@ -121,12 +118,9 @@ int main(int argc, char** argv) {
map.emplace(LibType::MSGPACK, new message_pack_sample());
#endif
#ifdef HAVE_PROTOBUF
#ifdef HAVE_STRUCT_PB
map.emplace(LibType::STRUCT_PB, new struct_pb_sample::struct_pb_sample_t());
#endif
map.emplace(LibType::PROTOBUF, new protobuf_sample_t());
#endif
map.emplace(LibType::STRUCT_PB0, new struct_pb_sample0());
map.emplace(LibType::STRUCT_PB, new struct_pb_sample());
#ifdef HAVE_FLATBUFFER
map.emplace(LibType::FLATBUFFER, new flatbuffer_sample_t());
#endif
Expand All @@ -145,9 +139,7 @@ int main(int argc, char** argv) {

run_benchmark(map, LibType::STRUCT_PACK);

#ifdef HAVE_STRUCT_PB
run_benchmark(map, LibType::STRUCT_PB);
#endif

return 0;
}
9 changes: 5 additions & 4 deletions src/struct_pack/benchmark/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ inline constexpr int ITERATIONS = 1000000;
enum class LibType {
STRUCT_PACK,
STRUCT_PB,
STRUCT_PB0,
MSGPACK,
PROTOBUF,
FLATBUFFER,
Expand Down Expand Up @@ -48,9 +47,11 @@ inline const std::unordered_map<SampleType, std::string> g_sample_name_map = {
std::to_string(OBJECT_COUNT) + " monsters(with zero-copy deserialize)"}};

inline const std::unordered_map<LibType, std::string> g_lib_name_map = {
{LibType::STRUCT_PACK, "struct_pack"}, {LibType::STRUCT_PB, "struct_pb"},
{LibType::STRUCT_PB0, "struct_pb0"}, {LibType::MSGPACK, "msgpack"},
{LibType::PROTOBUF, "protobuf"}, {LibType::FLATBUFFER, "flatbuffer"}};
{LibType::STRUCT_PACK, "struct_pack"},
{LibType::STRUCT_PB, "struct_pb"},
{LibType::MSGPACK, "msgpack"},
{LibType::PROTOBUF, "protobuf"},
{LibType::FLATBUFFER, "flatbuffer"}};

inline const std::vector<SampleType> g_sample_type_vec = {
SampleType::RECT, SampleType::RECTS, SampleType::PERSON,
Expand Down
Loading

0 comments on commit e773a82

Please sign in to comment.