# Different @code\_warntype behavior depending on number of fileds in 'struct'

**URL:** <https://discourse.julialang.org/t/different-code-warntype-behavior-depending-on-number-of-fileds-in-struct/65259>\
**Category:** General Usage\
**Tags:** code\_warntype, type-stability\
**Created:** [July 25, 2021, 10:49am UTC](https://discourse.julialang.org/t/different-code-warntype-behavior-depending-on-number-of-fileds-in-struct/65259 "2021-07-25T10:49:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sidelkin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sidelkin/32/43911_2.png) [@sidelkin](https://discourse.julialang.org/u/sidelkin)\
**Post date:** [July 25, 2021, 10:49am UTC](https://discourse.julialang.org/t/different-code-warntype-behavior-depending-on-number-of-fileds-in-struct/65259/1 "2021-07-25T10:49:43Z")

</div>

Hello.  
When the number of fields in a struct is less than 15, @code\_warntype diagnoses that the function is ‘type-stable’. But as their number grows, the function becomes ‘type-unstable’ from @code\_warntype’s point of view. Looks like a bug?

```julia
struct struct15; a1; a2; a3; a4; a5; a6; a7; a8; a9; a10; a11; a12; a13; a14; a15; end
struct struct16; a1; a2; a3; a4; a5; a6; a7; a8; a9; a10; a11; a12; a13; a14; a15; a16; end

function arrayofstruct15(args)      
      mystructs = map(zip(args...)) do x
            struct15(x...)
      end
end
function arrayofstruct16(args)      
      mystructs = map(zip(args...)) do x
            struct16(x...)
      end
end

args = ([1, 2], [1, 2], [1, 2], [1, 2], [1, 2], [1, 2], 
        [1, 2], [1, 2], [1, 2], [1, 2], [1, 2], [1, 2], 
        [1, 2], [1, 2], [1, 2], [1, 2],)

s15 = arrayofstruct15(args[1:end-1])
s16 = arrayofstruct16(args)
@code_warntype arrayofstruct15(args[1:end-1])
@code_warntype arrayofstruct16(args)

```

Results

```julia
Variables
  #self#::Core.Const(arrayofstruct15)
  args::NTuple{15, Vector{Int64}}
  #5::var"#5#6"
  mystructs::Vector{struct15}    

Body::Vector{struct15}
1 ─ (#5 = %new(Main.:(var"#5#6")))
│ %2 = #5::Core.Const(var"#5#6"())
│ %3 = Core._apply_iterate(Base.iterate, Main.zip, args)::Base.Iterators.Zip{NTuple{15, Vector{Int64}}}
│ %4 = Main.map(%2, %3)::Vector{struct15}
│ (mystructs = %4)
└── return %4
Variables
  #self#::Core.Const(arrayofstruct16)  
  args::NTuple{16, Vector{Int64}}      
  #7::var"#7#8"
  mystructs::Any

Body::Any
1 ─ (#7 = %new(Main.:(var"#7#8")))
│ %2 = #7::Core.Const(var"#7#8"())   
│ %3 = Core._apply_iterate(Base.iterate, Main.zip, args)::Base.Iterators.Zip{NTuple{16, Vector{Int64}}}
│ %4 = Main.map(%2, %3)::Any
│ (mystructs = %4)
└── return %4

```

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [July 25, 2021, 11:24am UTC](https://discourse.julialang.org/t/different-code-warntype-behavior-depending-on-number-of-fileds-in-struct/65259/2 "2021-07-25T11:24:33Z")

</div>

This is due to a heuristic in `map` here:

[https://github.com/JuliaLang/julia/blob/7c45ff0e94d394911e26d851d33a611d4ac256a6/base/tuple.jl#L222](https://github.com/JuliaLang/julia/blob/7c45ff0e94d394911e26d851d33a611d4ac256a6/base/tuple.jl#L222)

which is there to avoid overspecialization. Note that this has been increased to 32 in 1.7, so your example should be type stable in the next Julia release.

---

<div class="post-metadata">

**Author:** ![sidelkin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sidelkin/32/43911_2.png) [@sidelkin](https://discourse.julialang.org/u/sidelkin)\
**Post date:** [July 25, 2021, 3:54pm UTC](https://discourse.julialang.org/t/different-code-warntype-behavior-depending-on-number-of-fileds-in-struct/65259/3 "2021-07-25T15:54:34Z")

</div>

OK, thanks.
