Protocol buffers 一个基类的多个子类-protobuf性能

Protocol buffers 一个基类的多个子类-protobuf性能,protocol-buffers,protobuf-net,Protocol Buffers,Protobuf Net,现在我有一个应用程序,我的iPhone应用程序发送一个请求,该请求在.NET/C#中处理,序列化为XML,并在objective-C中解析。当前的响应类结构有一个基类(BaseResponse)和多个(超过25个)子类,每个请求类型对应于需要返回的不同内容。现在我想看看protobuf是否比XML更快更简单。据我所知,该类结构的.proto文件是: Message BaseResponse { Required Int field1 = 1; Optional SubResponse1 sub1

现在我有一个应用程序,我的iPhone应用程序发送一个请求,该请求在.NET/C#中处理,序列化为XML,并在objective-C中解析。当前的响应类结构有一个基类(BaseResponse)和多个(超过25个)子类,每个请求类型对应于需要返回的不同内容。现在我想看看protobuf是否比XML更快更简单。据我所知,该类结构的.proto文件是:

Message BaseResponse {
Required Int field1 = 1;
Optional SubResponse1 sub1= 2;
Optional SubResponse2 sub2 = 3;
Etc....
}

Message SubResponse1 {
....
}

Message SubResponse2 {
....
}
Etc for each sub response.

我的问题是:如果我有25个以上的可选元素(其中只有1个是非空的),那么这是否会完全消除使用protobuf的大小和性能优势?protobuf对这个应用程序有意义吗?

没有,它不会影响性能优势-您只需要检查objective-C代码中哪个是非空的。由于protobuf只序列化非空值,因此它在网络上仍然非常有效。protobuf规范本身实际上并不包含继承,因此您可以说您需要通过封装对其进行欺骗-但既然您提到了C#,请注意您所描述的内容(包括数据在线路上的显示方式,即它将是100%兼容的)如果您使用protobuf net作为C#实现,则可以通过继承直接完成—这在您现有的模型中应该是可能的。例如:

[ProtoContract]
[ProtoInclude(2, typeof(SubResponse1))]
[ProtoInclude(3, typeof(SubResponse2))]
public class BaseResponse
{
    // note Name and IsRequired here are optional - only
    // included to match your example
    [ProtoMember(1, IsRequired = true, Name="field1")]
    public int Field1 { get; set; }
    /*...*/
}
[ProtoContract]
public class SubResponse1 : BaseResponse
{/*...*/}
[ProtoContract]
public class SubResponse2 : BaseResponse
{/*...*/}
您可以通过以下方式获得.proto:

var proto = Serializer.GetProto<BaseResponse>();

不,它不会影响性能优势-您只需要检查objective-C代码中哪一个是非空的。由于protobuf只序列化非空值,因此它在网络上仍然非常有效。protobuf规范本身实际上并不包含继承,因此您可以说您需要通过封装对其进行欺骗-但既然您提到了C#,请注意您所描述的内容(包括数据在线路上的显示方式,即它将是100%兼容的)如果您使用protobuf net作为C#实现,则可以通过继承直接完成—这在您现有的模型中应该是可能的。例如:

[ProtoContract]
[ProtoInclude(2, typeof(SubResponse1))]
[ProtoInclude(3, typeof(SubResponse2))]
public class BaseResponse
{
    // note Name and IsRequired here are optional - only
    // included to match your example
    [ProtoMember(1, IsRequired = true, Name="field1")]
    public int Field1 { get; set; }
    /*...*/
}
[ProtoContract]
public class SubResponse1 : BaseResponse
{/*...*/}
[ProtoContract]
public class SubResponse2 : BaseResponse
{/*...*/}
您可以通过以下方式获得.proto:

var proto = Serializer.GetProto<BaseResponse>();