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

calculate serialize size before serialization and serialize to pre-allocated memory #1048

Open
alan0526 opened this issue Jun 4, 2024 · 4 comments
Labels
enhancement New feature or request optimization Making stuff faster

Comments

@alan0526
Copy link

alan0526 commented Jun 4, 2024

I am working on inter process communication base on shared memory and Unix Socket Domain recently, I plan to use glaze Beve to serialize my data.
I want calculate the object serialization size then alloc shared memory and write serialization data into it.
Is there some existing solution to do this. thanks.

@stephenberry
Copy link
Owner

stephenberry commented Jun 4, 2024

Serializing to a pre-allocated buffer versus a dynamic buffer like std::string typically has less than a 5% difference in performance. Furthermore, in inter-process communication you should typically reuse your serialization buffer. Glaze uses a doubling of the buffer capacity if growth is needed, so it is rare that you will have new allocations if you reuse your serialization buffer.

Is there a reason why you can't reuse a std::string (serialization) buffer?

A function to calculate the serialization size could be added, but it would need to iterate through your structures. This would actually add more overhead than simply reusing the std::string buffer. So you might have a tiny performance boost for the very first message you send, but subsequent messages would run slower needing to compute the size beforehand.

@stephenberry stephenberry added the question Further information is requested label Jun 4, 2024
@alan0526
Copy link
Author

alan0526 commented Jun 4, 2024

Before if I use string I need to serialize to std::string then write it to shared memory, there is an extra copy.
And the reason why I need to size is I want to know what the size of the shared memory to be created.

@stephenberry
Copy link
Owner

I see. I'll keep this issue alive to add the ability to compute the space required before serialization.

I typically use shared libraries which allow me to share the std::string directly across processes, which allows dynamic buffer sharing and avoids the extra copy. But, I can see how if you are wanting to connect services in other programming languages it is easier to not have to deal with dynamic memory and just compute the size beforehand.

@stephenberry stephenberry added enhancement New feature or request optimization Making stuff faster and removed question Further information is requested labels Jun 4, 2024
@alan0526
Copy link
Author

alan0526 commented Jun 5, 2024

thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request optimization Making stuff faster
Projects
None yet
Development

No branches or pull requests

2 participants