高性能克隆
我正在寻找一种以高性能方式深度克隆对象图的方法。我将让多个线程非常快速地克隆一个图,这样它们就可以处理某些状态,并在不感兴趣的情况下丢弃结果,返回到原始状态再试一次。
我目前正在通过二进制序列化使用深度克隆,虽然它可以工作,但速度并不快。我见过其他库,如 protobuf,但我的对象图中的类可能在外部程序集中定义,继承自主程序集中的类,并且如果可能的话,不希望在那些使用程序集中添加任何复杂性。
我遇到的有趣的事情之一是使用 自动生成IL。似乎还没有完全完成,我已经发布了内容以查看作者是否做了更多的工作,但我猜没有。有没有其他人开发或看到过通过 IL 进行深度克隆的功能更齐全的方法?或者另一种方法会很快?
I'm after a means of deep cloning an object graph in a perfomant way. I'm going to have multiple threads cloning a graph extremely quickly such that they can play with some state and throw away the results if they're not interesting, returning to the original to try again.
I'm currently using a deep clone via binary serialization, which although it works, isn't amazingly fast. I've seen other libraries like protobuf, but the classes in my object graph may be defined in external assemblies, inheriting from classes in the main assembly and don't wish to add any complexity in those consuming assemblies if possible.
One of the interesting things I did come across was cloning using automatically generated IL. It seems it's not quite finished and I've posted to see if the author has done any more on it, but I'm guessing not. Has anyone else developed or seen a more fully functional way of deep cloning via IL? Or another method that is going to be fast?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
除了序列化之外,我只考虑三种选择:
我们有一些控制二进制序列化的代码实例,以便我们可以序列化内部 GUID 表(我们有很多重复的 GUID,并通过 .NET Remoting 序列化非常大的列表)。它对我们来说效果很好,我们不需要第三方序列化框架,但是,它是带有一点代码生成的手工制作的东西。
CSLA.NET 框架 具有一个名为
UndoableBase
的类,它使用反射来序列化属性/字段值的Hashtable
。用于允许回滚内存中的对象。这可能适合您的“返回原始状态再试一次”这句话。就我个人而言,我会进一步研究基于反射(最好使用发出的 IL 以获得更好的性能)解决方案,这样您就可以利用类/成员属性来控制克隆过程。如果性能为王,这可能不会减少它。
Other than serialisation, I only consider three options:
We have some instances of code where we control the binary serialisation so that we can serialise an interned GUID table (we have lots of repeating GUIDs and serialise very large lists over .NET Remoting). It works well for us and we haven't needed a third party serialisation framework, however, it's hand-crafted stuff with a little code-gen.
The CSLA.NET framework features a class called
UndoableBase
that uses reflection to serialise aHashtable
of property/field values. Used for allowing rollbacks on objects in memory. This might fit with your "returning to original to try again" sentence.Personally I'd look further into a reflection-based (preferably with emitted IL for better performance) solution, this then allows you to take advantage of class/member attributes for control over the cloning process. If performance is king, this may not cut it.