Unexpected memory allocation behavior

I found that not long time ago by asking something similar here (but it is on the performance tips, as I llinked above).

That is a special case of non-specialization: the function is just passed around to another function. Since functions have each their own type, having a specialized method (a whole compilation) for each type of function would be counter-productive. Thus, the default is to not specialize, and that probably boxes the function, creating the allocation. In terms of performance that is probably harmless, unless in very special cases where the function is passed around to another function within a critical loop (and even there, I don’t think it had a measurable performance penalty in my case). But, if one wants the function to specialize to every different type of function input, the parameterization of the type (::F where F syntax) is a way to force that, and it is fine except if the use case expects many different functions to be passed around.

3 Likes