4

Let say I have this code:

void test(){
 assert(() {
   print("This is Test");
 });
}

As per this question, dart will remove assert on production build

but how about test() function which being called?

will this function be removed on build?

or will this have any significant impact on performance if I call empty function multiple times?

selharxets
  • 143
  • 4

2 Answers2

5

The compiler will optimize your code through inlining and removing calls to empty functions.

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • Thanks! could you add any references to this? – selharxets Nov 12 '18 at 06:26
  • 1
    I don't have a reference but that's basic compiler optimization and I saw it mentioned in Dart GitHub issues. – Günter Zöchbauer Nov 12 '18 at 06:28
  • I thought so too, but I was quite unsure as I haven't found any references related to Dart. Will mark this as accepted answer for now. I'll be glad if anyone could add references to this – selharxets Nov 12 '18 at 06:50
  • 1
    There are plans to improve docs and tools but not there yet https://github.com/dart-lang/sdk/issues/33920#issuecomment-430211662 – Günter Zöchbauer Nov 12 '18 at 06:52
  • 1
    This video might provide some insight (haven't watched it myself yet) https://youtu.be/WjdrUphF5l4 but Vyacheslav did several talks about Dart compilation and optimizations already). – Günter Zöchbauer Nov 12 '18 at 07:06
2

will this function be removed on build?

Not unless you use it only within other asserts. A typical example would be this:

assert(() {
  test();
  return true;
}());

If you only use it this way, then yes the function will be removed on build.

Rémi Rousselet
  • 256,336
  • 79
  • 519
  • 432