From 79a0e16c4beb04f1a9990c70f871d782c2d2d0e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20M=C3=BCller-Widmann?= Date: Fri, 20 Nov 2026 09:11:47 +0100 Subject: [PATCH 1/2] =?UTF-8?q?Specialize=20`=E2=88=87getindex!`=20on=20th?= =?UTF-8?q?e=20indices?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Julia does not specialize on `inds...` that are only passed through, so the broadcasting `∇getindex!` methods allocated on every call (6 allocations and ~190 ns instead of 0 allocations and ~16 ns for a 100-element column). Co-Authored-By: Claude Opus 5.5 --- Project.toml | 2 +- src/rulesets/Base/indexing.jl | 4 ++-- test/rulesets/Base/indexing.jl | 11 +++++++++++ 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/Project.toml b/Project.toml index faf9a412c..1d8c5e7b6 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "ChainRules" uuid = "082447d4-558c-5d27-93f4-14fc19e9eca2" -version = "1.74.13" +version = "1.74.14" [deps] Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e" diff --git a/src/rulesets/Base/indexing.jl b/src/rulesets/Base/indexing.jl index c6623822e..42a309121 100644 --- a/src/rulesets/Base/indexing.jl +++ b/src/rulesets/Base/indexing.jl @@ -147,7 +147,7 @@ function ∇getindex!(dx::AbstractArray, dy, inds::Integer...) @views dx[inds...] += dy return dx end -function ∇getindex!(dx::AbstractArray, dy, inds...) +function ∇getindex!(dx::AbstractArray, dy, inds::Vararg{Any,N}) where {N} # force specialization view(dx, inds...) .+= dy return dx end @@ -176,7 +176,7 @@ function ∇getindex!(dx::AbstractGPUArray, dy, inds::Integer...) view(dx, inds...) .+= Ref(dy) return dx end -function ∇getindex!(dx::AbstractGPUArray, dy, inds::Union{Integer, AbstractUnitRange, Base.Slice}...) +function ∇getindex!(dx::AbstractGPUArray, dy, inds::Vararg{Union{Integer, AbstractUnitRange, Base.Slice},N}) where {N} view(dx, inds...) .+= dy return dx end diff --git a/test/rulesets/Base/indexing.jl b/test/rulesets/Base/indexing.jl index a88eb55a0..ad2cfb45a 100644 --- a/test/rulesets/Base/indexing.jl +++ b/test/rulesets/Base/indexing.jl @@ -189,6 +189,17 @@ end dxs_gpu = unthunk(back(jl([1.0]))[2]) @test dxs_gpu == [jl([1.0])] end + + @testset "∇getindex! does not allocate" begin + function allocs_∇getindex!(dx, dy, inds::Vararg{Any,N}) where {N} + ChainRules.∇getindex!(dx, dy, inds...) # compile + return @allocated ChainRules.∇getindex!(dx, dy, inds...) + end + dx = zeros(3, 4) + @test allocs_∇getindex!(dx, rand(3), Base.to_indices(dx, (:, 2))...) == 0 + @test allocs_∇getindex!(dx, rand(2, 2), 2:3, 1:2) == 0 + @test allocs_∇getindex!(dx, 1.0, 2, 3) == 0 + end end # first & tail handled by getfield rules From 37782270aaa66d26e9970dc766e53bb191c17a42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20M=C3=BCller-Widmann?= Date: Fri, 20 Nov 2026 09:14:31 +0100 Subject: [PATCH 2/2] Fix format Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- src/rulesets/Base/indexing.jl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/rulesets/Base/indexing.jl b/src/rulesets/Base/indexing.jl index 42a309121..f4b3178f2 100644 --- a/src/rulesets/Base/indexing.jl +++ b/src/rulesets/Base/indexing.jl @@ -176,7 +176,9 @@ function ∇getindex!(dx::AbstractGPUArray, dy, inds::Integer...) view(dx, inds...) .+= Ref(dy) return dx end -function ∇getindex!(dx::AbstractGPUArray, dy, inds::Vararg{Union{Integer, AbstractUnitRange, Base.Slice},N}) where {N} +function ∇getindex!( + dx::AbstractGPUArray, dy, inds::Vararg{Union{Integer,AbstractUnitRange,Base.Slice},N} +) where {N} view(dx, inds...) .+= dy return dx end