Question on @inbounds


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

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.


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.

1 Like

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

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.

1 Like

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

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.

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.