Why is the function of a callable object (Functor) allocating?

I’m experimenting with functors. I noticed that when I pass an array to the functor, that it is allocating memory even if it should be possible without memory allocation.
Here is a simplified version:

struct Foo end
(::Foo)(x) = sum(x)
foo = Foo()
a = randn(10)
@btime foo($a) # 19.765 ns (1 allocation: 16 bytes)
@btime sum($a) # 5.792 ns (0 allocations: 0 bytes)

Why is foo($a) allocating memory?

In case that it matters: I’m using Julia 1.5.3

Because your foo is not const. So use a $ on in too:

julia> @btime $foo($a)
  3.701 ns (0 allocations: 0 bytes)
4.349093588060725
2 Likes