# Why isn't a simple map type stable?

**URL:** https://discourse.julialang.org/t/why-isnt-a-simple-map-type-stable/67028
**Category:** General Usage
**Created:** [August 26, 2021, 10:13am UTC](https://discourse.julialang.org/t/why-isnt-a-simple-map-type-stable/67028 "2021-08-26T10:13:34Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![robsmith11](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/robsmith11/32/29641_2.png) [@robsmith11](https://discourse.julialang.org/u/robsmith11)
#### Post date: [August 26, 2021, 10:13am UTC](https://discourse.julialang.org/t/why-isnt-a-simple-map-type-stable/67028/1 "2021-08-26T10:13:34Z")

</div>

Shouldn’t Julia be able to infer that the return type is `Vector{Float64}` in the example below? `prevx` is initialized to a `Float64` value and `randn()` returns a `Float64` value.

```julia
julia> function gen1(n)
        prevx = 0.0
        map(1:n) do _
         x = randn()
         r = prevx + x
         prevx = x
         r
        end
       end
gen1 (generic function with 1 method)

julia> @code_warntype gen1(10)
MethodInstance for gen1(::Int64)
  from gen1(n) in Main at REPL[44]:1
Arguments
  #self#::Core.Const(gen1)
  n::Int64
Locals
  #25::var"#25#26"
  prevx::Core.Box
Body::Vector
1 ─ (prevx = Core.Box())
│ Core.setfield!(prevx, :contents, 0.0)
│ (#25 = %new(Main.:(var"#25#26"), prevx))
│ %4 = #25::var"#25#26"
│ %5 = (1:n)::Core.PartialStruct(UnitRange{Int64}, Any[Core.Const(1), Int64])
│ %6 = Main.map(%4, %5)::Vector
└── return %6

```

---

<div class="post-metadata">

### Author: ![pereiragc](https://avatars.discourse-cdn.com/v4/letter/p/d78d45/32.png) [@pereiragc](https://discourse.julialang.org/u/pereiragc)
#### Post date: [August 26, 2021, 11:57am UTC](https://discourse.julialang.org/t/why-isnt-a-simple-map-type-stable/67028/2 "2021-08-26T11:57:31Z")

</div>

I believe that is the topic of the following section: [performance of captured variables](https://docs.julialang.org/en/v1/manual/performance-tips/#man-performance-captured-1).

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [August 26, 2021, 12:11pm UTC](https://discourse.julialang.org/t/why-isnt-a-simple-map-type-stable/67028/3 "2021-08-26T12:11:33Z")

</div>

yeah this is because you’re modifying that `prevx` so it needs to be captured, better write it like this:

```julia
julia> function gen2(n)
           prevx = 0.0
           res = Vector{Float64}(undef, n)
           for i in eachindex(res)
               x = randn()
               res[i] = prevx + x
               prevx = x
           end
           res
       end

julia> @benchmark gen1(100)
BenchmarkTools.Trial: 10000 samples with 9 evaluations.
 Range (min … max): 2.452 μs … 113.209 μs ┊ GC (min … max): 0.00% … 95.54%
 Time (median): 3.432 μs ┊ GC (median): 0.00%
 Time (mean ± σ): 3.565 μs ± 3.399 μs ┊ GC (mean ± σ): 2.95% ± 3.02%

  ▃ █
  █▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁ █
  3.25 μs Histogram: frequency by time 3.36 μs <

 Memory estimate: 5.67 KiB, allocs estimate: 305.

julia> @benchmark gen2(100)
BenchmarkTools.Trial: 10000 samples with 434 evaluations.
 Range (min … max): 254.009 ns … 1.995 μs ┊ GC (min … max): 0.00% … 72.86%
 Time (median): 309.528 ns ┊ GC (median): 0.00%
 Time (mean ± σ): 332.448 ns ± 139.497 ns ┊ GC (mean ± σ): 3.45% ± 6.93%

  █                                                              
  █▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁ ▁▁▁▁▁▁▁▁▁▁▁▁ ▁▁▁▁▁▁▁▁▁▁▁▁▁▁ ▁
  475 ns Histogram: frequency by time 265 ns <

 Memory estimate: 896 bytes, allocs estimate: 1.

```
