I was trying to use ProtoBuf In my project because of the assumption that it can really readuce the size of the data massage while on serailization in binary format which will ultimately reduce the transfer rate in my project,
But for me ,I could not see any difference in the reduction of size with Protobuf with API's SerializeToString() or SerializeToArray().
for example I have the below string which i am sending on 100ms to other application and i really need to save some bytes on this transfer,
I am trying to serialize this string message with ProtBuf in binary mode to reduce the size of the string on transfering the string message to another application,
below is the way i am doing that,but it doesn't seems to be reduce the size on serialization.please see that and let me know your valuable thoughts on it,
//proto file is as like below
message AMessage
{
optional string str1=1;
}
//---------Code for serailizing the above mentioned string message----//
AMessage A_msg;
A_msg.set_str1(m_txBuff); //m_txBuff -contains the above mentioned string string
std::string encode_string;
//----tried with SerializeToString(),but size of the string is similar to that of "text" mode--/
A_msg.SerializeToString(&encode_string);
//----tried with SerializeToArray(),but size of the string is similar to that of "text" mode--//
int size_bytes = A_msg.ByteSize();
void _buffer_bytes = malloc(size_bytes);
A_msg.SerializeToArray(buffer_bytes, size_bytes);
std::string encoded_string_B64 = base64_encode((unsigned char_)buffer_bytes, size_bytes);
//--------sending the data to other application --------//
infomq_cmd_publish((unsigned char*)encode_string.c_str(),encode_string.size(),0);
Protocol buffers are an efficient binary serialization format, not a compressor. Thus, strings do not become any smaller as their payload remains the same.
If you need to reduce the size, you could either gzip the data or use an application-level dictionary to substitute large strings with something smaller.
You might also want to take a look at http://pieroxy.net/blog/pages/lz-string/index.html for string-level compression.