Closed g-berthiaume closed 3 years ago
As an addendum:
Thanks for bitproto
.
It's an awesome tool. :)
Hi, @g-berthiaume
I made a similar picture to explain it, a minor difference from your picture is the bits layout, low bits on the left and high bits on the right, since bitproto uses little-endian.
Number 0x123
in binary format is 100100011
.
The type of preamble
is uint20
, which occupies 20bits after encoding.
But the value 0x123
owns 9bits, so you may think there are paddings of 11bits.
If we set preamble
to a larger value, say 2^20-1
, the 'paddings' goes away.
If we set preamble
to a much more larger value, say 2^32
, only the lower 20bits will be taken.
The encoder end must respect to the data type predefined in bitproto file, so that the decoding end will be able to parse the encoded data.
If we encode data without these 'paddings', the situation will be, there are 9bits from from preamble
, and then 1111....
from field start
, arranged tightly without any unused bits. The following picture describes what you expect.
But, How to decode this buffer ?
Only the type definitions in bitproto file tells the decoder end that, where the field boundaries are, how the data fields arrange, so that the decoder will read 20 bits at first for preamble
's value, and then read 15bits for start
's value.
By putting start
's bits ahead, it uses the wrong buffer, and decoder won't known that. bitproto doesn't store reflection data during encoding either. What the decoder will see, is all the data buffer, without any additional typing information, without any data arrangement information, it's designed to parse data according to the bitproto file.
Hope this can help you.
Thanks for your answer. I revised my question, and I realized that I was mistaken.
Thanks for your time and your project.
Problem
Currently, bitproto's serialized buffer is not packed.
Here's an example: Here's my simple schema:
We compile using:
Using the newly generated file, we can do the following tests:
When executing this test code, we print
Therefore, this structure is padded. If the structure would be packed (no padding), I would expect:
Here's a visualization of the packed structure.![image](https://user-images.githubusercontent.com/19377511/127569337-f7eb765b-9c3c-4e39-a707-c73069be64d2.png)
Solution
Adding a
-p
packed flag.Why is this valuable
This would enable bitproto's users to have better control over how their data is serialized.