Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[struct_pack] support fast_varint encode, allow encode int as varint by config #499

Merged
merged 40 commits into from
Nov 23, 2023

Conversation

poor-circle
Copy link
Collaborator

@poor-circle poor-circle commented Nov 20, 2023

Why

  1. varint code is slow when serialization/deserialization.
  2. it's inconvenient to use struct_pack::var_int**_t, user may just want to use normal int type with varint encode.

What is changing

  1. add a new encode named fast_varint. It 's very fast when serialization/deserialization. It can be used if your int field is likely to be zero or it max value in stuct is small likely.
  2. allow user encode normal int as varint to simply usage.

For encoding detail, see document[TODO]

Example

Here is a struct:

using namespace struct_pack;
struct example0 {
  int32_t a;
  int64_t b;
  uint32_t c;
  uint64_t d;
};

you can add config to change coding:

struct example1 {
  int32_t a;
  int64_t b;
  uint32_t c;
  uint64_t d;
  static constexpr auto struct_pack_config =
      USE_FAST_VARINT | ENCODING_WITH_VARINT | DISABLE_ALL_META_INFO;
};

you can also change encoding config by ADL:

struct example2 {
  int32_t a;
  int64_t b;
  uint32_t c;
  uint64_t d;
};

constexpr auto set_sp_config(example2*) {
  return USE_FAST_VARINT | ENCODING_WITH_VARINT | DISABLE_ALL_META_INFO;
}

include/ylt/struct_pack/calculate_size.hpp Outdated Show resolved Hide resolved
include/ylt/struct_pack/calculate_size.hpp Outdated Show resolved Hide resolved
include/ylt/struct_pack/calculate_size.hpp Outdated Show resolved Hide resolved
include/ylt/struct_pack/calculate_size.hpp Show resolved Hide resolved
include/ylt/struct_pack/packer.hpp Outdated Show resolved Hide resolved
include/ylt/struct_pack/unpacker.hpp Outdated Show resolved Hide resolved
include/ylt/struct_pack/unpacker.hpp Outdated Show resolved Hide resolved
@poor-circle poor-circle merged commit c8dbcf6 into alibaba:main Nov 23, 2023
30 checks passed
@poor-circle
Copy link
Collaborator Author

I'll add document later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants