Skip to content

Commit 41ea4bf

Browse files
committed
inference: redesign call complexity limiter
1 parent 9c8d66d commit 41ea4bf

File tree

6 files changed

+170
-98
lines changed

6 files changed

+170
-98
lines changed

base/array.jl

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -655,18 +655,28 @@ function _collect_indices(indsA, A)
655655
copy!(B, CartesianRange(indices(B)), A, CartesianRange(indsA))
656656
end
657657

658+
# define this as a macro so that the call to Inference
659+
# gets inlined into the caller before recursion detection
660+
# gets a chance to see it, so that recursive calls to the caller
661+
# don't trigger the inference limiter
658662
if isdefined(Core, :Inference)
659-
_default_eltype(@nospecialize itrt) = Core.Inference.return_type(first, Tuple{itrt})
663+
macro default_eltype(itrt)
664+
return quote
665+
Core.Inference.return_type(first, Tuple{$(esc(itrt))})
666+
end
667+
end
660668
else
661-
_default_eltype(@nospecialize itr) = Any
669+
macro default_eltype(itrt)
670+
return :(Any)
671+
end
662672
end
663673

664674
_array_for(::Type{T}, itr, ::HasLength) where {T} = Array{T,1}(Int(length(itr)::Integer))
665675
_array_for(::Type{T}, itr, ::HasShape) where {T} = similar(Array{T}, indices(itr))
666676

667677
function collect(itr::Generator)
668678
isz = iteratorsize(itr.iter)
669-
et = _default_eltype(typeof(itr))
679+
et = @default_eltype(typeof(itr))
670680
if isa(isz, SizeUnknown)
671681
return grow_to!(Array{et,1}(0), itr)
672682
else
@@ -680,12 +690,12 @@ function collect(itr::Generator)
680690
end
681691

682692
_collect(c, itr, ::EltypeUnknown, isz::SizeUnknown) =
683-
grow_to!(_similar_for(c, _default_eltype(typeof(itr)), itr, isz), itr)
693+
grow_to!(_similar_for(c, @default_eltype(typeof(itr)), itr, isz), itr)
684694

685695
function _collect(c, itr, ::EltypeUnknown, isz::Union{HasLength,HasShape})
686696
st = start(itr)
687697
if done(itr,st)
688-
return _similar_for(c, _default_eltype(typeof(itr)), itr, isz)
698+
return _similar_for(c, @default_eltype(typeof(itr)), itr, isz)
689699
end
690700
v1, st = next(itr, st)
691701
collect_to_with_first!(_similar_for(c, typeof(v1), itr, isz), v1, itr, st)

base/dict.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,9 @@ associative_with_eltype(DT_apply, kv, ::TP{K,V}) where {K,V} = DT_apply(K, V)(kv
158158
associative_with_eltype(DT_apply, kv::Generator, ::TP{K,V}) where {K,V} = DT_apply(K, V)(kv)
159159
associative_with_eltype(DT_apply, ::Type{Pair{K,V}}) where {K,V} = DT_apply(K, V)()
160160
associative_with_eltype(DT_apply, ::Type) = DT_apply(Any, Any)()
161-
associative_with_eltype(DT_apply::F, kv, t) where {F} = grow_to!(associative_with_eltype(DT_apply, _default_eltype(typeof(kv))), kv)
161+
associative_with_eltype(DT_apply::F, kv, t) where {F} = grow_to!(associative_with_eltype(DT_apply, @default_eltype(typeof(kv))), kv)
162162
function associative_with_eltype(DT_apply::F, kv::Generator, t) where F
163-
T = _default_eltype(typeof(kv))
163+
T = @default_eltype(typeof(kv))
164164
if T <: Union{Pair, Tuple{Any, Any}} && _isleaftype(T)
165165
return associative_with_eltype(DT_apply, kv, T)
166166
end

0 commit comments

Comments
 (0)