protobuf icon indicating copy to clipboard operation
protobuf copied to clipboard

Align struct fields for reduced memory overhead

Open steeling opened this issue 3 years ago • 3 comments
trafficstars

Struct aligning generated protos for optimized memory overhead could have significant savings for any binary manipulating lots of proto messages (grpc servers). I'm not sure if there's other considerations around marshaling/unmarhsaling protos, but wanted to bring up the discussion. Cheers!

https://en.wikipedia.org/wiki/Data_structure_alignment

steeling avatar Mar 11 '22 00:03 steeling

The generated messages follow the order specified in the .proto files. I don't think the Go generator should re-order them. If there is a benefit to doing so, I argue that it should be done in the .proto files.

dsnet avatar Mar 11 '22 01:03 dsnet

gotcha. FYI you can save a measly 16 bytes by swapping the unknownFields and sizeCache, and moving them to the end of the struct instead of the beginning.

steeling avatar Mar 11 '22 02:03 steeling

Thanks for the suggestion. Swapping unknownFields and sizeCache seems valuable since the latter is always 4B wide. Moving them to the end may or may not help depending on what fields are in between.

dsnet avatar Mar 11 '22 02:03 dsnet