← Back to Pull requests

Compute the linear index of Cartesian indexing in Horner form #217

Closed
unknown closed this pull request without merging on Oct 21, 2026 13:41 UTC
View on GitHub
unknown unknown commented on Oct 20, 2026 10:41 UTC
Author

Follow-up to #213: compute the linear index of Cartesian indexing in Horner form.

Before: static_size_linear_index computes the column-major index as a sum of strided terms, 1 + (i - 1) + n1 (j - 1) + (n1 n2) (k - 1).

After: Horner form, i + n1 ((j - 1) + n2 (k - 1)). Static dimensions still give constant factors. This affects all FixedSizeArrays, not only those with static dimensions.

History

The first StaticSizeArray version in #210 used Horner form. When any dimension could be static, it switched to the strided sum, with this rationale: "Unlike the Horner form, the terms of indices that don't change in a loop can be hoisted out of it if the dimensions are dynamic." A review commit later dropped that comment. The measurements below show the concern is small.

Trixi.jl

This is the volume integral of Trixi.jl's 2D elixir_euler_ec.jl, with the flux-differencing kernel inlined. Setup: 4096 elements, polydeg 3, Julia 1.12, 1 thread pinned to one core, Vector backend.

| | volume integral | rhs_hyperbolic! |
|---|---|---|
| strided sum (#213) | 2578–2588 µs | 3076–3108 µs |
| Horner (this PR) | 2476–2489 µs | 2986–2990 µs |

That is about −3.9% and −3.3%. Each row covers 3 processes run with JULIA_LLVM_ARGS="-align-all-nofallthru-blocks=6". Without forced alignment, the mean gain over 6 processes is 1.5% (2566 → 2527 µs), because the timing of this kernel swings by up to about 6% from one process to the next, depending on where the code is placed. In the kernel IR, the integer shifts go from 21 to 15.

Microbenchmarks: static and dynamic dimensions

I compared both formulas in one process on the same Memory, computing dst = 2 src and similar operations on a 32×32×32 array (8×64×64 for the pairs kernel). I tested all 8 static/dynamic patterns of the 3 dimensions (d = dynamic, s = static). The table shows Horner time / strided time, minimum of 200 calls:

| loop | ddd | sdd | dsd | dds | ssd | sds | dss | sss |
|---|---|---|---|---|---|---|---|---|
| i innermost (column-major) | 1.003 | 0.997 | 1.003 | 1.000 | 1.002 | 0.992 | 0.997 | 1.006 |
| k innermost (row-major) | 0.999 | 0.997 | 1.027 | 1.003 | 1.017 | 0.999 | 1.017 | 0.992 |
| j innermost | 1.000 | 1.001 | 1.002 | 1.003 | 0.986 | 0.999 | 1.003 | 1.005 |
| stencil along j, k | 1.000 | 1.009 | 1.010 | 0.996 | 0.986 | 1.006 | 0.996 | 0.988 |
| node pairs i, ii | 0.982 | 1.000 | 1.000 | 0.984 | 1.000 | 1.000 | 1.013 | 1.017 |

  • In most cases the two forms are within ±2%. The integer op counts in the IR differ a lot (e.g. 16 vs 80) without a timing effect, because LLVM rewrites both into the same address increments in loops.

  • Trade-off: with the last index innermost (row-major order) and mixed static/dynamic dimensions, Horner form is 1.7–2.7% slower in 3 of 8 patterns. This is the case the old comment was about; it is an uncommon loop order for column-major arrays.

  • All-dynamic arrays (ddd, i.e. every plain FixedSizeArray) show no difference.
  • Tests: the full suite, including the FileCheck tests for indexing, passes on Julia 1.10 and 1.12.

    🤖 Generated with Claude Code

    https://claude.ai/code/session_01D5z6TVTAFfLGSFafUoLzuo

    Diff: 406 lines (+304 / -102) across 5 files Reviewer: giordano Review duration: 1620m
    All Vouch review confidence checks passed
    Residual risk 0.18 = Change Risk (0.45) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.45 · 406 lines diff across 5 files
    0.45
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    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.359
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #217"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% 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: 44.8% (MEDIUM) · Higher = larger blast radius
    44.8%
    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: 60.5% · Lower = rushed or shallow review
    60.5%
    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: 17.7% · Safe to merge
    17.7%