如何在 protobuf-net 中启用字符串驻留?
我使用的是 v2 rev 421。 当我保存 protobuf-net 生成的流并将其放入字符串实用程序时,它发现了许多重复的字符串。我说的是应用程序生成的字符串,它可以被实习,但默认情况下字符串实习似乎没有打开。
我如何启用它?
谢谢。
I am using v2 rev 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里有两种不同类型的实习;反序列化时存在实习 - 这始终处于启用状态,因此如果数据中存在重复项,您应该只能在其中看到一个 .NET
string
实例您的托管类可以根据需要多次重复使用。更新 3.x
默认情况下不再启用字符串驻留。要启用它,请在您的模型中设置以下内容。
序列化时还存在实习,以避免在序列化时将数据复制到流中。默认情况下,这是不的,原因很简单,protobuf 规范中没有定义这样的行为; protobuf-net 默认情况下尝试保持在规范内,仅在选择加入的基础上使用扩展。
如果您想为 protobuf-net=to=protobuf-net 用法启用此功能,请为任何给定字符串启用
AsReference
选项:这使用 protobuf-net 特定于实现 代表。然而,对于互操作目的来说,它不会很好地发挥作用。如果您需要以可互操作的方式进行此操作,唯一要做的就是单独存储列表(可能在某个
List
中),并使用列表中的位置列出您的数据,即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.
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: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.