ciborium icon indicating copy to clipboard operation
ciborium copied to clipboard

[Feature]: Predict serialized len

Open flxo opened this issue 5 months ago • 1 comments

Is there an existing issue for this?

  • [x] I have searched the existing issues

Description

I'm using ciborium to serialize data directly into an nng::Message buffer, which implements std::io::Write. My observation is that this process incurs significant overhead due to frequent reallocations. Specifically, it appears that nng::Message's underlying buffer management, possibly via its push_back method, reallocates with each small write operation performed by ciborium.

While I can preallocate the nng::Message's capacity using Message::with_capacity, this requires me to guess the maximum serialization size, leading to potential memory waste or insufficient capacity.

My question is whether ciborium offers a mechanism to predict the exact serialized size of a data structure before the actual serialization occurs. Although I understand such a prediction might not be computationally free, I suspect its cost would be less than alternative approaches, such as serializing into a Vec (which reallocates in chunks, but still requires a subsequent copy) and then copying the data into the nng::Message.

Acceptance Criteria

No response

Suggestions for a technical implementation

No response

flxo avatar Jul 29 '25 09:07 flxo