Discussion:
[capnproto] Issue with Int8 and UInt8
s***@gmail.com
2017-12-07 22:55:00 UTC
Permalink
Hello,

I wanted to test the performance of serializing a single scaler type of
message. For example:
@0xf123cfa3565bb5a6;

struct TestBool {
value @0 :Bool;
}
struct TestInt8 {
value @0 :Int8;
}

When I was testing scaler types of Int8 and UInt8, by calling
auto r2 = message.getRoot<TestInt8>();
cout << r2.getValue() << "\nsize: " << size << endl;

returned nothing for the value:

size: 16


However, when I set the field to Int16, the correct value is returned.

127

size: 16

The complete code is pasted below:
capnp::MallocMessageBuilder message;
TestInt8::Builder r1 = message.getRoot<TestInt8>();
r1.setValue(127);
auto serialized = message.getSegmentsForOutput();
// auto serialized = capnp::messageToFlatArray(message);

//capnp::SegmentArrayMessageReader reader(serialized);
size_t size = 0;
for (auto segment : serialized) {
size += segment.asBytes().size();
}
auto r2 = message.getRoot<TestInt8>();
cout << r2.getValue() << "\nsize: " << size << endl;

Is there anything wrong? It only happened for Int8 and UInt8 cases.
Also, is the size I am getting the correct serialized size?

Best,
Shuo
--
You received this message because you are subscribed to the Google Groups "Cap'n Proto" group.
To unsubscribe from this group and stop receiving emails from it, send an email to capnproto+***@googlegroups.com.
Visit this group at https://groups.google.com/group/capnproto.
'Kenton Varda' via Cap'n Proto
2017-12-07 23:09:09 UTC
Permalink
Hi Shuo,

The problem you are seeing is a classic problem with C++'s iostream
classes. int8_t and uint8_t are aliases for `signed char` and `unsigned
char`. std::ostream::operator<<() treats both of these types as equivalent
to `char`. So, it writes the value as a single character rather than as an
integer. If the value is an unprintable character, it looks like nothing is
printed.

FWIW, the `kj::str()` universal stringification function does not have this
problem. It treats only bare `char` as a character. `signed char` and
`unsigned char` are treated as integers.

As to your second question, yes, a size of 16 is correct for both messages.
The first segment of a message always starts with an 8-byte root pointer
pointing to the root object (usually a struct). Object sizes are rounded up
to the nearest 8-byte boundary, so unless the object is totally empty, you
end up with at least 16 bytes per message. Of course, most of this size is
zero-valued so would compress away with capnp packing or any decent
compression algorithm.

-Kenton
Post by s***@gmail.com
Hello,
I wanted to test the performance of serializing a single scaler type of
@0xf123cfa3565bb5a6;
struct TestBool {
}
struct TestInt8 {
}
When I was testing scaler types of Int8 and UInt8, by calling
auto r2 = message.getRoot<TestInt8>();
cout << r2.getValue() << "\nsize: " << size << endl;
size: 16
However, when I set the field to Int16, the correct value is returned.
127
size: 16
capnp::MallocMessageBuilder message;
TestInt8::Builder r1 = message.getRoot<TestInt8>();
r1.setValue(127);
auto serialized = message.getSegmentsForOutput();
// auto serialized = capnp::messageToFlatArray(message);
//capnp::SegmentArrayMessageReader reader(serialized);
size_t size = 0;
for (auto segment : serialized) {
size += segment.asBytes().size();
}
auto r2 = message.getRoot<TestInt8>();
cout << r2.getValue() << "\nsize: " << size << endl;
Is there anything wrong? It only happened for Int8 and UInt8 cases.
Also, is the size I am getting the correct serialized size?
Best,
Shuo
--
You received this message because you are subscribed to the Google Groups
"Cap'n Proto" group.
To unsubscribe from this group and stop receiving emails from it, send an
Visit this group at https://groups.google.com/group/capnproto.
--
You received this message because you are subscribed to the Google Groups "Cap'n Proto" group.
To unsubscribe from this group and stop receiving emails from it, send an email to capnproto+***@googlegroups.com.
Visit this group at https://groups.google.com/group/capnproto.
Loading...