# Special casing small integers known at compile time

**URL:** <https://discourse.julialang.org/t/special-casing-small-integers-known-at-compile-time/69934>\
**Category:** Performance\
**Created:** [October 17, 2021, 3:12pm UTC](https://discourse.julialang.org/t/special-casing-small-integers-known-at-compile-time/69934 "2021-10-17T15:12:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lilith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lilith/32/27492_2.png) [@Lilith](https://discourse.julialang.org/u/Lilith)\
**Post date:** [October 17, 2021, 3:12pm UTC](https://discourse.julialang.org/t/special-casing-small-integers-known-at-compile-time/69934/1 "2021-10-17T15:12:24Z")

</div>

I see this sort of code sometimes and it makes me cringe

```julia
similar(a::Array{T,1}) where {T} = Vector{T}(undef, size(a,1))
similar(a::Array{T,2}) where {T} = Matrix{T}(undef, size(a,1), size(a,2))
similar(a::Array{T,1}, S::Type) where {T} = Vector{S}(undef, size(a,1))
similar(a::Array{T,2}, S::Type) where {T} = Matrix{S}(undef, size(a,1), size(a,2))

```

[https://github.com/JuliaLang/julia/blob/0ea61fa153c69e052097f1307dafbedd00049aae/base/array.jl#L373](https://github.com/JuliaLang/julia/blob/0ea61fa153c69e052097f1307dafbedd00049aae/base/array.jl#L373)  
Because we already have this

```julia
similar(a::AbstractArray{T}) where {T} = similar(a, T)
similar(a::AbstractArray, ::Type{T}) where {T} = similar(a, T, to_shape(axes(a)))
similar(a::AbstractArray{T}, dims::Tuple) where {T} = similar(a, T, to_shape(dims))
similar(a::AbstractArray{T}, dims::DimOrInd...) where {T} = similar(a, T, to_shape(dims))
similar(a::AbstractArray, ::Type{T}, dims::DimOrInd...) where {T} = similar(a, T, to_shape(dims))

```

[https://github.com/JuliaLang/julia/blob/0ea61fa153c69e052097f1307dafbedd00049aae/base/abstractarray.jl#L781](https://github.com/JuliaLang/julia/blob/0ea61fa153c69e052097f1307dafbedd00049aae/base/abstractarray.jl#L781)  
I believe that in this case it is written to special case low low numbers of arguments to avoid packing and unpacking into an `NTuple`, but I’m not sure. Is this necessary for maximum performance? Is it possible to delegate those special casings to the compiler whenever the `NTuple`’s `N` is known at compile time (almost always)? Does the compiler already do this?

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [October 17, 2021, 3:47pm UTC](https://discourse.julialang.org/t/special-casing-small-integers-known-at-compile-time/69934/2 "2021-10-17T15:47:58Z")

</div>

I believe this is one where due to bootstrap order, we don’t have those methods yet. in other words, I don’t think this is necessary outside Base.

---

<div class="post-metadata">

**Author:** ![Lilith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lilith/32/27492_2.png) [@Lilith](https://discourse.julialang.org/u/Lilith)\
**Post date:** [October 17, 2021, 4:43pm UTC](https://discourse.julialang.org/t/special-casing-small-integers-known-at-compile-time/69934/3 "2021-10-17T16:43:15Z")

</div>

Thanks! So then `Arrays.jl` is bootstrapped and `similar` is needed before `AbstractArrays.jl` is bootstrapped. What is the bootstrap order?

---

<div class="post-metadata">

**Author:** ![WschW](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wschw/32/6575_2.png) [@WschW](https://discourse.julialang.org/u/WschW)\
**Post date:** [October 17, 2021, 4:51pm UTC](https://discourse.julialang.org/t/special-casing-small-integers-known-at-compile-time/69934/4 "2021-10-17T16:51:16Z")

</div>

The bootstrap order looks to be in this file:

[https://github.com/JuliaLang/julia/blob/0ea61fa153c69e052097f1307dafbedd00049aae/base/Base.jl](https://github.com/JuliaLang/julia/blob/0ea61fa153c69e052097f1307dafbedd00049aae/base/Base.jl)

---

<div class="post-metadata">

**Author:** ![Lilith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lilith/32/27492_2.png) [@Lilith](https://discourse.julialang.org/u/Lilith)\
**Post date:** [October 17, 2021, 6:35pm UTC](https://discourse.julialang.org/t/special-casing-small-integers-known-at-compile-time/69934/5 "2021-10-17T18:35:21Z")

</div>

Thanks! And indeed `array.jl` is imported before `abstractarray.jl`  
[https://github.com/JuliaLang/julia/blob/0ea61fa153c69e052097f1307dafbedd00049aae/base/Base.jl#L128](https://github.com/JuliaLang/julia/blob/0ea61fa153c69e052097f1307dafbedd00049aae/base/Base.jl#L128)
