By ref and by pointer kills performance.
Bruce Carneal
bcarneal at gmail.com
Tue Feb 13 06:02:47 UTC 2024
On Tuesday, 13 February 2024 at 02:11:45 UTC, claptrap wrote:
> I was refactoring some code and changed a parameter from by
> value, to by pointer, and saw the performance drop by 50%. This
> is a highly reduced example of what I found, but basically
> passing something into a function by reference or pointer seems
> to make the compilers (it affects both DMD and LDC) treat it as
> if its volatile and must be loaded from memory on every use.
> This also inhibits the auto-vectorization of code by LDC.
>
> https://d.godbolt.org/z/oonq1drd9
>
> ...
>
> I'm not a compiler guy so maybe there's some rationale for this
> that I don't know but it seems like the compiler should be able
> to read "*value" once and cache it.
To reuse the value the compiler would have to prove that the
memory locations do not overlap. FORTRAN does not have this
problem, neither does ldc once you take responsibility for
non-overlap with the @restrict attribute as seen here:
https://d.godbolt.org/z/z9vYndWqP
When loops are involved between potentially overlapping indexed
arrays I've seen ldc go through the proof and do two versions of
the code with a branch.
More information about the Digitalmars-d
mailing list