# @inbounds for getindex not working

**URL:** https://discourse.julialang.org/t/inbounds-for-getindex-not-working/118631
**Category:** Performance
**Tags:** question, array, inbounds
**Created:** [August 26, 2024, 3:58pm UTC](https://discourse.julialang.org/t/inbounds-for-getindex-not-working/118631 "2024-08-26T15:58:53Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Andy\_Zhang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andy_zhang/32/210182_2.png) [@Andy\_Zhang](https://discourse.julialang.org/u/Andy_Zhang)
#### Post date: [August 26, 2024, 3:58pm UTC](https://discourse.julialang.org/t/inbounds-for-getindex-not-working/118631/1 "2024-08-26T15:58:53Z")

</div>

Here is the example code and output.

```julia
struct Minij{T<:Number} <: AbstractMatrix{T}
    n::Integer

    function Minij{T}(n::Integer) where {T<:Number}
        n >= 0 || throw(ArgumentError("$n < 0"))
        return new{T}(n)
    end
end

Minij(n::Integer) = Minij{Int}(n)

Base.size(A::Minij) = (A.n, A.n)

@inline Base.@propagate_inbounds function Base.getindex(A::Minij{T}, i::Integer, j::Integer) where {T}
    @boundscheck checkbounds(A, i, j)
    return T(min(i, j))
end

@inline function g(A, i)
    @boundscheck checkbounds(A, i)
    println("accessing ($A)[$i]")
end

a = Minij(3)

for i = 1:1
    g(1:2, 1)
    @inbounds g(1:2, -1)
    a[2, 2]
    @inbounds a[2, -1]
end

```

```julia
accessing (1:2)[1]
accessing (1:2)[-1]
ERROR: LoadError: BoundsError: attempt to access 3×3 Minij{Int64} at index [2, -1]
Stacktrace:
 [1] throw_boundserror(A::Minij{Int64}, I::Tuple{Int64, Int64})
   @ Base .\abstractarray.jl:737
 [2] checkbounds
   @ .\abstractarray.jl:702 [inlined]
 [3] getindex(A::Minij{Int64}, i::Int64, j::Int64)
   @ Main ...\perf.jl:17
 [4] top-level scope
   @ ...\perf.jl:32
in expression starting at D:\Projects\TypedMatrices\devtests\perf.jl:28

```

My question is: @inbounds works for function `g`, not for function `getindex`. It should removes the @boundscheck block in `getindex` funciton, but actually not.

---

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [August 26, 2024, 4:08pm UTC](https://discourse.julialang.org/t/inbounds-for-getindex-not-working/118631/2 "2024-08-26T16:08:43Z")

</div>

Yes, `@inbounds` only works in a compiled context with type-stable values. It’s an optional optimization (that can be explicitly ignored), not a semantic guarantee.

I did a rundown of this in London long ago, and these are all still true, I think:

[![](https://global.discourse-cdn.com/julialang/original/3X/0/6/06b71d551368fc5bd6fdf3b9541952ba9c96799b.jpeg "An Introduction to High Performance Custom Arrays | Matt Bauman | JuliaCon 2018") ](https://www.youtube.com/watch?v=jS9eouMJf_Y&t=959s)

---

<div class="post-metadata">

### Author: ![Andy\_Zhang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andy_zhang/32/210182_2.png) [@Andy\_Zhang](https://discourse.julialang.org/u/Andy_Zhang)
#### Post date: [August 26, 2024, 4:24pm UTC](https://discourse.julialang.org/t/inbounds-for-getindex-not-working/118631/3 "2024-08-26T16:24:32Z")

</div>

Thanks! Your video is very helpful.

Here is the updated example code for anyone who has the same issue and found this topic:

```julia
function main()
    a = Minij(3)

    for i = 1:1
        g(1:2, 1)
        @inbounds g(1:2, -1)
        a[2, 2]
        @inbounds a[2, -1]
    end
end

main()

```

Things should be moved into a function to ensure type stability.
