# Bounds check performance

**URL:** https://discourse.julialang.org/t/bounds-check-performance/121741
**Category:** General Usage
**Tags:** question, inbounds, bounds-check, effects
**Created:** [October 25, 2024, 9:39am UTC](https://discourse.julialang.org/t/bounds-check-performance/121741 "2024-10-25T09:39:33Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![shatanzikou](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shatanzikou/32/212877_2.png) [@shatanzikou](https://discourse.julialang.org/u/shatanzikou)
#### Post date: [October 25, 2024, 9:39am UTC](https://discourse.julialang.org/t/bounds-check-performance/121741/1 "2024-10-25T09:39:33Z")

</div>

The SciML Style guide reads that in Julia after ver1.9, bounds check will reduce performance. But I didn’t see others recommended this, there is no performance reducing in my test. I want to know why it is not recommended to use bounds check removal.

> **[GitHub - SciML/SciMLStyle: A style guide for stylish Julia developers](https://github.com/SciML/SciMLStyle?tab=readme-ov-file#avoid-bounds-check-removal-and-if-done-add-appropriate-manual-checks)**
>
> A style guide for stylish Julia developers

 ![image](https://global.discourse-cdn.com/julialang/original/3X/4/2/423c8c477440dfcfebeacad4d63e79565b0288cb.png)

---

<div class="post-metadata">

### Author: ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)
#### Post date: [October 25, 2024, 9:50am UTC](https://discourse.julialang.org/t/bounds-check-performance/121741/2 "2024-10-25T09:50:06Z")

</div>

Bounds check removal may decrease performance because it usually results in worse effects. To learn more about effects, see the `Base.@assume_effects` doc string in the manual. There’s some more info in the `Base.Compiler.Effects` doc string, which is not in the manual and not public.

Worse effects means various compiler optimizations may be inhibited.

> [@shatanzikou](#):
>
> I want to know why it is not recommended to use bounds check removal.

Apart from the above issues due to effects, obviously `@inbounds` is unsafe (may result in crashes, etc.), so it should only be used when clearly worth the risk and reviewer’s effort.

---

<div class="post-metadata">

### Author: ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)
#### Post date: [October 25, 2024, 4:11pm UTC](https://discourse.julialang.org/t/bounds-check-performance/121741/3 "2024-10-25T16:11:40Z")

</div>

It’s best to write code where bounds-check removal can be inferred, e.g. using `eachindex(A)`, not `1:length(A)` (which also has a bug for some code).

But when `@inbounds` works, and actually is slower without, what’s then the best alternative? I believe for some arrays, e.g. with asserts, you can induce the compiler to eliminate checks, even without `@inbounds`. It might (or might not in some case?) be as unsafe.
