I’m trying to modify the C Protocol Buffer (Protoc-C) to support a few options, one of which is optimize_no_pack. When optimize_no_pack is enabled, Protocol Buffers uses the non-packed encoding for repeated fields. Each value is encoded separately, which can be slightly larger but avoids the need to split the byte string during decoding.
To achieve it, I’ve attempted the following
Adding an init_no_pack function along with the default init function, https://gcc.godbolt.org/z/s1MdYPex6. This function helps with the goal of using non-packed encoding for repeated fields by explicitly initializing each repeated field with an array of default values. This ensures that each value in the repeated field is encoded separately, rather than being concatenated into a single byte string.
Modifying the virtual functions of GenerateStructQuantifierMember, GenerateStructMember, GenerateStaticQuantifierInit, GenerateStaticMemberInit. They generate different parts of the C structure that represents a protobuf message in your code. The part where I added if statements for optimize_no_pack are my code, and the rest are the original implementation. These functions help to generate different C structures and initialization code depending on the optimize_no_pack option. When optimize_no_pack is enabled, repeated fields are represented as static arrays: https://gcc.godbolt.org/z/hzerbMofh
When I tested the modified protocol buffer with the following proto file
option (optimize_no_pack) = true;
message TestOpaqueMess {
option (opaque) = true;
optional int32 test = 1;
required TestMess TestMess = 2;
optional SubMess test_mess_ins = 3 [(instance)=true];
repeated TestMess TestMessRepIns = 4 [(instance)=true, (max_count)=10];
required TestEnum test_enum = 5;
optional string test_string = 6 [default="123"];
repeated string test_string_ins = 7 [(max_size)=10, (max_count)=20];
required bytes test_bytes = 8 [default="10"];
oneof test_oneof {
string test_oneof_string = 9;
TestEnum test_oneof_enum = 10;
TestMess test_oneof_mess = 11;
int32 test_oneof_int = 12;
}
repeated TestMess TestMessRep = 13;
repeated string TestStringRep = 14;
repeated bytes TestBytesRep = 15;
}
I have this part of the initialization code that has error message
struct _Foo__TestOpaqueMess
{
ProtobufCMessage base;
ProtobufCAllocator *allocator;
protobuf_c_boolean has_test;
int32_t test;
Foo__TestMess *testmess;
protobuf_c_boolean has_test_mess_ins;
Foo__SubMess test_mess_ins;
size_t n_testmessrepins;
size_t c_testmessrepins;
Foo__TestMess* testmessrepins;
Foo__TestEnum test_enum;
char *test_string;
size_t n_test_string_ins;
size_t c_test_string_ins;
char* test_string_ins;
ProtobufCBinaryData test_bytes;
size_t n_testmessrep;
size_t c_testmessrep;
Foo__TestMess* *testmessrep;
size_t n_teststringrep;
size_t c_teststringrep;
char* teststringrep;
size_t n_testbytesrep;
size_t c_testbytesrep;
ProtobufCBinaryData* testbytesrep;
Foo__TestOpaqueMess__TestOneofCase test_oneof_case;
union {
char *test_oneof_string;
Foo__TestEnum test_oneof_enum;
Foo__TestMess *test_oneof_mess;
int32_t test_oneof_int;
};
};
char foo__test_opaque_mess__test_string__default_value[] = "123";
uint8_t foo__test_opaque_mess__test_bytes__default_value_data[] = "10";
#define FOO__TEST_OPAQUE_MESS__INIT
{ PROTOBUF_C_MESSAGE_INIT (&foo__test_opaque_mess__descriptor)
, NULL, 0,0, NULL, 0, { PROTOBUF_C_MESSAGE_INIT (&foo__sub_mess__descriptor)
, 0, 0,0, 0,0, 0,0,NULL,0, NULL, NULL }, 0,0,NULL,NULL, FOO__TEST_ENUM__VALUENEG123456, foo__test_opaque_mess__test_string__default_value, 0,0, { 1, foo__test_opaque_mess__test_bytes__default_value_data }, 0,0,NULL,NULL, 0,0, 0,NULL, FOO__TEST_OPAQUE_MESS__TEST_ONEOF__NOT_SET, {} }
the error message is error: incompatible types when initializing type ‘enum _Foo__TestEnum’ using type ‘void *’
I’m thinking my changes in EnumFieldGenerator::GenerateStaticQuantifierInit might not be correct but I can’t find the correct way to fix it. I appreciate any pointers.