Reputation: 35
I defined a message in *.proto file and set the values using reflection.
I need to find out how many bytes are parsed per second with SerializeToString()
API.
Is it possible to get the actual size of the message before calling SerializeToString?
Upvotes: 2
Views: 9101
Reputation: 61
I have to add more infos because this is not enough and can cause problem.
Your message does not have a fixed size and will resize to it's data. for example my simple:
message AbstractHeader {
int32 id = 1;
MessageType type = 2;
int32 lenght = 3;
}
Setted like that :
std::cout << "-> " << wp.ByteSizeLong() << std::endl;
wp.set_id(2147483647);
wp.set_lenght(2147483647);
wp.set_type(Proto::MessageType::ESP32);
std::cout << " byte-> " << wp.ByteSizeLong() << std::endl;
std::string tmpser = "";
wp.SerializeToString(&tmpser);
std::cout << "serialized -> " << tmpser.size() << std::endl;
-> 0
byte-> 14
serialized -> 14
but if you set :
wp.set_id(12);
wp.set_lenght(12);
-> 0
byte-> 6
serialized -> 6
So you cannot use it as a reference end to end size, i strongly advice simple old header class with a fixed size. not perfect, but get the job done.
Upvotes: 0
Reputation: 10800
It depends on which size you're interested in.
If you want to know how large the serialized protobuf message returned by MessageLite::SerializeToString()
is going to be you can use Message::ByteSizeLong()
.
Example:
ExampleMessage msg;
msg.set_example(12);
std::size_t expectedSize = msg.ByteSizeLong();
std::string result;
msg.SerializeToString(&result);
assert(expectedSize == result.size());
This is also the way SerializeToString()
calculates the size of the message internally to resize the std::string
to have enough space for the entire message.
On the other hand if you want to know how much memory the message currently requires in unserialized form you can use Message::SpaceUsedLong()
- which will give you an estimate of that size.
Example:
ExampleMessage msg;
msg.set_example(12);
std::size_t approximateInMemorySize = msg.SpaceUsedLong();
Upvotes: 4