Skip to content

Commit 633e920

Browse files
committed
Improve type stability
1 parent b77b0af commit 633e920

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

src/varnamedtuple.jl

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,30 @@ function _setindex!!(arr::AbstractArray, value, optic::IndexLens)
2727
end
2828

2929
# Some utilities for checking what sort of indices we are dealing with.
30-
_has_colon(::T) where {T<:Tuple} = any(x <: Colon for x in T.parameters)
31-
function _is_multiindex(::T) where {T<:Tuple}
32-
return any(x <: UnitRange || x <: Colon for x in T.parameters)
30+
# The non-generated function implementations of these would be
31+
# _has_colon(::T) where {T<:Tuple} = any(x <: Colon for x in T.parameters)
32+
# function _is_multiindex(::T) where {T<:Tuple}
33+
# return any(x <: UnitRange || x <: Colon for x in T.parameters)
34+
# end
35+
# However, constant propagation sometimes fails if the index tuple is too big (e.g. length
36+
# 4), so we play it safe and use generated functions. Constant propagating these is
37+
# important, because many functions choose different paths based on their values, which
38+
# would lead to type instability if they were only evaluated at runtime.
39+
@generated function _has_colon(::T) where {T<:Tuple}
40+
for x in T.parameters
41+
if x <: Colon
42+
return :(true)
43+
end
44+
end
45+
return :(false)
46+
end
47+
@generated function _is_multiindex(::T) where {T<:Tuple}
48+
for x in T.parameters
49+
if x <: UnitRange || x <: Colon
50+
return :(true)
51+
end
52+
end
53+
return :(false)
3354
end
3455

3556
"""

0 commit comments

Comments
 (0)