Skip to content

Commit

Permalink
make sysimg build faster (almost 2x) using more efficient Expr constr…
Browse files Browse the repository at this point in the history
…uctor
  • Loading branch information
JeffBezanson committed Mar 1, 2013
1 parent 96d297e commit 396e6c4
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 15 deletions.
9 changes: 6 additions & 3 deletions base/expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@ esc(e::ANY) = expr(:escape, {e})

## expressions ##

expr(hd::Symbol, args::ANY...) = Expr(hd, {args...}, Any)
expr(hd::Symbol, args::Array{Any,1}) = Expr(hd, args, Any)
copy(e::Expr) = Expr(e.head, isempty(e.args) ? e.args : astcopy(e.args), e.typ)
expr(hd::Symbol, args::ANY...) = Expr(hd, args...)
expr(hd::Symbol, args::Array{Any,1}) = (e=Expr(hd); e.args=args; e)
copy(e::Expr) = (n = Expr(e.head);
n.args = astcopy(e.args);
n.typ = e.typ;
n)
copy(s::SymbolNode) = SymbolNode(s.name, s.typ)
copy(n::GetfieldNode) = GetfieldNode(n.value, n.name, n.typ)

Expand Down
15 changes: 11 additions & 4 deletions base/inference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,6 @@ const fieldtype_tfunc = function (A, s, name)
Type{t}
end
t_func[fieldtype] = (2, 2, fieldtype_tfunc)
t_func[Expr] = (3, 3, (a,b,c)->Expr)
t_func[Box] = (1, 1, (a,)->Box)

# TODO: handle e.g. apply_type(T, R::Union(Type{Int32},Type{Float64}))
Expand Down Expand Up @@ -388,6 +387,12 @@ function builtin_tfunction(f::ANY, args::ANY, argtypes::ANY)
return (isa(a,CompositeKind) && subtype(a,Array) ?
a.parameters[1] : Any)
end
if is(f,Expr)
if length(argtypes) < 1
return None
end
return Expr
end
tf = get(t_func::ObjectIdDict, f, false)
if is(tf,false)
# struct constructor
Expand Down Expand Up @@ -1673,7 +1678,7 @@ function inlineable(f, e::Expr, sv, enclosing_ast)
if occ > 1
vnew = unique_name(enclosing_ast)
add_variable(enclosing_ast, vnew, aeitype)
push!(stmts, Expr(:(=), {vnew, aei}, Any))
push!(stmts, Expr(:(=), vnew, aei))
argexprs[i] = aeitype===Any ? vnew : SymbolNode(vnew,aeitype)
elseif !isType(aeitype) && !effect_free(aei)
push!(stmts, aei)
Expand Down Expand Up @@ -1705,7 +1710,9 @@ function mk_tupleref(texpr, i)
end

function mk_tuplecall(args)
Expr(:call1, {top_tuple, args...}, tuple(map(exprtype, args)...))
e = Expr(:call1, top_tuple, args...)
e.typ = tuple(map(exprtype, args)...)
e
end

const basenumtype = Union(Int32,Int64,Float32,Float64,Complex64,Complex128,Rational)
Expand Down Expand Up @@ -1958,7 +1965,7 @@ function tuple_elim_pass(ast::Expr)
else
elty = exprtype(tupelt)
tmpv = unique_name(ast)
tmp = Expr(:(=), {tmpv,tupelt}, Any)
tmp = Expr(:(=), tmpv, tupelt)
add_variable(ast, tmpv, elty)
insert!(body, i+n_ins, tmp)
vals[j] = SymbolNode(tmpv, elty)
Expand Down
2 changes: 1 addition & 1 deletion base/multi.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1128,7 +1128,7 @@ function localize_vars(expr, esca)
if esca
v = map(esc,v)
end
Expr(:localize, {:(()->($expr)), v...}, Any)
Expr(:localize, :(()->($expr)), v...)
end

macro spawn(expr)
Expand Down
14 changes: 8 additions & 6 deletions src/alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -832,16 +832,18 @@ jl_expr_t *jl_exprn(jl_sym_t *head, size_t n)
// do anything without being able to make Exprs.
JL_CALLABLE(jl_f_new_expr)
{
JL_NARGS(Expr, 3, 3);
JL_NARGSV(Expr, 1);
JL_TYPECHK(Expr, symbol, args[0]);
if (!jl_typeis(args[1], (jl_value_t*)jl_array_any_type)) {
jl_type_error("Expr", (jl_value_t*)jl_array_any_type, args[1]);
}
jl_array_t *ar = jl_alloc_cell_1d(nargs-1);
JL_GC_PUSH(&ar);
for(size_t i=1; i < nargs; i++)
jl_cellset(ar, i-1, args[i]);
jl_expr_t *ex = (jl_expr_t*)alloc_4w();
ex->type = (jl_type_t*)jl_expr_type;
ex->head = (jl_sym_t*)args[0];
ex->args = (jl_array_t*)args[1];
ex->etype = args[2];
ex->args = ar;
ex->etype = (jl_value_t*)jl_any_type;
JL_GC_POP();
return (jl_value_t*)ex;
}

Expand Down
2 changes: 1 addition & 1 deletion src/julia-syntax.scm
Original file line number Diff line number Diff line change
Expand Up @@ -2104,7 +2104,7 @@ So far only the second case can actually occur.
((not (any (lambda (x)
(match '($ (tuple (... x))) x))
e))
`(call (top expr) ,@(map expand-backquote e)))
`(call (top Expr) ,@(map expand-backquote e)))
(else
(let loop ((p (cdr e)) (q '()))
(if (null? p)
Expand Down

1 comment on commit 396e6c4

@StefanKarpinski
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nicely done. Related: I've often wondered if the expr function couldn't just be replaced by more constructor methods for Expr.

Please sign in to comment.