Skip to content

Commit 5cceb14

Browse files
nsajkomateuszbaran
andauthored
prevent some unnecessary uses of @pure in traits.jl (#1177)
There's no reason for a `Base.@pure` annotation in cases where Julia successfully infers `:consistent`, `:effect_free` and `:terminates_globally` without the annotation. Effect inference tested with Julia v1.9.2, the latest release. While the effect inference is not favorable for `_Length(::Int...)`, as the consistency is not inferred, `_Length` actually seems to be an internal function meant as merely an implementation detail for `Length`, whose effect inference is perfect for the relevant method. The script for testing the effect inference is appended. `:consistent` (`+c`), `:effect_free` (`+e`) and `:terminates_globally` (`+t`) are successfully inferred in each case: ```julia using StaticArrays const d = StaticArraysCore.Dynamic() const test_sizes = ( (1,2,3,4,5,6,7,8,9,1,2,3), (7,8,9,1,2,3,1,2,3,4,5,6), (2,3,4,2,3,4,2,3,4,2,3,4), (1,2,3,4,5,6,7,8,9,1,2,d), (7,8,9,1,2,3,1,2,3,4,5,d), (2,3,4,2,3,4,2,3,4,2,3,d), (d,2,3,4,5,6,7,8,9,1,2,3), (d,8,9,1,2,3,1,2,3,4,5,6), (d,3,4,2,3,4,2,3,4,2,3,4), (1,2,3,4,d,6,7,8,d,1,2,3), (7,8,9,1,d,3,1,2,d,4,5,6), (2,3,4,2,d,4,2,3,d,2,3,4), ) const test_lengths = (0, 1, 2, 3, d) const test_size_types = map((s -> Size{s}), test_sizes) const test_length_types = map((l -> Length{l}), test_lengths) const test_tuple_int_types = ( Tuple{}, Tuple{Int}, Tuple{Int,Int}, Tuple{Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int}, Tuple{Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int}, Tuple{Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int}, ) println("Length(::Size)") for S ∈ test_size_types display(Base.infer_effects(Length, (S,))) end println() println("(::Type{Tuple})(::Size)") for S ∈ test_size_types display(Base.infer_effects(Tuple, (S,))) end println() println("length(::Size)") for S ∈ test_size_types display(Base.infer_effects(length, (S,))) end println() println("length_val(::Size)") for S ∈ test_size_types display(Base.infer_effects(StaticArrays.length_val, (S,))) end println() println("==(::Size, ::Tuple{Vararg{Int}})") for S ∈ test_size_types, T ∈ test_tuple_int_types display(Base.infer_effects(==, (S, T))) end println() println("==(::Tuple{Vararg{Int}}, ::Size)") for S ∈ test_size_types, T ∈ test_tuple_int_types display(Base.infer_effects(==, (T, S))) end println() println("prod(::Size)") for S ∈ test_size_types display(Base.infer_effects(prod, (S,))) end println() println("size_tuple(::Size)") for S ∈ test_size_types display(Base.infer_effects(StaticArrays.size_tuple, (S,))) end println() println("==(::Length, ::Int)") for L ∈ test_length_types display(Base.infer_effects(==, (L, Int))) end println() println("==(::Int, ::Length)") for L ∈ test_length_types display(Base.infer_effects(==, (Int, L))) end println() println("sizematch(::Size, ::Size)") for S1 ∈ test_size_types, S2 ∈ test_size_types display(Base.infer_effects(StaticArrays.sizematch, (S1, S2))) end println() println("diagsize(::Size)") for S ∈ test_size_types display(Base.infer_effects(StaticArrays.diagsize, (S,))) end println() ``` Co-authored-by: Mateusz Baran <mateuszbaran89@gmail.com>
1 parent 0d16e67 commit 5cceb14

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "StaticArrays"
22
uuid = "90137ffa-7385-5640-81b9-e52037218182"
3-
version = "1.9.1"
3+
version = "1.9.2"
44

55
[deps]
66
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

src/traits.jl

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,32 +38,32 @@ Length(a::AbstractArray) = Length(Size(a))
3838
Length(::Type{A}) where {A <: AbstractArray} = Length(Size(A))
3939
@pure Length(L::Int) = Length{L}()
4040
Length(::Size{S}) where {S} = _Length(S...)
41-
@pure _Length(S::Int...) = Length{prod(S)}()
41+
_Length(S::Int...) = Length{prod(S)}()
4242
@inline _Length(S...) = Length{Dynamic()}()
4343

44-
# Some @pure convenience functions for `Size`
45-
@pure (::Type{Tuple})(::Size{S}) where {S} = S
44+
# Some convenience functions for `Size`
45+
(::Type{Tuple})(::Size{S}) where {S} = S
4646

4747
@pure getindex(::Size{S}, i::Int) where {S} = i <= length(S) ? S[i] : 1
4848

49-
@pure length(::Size{S}) where {S} = length(S)
50-
@pure length_val(::Size{S}) where {S} = Val{length(S)}
49+
length(::Size{S}) where {S} = length(S)
50+
length_val(::Size{S}) where {S} = Val{length(S)}
5151

5252
# Note - using === here, as Base doesn't inline == for tuples as of julia-0.6
53-
@pure Base.:(==)(::Size{S}, s::Tuple{Vararg{Int}}) where {S} = S === s
54-
@pure Base.:(==)(s::Tuple{Vararg{Int}}, ::Size{S}) where {S} = s === S
53+
Base.:(==)(::Size{S}, s::Tuple{Vararg{Int}}) where {S} = S === s
54+
Base.:(==)(s::Tuple{Vararg{Int}}, ::Size{S}) where {S} = s === S
5555

56-
@pure Base.prod(::Size{S}) where {S} = prod(S)
56+
Base.prod(::Size{S}) where {S} = prod(S)
5757

5858
Base.LinearIndices(::Size{S}) where {S} = LinearIndices(S)
5959

60-
@pure size_tuple(::Size{S}) where {S} = Tuple{S...}
60+
size_tuple(::Size{S}) where {S} = Tuple{S...}
6161

6262
# Some @pure convenience functions for `Length`
6363
@pure (::Type{Int})(::Length{L}) where {L} = Int(L)
6464

65-
@pure Base.:(==)(::Length{L}, l::Int) where {L} = L == l
66-
@pure Base.:(==)(l::Int, ::Length{L}) where {L} = l == L
65+
Base.:(==)(::Length{L}, l::Int) where {L} = L == l
66+
Base.:(==)(l::Int, ::Length{L}) where {L} = l == L
6767

6868
"""
6969
sizematch(::Size, ::Size)
@@ -72,7 +72,7 @@ Base.LinearIndices(::Size{S}) where {S} = LinearIndices(S)
7272
Determine whether two sizes match, in the sense that they have the same
7373
number of dimensions, and their dimensions match as determined by [`dimmatch`](@ref).
7474
"""
75-
@pure sizematch(::Size{S1}, ::Size{S2}) where {S1, S2} = sizematch(S1, S2)
75+
sizematch(::Size{S1}, ::Size{S2}) where {S1, S2} = sizematch(S1, S2)
7676
@inline sizematch(::Tuple{}, ::Tuple{}) = true
7777
@inline sizematch(S1::Tuple{Vararg{StaticDimension, N}}, S2::Tuple{Vararg{StaticDimension, N}}) where {N} =
7878
dimmatch(S1[1], S2[1]) && sizematch(Base.tail(S1), Base.tail(S2))
@@ -115,4 +115,4 @@ end
115115

116116
# Return the "diagonal size" of a matrix - the minimum of the two dimensions
117117
diagsize(A::StaticMatrix) = diagsize(Size(A))
118-
@pure diagsize(::Size{S}) where {S} = min(S...)
118+
diagsize(::Size{S}) where {S} = min(S...)

0 commit comments

Comments
 (0)