Looks like you create a new person in the loop, so each person has only a
single ID. As Thomas mentioned, if you don't actually have multiple values,
the framing is just overhead.

With a single value, the encoding basically looks like so:

   - *With packed = false*: varint encoded tag, varint encoded value
   - *With packed = true*: varint encoded tag, varint encoded length value
   == 1, varint encoded value

But with multiple IDs per person, it will be smaller. With 10 values, the
encoding basically looks like so:

   - *With packed = false*: (varint encoded tag, varint encoded value) x 10
   - *With packed = true*: varint encoded tag, varint encoded length value
   == 10, (varint encoded value) x 10

The break-even point is ~2 values in the repeated field, since both packed
and unpacked would use 4 varints to represent the pair of values (though
packed may possibly be 1 byte smaller since varint encoded length of 2 will
be only one byte, where as extra varint encoded tag could possibly be >1
byte, depending on the tag value).

----
*Josh Humphries*
[email protected]


On Mon, Dec 10, 2018 at 4:03 AM <[email protected]> wrote:

> The code i am using is below:
>
> #include <fstream>
> #include <iostream>
> #include <string>
> #include "addressbook.pb.h"
>
> #define FILENAME "ashu.pb"
>
> using namespace std;
>
> AddressBook address_book;
>
> int getFileSize()
> {
>     ifstream file(FILENAME, ifstream::in | ifstream::binary);
>
>     if(!file.is_open())
>     {
>         return -1;
>     }
>
>     file.seekg(0, ios::end);
>     int fileSize = file.tellg();
>     cout << "Size ----- " << fileSize << endl;
>     file.close();
>
>     return fileSize;
> }
>
> int main()
> {
>     int x,size;
>     Person *p = address_book.add_people();
>
>     Person::PhoneNumber* phone_number = p->add_phones();
>
>     fstream out(FILENAME, ios::out | ios::binary | ios::trunc);
>
>     for(int j=0;j<5000;j++)
>     {
>     p = address_book.add_people();
>
>         p->add_id(j);
>     }
>
>     x = address_book.SerializeToOstream(&out);
>     out.close();
>
>     size = getFileSize();
>     cout << "size " << size << endl;
>
>     AddressBook ad1;
>     fstream in(FILENAME, ios::in | ios::binary);
>     ad1.ParseFromIstream(&in);
>
>     return 0;
> }
>
> and i am using only id field of message Person. Is that the reason why i
> end up getting more size with packed.
>
> On Friday, December 7, 2018 at 12:12:04 PM UTC+5:30, [email protected]
> wrote:
>>
>> I am trying to find out difference in encoded size using packed equal to
>> true and without any packed encoding. I looped a int32 variable 5000 times.
>> With packed = true, i get size as 29876 and without packed encoding, i get
>> size as 24876. I am confused why i get opposite results. As per my
>> understanding, size with packed should be less as compared to non packed.
>> Can anyone please explain what the issue is?
>> My proto file is below for non packed and for packed i modify id filed
>> as; repeated int32 id = 2[packed=true]:
>>
>> syntax = "proto2";
>> message Person {
>>   optional string name = 1;
>>   repeated int32 id = 2;
>>   optional string email = 3;
>>
>>   enum PhoneType {
>>     MOBILE = 0;
>>     HOME = 1;
>>     WORK = 2;
>>   }
>>
>>   message PhoneNumber {
>>     optional string number = 1;
>>     optional PhoneType type = 2 [default = HOME];
>>   }
>>
>>   repeated PhoneNumber phones = 4;
>> }
>>
>> message AddressBook {
>>   repeated Person people = 1;
>> }
>>
> --
> You received this message because you are subscribed to the Google Groups
> "Protocol Buffers" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at https://groups.google.com/group/protobuf.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.

Reply via email to