Re:RISC-V J extension – Instructions for JITs
by mhh__ on Saturday March 12, 2022 @05:22AM
(#30650400)
Even ignoring the usual optimizations like using SIMD and loop unrolling to find parallelism when doing memcpy, the compiler actually has techniques for spotting certain loop idioms so it can actually replace the loop with a memcpy library call if it deems it profitable (e.g. tell it it's likely to have N>bigNumber and it'll go for a library)
Re:RISC-V J extension – Instructions for JITs
by cbm-vic-20 on Saturday March 12, 2022 @10:50AM
(#30652158)
There are additional optimizations like using C's printf without any extra arguments, the compiler will replace that with a call to puts, which doesn't have the formatting code. You can see this in Compiler Explorer.
Re:RISC-V J extension – Instructions for JITs
by ghusbands on Saturday March 12, 2022 @08:04AM
(#30651070)
Quite often, that doesn't end up very efficient, because without "restrict", the result has to be identical to what it would be if it was copied byte by byte, for all possible overlaps of the two inputs.
Re:RISC-V J extension – Instructions for JITs
by Sprite_tm on Saturday March 12, 2022 @09:09PM
(#30657195)
Lots of memcpy() implementations are still more efficient than a dumb byte-by-byte copy. They'll copy the (unaligned) head and the tail in bytes, but the bulk of the data using whatever data type and method is fastest.
The interesting thing is, I turned on compiler optimisations. When I examined the assembled output (even though my knowledge of assembly is poor), I discovered that it had made the optimisations that you would find in a more complex C implementation. The compiler obviously thought to itself "I see what you're doing here", and put in a better version.
So the moral of the story is: your compiler is likely to be able to figure out a lot.