2

How would protocol buffer handle compression of an already compressed byte[]?

Pseudo example:

[ProtoContract]
class Foo
{
  [ProtoMember(1)]
  public string Bar{ get; set; }

  [ProtoMember(2)]
  public byte[] CompressedFoo { get; set; }
}


// proto is my ProtocolBuffer.Net utility class

Foo _foo = new Foo() { Bar = "Hello World"; };

Foo _foo2 = new Foo() { Bar = "Goodbye cruel world"; };
_foo2.CompressedFoo = proto.Compress(_foo);

byte[] compressedFoo2 = proto.Compress(_foo2);

1 Answers1

1

The protobuf specification does not include any compression, unless you count the varint encoding of integer data. A byte[] in C# is treated as the protobuf bytes type, which means it is simply a length-prefixed raw dump of the bytes. So if CompressedFoo is (just as an example) 12 bytes, it will serialize it in 14 bytes, made up of:

  • 1 byte for the data-type (2, to indicate length-prefixed) and field number (2, from the [ProtoMember(2)]), mashed ("shifted") together and varint encoded
  • 1 byte for the length of the data (12), varint encoded
  • 12 bytes for the data (taken "as is" from CompressedFoo)
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • Thanks for the reply - I have asked another question on this link: http://stackoverflow.com/questions/10719169/protobuf-net-performance – user1412044 May 23 '12 at 11:40