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] enhance serialize performance by uninitialized resize #489

Merged
merged 3 commits into from
Nov 10, 2023

Conversation

poor-circle
Copy link
Collaborator

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

Why

In old version, if struct_pack save binary to a container, it will call member function resize(). which cause useless initialization.

What is changing

Add a helper function struct_pack::detail::resize(), which will resize some container(std::basic_string<char/unsigned char/signed char>, std::vector<char/unsigned char/signed char/std::byte>) without initialization.

In benchmark:

old version:

struct_pack serialize 1 rect :            3 ns
struct_pack serialize 20 rects :          17 ns
struct_pack serialize 1 person :          11 ns
struct_pack serialize 20 persons :        110 ns
struct_pack serialize 1 monster :         76 ns
struct_pack serialize 20 monsters :       652 ns

new version:

struct_pack serialize 1 rect :            2 ns
struct_pack serialize 20 rects :          11 ns
struct_pack serialize 1 person :          7 ns
struct_pack serialize 20 persons :        98 ns
struct_pack serialize 1 monster :         76 ns
struct_pack serialize 20 monsters :       618 ns

If the person's name field length is 10000(almost all data are string)
old version:

struct_pack serialize 20 persons :        15226 ns

new version:

struct_pack serialize 20 persons :        9406 ns

So in best case, struct_pack serialization is 1.4x faster than old version if you save your struct to std::vector/std::string

There is no change if you save your struct to stream/raw pointer.

@qicosmos qicosmos merged commit 873d75f into alibaba:main Nov 10, 2023
30 checks passed
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