From 11ae2d1e7af05c3410a368338c7d13a49c2d2ccd Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Tue, 2 May 2006 14:07:28 +0000 Subject: * Memory reduction: replaced expensive calls to ATmakeApplList by ATmakeApplArray, and got rid of ATreverse in substitute(). --- src/libexpr/nixexpr.cc | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) (limited to 'src/libexpr/nixexpr.cc') diff --git a/src/libexpr/nixexpr.cc b/src/libexpr/nixexpr.cc index 05c1ee45ceea..f88e28289047 100644 --- a/src/libexpr/nixexpr.cc +++ b/src/libexpr/nixexpr.cc @@ -142,12 +142,12 @@ ATerm bottomupRewrite(TermFun & f, ATerm e) if (ATgetType(e) == AT_APPL) { AFun fun = ATgetAFun(e); int arity = ATgetArity(fun); - ATermList args = ATempty; + ATerm args[arity]; - for (int i = arity - 1; i >= 0; i--) - args = ATinsert(args, bottomupRewrite(f, ATgetArgument(e, i))); + for (int i = 0; i < arity; ++i) + args[i] = bottomupRewrite(f, ATgetArgument(e, i)); - e = (ATerm) ATmakeApplList(fun, args); + e = (ATerm) ATmakeApplArray(fun, args); } else if (ATgetType(e) == AT_LIST) { @@ -285,19 +285,24 @@ Expr substitute(const ATermMap & subs, Expr e) if (ATgetType(e) == AT_APPL) { AFun fun = ATgetAFun(e); int arity = ATgetArity(fun); - ATermList args = ATempty; + ATerm args[arity]; - for (int i = arity - 1; i >= 0; i--) - args = ATinsert(args, substitute(subs, ATgetArgument(e, i))); + for (int i = 0; i < arity; ++i) + args[i] = substitute(subs, ATgetArgument(e, i)); - return (ATerm) ATmakeApplList(fun, args); + return (ATerm) ATmakeApplArray(fun, args); } if (ATgetType(e) == AT_LIST) { + unsigned int len = ATgetLength((ATermList) e); + ATerm es[len]; + ATermIterator i((ATermList) e); + for (unsigned int j = 0; i; ++i, ++j) + es[j] = substitute(subs, *i); ATermList out = ATempty; - for (ATermIterator i((ATermList) e); i; ++i) - out = ATinsert(out, substitute(subs, *i)); - return (ATerm) ATreverse(out); + for (unsigned int j = len; j; --j) + out = ATinsert(out, es[j - 1]); + return (ATerm) out; } return e; -- cgit 1.4.1