<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="generator" content="Asciidoctor 2.0.26">
<title>ProfilingTheStack</title>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Open+Sans:300,300italic,400,400italic,600,600italic%7CNoto+Serif:400,400italic,700,700italic%7CDroid+Sans+Mono:400,700">
<link rel="stylesheet" href="./asciidoctor.css">
<link rel="stylesheet" href="./mlton.css">

</head>
<body class="article">
<div id="mlton-header">
<div id="mlton-header-text">
<h2>
<a href="./Home">
MLton
20241230+git20251029+dfsg-5
</a>
</h2>
</div>
</div>
<div id="header">
<h1>ProfilingTheStack</h1>
</div>
<div id="content">
<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 <code>-profile-stack true</code>.  For example,
suppose that <code>list-rev.sml</code> contains the following.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="sml">Unresolved directive in ProfilingTheStack.adoc - include::https://raw.github.com/MLton/mlton/master/doc/examples/profiling/list-rev.sml[indent=0]</code></pre>
</div>
</div>
<div class="paragraph">
<p>Compile with stack profiling and then run the program.</p>
</div>
<div class="listingblock">
<div class="content">
<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">
<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 <code>rev</code>, defined on line 6 of
<code>list-rev.sml</code>, 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 <code>-profile-stack true</code> can be
noticeable since there is some extra bookkeeping at every nontail call
and return.</p>
</div>
</div>
</body>
</html>