Skip to content

Commit 7cb2f13

Browse files
committed
small optimizer improvements to tupleref_elim_pass and effect_free
improved versions of some tuple functions
1 parent c8ae026 commit 7cb2f13

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

base/inference.jl

+15-11
Original file line numberDiff line numberDiff line change
@@ -1851,7 +1851,7 @@ end
18511851
# cannot be affected by previous calls, except assignment nodes
18521852
function effect_free(e::ANY, sv, allow_volatile::Bool)
18531853
if isa(e,Symbol) || isa(e,SymbolNode) || isa(e,Number) || isa(e,String) ||
1854-
isa(e,TopNode) || isa(e,QuoteNode) || isa(e,Type)
1854+
isa(e,TopNode) || isa(e,QuoteNode) || isa(e,Type) || isa(e,Tuple)
18551855
return true
18561856
end
18571857
if isa(e,Expr)
@@ -2642,20 +2642,24 @@ function tupleref_elim_pass(e::Expr, sv)
26422642
if isa(ei,Expr)
26432643
tupleref_elim_pass(ei, sv)
26442644
if is_known_call(ei, tupleref, sv) && length(ei.args)==3 &&
2645-
isa(ei.args[2],Expr) && isa(ei.args[3],Int)
2645+
isa(ei.args[3],Int)
26462646
e1 = ei.args[2]
26472647
j = ei.args[3]
2648-
if is_known_call(e1, tuple, sv) && (1 <= j < length(e1.args))
2649-
ok = true
2650-
for k = 2:length(e1.args)
2651-
k == j+1 && continue
2652-
if !effect_free(e1.args[k], sv, true)
2653-
ok = false; break
2648+
if isa(e1,Expr)
2649+
if is_known_call(e1, tuple, sv) && (1 <= j < length(e1.args))
2650+
ok = true
2651+
for k = 2:length(e1.args)
2652+
k == j+1 && continue
2653+
if !effect_free(e1.args[k], sv, true)
2654+
ok = false; break
2655+
end
2656+
end
2657+
if ok
2658+
e.args[i] = e1.args[j+1]
26542659
end
26552660
end
2656-
if ok
2657-
e.args[i] = e1.args[j+1]
2658-
end
2661+
elseif isa(e1,Tuple) && (1 <= j <= length(e1))
2662+
e.args[i] = e1[j]
26592663
end
26602664
end
26612665
end

base/tuple.jl

+8-2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ ntuple(f::Function, n::Integer) =
3636
n==5 ? (f(1),f(2),f(3),f(4),f(5),) :
3737
tuple(ntuple(n-2,f)..., f(n-1), f(n))
3838

39+
argtail(x, rest...) = rest
40+
tail(x::Tuple) = argtail(x...)
41+
3942
# 0 argument function
4043
map(f::Callable) = f()
4144
# 1 argument function
@@ -44,7 +47,7 @@ map(f::Callable, t::(Any,)) = (f(t[1]),)
4447
map(f::Callable, t::(Any, Any)) = (f(t[1]), f(t[2]))
4548
map(f::Callable, t::(Any, Any, Any)) = (f(t[1]), f(t[2]), f(t[3]))
4649
map(f::Callable, t::(Any, Any, Any, Any)) = (f(t[1]), f(t[2]), f(t[3]), f(t[4]))
47-
map(f::Callable, t::Tuple) = tuple([f(ti) for ti in t]...)
50+
map(f::Callable, t::Tuple) = tuple(f(t[1]), map(f,tail(t))...)
4851
# 2 argument function
4952
map(f::Callable, t::(), s::()) = ()
5053
map(f::Callable, t::(Any,), s::(Any,)) = (f(t[1],s[1]),)
@@ -108,7 +111,10 @@ end
108111
isempty(x::()) = true
109112
isempty(x::Tuple) = false
110113

111-
reverse(x::Tuple) = (n=length(x); tuple([x[n-k+1] for k=1:n]...))
114+
revargs() = ()
115+
revargs(x, r...) = tuple(revargs(r...)..., x)
116+
117+
reverse(t::Tuple) = revargs(t...)
112118

113119
## specialized reduction ##
114120

0 commit comments

Comments
 (0)