I am considering writing a function
foo!(i1,i2,o1,o2,o3)
where i1
etc. are inputs and o1
etc. are outputs. Importantly: all the outputs are preallocated (mutable) vectors, and the results are to written in place into these vectors.
I would like to make it possible to call foo!
to request only some of the ouputs, but without clutering the code of foo
with either multiple methods, or if..then
s, but with full performance gain from not doing unnecessary computations. Did someone say “we are greedy”?
What I could do is create a new datatype NotWanted
with no content. For this type I implement SetIndex!
to be an inline no-op function. I would then pass an object of this type as argument when the results are… not wanted.
What I wonder is, can the compiler (and what must I do to help it) cut out from the method instance, all the operations in foo!
made unprofitable by the data being thrown away?
function foo!(i1,i2,o1,o2,o3)
o1[:] = 2.*i2
tmp = 3*i1 # data going nowhere, please do not compile
o2[:] .= tmp .+ o1 # "
o3 = ...
return
end
o1 = Vector{Float64}(undef,2)
o2 = NotWanted()
o3 = ...
foo(rand(2),rand(2),o1,o2,o3
@show o1,o3