dmd icon indicating copy to clipboard operation
dmd copied to clipboard

Improve optimization of nothrow code

Open dlangBugzillaToGithub opened this issue 11 years ago • 2 comments

Walter Bright (@WalterBright) reported this on 2014-03-16T16:48:55Z

Transferred from https://issues.dlang.org/show_bug.cgi?id=12384

Description

Consider:

----------------------------
import core.stdc.stdlib;

void fun() nothrow;

struct B {
  int* p;
  size_t x;
}

size_t fun4(ref B a) {
   B b;
   scope (exit) free(b.p);
   fun();
   return b.x + a.x;
}
----------------------------

This current sets up an exception handler to call free(b.p), even though no exceptions can be thrown. Instead, the code rewrite should be:

---------------------
size_t fun4(ref B a) {
   B b;
   fun();
   auto tmp = b.x + a.x;
   free(b.p);
   return tmp;
}
-----------------------

This will result in significant performance improvements. Currently, only one such rewrite case is handled in TryFinallyStatement::semantic(). There's more low hanging fruit, such as the return statement case above, that should be handled there.

(scope-statements are rewritten as try-finally-statements.)

dlangBugzillaToGithub avatar Mar 16 '14 16:03 dlangBugzillaToGithub

bugzilla (@WalterBright) commented on 2014-05-04T00:02:09Z

See also https://issues.dlang.org/show_bug.cgi?id=12442

dlangBugzillaToGithub avatar May 04 '14 00:05 dlangBugzillaToGithub

Exception handling has since been reviewed and this is now WONTFIX.

https://forum.dlang.org/thread/[email protected]

rikkimax avatar Sep 17 '25 07:09 rikkimax