golang / protobuf

Go support for Google's protocol buffers
BSD 3-Clause "New" or "Revised" License
9.74k stars 1.58k forks source link

Options to skip getters, enum maps, etc. #1474

Closed benma closed 2 years ago

benma commented 2 years ago

This is basically the same as https://github.com/golang/protobuf/issues/961, which was locked, but with added reasoning:

I use GopherJS to compile Go code using the generated protobuf messages to JS. The size of the generated output is bloated with lots of getters and enum maps, String() methods etc. that I never use.

I would love options to disable generation, which would significantly reduce my JS package size.

puellanivis commented 2 years ago

So, the difficulty here is in knowing where and when to stop adding feature flags to turn off feature XY. Soon, one could imagine a whole constellation of options, knobs, and sliders to adjust the output to exactly what you want, at the cost of simplicity and understandability for other code writers (which we should always remember, will also be us 6–12 months later).

Unfortunately, the only thing I can really recommend is that if you want/need such deviation of code generation, you might want to look into a way to accomplish it on your side. Hopefully, a compiler should be removing dead code. But as well, if you need lean and perfectly trimmed code, then no amount of knobs and dials on the code generator will ever be as exacting of a scalpel as just sitting down and cutting it out yourself.

I’m sorry, and I hate to give such a disappointing answer, but I fear (from having been there myself) that even after adding this option, you would find the amount of bloat cut would be underwhelming, and you may well not end up significantly closer at all to what you really want.

stapelberg commented 2 years ago

+1 to what @puellanivis said. Eliminating dead code should be done later in the stack, not when generating the code.