# What is the purpose of @inbounds in Julia?

**URL:** https://discourse.julialang.org/t/what-is-the-purpose-of-inbounds-in-julia/26215
**Category:** General Usage
**Created:** [July 10, 2019, 11:32am UTC](https://discourse.julialang.org/t/what-is-the-purpose-of-inbounds-in-julia/26215 "2019-07-10T11:32:12Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![dsbsnyh](https://avatars.discourse-cdn.com/v4/letter/d/bbce88/32.png) [@dsbsnyh](https://discourse.julialang.org/u/dsbsnyh)
#### Post date: [July 10, 2019, 11:32am UTC](https://discourse.julialang.org/t/what-is-the-purpose-of-inbounds-in-julia/26215/1 "2019-07-10T11:32:12Z")

</div>

```julia
function sum(A::AbstractArray)
       r = zero(el type(A))
       for i = 1: length(A)
            @inbounds r += A[i]
       end
       return r
end

```

@inbounds(blk)

Eliminates array bounds checking within expressions.  
In the example above the in-range check for referencing element i of array A is skipped to improve performance. My question is, I do not quite understand why is the @inbounds ever needed in the above example?

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [July 10, 2019, 11:48am UTC](https://discourse.julialang.org/t/what-is-the-purpose-of-inbounds-in-julia/26215/2 "2019-07-10T11:48:35Z")

</div>

> [@dsbsnyh](#):
>
> why is the @inbounds ever needed in the above example?

Since it has a `getindex`. _Logically_ the setup constrains it to be a valid index, but the compiler may or may not figure this out, so an `@inbounds` can help.

(Incidentally, the above example from the docstring is outdated, now `eachindex(A)` is the recommended index iterator.)

---

<div class="post-metadata">

### Author: ![ninjaaron](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ninjaaron/32/6392_2.png) [@ninjaaron](https://discourse.julialang.org/u/ninjaaron)
#### Post date: [July 10, 2019, 1:00pm UTC](https://discourse.julialang.org/t/what-is-the-purpose-of-inbounds-in-julia/26215/3 "2019-07-10T13:00:25Z")

</div>

Wait, does the compiler sometimes figure it out? I just always use `@inbounds` (where appropriate) because I figured the compiler wasn’t going to figure out that stuff.

---

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [July 10, 2019, 1:55pm UTC](https://discourse.julialang.org/t/what-is-the-purpose-of-inbounds-in-julia/26215/4 "2019-07-10T13:55:27Z")

</div>

For simple for loops, yes, as a general rule the compiler will figure it out.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [July 10, 2019, 3:41pm UTC](https://discourse.julialang.org/t/what-is-the-purpose-of-inbounds-in-julia/26215/5 "2019-07-10T15:41:23Z")

</div>

> [@ninjaaron](#):
>
> I just always use `@inbounds` (where appropriate) because I figured the compiler wasn’t going to figure out that stuff.

I use `@inbounds` super-rarely these days, since `1.2-pre` is getting so good about figuring out the simple stuff, and for the complicated access patterns and index calculations I usually prefer to error if I make mistakes.

I think that peppering code with `@inbounds` by default is becoming less and less advisable.
