By ref and by pointer kills performance.

max haughton maxhaton at gmail.com
Sun Feb 18 13:58:52 UTC 2024


On Wednesday, 14 February 2024 at 04:49:28 UTC, Walter Bright 
wrote:
> On 2/12/2024 7:31 PM, Richard (Rikki) Andrew Cattermole wrote:
>> dmd having bad codegen here isn't a surprise, that is to be 
>> expected.
>
> I'm used to people saying that DMD doesn't do data flow 
> analysis. It does. In fact, it is based on my OptimumC, which 
> was the first C compiler on DOS to do DFA back in the 1980s.
>
> This isn't a case of buggy DFA. It's a case of doing DFA 
> correctly.
>
> The issue is pointer aliasing. A pointer can point to anything, 
> including const data. Therefore, storing through a pointer can 
> alter any value that is reachable via a pointer. Therefore, 
> storing through a pointer invalidates any cached value already 
> read.
>
> This is what you're seeing.
>
> C99 tried to address this with __restrict, but few people use 
> it or understand it. D didn't bother with it because people 
> will inevitably misuse __restrict and get their data 
> mysteriously corrupted.


It's probably true that not many can rattle off the legalese that 
defines restrict semantics correctly (I know I can't, although 
have to say I've never been bitten by it), but within a roughly 
C-like programming model its where a lot of performance comes 
from when at the bleeding edge so I don't know if not including 
it at all is particularly good either.

Thinking about it, it's actually probably mainly an issue for 
library users rather than the author who actually wrote restrict 
to begin with - I have wondered in the past whether some kind of 
`isolated` style type qualifier could actually help here i.e. one 
could provide the compiler good information and make it very 
obvious to the programmer.


More information about the Digitalmars-d mailing list