如何在序列化之前获取协议缓冲消息的实际大小?
我在 *.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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这取决于您感兴趣的尺寸。
如果您想知道
message> message :: :: bytesizelong()bytesizelong() /a>。
Example:
This is also the way
SerializeToString()
另一方面,如果您想知道消息当前需要多少内存,则可以使用
message :: 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 useMessage::ByteSizeLong()
.Example:
This is also the way
SerializeToString()
calculates the size of the message internally to resize thestd::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:
我必须添加更多信息,因为这还不够,可能会引起问题。
您的消息没有固定的大小,并且会调整大小到数据的大小。例如,我的简单:
设置这样的设置:
但是,如果您设置:
,则不能将其用作参考端到端大小,我强烈建议简单的旧标头类,其大小固定。不完美,但要完成工作。
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:
Setted like that :
but if you set :
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.