← Back to Pull requests

Make indices zero-based first in the Horner linear index #236

Open
unknown opened this pull request with 314 lines diff across 3 files
View on GitHub
unknown unknown commented on Nov 26, 2026 14:07 UTC
Author

Cartesian getindex/setindex! compute the linear index in Horner form as

i + n1 * ((j + n2 * (k - 1)) - 1)       # current: subtract one from the partial sums

This PR changes the association to

1 + (i - 1) + n1 * ((j - 1) + n2 * (k - 1))   # this PR: make each index zero-based first

The number of integer operations is the same, but starting with Julia v1.12 (LLVM 18) the loop vectorizer does not vectorize short loops over an index of a FixedSizeArray with the current form.

Measurements

Gather-form derivative kernel over a FixedSizeArrayDims with dimensions (Val(1), Val(4), Val(4), Val(4), n), minimum time in µs, x86-64 (Ryzen 9 5950X), one pinned core:

| Julia | v1.5.2 | this PR |
|---|---|---|
| 1.10 | 30 | 30 |
| 1.11 | 29 | 30 |
| 1.12 | 59 | 25 |
| 1.14 | 58 | 20 |

The same kernel with a hand-written wrapper type using either index form reproduces both columns, so the difference is the index expression and not something else in FixedSizeArray. A sum of strided terms (1 + (i - 1) + n1 * (j - 1) + ...) does not vectorize on 1.12/1.14 either.

In Trixi.jl (trixi-framework/Trixi.jl#3357, 3D p4est advection, Julia 1.14): rhs! 307–320 µs → 290–302 µs, interface flux 84–86 µs → 62–65 µs, volume integral 180–184 µs → 167–175 µs. One kernel (prolong2interfaces!) gets slower, 23 µs → 32 µs, because LLVM unrolls its node loop differently; that is fixable in the Trixi.jl kernel and independent of the index form otherwise.

Open points (hence draft)

  • The current form was chosen in #217 for a 2.5–3.5% gain in Trixi.jl's flux differencing kernels over the strided sum. Flux differencing with this PR is unchanged within run-to-run noise (volume integral, min µs, v1.5.2 → this PR, Julia 1.14): tree_3d_dgsem/elixir_euler_ec.jl 1784 → 1760, p4est_3d_dgsem/elixir_euler_ec.jl 1110 → 1110, tree_2d_dgsem/elixir_euler_ec.jl 542–588 → 552–562, p4est_2d_dgsem/elixir_euler_shockcapturing_ec.jl 369 → 365. So for compressible Euler this PR is neutral; the gains above are for scalar advection.

  • Only measured on x86-64.

  • I have not identified which LLVM analysis distinguishes the two forms; the claim is from benchmarks and from counting packed vs. scalar FMAs in the native code.

  • The changelog entry will conflict with #235 (both add an "Unreleased" section).
  • The existing test suite passes locally on Julia 1.14.

    🤖 Generated with Claude Code

    https://claude.ai/code/session_01HgDfyeGjfcC7cikr7MfRB3

    Diff: 314 lines (+235 / -79) across 3 files Reviewer: giordano Review duration: 6046m
    All Vouch review confidence checks passed
    Residual risk 0.21 = Change Risk (0.40) × (1 − Review Confidence (0.47)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.40 · 314 lines diff across 3 files
    0.40
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.344
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #236"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 46.7% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 40.3% (MEDIUM) · Higher = larger blast radius
    40.3%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 46.7% · Lower = rushed or shallow review
    46.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 21.5% · Safe to merge
    21.5%