Maximum encoded size of messages

45 views
Skip to first unread message

Dietmar

unread,
Nov 27, 2023, 5:50:28 AM11/27/23
to nanopb
Hi,

I have another question regarding memory buffer sizes.

This structure is automatically generated by nanopb_generator, using PB_ENCODE_ARRAYS_UNPACKED:

typedef struct _t_ResponseMeasurement {
    int64_t measurement_a_side_accumulated_values[192]; /* 1728 B */
    uint32_t checksum_of_measurement_a_side_accumulated_values; /*    5 B */
    int64_t  measurement_b_side_accumulated_values[192]; /* 1728 B */
    uint32_t checksum_of_measurement_b_side_accumulated_values; /*    5 B */
    int32_t  measurement_conditions[9]; /*   45 B */
    uint32_t checksum_of_measurement_conditions; /*    5 B */
    uint32_t measurement_flags[14]; /*   70 B */
    uint32_t checksum_of_measurement_flags; /*    5 B */
    t_ResponseMeasurement_MeasurementState measurement_state; /*    2 B enum */
} t_ResponseMeasurement;

The automatically generated t_ResponseMeasurement_size specifies a size of 3593 Bytes, that's obviously the sum of the values above.

When calling pb_get_encoded_size(), the calculated size is 3596 Bytes.

Finally, when calling pb_encode_ex(... PB_ENCODE_DELIMITED), bytes_written is 3598 Bytes. That's obvious, I asked for the reason on Friday (https://groups.google.com/g/nanopb/c/4Fb5TQbmkz8/m/cunU1gSLAAAJ).

Does anybody have an idea why there's a difference of 3 Bytes between t_ResponseMeasurement_size and pb_get_encoded_size()? 4 Byte alignment possibly?

Thanks.
Best regards,
Dietmar

Petteri Aimonen

unread,
Nov 27, 2023, 7:56:10 AM11/27/23
to nan...@googlegroups.com
Hi,

> This structure is automatically generated by nanopb_generator, using
> PB_ENCODE_ARRAYS_UNPACKED:

The generated _size defines do not currently take into account the
PB_ENCODE_ARRAYS_UNPACKED format, which takes more space.

--
Petteri

Dietmar

unread,
Nov 27, 2023, 8:34:09 AM11/27/23
to nanopb
Hi Petteri,

thanks for your answer, this information helps a lot. The arrays are of known size, so I'll calculate the space and avoid the generated _size defines.

Thanks.
Best regards,
Dietmar

Dietmar

unread,
Nov 29, 2023, 2:48:41 AM11/29/23
to nanopb
Hi Petteri,

maybe PB_ENCODE_ARRAYS_UNPACKED is taken into account correctly, but there's an issue in my implementation. The structure t_ResponseMeasurement, which I've introduced earlier, is part of a oneof message:

typedef struct _t_Response {
    pb_size_t which_response;
    union {
        t_ResponseIdentification identification;
        t_ResponseState state;
        t_ResponseConfiguration configuration;
        t_ResponseMeasurement measurement;
        t_ResponseStart start;
        t_ResponseStop stop;
        t_ResponseHold hold;
        t_ResponseProceed proceed;
        t_ResponseError error;
        t_ResponseService service;
        t_ResponseVoltage voltage;
    } response;
} t_Response;


The generated _size defines look as follows:

#define t_ResponseConfiguration_size             0
#define t_ResponseError_size                     66
#define t_ResponseHold_size                      0
#define t_ResponseIdentification_size            36
#define t_ResponseMeasurement_size               3593
#define t_ResponseProceed_size                   0
#define t_ResponseService_size                   2
#define t_ResponseStart_size                     0
#define t_ResponseState_size                     0
#define t_ResponseStop_size                      0
#define t_ResponseVoltage_size                   6
#define t_Response_size                          3596


3593 compared to 3596 is exactly the difference which I'm looking for, possibly I encoded t_Response, but expected to see the size of t_ResponseMeasurement.

However, a hint in the documentation makes me doubt whether my assumption is correct: "Notice that neither which_payload field nor the unused fields in payload will consume any space in the resulting encoded message."

Thanks.
Best regards,
Dietmar
Reply all
Reply to author
Forward
0 new messages