Title: Parametric Heap Usage Analysis for Functional Programs
1Parametric Heap Usage Analysis for Functional
Programs
- Leena Unnikrishnan
- Scott D. Stoller
2Motivation
- Goal upper bound on live heap usage.
- Understanding memory usage of programs.
- Guiding automatic methods, e.g., help program
transformations select space-efficient programs. - Required for time analysis running times depend
on memory allocation, garbage collection, etc. - Determining/verifying space usage of embedded
applications.
3Challenges
- Determine live heap usage over entire program
execution, and over multiple execution paths. - Unlike running times, live heap space increases
and decreases during execution. - Static determination of liveness of objects.
4Overview
- Input Program P in a functional language with
lists. - Output worst-case live heap usage of P expressed
in terms of sizes of Ps inputs. - Example
- Input program reverse(ls) to reverse a list
- Output Max live heap usage 2ls - 1, if
ls gt 0 - Method
- Transform functions in P into bound functions
that describe their heap space usage and related
metrics. - Simplify and rewrite bound functions into
recurrences. - Solve recurrences into closed forms.
5Outline
- Bound functions
- Recurrences containing the max operator
- Soundness checks for R functions
- Examples
- Related work
- Conclusion
6Bound Functions
- Construct three bound functions for each function
f in program P. - Arguments of each bound function for f(v1,,vn)
are the sizes v1s,,vns, of v1,,vn. - Size of a boolean or number its value
- Size of a flat list its length
7Bound Functions
- For f(v1,,vn) in the input program, construct
- Live heap space bound function, Sf(v1s,,vns)
upper bound on live heap use of f, for all
possible arguments v1,,vn, of sizes v1s,,vns. - New result-space bound function,
Nf(v1s,,vns,v1w,,vnw) upper bound on amount of
newly allocated space in the result of f, for all
possible arguments v1,,vn, of sizes v1s,,vns.
viw is the amount of new space in vi. - New is w.r.t. start of evaluation of call to f.
- Size bound function, Rf(v1s,,vns) upper bound
on size of result of f, for all possible
arguments v1,,vn, of sizes v1s,,vns.
8Live Heap Space Bound Functions
- For f(v1,,vn) e, Sf(v1s,,vns) Se.
- Example clauses in the definition of S..
- Sv 0, Scons(e1, e2) 1 max(Se1,
Se2) - Sf(e1, e2) max(Se1,
- Ne1 Se2,
- Ne1 Ne2
Sf(Re1, Re2))
9Size Bound Functions
- For f(v1,,vn) e, Rf(v1s,,vns) Re.
- Example clauses in the definition of R..
- Rv vs , Rcons(e1, e2) 1 Re2
- Rif p then e1 else e2
- max(Re1, Re2), if p contains car
- if Rp then Re1 else Re2
- Rf(e1,,en) Rf(Re1,, Ren)
10New Result-Space Bound Functions
- For f(v) in the input, the N function is Nf(vs,
vw). - f(e) in input program yields Nf(Re, Ne) in
bound functions. - New is w.r.t. the start of evaluation of
f(e). - vw is the part of v created in e.
- Nf() is the part of the result created in e or
the body of f. - For f(v) e, Nf(vs, vw) Ne.
- Example clauses in the definition of N..
- Nv vw , Ncons(e1, e2) 1 Ne2
- Nf(e1,,en) Nf(Re1,,Ren, Ne1,,Nen)
11New Result-Space Bound Functions
12Outline
- Bound functions
- Recurrences containing the max operator
- Soundness checks for R functions
- Examples
- Related work
- Conclusion
13Composite Recurrences
- Bound functions are simplified into recurrences
that may contain max. We call these composite
recurrences. - Example For insert in insertion sort,
- Rins(n) max(1n, 1Rins(n-1)), if
ngt0 - Solve composite recurrences using a library of
solution templates. -
14Solving Composite Recurrences Example
- For a recurrence of the form
- where, the arguments of max are equal for
- (a) base cases, i.e.,
- e1(n) e2(n) ac(n-b-i1), for
n in ij1, ijb - (b) all other values of n, i.e.
- e1(n) e2(n) ae1(n-b), for ngt(ijb)
- the solution is T(n) e1(n), for ngt(ij).
15Outline
- Bound functions
- Recurrences containing the max operator
- Soundness checks for R functions
- Examples
- Related work
- Conclusion
16Soundness Checks on Uses of R Functions
- Intuition It is not necessarily the object with
the largest size that leads to worst-case live
heap usage. - R function results are arguments to S and N
functions. - Consider the R function, Rf(n), of greaterlist
in quicksort. - Rf(n) is the maximum of a set of values. Rf
is a complex R function.
17Soundness Checks on Uses of R functions
- Using the result of Rf(n) as an argument to a
bound function, say Sg, yields a true upper bound
down the line, only if the following hold - Case 1 If Sg(Rf(n)) is a recursive call in the
body of Sg, then using the result of Rf(n) in the
definition of Sg, yields the maximum result.
E.g., this holds for recurrences of the form - where
- base values of T are monotonically increasing
- largest base value lt value at smallest non-base
input - e(n) is monotonically increasing w.r.t. n
- Case 2 check if the solution of Sg is
monotonically increasing w.r.t. its single
argument.
18Improving Solvability of Recurrences
- Eliminate unused arguments from bound functions.
- Simplifies recurrences. May eliminate dependence
on other recurrences. - Use invariants to simplify max expressions.
- May simplify composite recurrences or make them
regular. - Example Sf(e1,,en) ? Nf(e1,,en,0,,0).
- So, max(Sf(e1,,en),
Nf(e1,,en,0,,0)) Sf(e1,,en). - Use simpler, approximate definitions of N
functions. - precise Nf(v1s,,vns, v1w,,vnw) Ne
- approx Nf(v1s,,vns)
Rf(v1s,,vns) - In our examples, even with this simplification,
we get tight bounds on worst-case space usage.
19Examples
Program S Function (vs gt c) Tight?
reverse(ls) Srev(lss) 2lss 1 Yes
insertion-sort(ls) Sis(lss) 2lss 1 Yes
selection-sort(ls) Sss(lss) lss(lss1)/2 Yes
merge-sort(ls) Sms(lss) 2lss 1 Yes
quicksort(ls) Sqs(lss) 32lss-1 1 No
longest-common-subseq(ls1, ls2) Slcs(ls1s, ls2s) 2ls2s 2 Yes
string-edit-distance(ls1, ls2) Sse(ls1s, ls2s) 2ls1 2ls2 1 Yes
binomial-coefficient(n, m) Sbc(ns,ms) 2ms2, if msltns Yes
- Derives linear, quadratic, and exponential
bounds. - Bounds are tight (exact) for all examples except
quicksort.
20Related Work
- Timing analysis using recurrence relations
Wegbreit 75, LeMetayer 88. - Static prediction of heap space Hofmann Jost
03 - Applies to a linearly-typed functional language.
- Derives linear heap bounds.
- Parametric prediction of heap memory requirements
Braberman et al 08 - Java-like language.
- Region-based memory management models heap
recovery. - Derives polynomial bounds.
21Related Work
- Memory resource bounds for low-level programs
Chin et al 08 - Assembly-like language.
- Explicit deallocation to model heap recovery.
- Derives linear bounds.
- Cost analysis Albert et al 07
- Java bytecode.
- Escape analysis to model heap recovery.
- Analysis for live heap at ISMM 09!
22Conclusion
- Automatic, accurate analysis of live heap usage
of functional programs. - Framework of bound functions that describe live
heap usage of input programs. - Methods to simplify and solve composite
recurrences. - Soundness checks involving monotonicity and
monotonicitylike properties of recurrences. - Results are at source-code level and are easy to
understand. - Results are not restricted to any complexity
class.
23Thank You!
24Extensions to Handle Other Data Types
- Specialize R functions and size arguments to each
data type. - Binary Trees
- Possible size measures height, total.
- R functions Rheight, Rtotal
- Extend S, N, R transformations to new data type.
- Rheighttree(e, el, er) 1 max(Rheightel,
Rheighter) - Rtotal tree(e, el, er) 1 Rtotalel
Rtotaler - Live heap usage of a given program may be better
represented in terms of one specific size
measure, than others.
25Example
Input Program
26Example
Input Program
27Example
Closed Forms