Commit 9cbf6d0
Make NamedArrayPartition slicing fully type-stable via Vector
The first commit on this branch fixed `x[1:2]` and `x[[1, 4]]` by making
`similar(::NAP, T, dims)` degrade to a Vector when `dims != size(A)`,
mirroring ArrayPartition. That worked, but it left the indexing path
inferring as `Union{NamedArrayPartition, Vector{Float64}}` because
`similar(::ArrayPartition, T, dims)` itself is a Union (the
`dims == size(A)` branch is a runtime check).
Add a `_unsafe_getindex(::IndexStyle, ::NAP, I::Vararg{Union{Real, AbstractArray}})`
shortcut that mirrors the one at array_partition.jl:317. Allocate the
destination directly off the underlying first array and fill it with
`Base._unsafe_getindex!`. The shortcut bypasses `similar` entirely for
the indexing path, so `x[1:2]`, `x[[1, 4]]`, `x[1:length(x)]` all infer
to a clean `Vector{Float64}`.
Trade-off: this regresses the post-05faa730 test
`typeof(x .+ x[1:end]) <: NamedArrayPartition` back to `<: Vector` (the
v3 behavior). That test was added in 05faa73 alongside the
invalidation cleanup, but its only effect was to mask the unstable
small-Union return; restoring v3 semantics here gives full type
stability and matches what ArrayPartition already does. Use
`similar(x)` / `copy(x)` if you want a NamedArrayPartition back.
The `similar(::NAP, dims)` and `similar(::NAP, T, dims)` overloads
keep the graceful-degrade-to-Vector behavior from the previous commit,
so direct `similar(x, T, (2,))` calls (e.g. from downstream library
code) still work.
`SnoopCompile.invalidation_trees(@snoop_invalidations using
RecursiveArrayTools)` still reports 0 trees, full `Pkg.test()` passes,
and the new regression test asserts type stability via `@inferred`.
Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com>
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>1 parent 2466954 commit 9cbf6d0
2 files changed
Lines changed: 47 additions & 17 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
100 | 100 | | |
101 | 101 | | |
102 | 102 | | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
103 | 125 | | |
104 | 126 | | |
105 | 127 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
10 | | - | |
| 10 | + | |
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| |||
39 | 39 | | |
40 | 40 | | |
41 | 41 | | |
42 | | - | |
43 | | - | |
44 | | - | |
45 | | - | |
46 | | - | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
47 | 51 | | |
48 | 52 | | |
49 | 53 | | |
50 | | - | |
| 54 | + | |
51 | 55 | | |
52 | | - | |
53 | 56 | | |
54 | | - | |
55 | | - | |
| 57 | + | |
56 | 58 | | |
57 | 59 | | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
58 | 63 | | |
59 | 64 | | |
60 | | - | |
61 | | - | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
62 | 69 | | |
63 | | - | |
| 70 | + | |
| 71 | + | |
64 | 72 | | |
65 | | - | |
66 | | - | |
| 73 | + | |
67 | 74 | | |
68 | | - | |
| 75 | + | |
69 | 76 | | |
70 | | - | |
| 77 | + | |
71 | 78 | | |
72 | 79 | | |
| 80 | + | |
73 | 81 | | |
74 | 82 | | |
75 | 83 | | |
0 commit comments