I analyzed memory usage of every type of writing (in java) on
int32,sint32,uint32.
While sint32 is useful for those integers that can be both negative or
positive, int32 type is useful for data that are most likely positive.
However uint32 type cover the same range covered by integers and for
negative numbers uses a lower number of byte (only 5) than int32 (it
uses 10).
More details: with int32 type the writeInt32NoTag method in
CodedOutputStream, if the number is negative uses the sign extension
calling writeRawVarint64 method, while with uint32 the
writeUInt32NoTag method ever calls the writeRawVarint32 method that
uses at most 32 bits.
Then my question is this: why to use int32 type even though uint32
uses less bytes covering the same range?
(the same is for int64 and uint64)
I quote below the code of writeInt32NoTag and writeUInt32NoTag
methods:

public void writeUInt32NoTag(final int value) throws IOException {
    writeRawVarint32(value);
}

public void writeInt32NoTag(final int value) throws IOException {
    if (value >= 0) {
      writeRawVarint32(value);
    } else {
      // Must sign-extend.
      writeRawVarint64(value);
    }
}


*************** INT32 *******************

Bytes     int32
1         0 <= value <= 127
2         128 <= value <= 16383
3         16384 <= value <= 2097151
4         2097152 <= value <= 268435455
5         268435455 < value
10                        value < 0

Bytes    sint32
1        -64 <= value <= 63
2        -8192 <= value <= 8191
3        -1048576 <= value <= 1048575
4        -134217728 <= value <= 134217727
5        value < -134217728 || value > 134217727

Bytes     uint32
1         0 <= value <= 127
2         128 <= value <= 16383
3         16384 <= value <= 2097151
4         2097152 <= value <= 268435455
5         268435455 < value
5                        value < 0

Bytes     float=fixed32
4        any value

-- 
You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/protobuf?hl=en.

Reply via email to