4 ms·
The code is not what I would consider acceptable from a human (no offence!). For example a first sight shows repetition eg. can_zero_in_permitted_state and
by throwaway_pdp09 6y ago
The code is not what I would consider acceptable from a human (no offence!).
For example a first sight shows repetition eg.
can_zero_in_permitted_state
and
can_fast_zero_in_permitted_state
are identical in body.
I get that it's output so that repetition is in a sense ok because it's doubtless correct, but even if it's meant to be read only and never human-edited it could have that cleaned up.
- rwmj 6y agoTo a certain extent that's kind of unavoidable with generated code. The code is still generated even if we make it look hand-written. (I guess it's actually possible that a "sufficiently smart" compiler will combine those functions - I imagine C++ compilers will do this since repetition in header files / templates is very common there.)
- jey 6y agoI'm pretty sure that C++ compilers don't do this either. In C++ this is resolved by leaning on the "One Definition Rule" which authorizes the linker to assume a symbol that's defined in multiple translation units is identical in all of them and so it can arbitrarily pick one of them to keep in the output.
- Karliss 6y agoODR and linker removing multiple copies of symbol with the same name and deduplicating function with different names but identical bodies are two different techniques for two different purpose. Modern C++ toolchains will do both. Linker choosing one of the copies for inline function is means of dealing with multiple definition problem, deduplicating identical functions is size optimization. Here an example GCC deduplicating can_zero_in_permitted_state and can_fast_zero_in_permitted_state by inserting jump from one to another https://godbolt.org/z/7xYrxG https://godbolt.org/z/7xYrxG. In the example static was removed as otherwise they would be removed completely.
- throwaway_pdp09 6y agoWith respect to all, I feel this is all missing the point. Expecting a compiler to remove dups just isn't sensible when you can do it yourself so easily on the generation side. Just drop the AST/equivalent data structure into a hash table, with a quick exists check before you add the next one. Those that match, turn that code into a sub-function which all the rest call.
- rwmj 6y agoI think we can all be right. A benefit of generating the code is that this deduplication is much easier than if all the code was hand-written (especially w.r.t keeping the code updated as new API functions get added).