gpt4 book ai didi

Copying content of a dynamic message into a generated one(将动态消息的内容复制到生成的消息中)

转载 作者:bug小助手 更新时间:2023-10-26 21:33:54 24 4
gpt4 key购买 nike



For reasons beyond my control I have to deal with generated and dynamic messages in the same codebase. I'm looking for an efficient way to copy content of a dynamic message into a specific generated one.

由于我无法控制的原因,我不得不在同一代码库中处理生成的消息和动态消息。我正在寻找一种有效的方法来将动态消息的内容复制到特定生成的消息中。


Message* msg;  // Dynamic I get from upstream
MyMessage mymsg; // Generated to be passed downstream

I know I can serialize msg and deserialize it back into mymsg

我知道我可以序列化msg并将其反序列化为mymsg。


mymsg.ParseFromString(msg->SerializeAsString());

This works fine, but doesn't seem to be very efficient.

这很有效,但似乎效率不是很高。


I tried using CopyFrom

我尝试使用CopyFrom


mymsg.CopyFrom(*msg);

but it fails with

但失败的原因是


Error: CHECK failed: (from.GetDescriptor()) == (descriptor): : Tried to copy from a message with a different type. to: foo.MyMessage, from: foo.MyMessage


An answer to this question hints, that some similar should be possible, but the solution eludes me.

这个问题的答案暗示,一些类似的事情应该是可能的,但我无法找到解决方案。


更多回答

"This works fine, but doesn't seem to be very efficient" - Is it a source of an actual performance problem? Did your profiler show this as a performance hotspot (when running an optimized build of the code)? If not, why do you really care? Micro-optimizing things that take a few handfuls of nanoseconds to take a few nanoseconds less, in code that's only called a few times (or a few times per second) in an application is not really worth the effort (IMHO).

“这工作得很好,但似乎效率不高”--这是实际性能问题的根源吗?您的分析器是否将其显示为性能热点(在运行代码的优化构建时)?如果不是,你为什么真的在乎呢?在应用程序中只调用几次(或每秒几次)的代码中,只需几纳秒就可以少花几纳秒的微优化工作是不值得付出努力的(IMHO)。

"I have to deal with generated and dynamic messages" - Could you explain what those terms mean in the context of your application? It's not clear from what you posted.

“我必须处理生成的和动态的消息”--您能解释一下这些术语在您的应用程序上下文中的含义吗?从你发布的内容中还不清楚。

优秀答案推荐

If the serialised version of each is identical, such that you convert one to the other via the wire format (as in your question), you possibly do not need the dynamic version at all. If your contact with "upstream" is "we're going to send you messages that look like this", then you don't need their code to parse them.

如果两者的序列化版本是相同的,这样您就可以通过连接格式将一个转换为另一个(如您的问题所示),那么您可能根本不需要动态版本。如果您与“上游”的联系人是“我们将向您发送类似于此的消息”,那么您不需要他们的代码来解析它们。


Admittedly, if you had to link to / interop with existing C++ code that was already written wround the dynamic version, you could be stuck with that.

诚然,如果您必须链接到已在动态版本中编写的现有C++代码的/interop,则可能会遇到这种情况。


If you are stuck with existing code, perhaps reflection is the answer. I don't know what could be done with C++ and the "reflection" Google build in. In the C# versions, one can iterate over / navigate the structure of a message finding fields, and copying them to like-named-like-positioned fields in another class. That's a simple algorithm that avoids writing a load of dull g.field1 = d.field; style code, but avoids the serialise / deserialise conversion. Whether or not the same thing can be done in C++ with what Google include, I don't know. However, the performance is not likely to be much better (if at all) than serialising / deserialising.

如果您被现有代码束缚住了,也许反射就是答案。我不知道用C++和Google内置的“反射”能做什么。在C#版本中,可以迭代/导航消息查找字段的结构,并将它们复制到另一个类中的同名相似定位的字段中。这是一个简单的算法,它避免了编写大量枯燥的g.field1=d.field;样式代码,但避免了序列化/反序列化转换。我不知道在C++中是否可以用Google Include做同样的事情。然而,性能不太可能比序列化/反序列化好多少(如果有的话)。


更多回答

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com