开发者

how do I enable string interning in protobuf-net?

I am using v2 re开发者_JAVA百科v 421. When I saved the stream produced by protobuf-net and put it through the strings utility, it discovered many duplicate strings. I am talking about the strings produced by the application, which can be interned, but the string interning does not seem to be on by default.

How do I enabled it?

Thanks.


There are two separate types of interning here; there's interning while deserializing - this is always enabled, so if duplicates are in the data, you should only see a single .NET string instance in your managed classes, re-used as many times as needed.

UPDATE for 3.x String interning is no longer enabled by default. To enable it set the following in your model.

RuntimeTypeModel.Default.InternStrings = true;

There is also interning while serializing, to avoid duplicating data into the stream while serializing. This is not on by default, for the simple reason that no such behaviour is defined in the protobuf spec; protobuf-net tries to stay inside the spec by default, only using extensions on an opt-in basis.

If you want to enable this for protobuf-net=to=protobuf-net usage, then enable the AsReference option for any given string:

[ProtoMember(13, AsReference = true)]
public string Foo { get; set; }

This uses a protobuf-net implementation-specific representation. It won't play very nicely for interop purposes, however. If you need this in an interoperable way, the only thing to do is store the lists separately (perhaps in a List<string> somewhere), and use the position in the list in your data, i.e.

// this is .... uglier, but probably easier if you need cross-platform
public int FooOffset {
    get { return Foos.IndexOf(Foo); }
    set { Foo = Foos[value]; }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜