Skip to content

Commit 9e09463

Browse files
Move VA/AP constructors and any/all optimizations to subpackage
Create lib/RecursiveArrayToolsShorthandConstructors as a separate subpackage for methods that cause invalidations: - VA[...] and AP[...] shorthand constructors (getindex on Type) - Optimized any/all for ArrayPartition (partition-level short-circuit) Users who want these opt in with: using RecursiveArrayToolsShorthandConstructors Invalidation trees after `using RecursiveArrayTools`: 3 → 1 (only NamedArrayPartition.setindex! remains, pre-existing) Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com> Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 616e8fc commit 9e09463

File tree

11 files changed

+63
-9
lines changed

11 files changed

+63
-9
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "RecursiveArrayTools"
22
uuid = "731186ca-8d62-57ce-b412-fbd966d074cd"
3-
authors = ["Chris Rackauckas <accounts@chrisrackauckas.com>"]
43
version = "3.48.0"
4+
authors = ["Chris Rackauckas <accounts@chrisrackauckas.com>"]
55

66
[deps]
77
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name = "RecursiveArrayToolsShorthandConstructors"
2+
uuid = "39fb7555-b4ad-4efd-8abe-30331df017d3"
3+
version = "1.0.0"
4+
5+
[deps]
6+
RecursiveArrayTools = "731186ca-8d62-57ce-b412-fbd966d074cd"
7+
8+
[compat]
9+
RecursiveArrayTools = "3.48"
10+
julia = "1.10"
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""
2+
RecursiveArrayToolsShorthandConstructors
3+
4+
Opt-in subpackage providing shorthand syntax and optimized methods that cause
5+
method table invalidations. These are separated from RecursiveArrayTools to
6+
avoid invalidating compiled code for users who don't need them.
7+
8+
```julia
9+
using RecursiveArrayToolsShorthandConstructors
10+
```
11+
12+
This enables:
13+
- `VA[a, b, c]` shorthand for `VectorOfArray([a, b, c])`
14+
- `AP[a, b, c]` shorthand for `ArrayPartition(a, b, c)`
15+
- Optimized `any`/`all` for `ArrayPartition` (partition-level short-circuiting)
16+
"""
17+
module RecursiveArrayToolsShorthandConstructors
18+
19+
using RecursiveArrayTools: VA, VectorOfArray, AP, ArrayPartition
20+
21+
# VA[...] shorthand
22+
Base.getindex(::Type{VA}, xs...) = VectorOfArray(collect(xs))
23+
24+
# AP[...] shorthand
25+
Base.getindex(::Type{AP}, xs...) = ArrayPartition(xs...)
26+
27+
# Optimized any/all for ArrayPartition — applies f partition-by-partition
28+
# for faster short-circuiting instead of element-by-element.
29+
Base.any(f, A::ArrayPartition) = any((any(f, x) for x in A.x))
30+
Base.any(f::Function, A::ArrayPartition) = any((any(f, x) for x in A.x))
31+
Base.any(A::ArrayPartition) = any(identity, A)
32+
Base.all(f, A::ArrayPartition) = all((all(f, x) for x in A.x))
33+
Base.all(f::Function, A::ArrayPartition) = all((all(f, x) for x in A.x))
34+
Base.all(A::ArrayPartition) = all(identity, A)
35+
36+
end # module

src/array_partition.jl

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -223,12 +223,10 @@ end
223223
end
224224
end
225225
Base.filter(f, A::ArrayPartition) = ArrayPartition(map(x -> filter(f, x), A.x))
226-
Base.any(f, A::ArrayPartition) = any((any(f, x) for x in A.x))
227-
Base.any(f::Function, A::ArrayPartition) = any((any(f, x) for x in A.x))
228-
Base.any(A::ArrayPartition) = any(identity, A)
229-
Base.all(f, A::ArrayPartition) = all((all(f, x) for x in A.x))
230-
Base.all(f::Function, A::ArrayPartition) = all((all(f, x) for x in A.x))
231-
Base.all(A::ArrayPartition) = all(identity, A)
226+
# Optimized any/all for ArrayPartition (partition-level short-circuiting) moved to
227+
# RecursiveArrayTools.ShorthandConstructors to avoid invalidations (779 children).
228+
# The AbstractArray default any/all works correctly, just without partition-level optimization.
229+
# `using RecursiveArrayTools.ShorthandConstructors` to enable the optimized versions.
232230

233231
for type in [AbstractArray, PermutedDimsArray]
234232
@eval function Base.copyto!(dest::$(type), A::ArrayPartition)
@@ -746,4 +744,5 @@ ODEProblem(func, AP[ [1.,2.,3.], [1. 2.;3. 4.] ], (0, 1)) |> solve
746744
747745
"""
748746
struct AP end
749-
Base.getindex(::Type{AP}, xs...) = ArrayPartition(xs...)
747+
# AP[...] shorthand moved to RecursiveArrayTools.ShorthandConstructors to avoid invalidations.
748+
# `using RecursiveArrayTools.ShorthandConstructors` to enable.

src/vector_of_array.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1315,4 +1315,5 @@ nested = VA[
13151315
13161316
"""
13171317
struct VA end
1318-
Base.getindex(::Type{VA}, xs...) = VectorOfArray(collect(xs))
1318+
# VA[...] shorthand moved to RecursiveArrayTools.ShorthandConstructors to avoid invalidations.
1319+
# `using RecursiveArrayTools.ShorthandConstructors` to enable.

test/basic_indexing.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using RecursiveArrayTools, Test
2+
using RecursiveArrayToolsShorthandConstructors
23

34
# Example Problem
45
recs = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

test/interface_tests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using RecursiveArrayTools, StaticArrays, Test
2+
using RecursiveArrayToolsShorthandConstructors
23
using FastBroadcast
34
using SymbolicIndexingInterface: SymbolCache
45

test/linalg.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using RecursiveArrayTools, Test, Random
2+
using RecursiveArrayToolsShorthandConstructors
23
using LinearAlgebra, ArrayInterface
34

45
n, m = 5, 6

test/partitions_test.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using RecursiveArrayTools, Test, Statistics, ArrayInterface, Adapt
2+
using RecursiveArrayToolsShorthandConstructors
23

34
@test length(ArrayPartition()) == 0
45
@test isempty(ArrayPartition())

test/runtests.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
using Pkg
2+
# Install the ShorthandConstructors subpackage for tests that need VA[...]/AP[...] syntax
3+
Pkg.develop(PackageSpec(
4+
path = joinpath(dirname(@__DIR__), "lib", "RecursiveArrayToolsShorthandConstructors")))
25
using RecursiveArrayTools
36
using Test
47
using SafeTestsets

0 commit comments

Comments
 (0)