December 18, 2018
On Tuesday, December 18, 2018 5:42:12 AM MST rikki cattermole via Digitalmars-d-learn wrote:
> On 19/12/2018 1:34 AM, Per Nordlöw wrote:
> > On Tuesday, 18 December 2018 at 10:42:51 UTC, Jonathan M Davis wrote:
> >> Unfortunately, D does not currently have a way to do that. Only functions can be marked with @trusted. However, the typical approach to this problem is to use a lambda, which is more or less syntactically the same except that it has some extra parens. e.g.
> >>
> >> () @trusted { doUnsafe(); }();
> >
> > Is there a performance hit when using this?
>
> Yes except for ldc with -O3.
> But it is a function not a delegate so it should be all nicely
> prefetched and ready to go. So I wouldn't worry about it too much.

Really? I would have thought that that would be a pretty obvious optimization (especially if inlining is enabled). I suppose that it doesn't entirely surprise me if dmd does a poor job of it, but I would have expected ldc to do better than that. I would think that such an improvement would be pretty low-hanging fruit for adding to dmd's optimizer though. If not, it sounds like further justification for adding a feature for this rather than having to use a lambda. Aside from that though, the lambda trick is simple enough that it wouldn't surprise me if Walter and Andrei's response to such a DIP would be to just use the lambda trick.

- Jonathan M Davis




December 19, 2018
On Tuesday, 18 December 2018 at 12:42:12 UTC, rikki cattermole wrote:
> Yes except for ldc with -O3.

ldc with -O2 generates the same code.
December 19, 2018
On Wednesday, 19 December 2018 at 06:11:48 UTC, Jonathan M Davis wrote:
> Really? I would have thought that that would be a pretty obvious optimization (especially if inlining is enabled). I suppose that it doesn't entirely surprise me if dmd does a poor job of it, but I would have expected ldc to do better than that. I would think that such an improvement would be pretty low-hanging fruit for adding to dmd's optimizer though.

AFAIK, it was added.
December 19, 2018
On 19/12/2018 7:11 PM, Jonathan M Davis wrote:
> Really? I would have thought that that would be a pretty obvious
> optimization (especially if inlining is enabled).

Assembly doesn't lie.
December 19, 2018
On Tuesday, 18 December 2018 at 13:52:29 UTC, Steven Schveighoffer wrote:
> On 12/18/18 6:29 AM, Simen Kjærås wrote:
>> @safe unittest {
>>      unsafe!({
>>          fun(2);
>>      });
>> 
>>      unsafe!fun(2);
>> }
>
> Wow, I really like this. The only real problem is that one generally searches for @trusted when looking for unsafe code, but unsafe is pretty obvious too. Also, args should be auto ref.

While @trusted wouldn't work, it could be named 'trusted' instead of 'unsafe'. That's not gonna help those who search for '@trusted', but it's closer, at least.

--
  Simen
December 19, 2018
On Wednesday, December 19, 2018 1:19:42 AM MST rikki cattermole via Digitalmars-d-learn wrote:
> On 19/12/2018 7:11 PM, Jonathan M Davis wrote:
> > Really? I would have thought that that would be a pretty obvious optimization (especially if inlining is enabled).
>
> Assembly doesn't lie.

I'm not saying that you're wrong, just expressing surprise. I've never checked to see what code actually got generated. But I do think that it's something that the compiler should be smart enough to do even if it isn't currently that smart, and the fact that it isn't is most defintely not a good thing.

- Jonathan M Davis



December 20, 2018
On 20/12/2018 5:02 AM, Jonathan M Davis wrote:
> On Wednesday, December 19, 2018 1:19:42 AM MST rikki cattermole via
> Digitalmars-d-learn wrote:
>> On 19/12/2018 7:11 PM, Jonathan M Davis wrote:
>>> Really? I would have thought that that would be a pretty obvious
>>> optimization (especially if inlining is enabled).
>>
>> Assembly doesn't lie.
> 
> I'm not saying that you're wrong, just expressing surprise. I've never
> checked to see what code actually got generated. But I do think that it's
> something that the compiler should be smart enough to do even if it isn't
> currently that smart, and the fact that it isn't is most defintely not a
> good thing.
> 
> - Jonathan M Davis

I agree, this would be a very simple AST rewrite. Would be well worth doing since its a common-ish pattern.
1 2
Next ›   Last »