高性能克隆

发布于 2024-11-30 14:00:58 字数 442 浏览 1 评论 0原文

我正在寻找一种以高性能方式深度克隆对象图的方法。我将让多个线程非常快速地克隆一个图,这样它们就可以处理某些状态,并在不感兴趣的情况下丢弃结果,返回到原始状态再试一次。

我目前正在通过二进制序列化使用深度克隆,虽然它可以工作,但速度并不快。我见过其他库,如 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 技术交流群。

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

发布评论

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

评论(1

‖放下 2024-12-07 14:00:58

除了序列化之外,我只考虑三种选择:

  • 坚持序列化,但自定义它。如果您想以声明方式对内容进行分类,并且很可能会获得性能提升,这可能会很有用。
  • 基于反射的对象行走,与 IL 发射器(例如 Fasterflect)结合使用。
  • 代码生成或通过逐字分配属性来编写您自己的克隆(我们有一些旧代码,它们使用我们所谓的复制构造函数,获取自身的实例并手动复制属性/字段)。

我们有一些控制二进制序列化的代码实例,以便我们可以序列化内部 GUID 表(我们有很多重复的 GUID,并通过 .NET Remoting 序列化非常大的列表)。它对我们来说效果很好,我们不需要第三方序列化框架,但是,它是带有一点代码生成的手工制作的东西。

CSLA.NET 框架 具有一个名为 UndoableBase 的类,它使用反射来序列化属性/字段值的 Hashtable。用于允许回滚内存中的对象。这可能适合您的“返回原始状态再试一次”这句话。

就我个人而言,我会进一步研究基于反射(最好使用发出的 IL 以获得更好的性能)解决方案,这样您就可以利用类/成员属性来控制克隆过程。如果性能为王,这可能不会减少它。

Other than serialisation, I only consider three options:

  • Stick with serialisation, but customise it. This might be useful if you want to declaratively bin stuff and there are very likely performance gains to be had.
  • Reflection-based object walking, in conjunction with an IL emitter such as Fasterflect.
  • Code-gen or code your own cloning by literally assigning properties to each other (we have some old code that uses what we call a copy-constructor for this, takes an instance of itself and manually copies the properties / fields across).

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 a Hashtable 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.

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