<div>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).</div><div><br></div><div>I was wondering what would be the cost of doing this, so ran a simple test, please see:</div>
<a href="https://github.com/timotheecour/dtools/blob/master/dtools/scratch/test1.d">https://github.com/timotheecour/dtools/blob/master/dtools/scratch/test1.d</a><div><br></div><div>(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).</div>
<div><br></div><div>After running:<br><div>ldc2 -release -O2 -run dtools/scratch/test1.d</div><div><div>I get:</div><div>reltime=20.7386(%); time(base)=2333 time(t2)=2817</div><div><br></div></div></div><div>Which means that the extra check to make sure the output reference doesn't escape a local incurs a 20% cost in my example.</div>
<div><br></div><div>Is there a better implementation (i have a single pointer comparison though so I'm not sure how that would be optimized) ?</div><div><br></div><div>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.</div>
<div><br></div><div>As an alternative, I have proposed a simple mechanism for safe references without runtime chesks (<a href="http://wiki.dlang.org/DIP38">http://wiki.dlang.org/DIP38</a>), which will statically enforce ref safety.</div>
<div><br></div><div><br></div><div><br></div>