Skip to content

Commit

Permalink
reduce max Union length in tmerge (#30833)
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffBezanson authored Jan 31, 2019
1 parent 2bae4ae commit a811eac
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
5 changes: 3 additions & 2 deletions base/compiler/typelimits.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#########################

const MAX_TYPEUNION_COMPLEXITY = 3
const MAX_TYPEUNION_LENGTH = 3
const MAX_INLINE_CONST_SIZE = 256

#########################
Expand Down Expand Up @@ -373,7 +374,7 @@ function tmerge(@nospecialize(typea), @nospecialize(typeb))
end
end
u = Union{types...}
if unioncomplexity(u) <= MAX_TYPEUNION_COMPLEXITY
if unionlen(u) <= MAX_TYPEUNION_LENGTH && unioncomplexity(u) <= MAX_TYPEUNION_COMPLEXITY
# don't let type unions get too big, if the above didn't reduce it enough
return u
end
Expand All @@ -400,7 +401,7 @@ function tuplemerge(a::DataType, b::DataType)
p = Vector{Any}(undef, lt + vt)
for i = 1:lt
ui = Union{ap[i], bp[i]}
if unioncomplexity(ui) < MAX_TYPEUNION_COMPLEXITY
if unionlen(ui) <= MAX_TYPEUNION_LENGTH && unioncomplexity(ui) <= MAX_TYPEUNION_COMPLEXITY
p[i] = ui
else
p[i] = Any
Expand Down
7 changes: 5 additions & 2 deletions base/compiler/typeutils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,13 @@ end

# unioncomplexity estimates the number of calls to `tmerge` to obtain the given type by
# counting the Union instances, taking also into account those hidden in a Tuple or UnionAll
unioncomplexity(u::Union) = 1 + unioncomplexity(u.a) + unioncomplexity(u.b)
function unioncomplexity(u::Union)
inner = max(unioncomplexity(u.a), unioncomplexity(u.b))
return inner == 0 ? 0 : 1 + inner
end
function unioncomplexity(t::DataType)
t.name === Tuple.name || return 0
c = 0
c = 1
for ti in t.parameters
ci = unioncomplexity(ti)
if ci > c
Expand Down
6 changes: 2 additions & 4 deletions test/compiler/inference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,8 @@ tmerge_test(Tuple{}, Tuple{Complex, Vararg{Union{ComplexF32, ComplexF64}}},
Tuple{Vararg{Complex}})
@test Core.Compiler.tmerge(Tuple{}, Union{Int16, Nothing, Tuple{ComplexF32, ComplexF32}}) ==
Union{Int16, Nothing, Tuple{Vararg{ComplexF32}}}
@test Core.Compiler.tmerge(Int32, Union{Int16, Nothing, Tuple{ComplexF32, ComplexF32}}) ==
Union{Int16, Int32, Nothing, Tuple{ComplexF32, ComplexF32}}
@test Core.Compiler.tmerge(Union{Int32, Nothing, Tuple{ComplexF32}}, Union{Int16, Nothing, Tuple{ComplexF32, ComplexF32}}) ==
Union{Int16, Int32, Nothing, Tuple{Vararg{ComplexF32}}}
@test Core.Compiler.tmerge(Union{Int32, Nothing, Tuple{ComplexF32}}, Union{Int32, Nothing, Tuple{ComplexF32, ComplexF32}}) ==
Union{Int32, Nothing, Tuple{Vararg{ComplexF32}}}

# issue 9770
@noinline x9770() = false
Expand Down

0 comments on commit a811eac

Please sign in to comment.