uint32 vs int32: who is the most efficient? (Java)

3,337 views
Skip to first unread message

JekDaniel

unread,
May 5, 2011, 4:05:12 AM5/5/11
to Protocol Buffers
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

Adam Vartanian

unread,
May 5, 2011, 9:17:40 AM5/5/11
to JekDaniel, Protocol Buffers
> Then my question is this: why to use int32 type even though uint32
> uses less bytes covering the same range?

The main reason would be if you ever plan on reading the message from
non-Java code. What you've stated is only true in Java because it has
no unsigned integral types, so protobufs have to use signed types even
when a message declares a field as unsigned. In any language that has
unsigned integral types, you would write -1 and the other side would
read 4294967295.

- Adam

JekDaniel

unread,
May 7, 2011, 5:06:08 AM5/7/11
to Protocol Buffers
Thank you very much! :)
Reply all
Reply to author
Forward
0 new messages