How can I make a wrapped function type that does not allocate on field access? In my actual code, there is a whole collection of “extras” with diverse abstract types, so I think it’s not great to try and parametrize them all?
@Sukera I think it’s the field access, because otherwise fbare should also allocate, no?
@BdeKoning To me that reads like it ensures type stability in presence of callable fields. From another thread I picked up the technique to parametrize the struct on the type of the function, FunctionWithExtra{A} for example, which gives a concrete type to the struct. This works to avoid alloction with FunctionBare, for example. Also,
It’s not the field access, @Sukera is correct. fwe is a non-constant global variable, so calling it allocates.
julia> struct FunctionWithExtra{A}
f::A
extra
end
julia> fwe = FunctionWithExtra(x -> (x*x; nothing), "some other info");
julia> fwe.f(1.0); # get complation out of the way
julia> @allocated fwe.f(1.0)
16
julia> let f = fwe.f
@allocated f(3.0)
end
0
The fbare version doesn’t allocate because fbare is an isbits type.