protobuf-net 对负整数的处理

发布于 2024-11-26 13:15:44 字数 928 浏览 0 评论 0原文

使用 protobuf-net,是否可以利用 Zigzag 编码来处理负整数?

当尝试序列化具有负值的对象属性时,protobuf-net 会回退到fixed32/fixed64 编码,而不是高效的 Zigzag 编码。

例如,

[ProtoContract]
internal class TestPoint
{
    [ProtoMember(1)]
    internal long Value;
}

var p = new TestPoint() { Value = -150 };

序列化为 11 字节。

var p = new TestPoint() { Value = 150 };

序列化为 3 个字节。

来自 ProtoWriter.cs:

            case WireType.Variant:
                if (value >= 0)
                {
                    WriteUInt64Variant((ulong)value, writer);
                    writer.wireType = WireType.None;
                }
                else
                {
                    DemandSpace(10, writer);
                    ...

有没有办法装饰序列化类中的成员以使用 WireType.SignedVariant?在源代码中找不到任何方法来执行此操作。

否则,使用 protobuf 处理负数会破坏原本出色的线压缩的目的。

请帮忙!

问候, 麦克风

Using protobuf-net, is it possible to leverage the Zigzag encoding for negative ints?

When attempting to serialize object properties that have negative values, protobuf-net falls back to fixed32/fixed64 encoding instead of the efficient Zigzag encoding.

E.g.

[ProtoContract]
internal class TestPoint
{
    [ProtoMember(1)]
    internal long Value;
}

var p = new TestPoint() { Value = -150 };

Serializes to 11 bytes.

var p = new TestPoint() { Value = 150 };

Serializes to 3 bytes.

From ProtoWriter.cs:

            case WireType.Variant:
                if (value >= 0)
                {
                    WriteUInt64Variant((ulong)value, writer);
                    writer.wireType = WireType.None;
                }
                else
                {
                    DemandSpace(10, writer);
                    ...

Is there a way to decorate a member in a serialized class to use the WireType.SignedVariant? Couldnt find any way to do this in the source code.

Otherwise, using protobuf for negative numbers defeats the purpose of an otherwise excellent wire compression.

Please assist!

Regards,
Mike

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绅刃 2024-12-03 13:15:44

当然; protobuf 有几种不同的编码;你想要的是 zig-zag 编码:

[ProtoMember(1, DataFormat = DataFormat.ZigZag)]

它在 protobuf-net 中有 xml 注释:

/// <summary>
/// When applied to signed integer-based data (including Decimal), this
/// indicates that zigzag variant encoding will be used. This means that values
/// with small magnitude (regardless of sign) take a small amount
/// of space to encode.
/// </summary>
ZigZag

Sure; protobuf has a few different encodings; what you want here is zig-zag encoding:

[ProtoMember(1, DataFormat = DataFormat.ZigZag)]

which has the xml-comment in protobuf-net:

/// <summary>
/// When applied to signed integer-based data (including Decimal), this
/// indicates that zigzag variant encoding will be used. This means that values
/// with small magnitude (regardless of sign) take a small amount
/// of space to encode.
/// </summary>
ZigZag
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文