On 8 April 2013 13:25, Jacob Carlborg <doob@me.com> wrote:
On 2013-04-08 10:29, Iain Buclaw wrote:

This information could possibly be helpful.  Though given that most of
(gdc) codegen is on par with g++, there's probably not much on the list
that isn't already detected by the backend optimisation passes.

Multiple calls to pure functions could be cached.

--
/Jacob Carlborg

Not always, but in some circumstances, yes.

---
struct Foo
{
  int a = 0;
  pure int bar (immutable int x)
  {
    ++a;
    return x * 2;
  }
}


void main()
{
  Foo f;
  int i = f.bar(2) + f.bar(2);

  assert (i == 8);
  assert (f.a == 2);
}
---


Again, the characteristics of D pure functions mean that the backend can const-fold away the function entirely under normal optimisations.

---
D main ()
{
  <bb 2>:
  # DEBUG f.a => 0
  # DEBUG f.a => 1
  # DEBUG f.a => 2
  # DEBUG i => 8
  return 0;
}
---


Possibly strongly-pure functions could be marked as pure in the 'C' sense, but only if they are also nothrow, eg to add to the above example:

---
pure nothrow int baz (Foo f)
{
  return f.bar(2) + f.bar(2);
}
---


Regards
--
Iain Buclaw

*(p < e ? p++ : p) = (c & 0x0f) + '0';