# Which block \`@inbounds\` actually avoid testing bounds?

**URL:** https://discourse.julialang.org/t/which-block-inbounds-actually-avoid-testing-bounds/56267
**Category:** General Usage
**Created:** [March 1, 2021, 5:37pm UTC](https://discourse.julialang.org/t/which-block-inbounds-actually-avoid-testing-bounds/56267 "2021-03-01T17:37:52Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [March 1, 2021, 5:37pm UTC](https://discourse.julialang.org/t/which-block-inbounds-actually-avoid-testing-bounds/56267/1 "2021-03-01T17:37:53Z")

</div>

For example, this simple function:

```julia
function f(vec1,vec2)
  for i in 2:length(vec1)
    vec1[i] = vec1[i] - vec2[i-1]
  end
  nothing
end

```

I get:

```julia
julia> @btime f($vec1,$vec2)
  759.284 ns (0 allocations: 0 bytes)

```

If I add `@inbounds` to the inner loop, I get:

```julia
julia> @btime f($vec1,$vec2)
  107.009 ns (0 allocations: 0 bytes)

```

> **Code**
>
> ```julia
> function f(vec1,vec2)
> @inbounds for i in 2:length(vec1)
> vec1[i] = vec1[i] - vec2[i-1]
> end
> nothing
> end
> 
> ```

But if I add `@inbounds` to the function declaration as a whole, I get again the slow performance:

```julia
julia> @btime f($vec1,$vec2)
  758.388 ns (0 allocations: 0 bytes)

```

> **Code**
>
> ```julia
> @inbounds function f(vec1,vec2)
> for i in 2:length(vec1)
> vec1[i] = vec1[i] - vec2[i-1]
> end
> nothing
> end
> 
> ```

It is not clear to me what is the actual `block` inbounds acts upon. I tried `Base.@propagage_inbounds` but the result is the same. I could not figure out [from the manual](https://docs.julialang.org/en/v1/devdocs/boundscheck/#Propagating-inbounds) how inbounds is, or not, propagating.

This also makes me wonder why the example of its use is something like:

```julia
      for i = 1:length(A)
          @inbounds r += A[i]
      end

```

and not just adding it to the loop as a whole. I have seen this pattern often, but never figured out why one would do that instead of flagging the whole loop.

---

<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: [March 1, 2021, 5:44pm UTC](https://discourse.julialang.org/t/which-block-inbounds-actually-avoid-testing-bounds/56267/2 "2021-03-01T17:44:04Z")

</div>

We clearly need to document this better, as it’s snagged [some of the best of us](https://github.com/JuliaLang/julia/issues/39308).

`@inbounds` is effectively a _runtime_ macro, not a recursive syntax transform. It turns of bounds checking, then runs the wrapped code, then turns it back on again. When you do `@inbounds function f() ... end`, you’re removing bounds checking _during the definition of the function_. Within the function, though, it doesn’t matter if you write:

```julia
@inbounds for ...
end
# or
for ...
    @inbounds r += A[i]
end
# or
@inbounds begin
    for ...
    end
end

```

I like scoping it as narrowly as possible if it’s convenient to do so since I might add more things into a `for` loop or an arbitrary block of code and forget that `@inbounds` is applied.

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [March 1, 2021, 6:38pm UTC](https://discourse.julialang.org/t/which-block-inbounds-actually-avoid-testing-bounds/56267/3 "2021-03-01T18:38:20Z")

</div>

> [@lmiq](#):
>
> never figured out why one would do that instead of flagging the whole loop.

For me, it’s aesthetic reasons. I never liked that macros mess up the alignment of blocks with `end`. Putting `@inbounds` inside the loop, when there’s just a single applicable expression for it, just looks cleaner.

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [March 1, 2021, 6:45pm UTC](https://discourse.julialang.org/t/which-block-inbounds-actually-avoid-testing-bounds/56267/4 "2021-03-01T18:45:31Z")

</div>

Macros look much like Python decorators, but decorators can be put _above_ the block they apply to.

Sometimes I wish macros would consume the next non-empty expression, or something (maybe discard leading whitespace?), then I could put `@inbounds`, `@threads`, etc. above the block. It would look nice and also be easy to comment out.
