It has been proposed to introduce runtime bounds checking to insure ref safety (avoiding escaping references to local variables), enabled with a -checkboundsref flag (or similar).

I was wondering what would be the cost of doing this, so ran a simple test, please see:
https://github.com/timotheecour/dtools/blob/master/dtools/scratch/test1.d

(note, I'm not claiming the code is correct (stack could go up or down) just that any mechanism for runtime ref safety check should bear the same cost at least).

After running:
ldc2 -release -O2 -run dtools/scratch/test1.d
I get:
reltime=20.7386(%); time(base)=2333 time(t2)=2817

Which means that the extra check to make sure the output reference doesn't escape a local incurs a 20% cost in my example.

Is there a better implementation (i have a single pointer comparison though so I'm not sure how that would be optimized) ?

If not, it will mean user will have to choose between enabling runtime safety checks (20% in my simple example), or throwing away ref safety by disabling checkboundsref flag.

As an alternative, I have proposed a simple mechanism for safe references without runtime chesks (http://wiki.dlang.org/DIP38), which will statically enforce ref safety.