@safe and null dereferencing

Steven Schveighoffer via Digitalmars-d digitalmars-d at puremagic.com
Thu Jul 27 11:09:21 PDT 2017


On 7/27/17 1:33 PM, Adrian Matoga wrote:
> 
> Why can't we just make the compiler insert null checks in @safe code? We 
> can afford bounds checking even in @system -O -release. C++ can afford 
> null check upon executing an std::function. The pointer would most 
> likely be in a register anyway, and the conditional branch would almost 
> always not be taken, so the cost of that check would be barely 
> measurable. Moreover, the compiler can elide the check e.g. if the 
> access via pointer is made in a loop in which the pointer doesn't 
> change. And if you prove that this tiny little check ruins performance 
> of your code, there's @trusted to help you.

The rationale from Walter has always been that the hardware is already 
doing this for us. I was always under the assumption that D only 
supported environments/systems where this happens. But technically 
there's nothing in the spec to require it. And it does seem apparent 
that we handle this situation.

This question/query is asking whether we should amend the spec with 
(what I think is) Walter's view, or if we should change the compiler to 
insert the checks.

-Steve


More information about the Digitalmars-d mailing list