且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何protobuf网实现可观的性能呢?

更新时间:2023-02-17 12:54:20

protobuf网采用了策略模式;根据需要(仅一次每种类型),它使用反射来看看类型,并建立了一套序列化器(基于一个共同的接口),它可以使用序列化和反序列化 - 这样的在使用的时候的它只是步执行组已知序列化器。

protobuf-net uses a strategy pattern; as needed (once only per type) it uses reflection to look at the types, and builds a set of serializers (based on a common interface) that it can use to serialize and deserialize - so when in use it is just stepping through the known set of serializers.

的是,它试图在交谈时尽量合理利用反射;它使用 Delegate.CreateDelegate 谈性,而 DynamicMethod的(和定制IL)交谈场(如果可能它依赖于目标框架)。这意味着,它总是谈论的的委托类型,而不仅仅是 DynamicInvoke (这是很慢)。

Inside that, it tries to make sensible use of reflection when talking to members; it uses Delegate.CreateDelegate to talk to properties, and DynamicMethod (and custom IL) to talk to fields (when possible; it depends on the target framework). This means that it is always talking to known delegate types, rather than just DynamicInvoke (which is very slow).

没有要疯了,在code确实有一定的优化(可以说是在可读性的费用)而言:

Without going mad, the code does have some optimisations (arguably at the expense of readability) in terms of:

  • 本地字节[] 缓冲(输入/输出流)
  • 使用固定大小的数组(而不是表等);也许是太多
  • 使用泛型以避免拳击
  • 在众多调整/旋转因子/等各地的二值化处理循环
  • local byte[] buffering (of the input/output streams)
  • using fixed-size arrays (rather than lists etc); perhaps too much
  • using generics to avoid boxing
  • numerous tweaks/twiddles/etc around the binary processing loops

在现在回想起来,我觉得我做的仿制药点的错误;复杂性意味着,迫使仿制药进入系统它弯曲变形的几个地方,积极导致一些重大的问题(复杂款)在紧凑的框架

In hindsight, I think I made a mistake on the generics point; the complexity meant that forcing generics into the system bent it out of shape in a few places, and actively causes some major problems (for complex models) on compact framework.

我有一些设计(仅在我的头)使用这个重构的的-generic接口,并代替(合适的框架)更多地使用的ILGenerator 对code>(我的第一选择本来防爆pression ,但强制更高framework版本)。但问题是,这是要花费相当长的时间去工作,直到最近我一直pretty的淹没

I have some designs (in my head only) to refactor this using non-generic interfaces, and to instead (for suitable frameworks) make more use of ILGenerator (my first choice would have been Expression, but that forces a higher framework version). The problem, however, is that this is going to take a considerable amount of time to get working, and until very recently I've been pretty swamped.

最近我已经成功地再次启动花一些时间在protobuf网一>,所以希望我清楚我的请求等的积压,并很快上上手。这也是我打算把它处理模型的的反射比(即单独描述线映射)。

Recently I've managed to start spending some time on protobuf-net again, so hopefully I'll clear my backlog of requests etc and get started on that soon. It is also my intention to get it working with models other than reflection (i.e. describing the wire mapping separately).


和不产生任何产生code

and doesn't produce any generated code

我也应该澄清的是,如果你想使用生成的code有两个(可选)codeGEN路线; protogen.exe,或href="http://marcgravell.blogspot.com/2009/07/protobuf-net-now-with-added-orcas.html"> VS附加的需要的 - 它的主要用途是,如果你有一个现有的.proto文件,或意图互操作另一种语言(C ++等)的契约优先发展

I should also clarify that there are two (optional) codegen routes if you want to use generated code; protogen.exe, or the VS add-in, allow code generation from a .proto file. But this is not needed - it is useful mainly if you have an existing .proto file, or intent to interoperate with another language (C++ etc) for contract-first development.