<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<meta name="Generator" content="Microsoft Word 14 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:Tahoma;
        panose-1:2 11 6 4 3 5 4 4 2 4;}
@font-face
        {font-family:Verdana;
        panose-1:2 11 6 4 3 5 4 4 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0cm;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:"Times New Roman","serif";}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
span.EmailStyle17
        {mso-style-type:personal-reply;
        font-family:"Verdana","sans-serif";
        color:#1F497D;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-family:"Calibri","sans-serif";
        mso-fareast-language:EN-US;}
@page WordSection1
        {size:612.0pt 792.0pt;
        margin:72.0pt 72.0pt 72.0pt 72.0pt;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-GB" link="blue" vlink="purple">
<div class="WordSection1">
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Verdana&quot;,&quot;sans-serif&quot;;color:#1F497D">Would it be worth turning this into a Trac ticket?<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Verdana&quot;,&quot;sans-serif&quot;;color:#1F497D"><o:p>&nbsp;</o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Verdana&quot;,&quot;sans-serif&quot;;color:#1F497D">Simon<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Verdana&quot;,&quot;sans-serif&quot;;color:#1F497D"><o:p>&nbsp;</o:p></span></p>
<div style="border:none;border-left:solid blue 1.5pt;padding:0cm 0cm 0cm 4.0pt">
<div>
<div style="border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0cm 0cm 0cm">
<p class="MsoNormal"><b><span lang="EN-US" style="font-size:10.0pt;font-family:&quot;Tahoma&quot;,&quot;sans-serif&quot;">From:</span></b><span lang="EN-US" style="font-size:10.0pt;font-family:&quot;Tahoma&quot;,&quot;sans-serif&quot;"> ghc-devs-bounces@haskell.org [mailto:ghc-devs-bounces@haskell.org]
<b>On Behalf Of </b>Eyal Lotem<br>
<b>Sent:</b> 07 February 2013 02:14<br>
<b>To:</b> ghc-devs@haskell.org<br>
<b>Subject:</b> Stable pointers and hash table performance<o:p></o:p></span></p>
</div>
</div>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
<div>
<div>
<p class="MsoNormal">Hey,<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
</div>
<div>
<p class="MsoNormal">Background<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">----------------<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">I built a hash table in C: &nbsp;<a href="https://github.com/Peaker/small_hash">https://github.com/Peaker/small_hash</a><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
</div>
<div>
<p class="MsoNormal">According to a few simple benchmarks (mainly, 10 million insertions), my C hash table is much faster than any Haskell data structure I tried (e.g: 8 times faster than IntMap).<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">You can run the C benchmark (in small_hash) via: &quot;make &amp;&amp; ./benchmark 10000000&quot;.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
</div>
<div>
<p class="MsoNormal">The other benchmarks are at:&nbsp;<a href="https://github.com/Peaker/hashtable_benchmarks">https://github.com/Peaker/hashtable_benchmarks</a><o:p></o:p></p>
</div>
<div>
<div>
<p class="MsoNormal">Benchmarks results on my hardware:&nbsp;<a href="http://hpaste.org/81902">http://hpaste.org/81902</a><o:p></o:p></p>
</div>
</div>
<div>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
</div>
<div>
<p class="MsoNormal">I was hoping to get that fast mapping goodness to Haskell, by building an FFI to it:<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><a href="https://github.com/Peaker/small_hash_hs">https://github.com/Peaker/small_hash_hs</a><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
</div>
<div>
<p class="MsoNormal">In order to do FFI bindings to a C data structure, that can contain arbitrary Haskell values, I must create a lot of StablePtrs (for each Haskell value stored in the C hash table).<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
</div>
<div>
<p class="MsoNormal">Problem 1:<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">-------------<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">Disappointingly, the FFI bindings were dozens of times slower(!) than the C code.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
</div>
<div>
<p class="MsoNormal">When using &#43;RTS -H200M the speed is about 10 times slower(!) than the C code.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">oprofile shows that the culprit is the Stable Ptr code.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
</div>
<div>
<p class="MsoNormal">Digging in, I found that stable ptrs and stable names shared a table, despite having quite different characteristics:<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">&nbsp;* Stable Names aren't considered roots by GC, ptrs are.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">&nbsp;* Making two stable ptrs for the same Haskell value doesn't really require them to unify -- so there's no need for the hash table or the &quot;refs&quot; counter for stable ptrs.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
</div>
<div>
<p class="MsoNormal">My Changes:<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">-----------------<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">I split the Stable Names into their own table (which allowed removing the &quot;refs&quot; field from them).<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">Stable Ptrs no longer use a hash table to unify, don't need &quot;refs&quot;, or &quot;sn_obj&quot;, or &quot;old&quot;.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
</div>
<div>
<p class="MsoNormal">This made Stable Ptrs cost just 1 C ptr in the table and really cheap to create/destroy.<o:p></o:p></p>
</div>
<div>
<div>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
</div>
<div>
<p class="MsoNormal">The commits are here:<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><a href="https://github.com/Peaker/ghc/commits/master">https://github.com/Peaker/ghc/commits/master</a><o:p></o:p></p>
</div>
</div>
<div>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
</div>
<div>
<div>
<p class="MsoNormal">They are not polished (I didn't thoroughly review myself yet, didn't run the test suite, fix out-of-date comments involved) and not ready for integration yet.<o:p></o:p></p>
</div>
</div>
<div>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
</div>
<div>
<p class="MsoNormal">Result:<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">---------<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">The hash table benchmark via the FFI is now only ~2 times slower than the C data structure and much faster than any other mapping structure for that particular benchmark (10 million insertions of identity integers, sequentially). A lot
 of the cost here is due to fine-grained allocation, as the C code does bulk allocation. I shall improve this benchmark later, and I believe it will tighten the gap some more.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
</div>
<div>
<p class="MsoNormal">Problem 2:<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">--------------<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><a href="http://hackage.haskell.org/trac/ghc/ticket/7670">http://hackage.haskell.org/trac/ghc/ticket/7670</a><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
</div>
<div>
<p class="MsoNormal">Every minor GC iterates a whole lot of stable ptrs. This means that with the default heap size, runtime is about 8 times longer (than with a 200M heap size).<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
</div>
<div>
<p class="MsoNormal">To avoid this, I'd like to split the stable ptrs table into generations.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
</div>
<div>
<p class="MsoNormal">To do this, I need to know which of the pointed objects were promoted and to what generation, so I can promote my stable ptrs accordingly.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">It seems like the correct time to do this is in the updateStablePtrTable (now named &quot;updateStableTables&quot;)<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
</div>
<div>
<p class="MsoNormal">This is the part I need help with.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">How do I know if an object was promoted to a higher generation when I traverse the objects?<o:p></o:p></p>
</div>
<div>
<div>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
</div>
<div>
<p class="MsoNormal">Thanks!&nbsp;<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">Eyal<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
</div>
</div>
</div>
</div>
</div>
</body>
</html>