Skip to content

Commit 9ef12b3

Browse files
authored
decrease method count for eltype for Tuple, decrease max_methods (#58788)
The `eltype` function was one of the few functions in the sysimage with a `max_methods` value (the world-splitting threshold) greater than the default. This was a workaround for the unnecessarily large number of methods of `eltype(::Type{<:Tuple})`. The `max_methods` value was increased in PR #48322 to help effect inference.
1 parent 8d953e1 commit 9ef12b3

File tree

2 files changed

+12
-19
lines changed

2 files changed

+12
-19
lines changed

base/tuple.jl

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -269,10 +269,18 @@ first(t::Tuple) = t[1]
269269

270270
# eltype
271271

272-
eltype(::Type{Tuple{}}) = Bottom
273272
# the <: here makes the runtime a bit more complicated (needing to check isdefined), but really helps inference
274-
eltype(t::Type{<:Tuple{Vararg{E}}}) where {E} = @isdefined(E) ? (E isa Type ? E : Union{}) : _compute_eltype(t)
275-
eltype(t::Type{<:Tuple}) = _compute_eltype(t)
273+
_eltype_ntuple(t::Type{<:Tuple{Vararg{E}}}) where {E} = @isdefined(E) ? (E isa Type ? E : Union{}) : _compute_eltype(t)
274+
# We'd like to be able to infer eltype(::Tuple), so keep the number of eltype(::Type{<:Tuple}) methods at max_methods!
275+
function eltype(t::Type{<:Tuple})
276+
if t <: Tuple{}
277+
Bottom
278+
elseif t <: NTuple
279+
_eltype_ntuple(t)
280+
else
281+
_compute_eltype(t)
282+
end
283+
end
276284
function _compute_eltype(@nospecialize t)
277285
@_total_meta
278286
has_free_typevars(t) && return Any
@@ -297,21 +305,6 @@ function _compute_eltype(@nospecialize t)
297305
return r
298306
end
299307

300-
# We'd like to be able to infer eltype(::Tuple), which needs to be able to
301-
# look at these four methods:
302-
#
303-
# julia> methods(Base.eltype, Tuple{Type{<:Tuple}})
304-
# 4 methods for generic function "eltype" from Base:
305-
# [1] eltype(::Type{Union{}})
306-
# @ abstractarray.jl:234
307-
# [2] eltype(::Type{Tuple{}})
308-
# @ tuple.jl:199
309-
# [3] eltype(t::Type{<:Tuple{Vararg{E}}}) where E
310-
# @ tuple.jl:200
311-
# [4] eltype(t::Type{<:Tuple})
312-
# @ tuple.jl:209
313-
typeof(function eltype end).name.max_methods = UInt8(4)
314-
315308
# key/val types
316309
keytype(@nospecialize t::Tuple) = keytype(typeof(t))
317310
keytype(@nospecialize T::Type{<:Tuple}) = Int

test/ambiguous.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ end
348348
let need_to_handle_undef_sparam =
349349
Set{Method}(detect_unbound_args(Base; recursive=true, allowed_undefineds))
350350
pop!(need_to_handle_undef_sparam, which(Base._totuple, (Type{Tuple{Vararg{E}}} where E, Any, Any)))
351-
pop!(need_to_handle_undef_sparam, which(Base.eltype, Tuple{Type{Tuple{Any}}}))
351+
pop!(need_to_handle_undef_sparam, which(Base._eltype_ntuple, Tuple{Type{Tuple{Any}}}))
352352
pop!(need_to_handle_undef_sparam, first(methods(Base.same_names)))
353353
@test_broken isempty(need_to_handle_undef_sparam)
354354
pop!(need_to_handle_undef_sparam, which(Base._cat, Tuple{Any, AbstractArray}))

0 commit comments

Comments
 (0)