Do `borrowing` parameters cause extra pointers?

I've heard borrowing parameters described as const T& or const T* as analogies to C/C++.

I'm trying to understand which it actual is, or more to the point when an extra pointer might get involved, that wouldn't be there otherwise (vs an unadorned parameter).

I can see from the compiler's output that at least for trivial types (e.g. Int) borrowing / consuming have absolutely no effect on the parameter (nor the code inside the function) - it's still passed in a register (or spilled onto the stack), i.e. by value.

Is that always the case, for value types; the calling convention is unchanged?

And for reference types, where the value passed in a register is a pointer, is it similarly always the case that the borrowing / consuming version is still just that same pointer? As opposed to a pointer to that pointer?

I mean, the answer seems like it should be really obviously "yes" to both questions, but, I just want to check. Value types can get really complicated.

Yes, borrowing/consuming do not affect the way a type is passed to a function. The compiler can optimize some retains/releases for classes with these conventions, but what gets passed is still a single pointer to the heap allocation. The only exception (I think) are types like Atomic and Mutex who are always passed by pointer with borrowing.

3 Likes

Surely they passed indirectly when consuming as well.

2 Likes

They might be (not at a computer at the moment) since they’re address only

1 Like