gpt4 book ai didi

c# - protobuf-net 中的 prepareserializer 有什么作用?

转载 作者:行者123 更新时间:2023-12-05 00:35:49 25 4
gpt4 key购买 nike

我假设它会查看您的模型并以某种方式准备好,这样您的前几个序列化就不会减慢。如果我的消息模型有一个带有子类的消息类怎么办?将我的父类放在类型参数中是否也为所有子类做好准备?

最佳答案

(这个答案假设 protobuf-net v2)

如果您的意思是 Serializer.PrepareSerializer<T>() ,那么它肯定会检查所有的子类型,因此类型模型将为它们准备好(意思是:它将找出哪些字段/属性等需要序列化)。它将预编译(即 IL-emit)父类的代码,但(查看代码)不是专门针对派生类型的。如果无人看管,派生类型将在首次需要时自行编译。我认为!如果你真的想要,我可以做一个彻底的检查。

但是,如果您使用 RuntimeTypeModel.Default.CompileInPlace() ,它构建了整个模型 - 已知的一切都已准备就绪。当然,这就留下了必须先告诉模型关于它们的两难境地;p

我会仔细检查一下子类型序列化器是在什么时候准备好的,只是为了确定。级联它们可能确实有意义。

更新:

看起来它确实级联到派生类型,但不是级联到父类型(如果有):

    [Test]
public void CheckTypeSpecificCompileInPlaceCascadesToBaseAndChildTypes()
{
var model = TypeModel.Create();
model[typeof(B)].CompileInPlace();

Assert.IsTrue(model.IsPrepared(typeof(D)), "D"); // sub-sub-type
Assert.IsTrue(model.IsPrepared(typeof(C)), "C"); // sub-type
Assert.IsTrue(model.IsPrepared(typeof(B)), "B"); // self
Assert.IsTrue(model.IsPrepared(typeof(A)), "A"); // base-type
}

[Test]
public void CheckGlobalCompileInPlaceCascadesToBaseAndChildTypes()
{
var model = TypeModel.Create();
model.Add(typeof (B), true); // give the model a clue!
model.CompileInPlace();

Assert.IsTrue(model.IsPrepared(typeof(D)), "D"); // sub-sub-type
Assert.IsTrue(model.IsPrepared(typeof(C)), "C"); // sub-type
Assert.IsTrue(model.IsPrepared(typeof(B)), "B"); // self
Assert.IsTrue(model.IsPrepared(typeof(A)), "A"); // base-type
}

在这里,第二个测试通过了;第一个测试未通过引用“A” - 所以子类型(“C”和“D”)被完全编译。由于基本类型仍会按需编译,因此按原样可能没问题,但如果有用的话,我可能会将其简化为祖先类型。

( IsPrepared 方法只存在于我的本地副本中)

关于c# - protobuf-net 中的 prepareserializer 有什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8933251/

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