Skip to content

Split the BroadcastStyle method to avoid invalidations - #231

Merged
giordano merged 3 commits into
mainfrom
vc/broadcaststyle-subarray-invalidations
Oct 24, 2026
Merged

giordano merged 3 commits into
mainfrom
vc/broadcaststyle-subarray-invalidations

Conversation

@vchuravy

Copy link
Copy Markdown
Contributor

The BroadcastStyle method for FixedSizeArrayDims and their views has a Union of both signatures:

function Base.BroadcastStyle(::Union{Type{<:(FixedSizeArrayDims{T, N, D, Mem} where {T, D})},
                                     Type{<:(SubArray{T, N, <:FixedSizeArrayDims{T, N, <:Any, Mem}} where {T})}}) where {N, Mem}

When the package is loaded, this method invalidates compiled code that calls BroadcastStyle of unrelated types. This PR splits it into two methods with the same bodies, one per signature, which do not.

Found via the invalidations check of trixi-framework/Trixi.jl#3357, which adds FixedSizeArrays.jl as a dependency. With @snoop_invalidations using Trixi on Julia 1.10:

invalidations
Trixi.jl main (without FixedSizeArrays.jl) 1863
trixi-framework/Trixi.jl#3357 with FixedSizeArrays.jl v1.5.0 1875
trixi-framework/Trixi.jl#3357 with this PR 1867

The 8 removed invalidations are all BroadcastStyle of Tuple types, e.g.:

MethodInstance for Base.Broadcast.BroadcastStyle(::Type{<:Tuple})
MethodInstance for Base.Broadcast.BroadcastStyle(::Type{<:NTuple{5, Any}})
MethodInstance for Base.Broadcast.BroadcastStyle(::Type{<:Tuple{Any, Any, Any}})
...

The remaining 4 come from the method for views. They are BroadcastStyle(::Type{SubArray}), combine_styles(::SubArray, ...) and materialize!(::SubArray, ...) compiled for abstract SubArrays. Any BroadcastStyle method for views would invalidate these. The cconvert method only replaces a method of StaticArrays.jl as the root of the same invalidations and adds none.

The broadcast style is unchanged, e.g. for view(a, :, :), view(a, 1:2, :) and view(a, :, 2) (which keeps DefaultArrayStyle{1} as before) of a FixedSizeArray, and for views of arrays with static dimensions. The existing test of the style of views (test/static_dims.jl) still passes. The tests pass on Julia 1.10 and 1.12.

🤖 Generated with Claude Code

The single `BroadcastStyle` method with a `Union` of the signatures for
`FixedSizeArrayDims` and for their views invalidates compiled code calling
`BroadcastStyle` of unrelated types, e.g. `BroadcastStyle(::Type{<:Tuple})`.
Two methods, one for each signature, do not.

Assisted-by: Claude Code (Opus 5.5)
@codecov

codecov Bot commented Oct 24, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.42%. Comparing base (d4db76d) to head (70a73a9).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #231      +/-   ##
==========================================
+ Coverage   94.39%   94.42%   +0.03%     
==========================================
  Files           8        8              
  Lines         321      323       +2     
==========================================
+ Hits          303      305       +2     
  Misses         18       18              

☔ View full report in Codecov by Harness.
📢 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.

@ranocha ranocha left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me. Nice to see that the invalidations CI job at least helped to fix some invalidations.

@giordano
giordano merged commit fc90003 into main Oct 24, 2026
21 checks passed
@giordano
giordano deleted the vc/broadcaststyle-subarray-invalidations branch October 24, 2026 18:35
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.

3 participants