# Question on @inbounds

**URL:** <https://discourse.julialang.org/t/question-on-inbounds/98023>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [April 28, 2023, 7:40am UTC](https://discourse.julialang.org/t/question-on-inbounds/98023 "2023-04-28T07:40:17Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![gitboy16](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gitboy16/32/24906_2.png) [@gitboy16](https://discourse.julialang.org/u/gitboy16)\
**Post date:** [April 28, 2023, 7:40am UTC](https://discourse.julialang.org/t/question-on-inbounds/98023/1 "2023-04-28T07:40:17Z")

</div>

Hi,

Is there an automatic way to find out where to add @inbounds in order to improve performance?

I am testing a very long module and would like to know where it would benefit to insert @inbounds.

Alternatively is there a way to make a whole module use @inbounds by default?

I am trying to match the performance I get when starting Julia with --check-bounds=no.

Thank you

---

<div class="post-metadata">

**Author:** ![jakobnissen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jakobnissen/32/13477_2.png) [@jakobnissen](https://discourse.julialang.org/u/jakobnissen)\
**Post date:** [April 28, 2023, 7:44am UTC](https://discourse.julialang.org/t/question-on-inbounds/98023/2 "2023-04-28T07:44:01Z")

</div>

Hello and welcome,

First, I’d advice you to not use `@inbounds` until you’ve already made other optimisations. `@inbounds` is unsafe, and has the potential to cause memory issues (like segfaults) in your code, whereas say, reducing the number of allocations or improving your algorithms has no such side effect.

Second, I’d recommend you to use iteration (for loops), or `eachindex` where applicable. In many cases, Julia’s compiler will automatically remove boundschecks with iteration or `eachindex` because it can prove there can be no out-of-bounds access

If you have determined, after doing the above, that you need `@inbounds`, I would use the `Profile` stdlib to profile your program and determine the “hot loops” where you code spends mosts of its time, then add `@inbounds` to these.

It is not advisable to use `@inbounds` for whole modules - but luckily, it is also generally not needed. Since `@inbounds` is quite unsafe, it’s better to use it strategically than to plaster it all over a codebase.

---

<div class="post-metadata">

**Author:** ![gitboy16](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gitboy16/32/24906_2.png) [@gitboy16](https://discourse.julialang.org/u/gitboy16)\
**Post date:** [April 28, 2023, 7:56am UTC](https://discourse.julialang.org/t/question-on-inbounds/98023/3 "2023-04-28T07:56:04Z")

</div>

Thank you for your feedback. I have spent quite some time removing allocation and optimizing the module. I also used JET.jl to remove potential errors. I have inserted @inbounds in front of all the loops I coukd find but still I see quite a difference when I run Julia with --check-bounds=no hence I would like to find out where I am leaving performance on the table. In my case I would really be interested to make my whole module @inbounds. I believe it is safe.

---

<div class="post-metadata">

**Author:** ![jakobnissen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jakobnissen/32/13477_2.png) [@jakobnissen](https://discourse.julialang.org/u/jakobnissen)\
**Post date:** [April 28, 2023, 8:20am UTC](https://discourse.julialang.org/t/question-on-inbounds/98023/4 "2023-04-28T08:20:50Z")

</div>

Okay this is interesting. Perhaps you could profile the same program with and without `--check-bounds=no`? There is currently talk of completely removing `--check-bounds` from Julia, so perhaps a concrete case where it can be demonstrated that performance is significantly degraded without the option, AND where it can’t easily be restored with `@inbounds` would be appreciated.

Relevant issue is [Consider removing `--check-bounds=no`? · Issue #48245 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/48245)

---

<div class="post-metadata">

**Author:** ![gitboy16](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gitboy16/32/24906_2.png) [@gitboy16](https://discourse.julialang.org/u/gitboy16)\
**Post date:** [April 28, 2023, 8:39am UTC](https://discourse.julialang.org/t/question-on-inbounds/98023/5 "2023-04-28T08:39:36Z")

</div>

To me and some other users, I think this is a bad idea and I would consider it a regression. If you read the thread you posted you can see that someone already posted some serious speed up. Check the post of sloede on the 24 of Jan.

---

<div class="post-metadata">

**Author:** ![gitboy16](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gitboy16/32/24906_2.png) [@gitboy16](https://discourse.julialang.org/u/gitboy16)\
**Post date:** [May 3, 2023, 1:18pm UTC](https://discourse.julialang.org/t/question-on-inbounds/98023/6 "2023-05-03T13:18:32Z")

</div>

Is it correct if I say that there is no way right now to make a whole module use @inbounds?

---

<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:** [May 3, 2023, 2:01pm UTC](https://discourse.julialang.org/t/question-on-inbounds/98023/7 "2023-05-03T14:01:53Z")

</div>

> [@gitboy16](#):
>
> Is there an automatic way to find out where to add @inbounds in order to improve performance?

The compiler already does this. In the cases where it can prove `@inbounds` is a valid transformation it will automatically omit those checks.

Are you putting your `@inbounds` inside of functions or outside of them? Something like `@inbounds function f() ...` won’t work like you intend — the macro needs to be inside the function.

---

<div class="post-metadata">

**Author:** ![gitboy16](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gitboy16/32/24906_2.png) [@gitboy16](https://discourse.julialang.org/u/gitboy16)\
**Post date:** [May 3, 2023, 2:07pm UTC](https://discourse.julialang.org/t/question-on-inbounds/98023/8 "2023-05-03T14:07:40Z")

</div>

I put it inside the function. But I would like to apply it to a whole module, basically replicating the same effect as starting julia with --check-bounds=no but at a module level.
