Some people use ref for performance to prevent the copying that must occur when passing by value. I propose a small optimisation to make this unnecessary in a bunch of cases.

At the ABI level (no change in language semantics), if a given parameter would otherwise be copied and passed on the stack:
1) immutable parameters to all functions are passed by reference
2) const parameters to pure functions are passed by reference, if and only if all other parameters - including any implicit this parameter - contain no mutable references (otherwise "weakly" pure functions could modify the data we are now referring to instead of having copied, changing semantics).

The main benefit from this would be:

class C
{
  void doSomething(const float[16] m0) const pure
  {
    // ...
  }

  void doSomethingElse(immutable float[16] m1)
  {
    // ...
  }
}

void foo(C c, float[16] m0, immutable float[16] m1)
{
  // ...
  c.doSomething(m0); // no copying of m0 here
  c.doSomethingElse(m1); // or m1 here
  // ...
}

I presume this has been thought of before, is there a reason why we don't do it already? I guess 2) is a bit complicated, but 1) is easy.

Reply via email to