如何在序列化之前获取协议缓冲消息的实际大小?

发布于 2025-02-07 07:28:55 字数 132 浏览 0 评论 0原文

我在 *.proto文件中定义了一条消息,并使用反射设置了值。 我需要通过serializetostring() api来找出每秒分解多少个字节。 在调用serializetostring之前,是否可以获取消息的实际大小?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

追星践月 2025-02-14 07:28:55

这取决于您感兴趣的尺寸。

如果您想知道 message> message :: :: bytesizelong()bytesizelong() /a>。

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()


另一方面,如果您想知道消息当前需要多少内存,则可以使用 message :: spaceusedlong() - 这将为您提供该大小的估计。

例子:

ExampleMessage msg;
msg.set_example(12);

std::size_t approximateInMemorySize = msg.SpaceUsedLong();

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();
臻嫒无言 2025-02-14 07:28:55

我必须添加更多信息,因为这还不够,可能会引起问题。

您的消息没有固定的大小,并且会调整大小到数据的大小。例如,我的简单:

message AbstractHeader {
    int32       id = 1;
    MessageType type = 2;
    int32       lenght = 3;
}

设置这样的设置:

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

但是,如果您设置:

wp.set_id(12);
wp.set_lenght(12);

-> 0
 byte-> 6
serialized -> 6

,则不能将其用作参考端到端大小,我强烈建议简单的旧标头类,其大小固定。不完美,但要完成工作。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文