Many thanks to all of you answered. I learned a lot.
There are two issues I stumbled:
(I) The vast memory allocations of my naive test function:
function stupidtest(f)
for x=1. : 1.e8
f(x)
end
end
@time(stupidtest(sqrt))
1.701242 seconds (200.00 M allocations: 2.980 GB, 5.49% gc time)
As Milan pointed out, passing functions as an argument and then calling
them massively leads to unexpected memory allocations.
The performance tips doc suggests checking for type problems:
@code_warntype(stupidtest(sqrt))
...
(f::F)(x::Int64)::Any <== the '::Any' part is red
...
My reading of this is this: the compiler can't interfere
the return type of the function argument and the user can't
(currently?) declare it. Right?
(II) My other question: constant folding in expressions like
f(x)=x+3.*sin(.3)
Yichao Yu pointed to issue #9942, whose opening
post is about constant folding in loops like
for i=1:n; x+=sin(.34); end
So I will watch this issue - and next time I stumble upon something,
I will search through the issues. Seems like a great place to dig
deeper into the state of the language. There are proposals
(#414, #13555) about pure function annotation which are relevant here.
Best wishes, Meik