From 543002dfd6de323b04cc9f449037ddf1e04c37e7 Mon Sep 17 00:00:00 2001 From: Brian Chen Date: Mon, 20 Jun 2022 19:55:02 -0700 Subject: [PATCH] Use broadcasting instead of `map` for non-numeric array projectors This improves type stability when elements hit `ProjectTo(::Any)`. --- Project.toml | 2 +- src/projection.jl | 2 +- test/projection.jl | 4 ++++ 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/Project.toml b/Project.toml index 74b537404..782129559 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "ChainRulesCore" uuid = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4" -version = "1.15.0" +version = "1.15.1" [deps] Compat = "34da2185-b29b-5c13-b0c7-acf172513d20" diff --git a/src/projection.jl b/src/projection.jl index 8eba26353..344f7d85e 100644 --- a/src/projection.jl +++ b/src/projection.jl @@ -207,7 +207,7 @@ _eltype_projectto(::Type{<:Irrational}) = ProjectTo{Real}() # In other cases, store a projector per element: function ProjectTo(xs::AbstractArray) - elements = map(ProjectTo, xs) + elements = ProjectTo.(xs) if elements isa AbstractArray{<:ProjectTo{<:AbstractZero}} return ProjectTo{NoTangent}() # short-circuit if all elements project to zero else diff --git a/test/projection.jl b/test/projection.jl index 3e70772ac..6017581fb 100644 --- a/test/projection.jl +++ b/test/projection.jl @@ -107,6 +107,10 @@ struct NoSuperType end @test Tuple(ProjectTo(Any[1, 2 + 3im])(1:2)) === (1.0, 2.0 + 0.0im) @test ProjectTo(Any[true, false]) isa ProjectTo{NoTangent} + # projecting other things should still infer + @inferred ProjectTo([one, one]) + @inferred ProjectTo(["x", "y"]) + # empty arrays @test isempty(ProjectTo([])(1:0)) @test_throws DimensionMismatch ProjectTo(Int[])([2])