#By-value and by-pointer API dichotomy

1 messages · Page 1 of 1 (latest)

glossy nebula
#

Does this API make sense? Specifically, having two functions one of which accepts T by value and one of which accepts T by pointer. Since T is generic, it might be efficient to pass T by value or it might be more efficient to pass it by pointer, depending on @sizeOf(T). Or is there some magic optimization that makes setPtr unnecessary?

pub fn set(many_ptr: @This(), index: usize, value: T) void {
    many_ptr.backing[index] = value;
}

pub fn setPtr(many_ptr: @This(), index: usize, value_ptr: *const T) void {
    many_ptr.backing[index] = value_ptr.*,
}
crude oyster
#

best to always pass by value and let the compiler decide for you

glossy nebula
#

That's kind of the question.

crude oyster
#

under the hood, the compiler will see if T is too big to be passed by value or not, if it deems T being too big, it will pass by a pointer to constant implicitly

glossy nebula
#

Is that the PRO (Parameter Reference Optimization)?

crude oyster
#

yes

glossy nebula
#

Isn't it going away?

crude oyster
#

it will be reworked, won't go away

glossy nebula
#

Aha, interesting.