<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="generator" content="AsciiDoc 8.6.8">
<title>ProfilingTheStack</title>
<link rel="stylesheet" href="./asciidoc.css" type="text/css">
<link rel="stylesheet" href="./pygments.css" type="text/css">


<script type="text/javascript" src="./asciidoc.js"></script>
<script type="text/javascript">
/*<![CDATA[*/
asciidoc.install();
/*]]>*/
</script>
<link rel="stylesheet" href="./mlton.css" type="text/css"/>
</head>
<body class="article">
<div id="banner">
<div id="banner-home">
<a href="./Home">MLton 20130715</a>
</div>
</div>
<div id="header">
<h1>ProfilingTheStack</h1>
</div>
<div id="content">
<div id="preamble">
<div class="sectionbody">
<div class="paragraph"><p>For all forms of <a href="Profiling">Profiling</a>, you can gather counts for all
functions on the stack, not just the currently executing function.  To
do so, compile your program with <span class="monospaced">-profile-stack true</span>.  For example,
suppose that <span class="monospaced">list-rev.sml</span> contains the following.</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span class="k">fun</span><span class="w"> </span><span class="n">append</span><span class="w"> </span><span class="p">(</span><span class="n">l1</span><span class="p">,</span><span class="w"> </span><span class="n">l2</span><span class="p">)</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">   </span><span class="k">case</span><span class="w"> </span><span class="n">l1</span><span class="w"> </span><span class="k">of</span><span class="w"></span>
<span class="w">      </span><span class="p">[]</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">l2</span><span class="w"></span>
<span class="w">    </span><span class="p">|</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="n">::</span><span class="w"> </span><span class="n">l1</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="n">::</span><span class="w"> </span><span class="n">append</span><span class="w"> </span><span class="p">(</span><span class="n">l1</span><span class="p">,</span><span class="w"> </span><span class="n">l2</span><span class="p">)</span><span class="w"></span>

<span class="k">fun</span><span class="w"> </span><span class="n">rev</span><span class="w"> </span><span class="n">l</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">   </span><span class="k">case</span><span class="w"> </span><span class="n">l</span><span class="w"> </span><span class="k">of</span><span class="w"></span>
<span class="w">      </span><span class="p">[]</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="p">[]</span><span class="w"></span>
<span class="w">    </span><span class="p">|</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="n">::</span><span class="w"> </span><span class="n">l</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">append</span><span class="w"> </span><span class="p">(</span><span class="n">rev</span><span class="w"> </span><span class="n">l</span><span class="p">,</span><span class="w"> </span><span class="p">[</span><span class="n">x</span><span class="p">])</span><span class="w"></span>

<span class="k">val</span><span class="w"> </span><span class="n">l</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">List</span><span class="p">.</span><span class="n">tabulate</span><span class="w"> </span><span class="p">(</span><span class="mi">1000</span><span class="p">,</span><span class="w"> </span><span class="k">fn</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">i</span><span class="p">)</span><span class="w"></span>
<span class="k">val</span><span class="w"> </span><span class="p">_</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="n">+</span><span class="w"> </span><span class="n">hd</span><span class="w"> </span><span class="p">(</span><span class="n">rev</span><span class="w"> </span><span class="n">l</span><span class="p">)</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>Compile with stack profiling and then run the program.</p></div>
<div class="listingblock">
<div class="content monospaced">
<pre>% mlton -profile alloc -profile-stack true list-rev.sml
% ./list-rev</pre>
</div></div>
<div class="paragraph"><p>Display the profiling data.</p></div>
<div class="listingblock">
<div class="content monospaced">
<pre>% mlprof -show-line true list-rev mlmon.out
6,030,136 bytes allocated (108,336 bytes by GC)
       function          cur  stack  GC
----------------------- ----- ----- ----
append  list-rev.sml: 1 97.6% 97.6% 1.4%
&lt;gc&gt;                     1.8%  0.0% 1.8%
&lt;main&gt;                   0.4% 98.2% 1.8%
rev  list-rev.sml: 6     0.2% 97.6% 1.8%</pre>
</div></div>
<div class="paragraph"><p>In the above table, we see that <span class="monospaced">rev</span>, defined on line 6 of
<span class="monospaced">list-rev.sml</span>, is only responsible for 0.2% of the allocation, but is
on the stack while 97.6% of the allocation is done by the user program
and while 1.8% of the allocation is done by the garbage collector.</p></div>
<div class="paragraph"><p>The run-time performance impact of <span class="monospaced">-profile-stack true</span> can be
noticeable since there is some extra bookkeeping at every nontail call
and return.</p></div>
</div>
</div>
</div>
<div id="footnotes"><hr></div>
<div id="footer">
<div id="footer-text">
</div>
<div id="footer-badges">
</div>
</div>
</body>
</html>
