Skip to content

Conversation

@timholy
Copy link
Contributor

@timholy timholy commented Oct 3, 2025

Julia is quite good at eliminating bounds checks on its own, and incorrect use of @inbounds can lead to hard-to-diagnose bugs. There are also cases where adding it actively hurts performance (e.g., JuliaLang/julia#48245). It's much better to allow the sophisticated analysis performed by inference to make this decision and it is to reflexively add @inbounds everywhere.

Fixes #1265.

Julia is quite good at eliminating bounds checks on its own, and
incorrect use of `@inbounds` can lead to hard-to-diagnose bugs. There
are also cases where adding it actively hurts performance (e.g.,
JuliaLang/julia#48245). It's much better to
allow the sophisticated analysis performed by inference to make this
decision and it is to reflexively add `@inbounds` everywhere.

Fixes JuliaStats#1265.
@codecov-commenter
Copy link

Codecov Report

❌ Patch coverage is 88.40580% with 16 lines in your changes missing coverage. Please review.
✅ Project coverage is 86.35%. Comparing base (a1b8bb3) to head (c175d7f).
⚠️ Report is 2 commits behind head on master.

Files with missing lines Patch % Lines
src/multivariate/dirichlet.jl 73.68% 5 Missing ⚠️
src/mixtures/mixturemodel.jl 0.00% 4 Missing ⚠️
src/multivariates.jl 33.33% 2 Missing ⚠️
src/samplers/poisson.jl 0.00% 2 Missing ⚠️
src/common.jl 83.33% 1 Missing ⚠️
src/genericrand.jl 90.00% 1 Missing ⚠️
src/multivariate/mvnormal.jl 90.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2005      +/-   ##
==========================================
+ Coverage   86.28%   86.35%   +0.07%     
==========================================
  Files         146      146              
  Lines        8787     8782       -5     
==========================================
+ Hits         7582     7584       +2     
+ Misses       1205     1198       -7     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@devmotion
Copy link
Member

devmotion commented Oct 3, 2025

This is maybe a bit extreme to fix #1265 (which arguably is not an actual problem for most users and could be addressed by ensuring one-based indices when not operating with generic indices).

AFAIK the benchmarks in this package haven't been updated in a long time, and also seem to cover only a few samplers and a few evaluations for mixture models. All other distributions are not covered by the benchmarks at all.

So IMO,

  1. If a specific @inbounds can be shown to not improve performance, we could/should remove it
  2. If an @inbounds is unsafe but actually improves performance, we should guard it with a requires_one_based_indexing (quick fix) or rewrite the expressions with generic indices

In all other cases (safe @inbounds that improve performance), I don't see a strong reason to remove them.

Currently, my impression is that this PR removes @inbounds statements without checking point 1.

@timholy
Copy link
Contributor Author

timholy commented Oct 3, 2025

If a specific @inbounds can be shown to not improve performance, we could/should remove it

I think in 2025 the burden-of-proof should go the other way: you should keep them only if you can show them to be necessary. Julia is not the same language it was back in 2014 and the compiler is much better at this than we are. Constant-propagation is an extremely useful optimization, one likely to be useful for this package, and @inbounds annotations are known to hurt performance in such cases.

I can't remember the last time I annotated a line with @inbounds and in recent memory I have never seen bounds-checking show up in profiles. Maybe for GPU code it's an issue, but not for CPU code. They should almost never be necessary or helpful.

@devmotion
Copy link
Member

OK, I'm convinced 🙂 I just didn't trust the reasoning based on benchmarking in #1265 (comment) as it only covers a tiny subset of the package (which makes the performance checks a bit questionable...).

If we/users see regressions, we can always add @inbounds back.

@devmotion devmotion merged commit 2de1cc8 into JuliaStats:master Oct 3, 2025
14 checks passed
@timholy timholy deleted the teh/no_inbounds branch October 3, 2025 09:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Incorrect uses of @inbounds cause miscalculation of statistics

3 participants