# Why does 'map' recompile each time?

**URL:** <https://discourse.julialang.org/t/why-does-map-recompile-each-time/71149>\
**Category:** Performance\
**Created:** [November 8, 2021, 1:32pm UTC](https://discourse.julialang.org/t/why-does-map-recompile-each-time/71149 "2021-11-08T13:32:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Iulian.Cioarca](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iulian.cioarca/32/30166_2.png) [@Iulian.Cioarca](https://discourse.julialang.org/u/Iulian.Cioarca)\
**Post date:** [November 8, 2021, 1:32pm UTC](https://discourse.julialang.org/t/why-does-map-recompile-each-time/71149/1 "2021-11-08T13:32:21Z")

</div>

Consider the follosing example:

```julia
y = [1 2 3]
@time findall(map( x-> x.== 3, y))
 0.077566 seconds (69.12 k allocations: 4.160 MiB, 13.08% gc time, 99.57% compilation time)
1-element Vector{CartesianIndex{2}}:
 CartesianIndex(1, 3)

```

Why is the function recompiled each time I call it?

---

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [November 8, 2021, 1:38pm UTC](https://discourse.julialang.org/t/why-does-map-recompile-each-time/71149/2 "2021-11-08T13:38:11Z")

</div>

Because you’re defining a new anonymous function for each invocation.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [November 8, 2021, 1:41pm UTC](https://discourse.julialang.org/t/why-does-map-recompile-each-time/71149/3 "2021-11-08T13:41:33Z")

</div>

To see this:

```julia
julia> f = x -> x .== 3
#57 (generic function with 1 method)

julia> @time findall(map(f, y))
  0.030139 seconds (72.98 k allocations: 4.008 MiB, 98.57% compilation time)
1-element Vector{CartesianIndex{2}}:
 CartesianIndex(1, 3)

julia> @time findall(map(f, y))
  0.000016 seconds (3 allocations: 160 bytes)
1-element Vector{CartesianIndex{2}}:
 CartesianIndex(1, 3)

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [November 8, 2021, 2:42pm UTC](https://discourse.julialang.org/t/why-does-map-recompile-each-time/71149/4 "2021-11-08T14:42:47Z")

</div>

> [@Iulian.Cioarca](#):
>
> ```julia
> findall(map( x-> x.== 3, y))
> 
> ```

The dot in `.==` is for broadcasting, it ‘maps’ the operator over its input collection. So it’s redundant here, the argument of `map` is also mapped over the collection. Therefore, you should drop it: `map( x->x==3, y)`, not `map( x-> x.== 3, y)`.

Furthermore, `findall` takes a ‘mapped’ function as its first argument, too, so you can just do this instead:

```julia
findall( x->x==3, y)

```

Or, more tersely,

```julia
findall(==(3), y)

```

No reason to use `findall`, `map` _and_ broadcast, when just `findall` will do.

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [November 8, 2021, 4:10pm UTC](https://discourse.julialang.org/t/why-does-map-recompile-each-time/71149/5 "2021-11-08T16:10:44Z")

</div>

Note that if it’s inside a function, a closure will only be compiled once:

```julia
julia> y = [1, 2, 3];

julia> @time findall(x -> x == 3, y);
  0.038798 seconds (79.31 k allocations: 4.512 MiB, 99.87% compilation time)

julia> foo(y) = findall(x -> x == 3, y);

julia> @time foo(y);
  0.037377 seconds (80.87 k allocations: 4.604 MiB, 99.96% compilation time)

julia> @time foo(y);
  0.000003 seconds (1 allocation: 80 bytes)

```
