Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Let's assume we compile our program for 64 bit machine, which memory block is every 8 bytes.
Previously, we have this proto.FieldBase struct having 112 bytes as follows:
By re-arrange the struct order, we can save 8 bytes, so the size is now 104 bytes per proto.FieldBase:
At the moment we have 394 predefined messages in the factory containing 1307 fields in total, we save 1307 * 8 = 10.456 bytes (10KB). We also aligning the proto.Component to save 8 bytes, we save approx. 600 bytes. And we also align proto.DeveloperField but this depend on user input. Total 11KB, sound small but it can accumulate if user add Product Profile (manufacturer specific message). In general, it's always good to save what we can save.
Aside from that change, we also make (*Decoder).readByte as inline call by reducing unnecessary condition cost:
./decoder.go:796:6: cannot inline (*Decoder).readByte: function too complex: cost 86 exceeds budget 80
./decoder.go:796:6: can inline (*Decoder).readByte with cost 79
It's ok to return the value of b[0] even if error occurs, since in our convention we will ignore the value when error occurs on reading bytes.