<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="/assets/rss.xsl"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
    <channel>
        <title>Max Bernstein&apos;s Blog</title>
        <description></description>
        <link>https://bernsteinbear.com</link>
        <atom:link href="https://bernsteinbear.com/feed.xml" rel="self" type="application/rss+xml" />
        <item shouldShow="false">
            <title>Sorry for marking all the posts as unread</title>
            <description>
              I noticed that the URLs were all a little off (had two slashes
              instead of one) and went in and fixed it. I did not think
              everyone's RSS software was going to freak out the way it did.

              PS: this is a special RSS-only post that is not visible on the
              site. Enjoy.
            </description>
            <pubDate>Wed, 31 Jan 2024 00:00:00 +0000</pubDate>
            <guid isPermaLink="false">rss-only-post-1</guid>
        </item>
        
        <item>
            <title>SSA from bytecode: imperative pearl</title>
            <description>&lt;p&gt;Ufuk made a comment that he enjoyed my description of SSA from bytecode in
&lt;a href=&quot;https://www.youtube.com/watch?v=h9_FyHYUhww&quot;&gt;Aaron’s livestream with me&lt;/a&gt;.
While filming the livestream—an intro to ZJIT internals—I got a little
nervous that people would shy away from our 1900 line &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;add_iseq_to_hir&lt;/code&gt;
function. This function is responsible for converting CRuby’s stack based
bytecode for the YARV virtual machine into a high-level SSA IR for ZJIT to
analyze and optimize.&lt;/p&gt;

&lt;p&gt;It takes bytecode that looks roughly like this:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;push 3
push 4
add
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;into SSA form, giving an SSA value to each stack slot, ish:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;v0 = const 3
v1 = const 4
v2 = add v0, v1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;While our function is 2000 lines long because it handles a lot of specific
opcodes and weird facts about those opcodes, the core of it is small. Here is
that core.&lt;/p&gt;

&lt;h2 id=&quot;what-if-a-stack-but-virtual&quot;&gt;What if a stack, but virtual&lt;/h2&gt;

&lt;p&gt;The bytecode assumes that when it is run, it will be operating on a stack. This
means that every opcode has implicit stack inputs and implicit stack outputs.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;push 3&lt;/code&gt;, for example, has empty stack inputs and one stack output. By
contrast, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;add&lt;/code&gt; has two stack inputs and one stack output. Some instructions
such as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nop&lt;/code&gt; are empty on both counts. As far as I can tell, all instructions
have &lt;em&gt;at most&lt;/em&gt; one output.&lt;/p&gt;

&lt;p&gt;These counts are known when looking at the bytecode: we can reason statically
about how many inputs and outputs each opcode has. Some are variadic, such as
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;send&lt;/code&gt; (call), but they come bearing an argument count in a nearby field.&lt;/p&gt;

&lt;p&gt;Let’s model how the first example runs on the VM by adding a view of the stack
after each instruction has run. The stack will be displayed as a list
left-to-right, where the left is the bottom and the right is the top.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;       ; []
push 3 ; [3]
push 4 ; [3, 4]
add    ; [7]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Because the input counts and output counts are static, when ZJIT reads through
the bytecode, we can run the stack parts of it at compile-time.&lt;/p&gt;

&lt;p&gt;We don’t &lt;em&gt;really&lt;/em&gt; need to know what the values on the stack are; we only need
to keep track of where they came from. And when I say “where they came from”, I
mean that we name each instruction and refer to its output by the instruction’s
name. I like how the paper &lt;a href=&quot;/assets/img/design-hotspot-client-compiler.pdf&quot;&gt;Design of the Java HotSpot™ Client Compiler for
Java 6&lt;/a&gt; (PDF) puts it:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;An instruction that loads or computes a value represents both the operation
and its result, so that operands can be represented as pointers to previous
instructions.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Here is what that looks like for the above example. In addition to the run-time
stack, I will also write the SSA instruction to indicate that we are generating
new code:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;       ; []
push 3 ; v0 = const 3
       ; [v0]
push 4 ; v1 = const 4
       ; [v0, v1]
add    ; v2 = add v0, v1
       ; [v2]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This is neat! We have transformed stack based bytecode to a form where
instructions can directly reference their inputs. You may notice, though, that
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v0&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v2&lt;/code&gt; both end up in the same stack slot (the first element). This is
why I said earlier “giving an SSA value to each stack slot, &lt;em&gt;ish&lt;/em&gt;”.&lt;/p&gt;

&lt;p&gt;The difference between the two is &lt;em&gt;time&lt;/em&gt;. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v0&lt;/code&gt; was in the operand stack at one
point but now &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v2&lt;/code&gt; is. This is kind of why I describe SSA as “pushing time
through the IR”.&lt;/p&gt;

&lt;p&gt;Alright, let’s implement it.&lt;/p&gt;

&lt;h2 id=&quot;implementation&quot;&gt;Implementation&lt;/h2&gt;

&lt;p&gt;We’ll start with the real (“concrete”) interpreter of the bytecode, which
operates on a value stack. This gives you a simplified version of how Ruby
internals work:&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;interpret&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bytecode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;stack&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;bytecode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;each&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;op&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;op&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:push&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;stack&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;right&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;pop&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;left&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;pop&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;stack&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;left&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;right&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;raise&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Unknown op &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;op&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;stack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;pop&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;bytecode&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:push&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:push&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

&lt;span class=&quot;nb&quot;&gt;puts&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;interpret&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bytecode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# 7&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now let’s look at the abstract intepreter, which by contrast operates on an SSA
stack. You can see that they are quite similar looking:&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;to_ssa&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bytecode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;code&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;stack&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;bytecode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;each&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;op&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;op&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:push&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:&quot;v&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;code&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;&quot;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;code&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt; = const &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;stack&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;name&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:&quot;v&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;code&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;&quot;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;right&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;pop&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;left&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;pop&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;code&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt; = add &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;, &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;right&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;stack&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;name&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;raise&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Unknown op &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;op&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;code&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;bytecode&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:push&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:push&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

&lt;span class=&quot;nb&quot;&gt;puts&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;to_ssa&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bytecode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# v0 = const 3&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# v1 = const 4&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# v2 = add v0, v1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;name&lt;/code&gt;s could be anything as long as they are unique. For simplicity and
ease of reading, I picked the length of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;code&lt;/code&gt; array, which grows
monotonically. This is an array/vector-based analogue to the real-pointer
approach in the HotSpot paper from above.&lt;/p&gt;

&lt;p&gt;Ta da! You have built a small abstract interpreter.&lt;/p&gt;

&lt;p&gt;Now to make it global (to handle control flow) you need some kind of
inter-basic-block data flow mechanism such as phi nodes or basic block
parameters. Perhaps more on that in another post, but for now, you may enjoy
&lt;a href=&quot;/blog/ssa/&quot;&gt;my collection of other people’s writing&lt;/a&gt; about that.&lt;/p&gt;

&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;I originally wanted to title this blog post “you could have invented abstract
interpretation” or similar, but I decided that I find those titles obnoxious
because they assume the reader is or thinks of themself as unintelligent.
Perhaps &lt;em&gt;we&lt;/em&gt; could have invented it.&lt;/p&gt;

&lt;p&gt;The title is a bit of a tongue-in-cheek nod to the “functional pearl” papers
that I always felt should be simpler than they are but leave me confused. And
also kind of a nicer nod to &lt;a href=&quot;/assets/img/appel-basic-block-arguments.pdf&quot;&gt;SSA is Functional
Programming&lt;/a&gt; (PDF) by Andrew Appel.&lt;/p&gt;

&lt;h2 id=&quot;references&quot;&gt;References&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;/assets/img/ertl92.pdf&quot;&gt;A New Approach to Forth Native Code Generation&lt;/a&gt; (PDF, 1992)&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;/assets/img/caffeine.pdf&quot;&gt;Java Bytecode to Native Code Translation: The Caffeine Prototype and Preliminary Results&lt;/a&gt; (PDF, 1996) section 3.2&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;/assets/img/cacao-jvm.pdf&quot;&gt;CACAO - A 64 bit JavaVM Just-in-Time Compiler&lt;/a&gt; (PDF, 1997) section 3&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://dl.acm.org/doi/pdf/10.1145/277650.277740&quot;&gt;Fast, Effective Code Generation in a Just-In-Time Java Compiler&lt;/a&gt; (PDF, 1998) section 2.2, the mimic stack&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://dl.acm.org/doi/pdf/10.1145/304065.304113&quot;&gt;The Jalapeño Dynamic Optimizing Compiler for Java™&lt;/a&gt; (PDF, 1999)&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://dspace.mit.edu/entities/publication/6896835b-c46b-4ebb-8ecc-1f5cbac364b5&quot;&gt;Dynamic optimization through the use of automatic runtime specialization&lt;/a&gt; (1999) section 3.4&lt;/li&gt;
&lt;/ul&gt;
</description>
            <pubDate>Thu, 12 Nov 2026 00:00:00 +0000</pubDate>
            <niceDate>November 12, 2026</niceDate>
            <link>https://bernsteinbear.com/blog/ssa-bytecode/?utm_source=rss</link>
            <guid isPermaLink="true">https://bernsteinbear.com/blog/ssa-bytecode/</guid>
        </item>
        
        <item>
            <title>I am searching for a coffee alternative</title>
            <description>&lt;p&gt;I had an in-person work team meeting last week. In the office, they had
&lt;a href=&quot;https://bevi.co/&quot;&gt;Bevi&lt;/a&gt; seltzer machines. When Bevi machines first arrived at
Facebook, where I used to work, I looked down on them. They seemed to be a cost
reduction, a sad imitation of The Good Stuff that came in cans and bottles in
the fridge. Good Stuff such as LaCroix&lt;sup id=&quot;fnref:oncall&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:oncall&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;1&lt;/a&gt;&lt;/sup&gt; and Ito En tea&lt;sup id=&quot;fnref:tea&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:tea&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;2&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;

&lt;p&gt;But this week was different. I noticed that this Bevi had a &lt;em&gt;caffeine&lt;/em&gt;
option&lt;sup id=&quot;fnref:bevi-caffeine&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:bevi-caffeine&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;3&lt;/a&gt;&lt;/sup&gt;. After some experimentation, I decided that for the
week, I would drink unflavored, chilled, sparkling, caffeinated water instead
of coffee. And it was great. Basically none of the coffee side effects.&lt;/p&gt;

&lt;p&gt;For several days straight I thoroughly enjoyed a 100-120mg caffeine sparkling
water.&lt;/p&gt;

&lt;p&gt;But all good things must come to an end. You cannot (yet?) buy a Bevi machine
for home. So I am unfortunately now searching for alternatives.&lt;/p&gt;

&lt;p&gt;I took a stroll through our local Star Market in search of a replacement. I
looked online, too, since we have a weird selection of beverages at that
market. I ended up with an assortment of brightly colored cans. Time to do a
taste test.&lt;/p&gt;

&lt;h2 id=&quot;requirements&quot;&gt;Requirements&lt;/h2&gt;

&lt;p&gt;In order to swap out coffee for something else, I have the following criteria:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;no (gratuitous) sugar&lt;/li&gt;
  &lt;li&gt;not sweet (no fake sugar either)&lt;/li&gt;
  &lt;li&gt;no weirdo other chemicals&lt;/li&gt;
  &lt;li&gt;does not taste like jet fuel&lt;/li&gt;
  &lt;li&gt;reasonably priced&lt;/li&gt;
  &lt;li&gt;easy to dose&lt;/li&gt;
  &lt;li&gt;ideally sparkling&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We’ll judge based on those.&lt;/p&gt;

&lt;h2 id=&quot;taste-test&quot;&gt;Taste test&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;UPDATE Energy Drink: Peach.&lt;/strong&gt; 12 fl oz. Unclear how much caffeine.&lt;/p&gt;

&lt;p&gt;Right out of the can, there is a horrific fake sugar taste. It’s revolting. I
guess I missed sucralose in the ingredients when I picked it up. Still
disgusting after the second sip and it lingers in the mouth. No thanks.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ZOA Energy Drink: Lemon Lime.&lt;/strong&gt; 12 fl oz. 160 mg caffeine.&lt;/p&gt;

&lt;p&gt;Ever so slightly more pleasant coming out of the can but there is still a
chemical sweetness to it. Also missed the sucralose. Gross. No thanks.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;SPARKLING ICE CAFFEINE: Citrus Twist.&lt;/strong&gt; 16 fl oz. 70 mg caffeine.&lt;/p&gt;

&lt;p&gt;This time I checked before opening. It has sucralose. Ugh. I guess I was
filtering for Stevia at the store. Here goes nothing… ew, absolutely not.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Liquid Death Sparkling Energy: Murder Mystery.&lt;/strong&gt; 12 fl oz. 100 mg caffeine.&lt;/p&gt;

&lt;p&gt;Damn. Sucralose again. Ew. Cherry. Gone.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;hi ball energy: grapefruit.&lt;/strong&gt; 16 fl oz. 160 mg caffeine.&lt;/p&gt;

&lt;p&gt;Okay, this is one of the ones I found on the internet first after searching
instead of one of the random market finds. It thankfully has no recognizable
artificial sweeteners in the ingredient list. I tend to like grapefruit things.
Here goes nothing.&lt;/p&gt;

&lt;p&gt;It basically tastes like grapefruit LaCroix, which is a win. No sweetener
detected. Good news. I like this one by and away the best. Unfortunately, it is
expensive, coming in at $3/can.&lt;/p&gt;

&lt;p&gt;I intended to do the taste test over a couple of days to avoid
over-caffeination, but that was not necessary because most of them were just
vile.&lt;/p&gt;

&lt;h2 id=&quot;wrapping-up-and-future-directions&quot;&gt;Wrapping up and future directions&lt;/h2&gt;

&lt;p&gt;I also came across &lt;strong&gt;Water Joe&lt;/strong&gt; in my search. Seems there was a man in
Wisconsin with similar goals. I want to acquire and sample.&lt;/p&gt;

&lt;p&gt;Should I caffeinate my own beverages with liquid caffeine? Seems both less
convenient and more hazardous.&lt;/p&gt;

&lt;p&gt;Thankfully this is all just because I like the feeling of being caffeinated and
not because I am dependent. I will accidentally go several days without
drinking something caffeinated and not really notice. I would like to stay that
way, so I am avoiding methods that could spike my caffeine intake and
accidentally make me dependent.&lt;/p&gt;

&lt;p&gt;Please let me know if you have any recommendations…&lt;/p&gt;
&lt;div class=&quot;footnotes&quot; role=&quot;doc-endnotes&quot;&gt;
  &lt;ol&gt;
    &lt;li id=&quot;fn:oncall&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;For a time, I was the “LaCroix oncall”. You could page the oncall
rotation and ask what flavor you should drink. I also deputized myself to
investigate the occasional LaCroix-related shortage and post internally
about it. &lt;a href=&quot;#fnref:oncall&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:tea&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;There was a period where I was drinking several liters of cold jasmine
tea per day. I stopped when I learned that this could do unexpected things
to your liver. &lt;a href=&quot;#fnref:tea&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:bevi-caffeine&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;Did Bevi have caffeine when I worked at Facebook? Seems like
no, they only got it in 2022, and I was Not In The Office that year before
I left for grad school. &lt;a href=&quot;#fnref:bevi-caffeine&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
  &lt;/ol&gt;
&lt;/div&gt;
</description>
            <pubDate>Tue, 10 Nov 2026 00:00:00 +0000</pubDate>
            <niceDate>November 10, 2026</niceDate>
            <link>https://bernsteinbear.com/blog/coffee-alternative/?utm_source=rss</link>
            <guid isPermaLink="true">https://bernsteinbear.com/blog/coffee-alternative/</guid>
        </item>
        
        <item>
            <title>Rails is a JIT compiler</title>
            <description>&lt;p&gt;&lt;em&gt;Originally published on &lt;a href=&quot;https://railsatscale.com/2026-10-21-rails-is-a-jit-compiler/&quot;&gt;Rails At Scale&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The title was bait to get you to open this article. But fear not! We will be
generating code at run-time today. (See &lt;a href=&quot;https://github.com/rails/rails/pull/59250&quot;&gt;the
PR&lt;/a&gt; for a sneak peek.)&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://railsatscale.com/authors/sam-schmidt/&quot;&gt;Sam&lt;/a&gt; popped into the JIT channel asking how we might
optimize Rails enums in Roundhouse, motivated by the following code snippet:&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Order&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;ActiveRecord&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Base&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;enum&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:status&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;pending: &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;paid: &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;shipped: &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;refunded: &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;shippable?&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;paid?&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;refunded?&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In particular, there is a lot of dynamic behavior going on with the code
generated by Rails &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;enum&lt;/code&gt; and Sam wanted to know if inlining it all away in an
(external-to-Rails) build step would help.&lt;/p&gt;

&lt;p&gt;That didn’t sound right to me because I thought that:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Rails got a lot better in recent years about removing these dynamic lookups
in favor of creating and caching methods&lt;/li&gt;
  &lt;li&gt;ZJIT should be able to handle dispatch to methods (generated or otherwise)
just fine&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So I inquired and Sam presented to us the &lt;a href=&quot;https://github.com/rails/rails/blob/36e0f96b9d7a022755a53a2a9fb7a67ad4e3669e/activerecord/lib/active_record/enum.rb#L335&quot;&gt;implementation of
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;enum&lt;/code&gt;&lt;/a&gt;
which contained this gem:&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;define_method&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value_method_name&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;?&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;public_send&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:&quot;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;_for_database&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now fortunately, this is not the fully dynamic lookup I was worried about! No
hashes, etc. This is Rails using the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;enum&lt;/code&gt; to generate a method on the
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ActiveRecord&lt;/code&gt; class. So in our case, for example, it turns &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pending&lt;/code&gt; into a
method on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Order&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
&lt;span class=&quot;no&quot;&gt;Order&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;define_method&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;pending?&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;public_send&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:status_for_database&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;em&gt;Unfortunately,&lt;/em&gt; it is still using very dynamic behavior. There is no reason to
be using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;public_send&lt;/code&gt; for method dispatch if you know the name statically: it
doesn’t get cached and has a lot more machinery than we need. There is also no
reason to use a bmethod (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;define_method&lt;/code&gt; with a block that captures the
environment) and look up the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;value&lt;/code&gt; dynamically; the enum values are burned in
at &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;enum&lt;/code&gt; definition time.&lt;/p&gt;

&lt;p&gt;I wanted to make it use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;module_eval&lt;/code&gt; with a string instead to generate a
normal looking method. I don’t know anything about Rails so I nerd sniped
&lt;a href=&quot;https://railsatscale.com/authors/aaron-patterson/&quot;&gt;Aaron&lt;/a&gt; into fixing it.&lt;/p&gt;

&lt;h2 id=&quot;nerd-sniping-aaron&quot;&gt;Nerd sniping Aaron&lt;/h2&gt;

&lt;p&gt;The change to the the code started off fairly mechanical: we should be able to
do &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;module_eval&lt;/code&gt; to generate a similar looking method:&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;code&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;code&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;def &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value_method_name&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;?; &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;_for_database == &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;inspect&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;; end&quot;&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# ...&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;module_eval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;code&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;kp&quot;&gt;__FILE__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kp&quot;&gt;__LINE__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;unless&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;code&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;empty?&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The differences here being:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Direct dispatch to the method instead of going through &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;public_send&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;Burning the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;value&lt;/code&gt; into the method instead of looking it up from the parent
environment of the block&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;It passed almost all the tests but then we ran into a couple of weird cases:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;People have the darndest enum names
    &lt;ol&gt;
      &lt;li&gt;The Rails test suite, for example, uses fake timezones: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;enum :timezone, [:&quot;Etc/GMT+1&quot;, :&quot;Etc/GMT-1&quot;]&lt;/code&gt;&lt;/li&gt;
      &lt;li&gt;Our application, has an instance with a capital letter: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;enum :thing, [:Foo, :Bar]&lt;/code&gt;&lt;/li&gt;
      &lt;li&gt;Probably other stuff that wouldn’t be a identifier in Ruby&lt;/li&gt;
    &lt;/ol&gt;
  &lt;/li&gt;
  &lt;li&gt;Floats. Naturally. While all the enum values are required to be serializable
(and therefore reifiable in Ruby source code), the roundtrippability gets a
little weird with floats. So we have this irritating check: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;!(Float === value &amp;amp;&amp;amp; !value.finite?)&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In the end it wasn’t quite a one-line change because
&lt;a href=&quot;https://railsatscale.com/authors/ufuk-kayserilioglu/&quot;&gt;Ufuk&lt;/a&gt; also pointed out that we should be using
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ActiveSupport::CodeGenerator&lt;/code&gt; instead of yolo-ing it. But it was pretty close
to it.&lt;/p&gt;

&lt;p&gt;Check out &lt;a href=&quot;https://github.com/rails/rails/pull/59250&quot;&gt;the PR&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;performance-numbers&quot;&gt;Performance numbers&lt;/h2&gt;

&lt;p&gt;Go check out the PR! But you’re still here, so I’ll repeat the numbers here.
The following microbenchmark of the code at the beginning of the article:&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;no&quot;&gt;IPS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ips&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;report&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;shippable?&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;order&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;shippable?&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;got a &lt;em&gt;lot&lt;/em&gt; better.&lt;/p&gt;

&lt;p&gt;Here are the performance numbers for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;main&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;aaron@Aarons-MBP ~/g/rails (main)&amp;gt; ruby -I~/git/ips/lib test.rb
-- create_table(:orders)
   -&amp;gt; 0.0073s
          shippable?:     1.721M i/s (± 2.3%, GC  4.4%)
aaron@Aarons-MBP ~/g/rails (main)&amp;gt; ruby --yjit -I~/git/ips/lib test.rb
-- create_table(:orders)
   -&amp;gt; 0.0048s
          shippable?:     3.568M i/s (± 2.6%, GC  8.2%)
aaron@Aarons-MBP ~/g/rails (main)&amp;gt; ruby --zjit -I~/git/ips/lib test.rb
-- create_table(:orders)
   -&amp;gt; 0.0029s
          shippable?:     3.812M i/s (± 2.8%, GC  8.9%)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;and for Aaron’s PR:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;aaron@Aarons-MBP ~/g/rails (fix-enum)&amp;gt; ruby -I~/git/ips/lib test.rb
-- create_table(:orders)
   -&amp;gt; 0.0073s
          shippable?:     3.145M i/s (± 1.5%)
aaron@Aarons-MBP ~/g/rails (fix-enum)&amp;gt; ruby --yjit -I~/git/ips/lib test.rb
-- create_table(:orders)
   -&amp;gt; 0.0065s
          shippable?:    16.274M i/s (± 1.3%)
aaron@Aarons-MBP ~/g/rails (fix-enum)&amp;gt; ruby --zjit -I~/git/ips/lib test.rb
-- create_table(:orders)
   -&amp;gt; 0.0027s
          shippable?:    18.509M i/s (± 2.2%)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Wahoo!&lt;/p&gt;

&lt;h2 id=&quot;lessons&quot;&gt;Lessons&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Static is better than dynamic. Burn in those constants.&lt;/li&gt;
  &lt;li&gt;In particular, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;public_send&lt;/code&gt; is slow!&lt;/li&gt;
  &lt;li&gt;We can fix performance issues upstream! Especially if the upstream libraries
already do code generation. Not all problems require a custom compiler.&lt;/li&gt;
&lt;/ul&gt;
</description>
            <pubDate>Wed, 21 Oct 2026 00:00:00 +0000</pubDate>
            <niceDate>October 21, 2026</niceDate>
            <link>https://bernsteinbear.com/blog/rails-is-a-jit-compiler/?utm_source=rss</link>
            <guid isPermaLink="true">https://bernsteinbear.com/blog/rails-is-a-jit-compiler/</guid>
        </item>
        
        <item>
            <title>Objects, attributes, maps, oh my!</title>
            <description>&lt;p&gt;Or, &lt;em&gt;IC UC we all C for ICs&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;In many dynamically typed programming languages, objects look like a big ol’
mapping of names to values. The languages encourage this flexibility. Consider
for example Python:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;C&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Or Ruby:&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;C&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;initialize&lt;/span&gt;
    &lt;span class=&quot;vi&quot;&gt;@a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
    &lt;span class=&quot;vi&quot;&gt;@b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Or JavaScript&lt;sup id=&quot;fnref:js-synonyms&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:js-synonyms&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;1&lt;/a&gt;&lt;/sup&gt;:&lt;/p&gt;

&lt;div class=&quot;language-javascript highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;obj&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{};&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This might encourage the VM implementor to model object attributes using a hash
table. Hash tables, after all, are very good at mapping names to values.&lt;/p&gt;

&lt;p&gt;Here is, by way of example, a Python sketch of what this object model looks
like. Because it is a sketch, it uses &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;write_attr&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;read_attr&lt;/code&gt; instead of
the much more common syntax that your programming language comes with.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;MISSING&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Instance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;# TODO: the rest of your object system
&lt;/span&gt;        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cls&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_fields&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;read_attr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fieldname&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_fields&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fieldname&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MISSING&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;write_attr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fieldname&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_fields&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fieldname&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Instance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;write_attr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;a&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;write_attr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;b&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;read_attr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;a&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;read_attr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;b&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This has its benefits:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;it is straightforward to understand&lt;/li&gt;
  &lt;li&gt;… uh, well, maybe that’s it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It also has its drawbacks:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;it uses a hash table for a very common operation: while this is technically
O(1), hashing and probing are very slow&lt;/li&gt;
  &lt;li&gt;it has an indirection: jumping from an object to its separate hash table
field storage requires a lot of dependent memory loads. object attributes are
accessed frequently and should therefore be stored in-line on the object
instead of another memory access (and potentially cache miss) away&lt;/li&gt;
&lt;/ul&gt;

&lt;figure&gt;
&lt;object class=&quot;svg&quot; type=&quot;image/svg+xml&quot; data=&quot;/assets/img/hashtable-memory.excalidraw.svg&quot;&gt;&lt;/object&gt;
&lt;figcaption&gt;This is a pretty good case where the hash table doesn&apos;t have its own internal
fragmentation.&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;This means that literally every operation &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a.b&lt;/code&gt; is doing hundreds or thousands
of CPU instructions, including multiple dependent memory loads. This is &lt;em&gt;not
good&lt;/em&gt; for performance.&lt;/p&gt;

&lt;p&gt;So runtime implementors tend to implement this thing called maps, or layouts,
or shapes, or hidden classes, or structures, or probably a secret sixth name I
don’t know about, which optimizes reading, writing, and storage of attributes.&lt;/p&gt;

&lt;p&gt;But what does this shape code look like? How does it actually help performance?
A lot of big production runtimes have pretty hairy code with domain-specific
edge cases so they may not help get a handle on the basics. And a lot of blog
posts, while excellent high-level overviews, are lacking in implementation. So
here we go once more, blogging where everyone else has already blogged before.
With any luck, you should be able to write a page of code and hold the whole
thing in your head at once.&lt;/p&gt;

&lt;h2 id=&quot;500-lines-or-less&quot;&gt;500 lines or less&lt;/h2&gt;

&lt;p&gt;I decided to once more give CF Bolz-Tereick’s &lt;a href=&quot;https://aosabook.org/en/500L/a-simple-object-model.html&quot;&gt;A Simple Object
Model&lt;/a&gt; the thorough
read it deserves. In the past, I tried a couple times and bounced off of it
because it models Python with reasonable fidelity and the Python object model
is &lt;em&gt;really hairy&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;But did I read it thoroughly this time? Well, no. This time I allowed myself to
skim the class- and method-related parts and instead focus on the instance
attribute and map parts. Inside CF’s post is the core of a very clean map
system and I wanted to experiment with that, so I extracted it out.&lt;/p&gt;

&lt;p&gt;CF’s post starts off very similar to our own sketch, modeling objects as
unordered maps of attributes. It also fills in some of the TODO stuff I left
out earlier around classes. I’ll copy my reduced implementation of it here so
you can take a gander:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# The base object model code:
&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cls&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fields&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cls&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cls&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_fields&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fields&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;read_attr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fieldname&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_read_dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fieldname&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;write_attr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fieldname&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_write_dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fieldname&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;_read_dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fieldname&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_fields&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fieldname&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MISSING&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;_write_dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fieldname&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_fields&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fieldname&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;MISSING&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Instance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cls&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Class&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;nb&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cls&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{})&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Class&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;base_class&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fields&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;metaclass&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;nb&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;metaclass&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fields&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;base_class&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;base_class&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;OBJECT&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Class&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;object&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;base_class&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fields&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{},&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;metaclass&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;TYPE&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Class&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;type&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;base_class&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;OBJECT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fields&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{},&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;metaclass&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;TYPE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cls&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TYPE&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;OBJECT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cls&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TYPE&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Which you can use like this (test code also taken from CF’s post):&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;A&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Class&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;A&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;base_class&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;OBJECT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fields&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{},&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;metaclass&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TYPE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Instance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;write_attr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;a&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;read_attr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;a&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;write_attr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;b&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;read_attr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;a&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;read_attr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;b&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;write_attr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;a&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;read_attr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;a&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;read_attr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;b&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;There’s a bit of delegation going on in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Base&lt;/code&gt; class because the post
already anticipates what we’re about to do: swap out the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Instance&lt;/code&gt;
implementation for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_read_dict&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_write_dict&lt;/code&gt; for better performance.&lt;/p&gt;

&lt;h2 id=&quot;cartography&quot;&gt;Cartography&lt;/h2&gt;

&lt;p&gt;OK, let’s add on this “map” data structure. The purpose of the map is to do
three things:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Get out of the business of dealing with strings and hashing in the fast
path. We would like to instead do operations on small integers.&lt;/li&gt;
  &lt;li&gt;Remove the indirection. We would like to store the attributes in-line in the
object instead of elsewhere in memory.&lt;/li&gt;
  &lt;li&gt;Be/have a stable identifier that describes a particular object layout.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;For the former&lt;/strong&gt;, we can think of an object’s map field as an (database) index or
a write-through cache. It is responsible for reflecting on the layout of a
given object:&lt;/p&gt;

&lt;figure&gt;
&lt;object class=&quot;svg&quot; type=&quot;image/svg+xml&quot; data=&quot;/assets/img/hashtable-shape-memory.excalidraw.svg&quot;&gt;&lt;/object&gt;
&lt;figcaption&gt;This model is &lt;i&gt;mostly&lt;/i&gt; there...&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;You can ask the map questions like “what attributes does this object have?” and
“where is attribute &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a&lt;/code&gt; relative to the start of the object?”&lt;/p&gt;

&lt;!-- another invariant: compare by ID... --&gt;

&lt;p&gt;Just looking at the diagram, it may seem silly: after all, we have exchanged
one hash table for another. There’s one big difference, though, which is that
in the shape model, we don’t &lt;em&gt;have&lt;/em&gt; to always use the hash table. If we
remember the offset since the last time we looked it up in the shape, we can
re-use the offset. This mostly appears in an interpreter feature called an
inline cache.&lt;/p&gt;

&lt;p&gt;This brings us to &lt;strong&gt;the second bit&lt;/strong&gt;: if we have the offset into the object, we
can read right off of it. No need to go do a second load from another region of
memory. We have the object in hand and hopefully already in the cache.&lt;/p&gt;

&lt;p&gt;…At least, until the map &lt;em&gt;changes&lt;/em&gt;. Then we can’t re-use the offset. Remember, these languages encourage this
flexibility. An object could gain or lose an attribute at any time. What then?
Ideally we have a very fast check that can tell us if the object’s in-memory
layout changed.&lt;/p&gt;

&lt;p&gt;This brings us to &lt;strong&gt;the third bit&lt;/strong&gt;. We need to be able to rely on the map to
be immutable, up-to-date, and a stable identifier for a given in-memory layout.
This way we can do a very quick check (“is the map the same as last read?”)
before a load.&lt;/p&gt;

&lt;p&gt;This means that when we add a new attribute to an object, we need to give it a
new map. Maps are actually &lt;em&gt;trees&lt;/em&gt; (tries?) and are allocated like a persistent
data structure. Every new object starts with an empty map. As the program adds
attributes, it follows the edges in the tree to find or create its new map.&lt;/p&gt;

&lt;figure&gt;
&lt;object class=&quot;invert&quot; data=&quot;/assets/img/maptransition.png&quot;&gt;&lt;/object&gt;
&lt;figcaption&gt;Image from CF Bolz-Tereick, CC BY 3.0.&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;I recommend reading &lt;a href=&quot;https://aosabook.org/en/500L/a-simple-object-model.html#instance-optimization&quot;&gt;the section on
maps&lt;/a&gt;
because it goes into a little more detail.&lt;/p&gt;

&lt;p&gt;Keeping all of that in mind, let’s take a look at CF’s Python implementation of
objects-with-maps and also of maps themselves:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Instance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Instance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cls&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Class&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;nb&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cls&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;map&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;EMPTY_MAP&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;storage&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;_read_dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fieldname&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;index&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fieldname&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;index&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MISSING&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;storage&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;_write_dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fieldname&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;index&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fieldname&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;index&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;storage&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;new_map&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;next_map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fieldname&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;storage&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;map&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;new_map&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;attrs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;attrs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;attrs&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;next_maps&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;get_index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fieldname&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;attrs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fieldname&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;next_map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fieldname&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fieldname&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;attrs&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fieldname&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;next_maps&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;next_maps&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fieldname&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;attrs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;attrs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;copy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;attrs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fieldname&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;attrs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;next_maps&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fieldname&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;attrs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;EMPTY_MAP&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;You may notice that the storage is not in-line, as I had originally promised.
This is because we don’t have that kind of object layout control in Python.
Attribute storage, when implemented in a language such as C++ or Rust, is
typically in-line with the object header.&lt;/p&gt;

&lt;p&gt;Ignoring that, though, we can see that the common case still seems to read and
write attributes using string names. What gives? Well, let’s use the index
storage as intended in an &lt;em&gt;inline cache&lt;/em&gt;.&lt;/p&gt;

&lt;h2 id=&quot;inline-caches&quot;&gt;Inline caches&lt;/h2&gt;

&lt;p&gt;Let’s start by looking at the string attribute API. We’ll make a point class and
read the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x&lt;/code&gt; attribute. We’ll keep the attribute name constant since this is
generally fixed in the program: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;obj.x&lt;/code&gt; normally looks like some bytecode
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LOAD_ATTRIBUTE, &quot;x&quot;&lt;/code&gt; where the name &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x&lt;/code&gt; is embedded in the bytecode somehow.&lt;/p&gt;

&lt;p&gt;This is important for the inline cache: varying the receiver is one thing but
varying the name being looked up adds a lot more complexity. It’s doable but
irritating.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;Point&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Class&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Point&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;base_class&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;OBJECT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fields&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{},&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;metaclass&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TYPE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;P&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Instance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Point&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;write_attr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;x&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;write_attr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;y&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# String API :(
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;read_attr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;x&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# Always looking up index :(
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;read_attr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;x&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# Every time :(
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;read_attr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;x&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Then we will make a memory/speed trade-off. We will allocate space for the
aforementioned &lt;em&gt;expected map&lt;/em&gt; and the &lt;em&gt;cached index&lt;/em&gt; mentioned earlier. This
lets us do a really fast check followed by an indexed load in the (hopefully)
common case.&lt;/p&gt;

&lt;p&gt;The logic is as follows:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;If we have a matching map, great! Load from storage.&lt;/li&gt;
  &lt;li&gt;If we don’t, look up the value and index by string name.&lt;/li&gt;
  &lt;li&gt;And then cache the map / index combo for next time.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If the attribute is missing, we don’t (for now?) cache the negative lookup.
Hopefully this case is rare.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;InlineCache&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;map&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;idx&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;read&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fieldname&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;obj_map&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;map&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obj_map&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;storage&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;idx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;index&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obj_map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fieldname&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;index&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MISSING&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;map&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obj_map&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;idx&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;index&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;storage&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;IC&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;InlineCache&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# Looked up by string :(
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IC&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;read&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;x&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# Map hit! Looked up by index! :)
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IC&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;read&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;x&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# Map hit! Looked up by index! :)
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IC&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;read&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;x&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;You may note that I used &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;is&lt;/code&gt; (pointer equality) instead of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;==&lt;/code&gt;
(user-defined/deep equality). This is intentional! Because we are guaranteeing
the maps are immutable, we don’t need to check equality on the fields of the
map; just its pointer/numeric ID.&lt;/p&gt;

&lt;p&gt;This &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;InlineCache&lt;/code&gt; class is mostly just a “sketch” of what an inline cache
does. It is an “interpreted IC”. This shape, if you make sure the fast path is
inlined and the slow path is outlined, can help your interpreter a lot. It does
definitely improve performance—I recommend benchmarking a silly little
attribute lookup function—but most runtimes with inline caches also come with
JIT compilers. These compilers try to remove the “interpreted”-ness of the
inline cache implementation. See for example:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://mrale.ph/blog/2015-01-11-whats-up-with-monomorphism.html&quot;&gt;Slava’s post&lt;/a&gt; that
explains with IR sketches and JS-like pseudocode
    &lt;ul&gt;
      &lt;li&gt;also &lt;a href=&quot;https://mrale.ph/blog/2012-06-03-explaining-js-vms-in-js-inline-caches.html&quot;&gt;this older one&lt;/a&gt;
which is similar&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://caiolima.github.io/jsc/2020/03/12/jsc-inline-cache.html&quot;&gt;Caio’s
post&lt;/a&gt; that
explains what this looks like in JavaScriptCore with x86 asm&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.mgaudet.ca/technical/2018/6/5/an-inline-cache-isnt-just-a-cache&quot;&gt;Matthew’s post&lt;/a&gt;
that explains what this looks like in pseudo-asm for speculative numeric addition
    &lt;ul&gt;
      &lt;li&gt;mostly runtimes don’t do self-modifying asm ICs like this because of W^X
protection and also you need to flush the icache&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Another thing to notice is that &lt;strong&gt;order matters&lt;/strong&gt;. Adding attributes in different
orders (e.g. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a&lt;/code&gt; then &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;b&lt;/code&gt; vs &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;b&lt;/code&gt; then &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a&lt;/code&gt;) will result in different shapes.
Another frequent cause of this is conditionally storing attributes in
initialization (e.g. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;if cond: self.a = 1&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;This shape and cache behavior is not really visible to the application
programmer except in performance regressions&lt;sup id=&quot;fnref:attribute-order&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:attribute-order&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;2&lt;/a&gt;&lt;/sup&gt;, where either
your inline caches will frequently miss or you will not have optimal
(monomorphic) caches. This might not sound scary but the difference between a
cache hit and a cache miss in a hot path could be 2x performance or more and is
worth investigating in your application.&lt;/p&gt;

&lt;p&gt;Also, &lt;strong&gt;quantity matters&lt;/strong&gt;. VM implementors measured and determined that most
objects are small (&amp;lt;20 ish fields). You may find that putting too many fields
on an object leads to it getting penalized and put back in “hash table mode”.
Ruby, for example, has some fixed number of in-object instance variables
(attributes) before its shape becomes “complex”.&lt;/p&gt;

&lt;p&gt;Last, notice that this cache is not specific to this object; any number of
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Point&lt;/code&gt; instances can get a cache hit. Actually, looking at it, an object need
not even be a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Point&lt;/code&gt;…&lt;/p&gt;

&lt;h2 id=&quot;class-vs-shape&quot;&gt;Class vs shape&lt;/h2&gt;

&lt;p&gt;What does it mean that two objects with different classes can have the same
shape?&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;Foo&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Class&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Foo&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;base_class&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;OBJECT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fields&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{},&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;metaclass&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TYPE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;F&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Instance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Foo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;F&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;write_attr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;x&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;F&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;write_attr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;y&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;Bar&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Class&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Bar&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;base_class&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;OBJECT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fields&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{},&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;metaclass&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TYPE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;B&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Instance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Bar&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;B&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;write_attr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;x&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;B&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;write_attr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;y&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Same shape/map!
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;F&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;map&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;B&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;map&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# But different class!
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;F&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cls&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;B&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cls&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;For one, it means that the inline cache may have a higher hit rate. Instances
of subclasses that share code, for example, may still count as &lt;em&gt;monomorphic&lt;/em&gt; in
the IC.&lt;/p&gt;

&lt;p&gt;It also means that you can’t use the shape as a proxy for knowing about the
class. If you have something like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;attr_reader&lt;/code&gt; in Ruby, where the VM generates
a synthetic method just for looking up an instance variable, you need to do two
things:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;resolve the method based on the &lt;em&gt;object’s class&lt;/em&gt; to find out that it’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;attr_reader&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;resolve the instance variable based on the &lt;em&gt;object’s shape&lt;/em&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This two-stage lookup adds both complexity and run-time overhead. If we
specialized maps so that having a different class implied having a different
map, we would be able to specialize that two-stage dispatch with just one field
load.&lt;/p&gt;

&lt;p&gt;This is kind of an interesting idea: what else can we specialize the map on?&lt;/p&gt;

&lt;h2 id=&quot;storage-strategies&quot;&gt;Storage strategies&lt;/h2&gt;

&lt;p&gt;Perhaps unsurprisingly, CF Bolz-Tereick reappears with Lukas Diekmann and
Laurence Tratt in &lt;a href=&quot;https://dl.acm.org/doi/pdf/10.1145/2544173.2509531&quot;&gt;Storage Strategies for Collections in Dynamically Typed
Languages&lt;/a&gt; (PDF, 2013).&lt;/p&gt;

&lt;p&gt;The main idea is that some facts about objects (properties, attributes, har
har) are both:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;worth splitting/monomorphizing on (there is some kind of “payoff” for the
increase in branches and code size)&lt;/li&gt;
  &lt;li&gt;similarly sensitive to code location (int lists allocated here, float lists
over there)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Their treatment is for collections (for example, optimizing the common cases of
an empty list, or a list of small integers, …). In addition to giving more
information to the optimizer, it helps both reduce memory and reduce pointer
chasing.&lt;/p&gt;

&lt;p&gt;This kind of thinking extends beyond collections: do you ever have objects
where an attribute is always a certain type? Maybe your &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Point&lt;/code&gt; class always
has floats in it? Consider, then, what I think of as CF’s follow-up paper, type
freezing: &lt;a href=&quot;https://dl.acm.org/doi/pdf/10.1145/3368826.3377907&quot;&gt;Type Freezing: Exploiting Attribute Type Monomorphism in Tracing JIT
Compilers&lt;/a&gt; (PDF, 2020)&lt;/p&gt;

&lt;p&gt;This paper changes object maps from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{name: offset}&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{name: (offset,
type)}&lt;/code&gt; to reduce guards. Then it goes further by even addressing nested
attributes and their types. This nesting extension is mostly motivated by the
meta-circularity of the implementation and would have less impact for a
“normal” runtime.&lt;/p&gt;

&lt;p&gt;See also:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://bibliography.selflanguage.org/_static/implementation.pdf&quot;&gt;An Efficient Implementation of SELF, a Dynamically-Typed Object-Oriented Language Based on Prototypes&lt;/a&gt; (PDF, 1991)&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://dl.acm.org/doi/pdf/10.1145/178243.178478&quot;&gt;Optimizing Dynamically-Dispatched Calls with Run-Time Type Feedback&lt;/a&gt; (PDF, 1994)&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://dl.acm.org/doi/pdf/10.1145/2647508.2647517&quot;&gt;An Object Storage Model for the Truffle Language Implementation Framework&lt;/a&gt; (PDF, 2014)&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/pdf/1507.02437&quot;&gt;Extending Basic Block Versioning with Typed Object Shapes&lt;/a&gt; (PDF, 2015)&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://dl.acm.org/doi/pdf/10.1145/2814228.2814231&quot;&gt;Just-in-Time Data Structures&lt;/a&gt; (PDF, 2015)&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://dl.acm.org/doi/10.1145/2983990.2984001&quot;&gt;Efficient and thread-safe objects for dynamically-typed languages&lt;/a&gt; (PDF, 2016)&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://dl.acm.org/doi/pdf/10.1145/3276478&quot;&gt;Parallelization of dynamic languages: synchronizing built-in collections&lt;/a&gt; (PDF, 2018)&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://dl.acm.org/doi/pdf/10.1145/3563838.3567678&quot;&gt;Profile Guided Offline Optimization of Hidden Class Graphs for JavaScript VMs in Embedded Systems&lt;/a&gt; (PDF, 2022)&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.jot.fm/issues/issue_2025_01/a1.pdf&quot;&gt;Optimizations for Dynamic Collections&lt;/a&gt; (PDF, 2025)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;class-versioning&quot;&gt;Class versioning&lt;/h2&gt;

&lt;p&gt;You can make even stronger bets about classes. See for example the &lt;a href=&quot;https://pypy.org/posts/2011/03/controlling-tracing-of-interpreter-with_21-6524148550848694588.html#versioning-of-classes&quot;&gt;PyPy blog
class versioning
section&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;once-more-with-feeling-c&quot;&gt;Once more with &lt;del&gt;feeling&lt;/del&gt; C++&lt;/h2&gt;

&lt;p&gt;I felt like this blog post would not be complete without a manual memory layout
rehashing of CF’s article. It’s all well and good to write the Python but what
do “real” runtimes look like?&lt;/p&gt;

&lt;p&gt;Well, they have compact object headers that contain the shape/map and some
other flag bits. They allocate their attributes inline with an object. And they
probably also have an “overflow” slot for when their initial object sizing
estimates were too small.&lt;/p&gt;

&lt;p&gt;So I made a very very small runtime in C++ that allocates objects and adds
attributes and has a shape system. It is probably &lt;em&gt;riddled&lt;/em&gt; with bugs. But it
sketches the kind of memory layouts that underpin Skybison, V8, JSC, and so on.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://gist.github.com/tekknolagi/caf8462b75ac8ba7e33f6ade06b3f914&quot;&gt;Take a look&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;It doesn’t have the overflow slot but it’s a decent sketch of the underlying
model.&lt;/p&gt;

&lt;h2 id=&quot;and-more&quot;&gt;And more&lt;/h2&gt;

&lt;p&gt;I fear I am running out of steam on this blog post but there is more to talk
about. I originally wanted to go add object shapes to someone’s Lox interpreter
and then add inline caches to the interpreter.&lt;/p&gt;

&lt;p&gt;Perhaps I will come back and update this post. Or perhaps you will write a
sequel.&lt;/p&gt;

&lt;!--
TODO: shape GC (because of classes?)
--&gt;

&lt;h2 id=&quot;thank-you&quot;&gt;Thank you&lt;/h2&gt;

&lt;p&gt;Thank you to CF Bolz-Tereick, Nery Campusano, and Jacob Denbeaux for thoughtful
feedback on this post.&lt;/p&gt;
&lt;div class=&quot;footnotes&quot; role=&quot;doc-endnotes&quot;&gt;
  &lt;ol&gt;
    &lt;li id=&quot;fn:js-synonyms&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;Or the following equivalents…&lt;/p&gt;

      &lt;div class=&quot;language-javascript highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// Or&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;C&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// Or&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;C&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;constructor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;      &lt;/div&gt;
      &lt;p&gt;&lt;a href=&quot;#fnref:js-synonyms&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:attribute-order&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;See &lt;a href=&quot;https://github.com/rails/rails/pull/57020&quot;&gt;this Rails
PR&lt;/a&gt; by Hartley to make shapes
more monomorphic. It was initially filed because ZJIT was not capable of
handling shape polymorphic reads, but it helps other JITs stay monomorphic
too. Also in this vein is &lt;a href=&quot;https://github.com/mame/optcarrot/pull/59&quot;&gt;my PR to
optcarrot&lt;/a&gt;.&lt;/p&gt;

      &lt;p&gt;ZJIT now does much better with polymorphic shapes but this kind of stuff is
generally important across languages and runtimes. Enough for people to do
&lt;a href=&quot;/assets/img/optimization-coaching.pdf&quot;&gt;large bodies&lt;/a&gt; (PDF) &lt;a href=&quot;/assets/img/optimization-coaching-js.pdf&quot;&gt;of
research&lt;/a&gt; (PDF) about
intelligibly surfacing this kind of information to the application
programmer. &lt;a href=&quot;#fnref:attribute-order&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
  &lt;/ol&gt;
&lt;/div&gt;
</description>
            <pubDate>Wed, 21 Oct 2026 00:00:00 +0000</pubDate>
            <niceDate>October 21, 2026</niceDate>
            <link>https://bernsteinbear.com/blog/object-shapes/?utm_source=rss</link>
            <guid isPermaLink="true">https://bernsteinbear.com/blog/object-shapes/</guid>
        </item>
        
        <item>
            <title>Support Local Variables</title>
            <description>&lt;p&gt;Discovering and supporting all of the wild semantics for local variables in
Ruby was so interesting that the ZJIT team decided to write a paper about it:
&lt;a href=&quot;/assets/img/support-local-variables.pdf&quot;&gt;Support Local Variables&lt;/a&gt; (PDF). It’s
also the first bit of academic literature that documents and explains ZJIT.&lt;/p&gt;

&lt;p&gt;It’s published at VMIL 2026, where someone on our team will be presenting it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Abstract&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Ruby is a dynamically typed and object-oriented programming language. Its
primary implementation, CRuby, contains a bytecode virtual machine and a
mature lazy basic block versioning (LBBV) just-in-time (JIT) compiler
called YJIT.&lt;/p&gt;

  &lt;p&gt;In order to both implement more advanced optimizations than YJIT supports
and also encourage more outside contributions, we present a new
method-based JIT called ZJIT. Like YJIT, ZJIT compiles from bytecode to
machine code. Unlike YJIT, ZJIT has multiple global and local
optimization passes.&lt;/p&gt;

  &lt;p&gt;ZJIT’s high-level intermediate representation is in static single
assignment (SSA) form. In order to optimize Ruby’s local variables, ZJIT
lifts local variables into SSA values. This is a departure from how
other Ruby compilers handle locals: other JIT compilers either leave local
variables as memory loads and stores or do advanced partial evaluation to
recover SSA values from memory.&lt;/p&gt;

  &lt;p&gt;While implementing locals, we (re-)discovered what features make local
variables in Ruby especially challenging to compile correctly and
efficiently. We demonstrate these features and illustrate how we solved
these problems in ZJIT.&lt;/p&gt;
&lt;/blockquote&gt;
</description>
            <pubDate>Fri, 04 Sep 2026 00:00:00 +0000</pubDate>
            <niceDate>September 4, 2026</niceDate>
            <link>https://bernsteinbear.com/blog/support-local-variables/?utm_source=rss</link>
            <guid isPermaLink="true">https://bernsteinbear.com/blog/support-local-variables/</guid>
        </item>
        
        <item>
            <title>Another partial SSI trick with canonicalize</title>
            <description>&lt;p&gt;&lt;em&gt;See &lt;a href=&quot;/blog/partial-ssi/&quot;&gt;the previous post&lt;/a&gt; for context and explanation of SSI&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;After reading &lt;a href=&quot;https://cfallin.org/blog/2026/04/09/aegraph/&quot;&gt;Chris Fallin’s aegraph
post&lt;/a&gt;, new ZJIT contributor dak2
landed a block-local version of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;canonicalize&lt;/code&gt; function in
&lt;a href=&quot;https://github.com/ruby/ruby/pull/16828&quot;&gt;#16828&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The pseudocode of the block-local canonicalize function looks like this:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;block&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reverse_post_order&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;rewrite_map&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;insn&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;block&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;insns&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;insn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;operands&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;map_in_place&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rewrite_map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;insn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;opcode&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;GuardType&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;rewrite_map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;insn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;insn&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;As a refresher, this turns IR like this:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;v0:Object = ...
v1:Int = GuardType v0, Int
... do something with v1

v2:Int = GuardType v0, Int
... do something with v2
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;into this:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;v0:Object = ...
v1:Int = GuardType v0, Int
... do something with v1

v2:Int = GuardType v1, Int
... do something with v2
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Note that the second use of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v0&lt;/code&gt; has been turned into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v1&lt;/code&gt;. This is important
because a later constant-folding pass can observe that the input &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v1&lt;/code&gt; of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GuardType
v1, Int&lt;/code&gt; is &lt;em&gt;already an Int&lt;/em&gt; and can therefore replace all uses of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v2&lt;/code&gt; with
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v1&lt;/code&gt; and delete the guard.&lt;/p&gt;

&lt;p&gt;Because in this local version we make a new &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rewrite_map&lt;/code&gt; for each block, we
don’t carry any rewrites across blocks (but ZJIT still gained a lot because
until recently its IR was in maximal SSA form, so a block-local pass had more
global effects).&lt;/p&gt;

&lt;p&gt;About five days later, dak2 came back with a global version in
&lt;a href=&quot;https://github.com/ruby/ruby/pull/17013&quot;&gt;#17013&lt;/a&gt;! This PR came with a bunch of
changes in the name of performance—which I appreciate—but I like doing the
silly slow thing first, especially because the PR is so much smaller. We can
always refactor it later to be faster and, in the meantime, use the slow but
maybe-easier-to-verify thing as a correctness oracle. So I did&lt;sup id=&quot;fnref:much-later&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:much-later&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;1&lt;/a&gt;&lt;/sup&gt; the
silly slow thing in &lt;a href=&quot;https://github.com/ruby/ruby/pull/17766&quot;&gt;#17766&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This copy-happy version of canonicalize looks like:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;rewrite_maps&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;block&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;block&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;blocks&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;dominators&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;compute_dominators&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;block&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reverse_post_order&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;rewrite_map&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rewrite_maps&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dominators&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;idom&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;block&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;clone&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;insn&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;block&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;insns&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;insn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;operands&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;map_in_place&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rewrite_map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;insn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;opcode&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;GuardType&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;rewrite_map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;insn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;insn&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;rewrite_maps&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;block&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rewrite_map&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;(Which you may or may not notice looks a lot like &lt;a href=&quot;https://github.com/beehive-lab/Maxine-VM/blob/e213a842f78983e2ba112ae46de8c64317bc206e/com.sun.c1x/src/com/sun/c1x/opt/GlobalValueNumberer.java&quot;&gt;Maxine’s GVN
implementation&lt;/a&gt;.
This is not a coincidence.)&lt;/p&gt;

&lt;p&gt;The core stays the same as the block-local version but now we can cascade
rewrites along the dominator tree. I say that but we’re not actually computing
a (top-down walkable) dominator tree—we’re only building a (bottom-up) map of
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;idom&lt;/code&gt; using &lt;a href=&quot;/assets/img/dominators-engineered.pdf&quot;&gt;the engineered algorithm&lt;/a&gt;
(PDF). The rewrites still cascade down the dominator tree because this
RPO-walk+idom-clone approach ends up being equivalent to actually walking a
dominator tree.&lt;/p&gt;

&lt;p&gt;The block iteration order is different (RPO vs domtree pre-order) but the only
property we care about maintaining is that we visit dominators before blocks
that get dominated, and that is true in both.&lt;/p&gt;

&lt;p&gt;But where was I going with all this?&lt;/p&gt;

&lt;p&gt;…&lt;/p&gt;

&lt;p&gt;Oh, right. More &lt;a href=&quot;/blog/partial-ssi/&quot;&gt;partial SSI&lt;/a&gt;. In the last post, we
inserted &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RefineType&lt;/code&gt; in SSA construction so that we can infer things about the
Ruby type of the conditional. For example:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;bb0:
  v0: Object = ...
  v1: CBool = Test v0
  v2: Truthy = RefineType v0, Truthy
  v3: Falsy = RefineType v0, Falsy
  CondBranch v1, bb1(v2), bb2(v3)

bb1(v4:Truthy):
  ...

bb2(v5:Falsy):
  ...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This is neat when the branch comes from Ruby code but sometimes we synthesize
branches so we can’t do this in SSA construction. The general case looks like
this:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;bb0:
  v0: CBool = ...
  CondBranch v0, bb1, bb2

bb1:
  ...

bb2:
  ...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In this more general case, we stil want &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bb1&lt;/code&gt; to know that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v0&lt;/code&gt; is
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CBool[true]&lt;/code&gt; in that branch and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bb2&lt;/code&gt; to know that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v0&lt;/code&gt; is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CBool[false]&lt;/code&gt; in
its branch (and in blocks dominated by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bb1&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bb2&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;Well, this is another thing we can do in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;canonicalize&lt;/code&gt;!&lt;/p&gt;

&lt;p&gt;All we need to do is at the beginning of each block B:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Check if B has one incoming control edge E&lt;sup id=&quot;fnref:block-vs-edge&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:block-vs-edge&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;2&lt;/a&gt;&lt;/sup&gt;&lt;/li&gt;
  &lt;li&gt;Check if the terminator T for E.block is a conditional branch&lt;/li&gt;
  &lt;li&gt;If T.iftrue == B, seed the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rewrite_map&lt;/code&gt; with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T.cond =&amp;gt; Const(CBool[true])&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;If T.iffalse == B, seed the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rewrite_map&lt;/code&gt; with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T.cond =&amp;gt; Const(CBool[false])&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you plan on running &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;canonicalize&lt;/code&gt; multiple times, you may end up generating
many constant instructions in your IR. To avoid this, you can intern them and,
for example, place them in the entry block. This helps make the pass idempotent
instead of always allocating new instructions.&lt;/p&gt;

&lt;p&gt;So what does this buy us?&lt;/p&gt;

&lt;p&gt;Well, I admit I was looking at the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;30k_ifelse&lt;/code&gt; benchmark on ruby-bench and
wondering how to further collapse a bunch of IR that came out of my prototype
value numbering implementation. The IR after value numbering looked like:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;bb0:
  v0: CBool = ...
  CondBranch v0, bb1, bb2

bb1:
  ...
  CondBranch v0, ...

bb2:
  ...
  CondBranch v0, ...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;and it felt a little silly that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bb1&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bb2&lt;/code&gt; didn’t get more information
about &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v0&lt;/code&gt; by being branch targets. This couple-line change managed to collapse
a bunch of those branches.&lt;/p&gt;

&lt;p&gt;So, perhaps a bit contrived, but it feels like a useful tool to have.&lt;/p&gt;

&lt;p&gt;See you all next time!&lt;/p&gt;

&lt;h2 id=&quot;see-also&quot;&gt;See also&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/LineageOS/android_art/blob/1690c6912a7972c9e62c39b48c706de9b8b18b4a/compiler/optimizing/constant_folding.cc#L316&quot;&gt;Different implementation in ART&lt;/a&gt;&lt;/p&gt;
&lt;div class=&quot;footnotes&quot; role=&quot;doc-endnotes&quot;&gt;
  &lt;ol&gt;
    &lt;li id=&quot;fn:much-later&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;This PR landed much later than dak2’s because I wanted to wait
for the &lt;a href=&quot;https://github.com/ruby/ruby/pull/17311&quot;&gt;SSA minimization pass&lt;/a&gt; to
land—more on that another time—so that global canonicalization could do
more. Otherwise, because we had maximal SSA, we didn’t really re-use SSA
values across blocks. &lt;a href=&quot;#fnref:much-later&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:block-vs-edge&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;It’s possible to have one block A do a conditional branch to
another block B for both the iftrue case and the iffalse case. This is
useful if, for example, it is passing different data along the block
arguments of each edge. For this reason, we check the number of incoming
edges, not the number of predecessor blocks. &lt;a href=&quot;#fnref:block-vs-edge&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
  &lt;/ol&gt;
&lt;/div&gt;
</description>
            <pubDate>Thu, 13 Aug 2026 00:00:00 +0000</pubDate>
            <niceDate>August 13, 2026</niceDate>
            <link>https://bernsteinbear.com/blog/more-partial-ssi/?utm_source=rss</link>
            <guid isPermaLink="true">https://bernsteinbear.com/blog/more-partial-ssi/</guid>
        </item>
        
        <item>
            <title>A quick look at zero-knowledge proofs</title>
            <description>&lt;p&gt;&lt;em&gt;NB: This isn’t about crypto. I don’t care about crypto.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://www.chrisgregory.me/&quot;&gt;Chris&lt;/a&gt; messaged me the other week asking
if I wanted to implement zero-knowledge proofs. I initially was not interested,
but then he said:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;What if I told you there’s a version of them that has nothing to do with
cryptocurrencies? What if I told you it involves graph theory? What if I told
you there’s a 30 line implementation?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now &lt;em&gt;that&lt;/em&gt; was interesting.&lt;/p&gt;

&lt;p&gt;The idea of a zero-knowledge proof (ZKP) is that there are two parties: the
prover and the verifier. The prover asserts that it has a solution to a
(generally NP-complete) problem. The prover can convince the verifier of this
without sharing the actual solution to the problem.&lt;/p&gt;

&lt;p&gt;The canonical example is 3-coloring a graph. That is, the prover asserts that,
for a given (shared) graph, it has a valid 3-coloring. It wants to convince the
verifier of this without revealing the actual color assignment.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;As a quick recap, graph coloring is the problem where given a graph,
we find a way to assign each node a color such that no two adjacent nodes
have the same color. 3-coloring is coloring with at most 3 colors.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;!--
graph {
    0 -- 1;
    1 -- 2;
    2 -- 3;
    3 -- 4;
    4 -- 0;
    0 -- 2;
}
--&gt;
&lt;object&gt;
&lt;svg width=&quot;127pt&quot; height=&quot;332pt&quot; viewBox=&quot;0.00 0.00 127.00 332.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 328)&quot;&gt;
&lt;title&gt;%0&lt;/title&gt;
&lt;polygon fill=&quot;#ffffff&quot; stroke=&quot;transparent&quot; points=&quot;-4,4 -4,-328 123,-328 123,4 -4,4&quot; /&gt;
&lt;!-- 0 --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;0&lt;/title&gt;
&lt;ellipse fill=&quot;none&quot; stroke=&quot;#000000&quot; cx=&quot;82&quot; cy=&quot;-306&quot; rx=&quot;27&quot; ry=&quot;18&quot; /&gt;
&lt;text text-anchor=&quot;middle&quot; x=&quot;82&quot; y=&quot;-301.8&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot; fill=&quot;#000000&quot;&gt;0&lt;/text&gt;
&lt;/g&gt;
&lt;!-- 1 --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;1&lt;/title&gt;
&lt;ellipse fill=&quot;none&quot; stroke=&quot;#000000&quot; cx=&quot;27&quot; cy=&quot;-234&quot; rx=&quot;27&quot; ry=&quot;18&quot; /&gt;
&lt;text text-anchor=&quot;middle&quot; x=&quot;27&quot; y=&quot;-229.8&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot; fill=&quot;#000000&quot;&gt;1&lt;/text&gt;
&lt;/g&gt;
&lt;!-- 0&amp;#45;&amp;#45;1 --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;0&amp;#45;&amp;#45;1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;#000000&quot; d=&quot;M69.5196,-289.6621C60.5186,-277.8788 48.4207,-262.0416 39.431,-250.2733&quot; /&gt;
&lt;/g&gt;
&lt;!-- 2 --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;2&lt;/title&gt;
&lt;ellipse fill=&quot;none&quot; stroke=&quot;#000000&quot; cx=&quot;65&quot; cy=&quot;-162&quot; rx=&quot;27&quot; ry=&quot;18&quot; /&gt;
&lt;text text-anchor=&quot;middle&quot; x=&quot;65&quot; y=&quot;-157.8&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot; fill=&quot;#000000&quot;&gt;2&lt;/text&gt;
&lt;/g&gt;
&lt;!-- 0&amp;#45;&amp;#45;2 --&gt;
&lt;g id=&quot;edge6&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;0&amp;#45;&amp;#45;2&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;#000000&quot; d=&quot;M79.8813,-288.0535C76.6237,-260.4592 70.4191,-207.9026 67.1454,-180.1727&quot; /&gt;
&lt;/g&gt;
&lt;!-- 1&amp;#45;&amp;#45;2 --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;1&amp;#45;&amp;#45;2&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;#000000&quot; d=&quot;M36.0055,-216.937C41.9752,-205.626 49.7851,-190.8282 55.7952,-179.4407&quot; /&gt;
&lt;/g&gt;
&lt;!-- 3 --&gt;
&lt;g id=&quot;node4&quot; class=&quot;node&quot;&gt;
&lt;title&gt;3&lt;/title&gt;
&lt;ellipse fill=&quot;none&quot; stroke=&quot;#000000&quot; cx=&quot;65&quot; cy=&quot;-90&quot; rx=&quot;27&quot; ry=&quot;18&quot; /&gt;
&lt;text text-anchor=&quot;middle&quot; x=&quot;65&quot; y=&quot;-85.8&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot; fill=&quot;#000000&quot;&gt;3&lt;/text&gt;
&lt;/g&gt;
&lt;!-- 2&amp;#45;&amp;#45;3 --&gt;
&lt;g id=&quot;edge3&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;2&amp;#45;&amp;#45;3&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;#000000&quot; d=&quot;M65,-143.8314C65,-133 65,-119.2876 65,-108.4133&quot; /&gt;
&lt;/g&gt;
&lt;!-- 4 --&gt;
&lt;g id=&quot;node5&quot; class=&quot;node&quot;&gt;
&lt;title&gt;4&lt;/title&gt;
&lt;ellipse fill=&quot;none&quot; stroke=&quot;#000000&quot; cx=&quot;92&quot; cy=&quot;-18&quot; rx=&quot;27&quot; ry=&quot;18&quot; /&gt;
&lt;text text-anchor=&quot;middle&quot; x=&quot;92&quot; y=&quot;-13.8&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot; fill=&quot;#000000&quot;&gt;4&lt;/text&gt;
&lt;/g&gt;
&lt;!-- 3&amp;#45;&amp;#45;4 --&gt;
&lt;g id=&quot;edge4&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;3&amp;#45;&amp;#45;4&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;#000000&quot; d=&quot;M71.6742,-72.2022C75.8593,-61.0419 81.2321,-46.7143 85.4026,-35.593&quot; /&gt;
&lt;/g&gt;
&lt;!-- 4&amp;#45;&amp;#45;0 --&gt;
&lt;g id=&quot;edge5&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;4&amp;#45;&amp;#45;0&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;#000000&quot; d=&quot;M95.7474,-35.9069C97.7466,-46.3759 99.9966,-59.8759 101,-72 104.9591,-119.8364 104.5886,-132.1343 101,-180 98.0891,-218.8263 90.2002,-263.8211 85.5539,-288.1513&quot; /&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/object&gt;

&lt;!--
graph {
    0 [style=filled; color=navy; fontcolor=white];
    1 [style=filled; color=darkgreen; fontcolor=white];
    2 [style=filled; color=crimson; fontcolor=white];
    3 [style=filled; color=navy; fontcolor=white];
    4 [style=filled; color=darkgreen; fontcolor=white];
    0 -- 1;
    1 -- 2;
    2 -- 3;
    3 -- 4;
    4 -- 0;
    0 -- 2;
}
--&gt;
&lt;object&gt;
&lt;svg width=&quot;127pt&quot; height=&quot;332pt&quot; viewBox=&quot;0.00 0.00 127.00 332.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 328)&quot;&gt;
&lt;title&gt;%0&lt;/title&gt;
&lt;polygon fill=&quot;#ffffff&quot; stroke=&quot;transparent&quot; points=&quot;-4,4 -4,-328 123,-328 123,4 -4,4&quot; /&gt;
&lt;!-- 0 --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;0&lt;/title&gt;
&lt;ellipse fill=&quot;#000080&quot; stroke=&quot;#000080&quot; cx=&quot;82&quot; cy=&quot;-306&quot; rx=&quot;27&quot; ry=&quot;18&quot; /&gt;
&lt;text text-anchor=&quot;middle&quot; x=&quot;82&quot; y=&quot;-301.8&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot; fill=&quot;#ffffff&quot;&gt;0&lt;/text&gt;
&lt;/g&gt;
&lt;!-- 1 --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;1&lt;/title&gt;
&lt;ellipse fill=&quot;#006400&quot; stroke=&quot;#006400&quot; cx=&quot;27&quot; cy=&quot;-234&quot; rx=&quot;27&quot; ry=&quot;18&quot; /&gt;
&lt;text text-anchor=&quot;middle&quot; x=&quot;27&quot; y=&quot;-229.8&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot; fill=&quot;#ffffff&quot;&gt;1&lt;/text&gt;
&lt;/g&gt;
&lt;!-- 0&amp;#45;&amp;#45;1 --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;0&amp;#45;&amp;#45;1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;#000000&quot; d=&quot;M69.5196,-289.6621C60.5186,-277.8788 48.4207,-262.0416 39.431,-250.2733&quot; /&gt;
&lt;/g&gt;
&lt;!-- 2 --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;2&lt;/title&gt;
&lt;ellipse fill=&quot;#dc143c&quot; stroke=&quot;#dc143c&quot; cx=&quot;65&quot; cy=&quot;-162&quot; rx=&quot;27&quot; ry=&quot;18&quot; /&gt;
&lt;text text-anchor=&quot;middle&quot; x=&quot;65&quot; y=&quot;-157.8&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot; fill=&quot;#ffffff&quot;&gt;2&lt;/text&gt;
&lt;/g&gt;
&lt;!-- 0&amp;#45;&amp;#45;2 --&gt;
&lt;g id=&quot;edge6&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;0&amp;#45;&amp;#45;2&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;#000000&quot; d=&quot;M79.8813,-288.0535C76.6237,-260.4592 70.4191,-207.9026 67.1454,-180.1727&quot; /&gt;
&lt;/g&gt;
&lt;!-- 1&amp;#45;&amp;#45;2 --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;1&amp;#45;&amp;#45;2&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;#000000&quot; d=&quot;M36.0055,-216.937C41.9752,-205.626 49.7851,-190.8282 55.7952,-179.4407&quot; /&gt;
&lt;/g&gt;
&lt;!-- 3 --&gt;
&lt;g id=&quot;node4&quot; class=&quot;node&quot;&gt;
&lt;title&gt;3&lt;/title&gt;
&lt;ellipse fill=&quot;#000080&quot; stroke=&quot;#000080&quot; cx=&quot;65&quot; cy=&quot;-90&quot; rx=&quot;27&quot; ry=&quot;18&quot; /&gt;
&lt;text text-anchor=&quot;middle&quot; x=&quot;65&quot; y=&quot;-85.8&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot; fill=&quot;#ffffff&quot;&gt;3&lt;/text&gt;
&lt;/g&gt;
&lt;!-- 2&amp;#45;&amp;#45;3 --&gt;
&lt;g id=&quot;edge3&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;2&amp;#45;&amp;#45;3&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;#000000&quot; d=&quot;M65,-143.8314C65,-133 65,-119.2876 65,-108.4133&quot; /&gt;
&lt;/g&gt;
&lt;!-- 4 --&gt;
&lt;g id=&quot;node5&quot; class=&quot;node&quot;&gt;
&lt;title&gt;4&lt;/title&gt;
&lt;ellipse fill=&quot;#006400&quot; stroke=&quot;#006400&quot; cx=&quot;92&quot; cy=&quot;-18&quot; rx=&quot;27&quot; ry=&quot;18&quot; /&gt;
&lt;text text-anchor=&quot;middle&quot; x=&quot;92&quot; y=&quot;-13.8&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot; fill=&quot;#ffffff&quot;&gt;4&lt;/text&gt;
&lt;/g&gt;
&lt;!-- 3&amp;#45;&amp;#45;4 --&gt;
&lt;g id=&quot;edge4&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;3&amp;#45;&amp;#45;4&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;#000000&quot; d=&quot;M71.6742,-72.2022C75.8593,-61.0419 81.2321,-46.7143 85.4026,-35.593&quot; /&gt;
&lt;/g&gt;
&lt;!-- 4&amp;#45;&amp;#45;0 --&gt;
&lt;g id=&quot;edge5&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;4&amp;#45;&amp;#45;0&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;#000000&quot; d=&quot;M95.7474,-35.9069C97.7466,-46.3759 99.9966,-59.8759 101,-72 104.9591,-119.8364 104.5886,-132.1343 101,-180 98.0891,-218.8263 90.2002,-263.8211 85.5539,-288.1513&quot; /&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/object&gt;

&lt;p&gt;How do you do this? Assorted blog posts and fancy-looking demonstrations were
interesting but did not help us understand much.&lt;/p&gt;

&lt;p&gt;Chris and I went around in circles for a bit until we decided to take a look at
&lt;a href=&quot;https://dl.acm.org/doi/pdf/10.1145/116825.116852&quot;&gt;one of the original papers&lt;/a&gt;
(PDF) by Goldreich, Micali, and Widgerson. We only really read page 23 (labeled
page 713 in the PDF) but that was enough to get things going.&lt;/p&gt;

&lt;h2 id=&quot;the-papers-protocol&quot;&gt;The paper’s protocol&lt;/h2&gt;

&lt;p&gt;Protocol 4 from the paper describes an interactive 3-color proof session
between the prover (P, with numbered steps) and the verifier (V, with numbered
steps), reproduced here:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;em&gt;common input&lt;/em&gt; A graph &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;G(V, E)&lt;/code&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;n = |V|&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;m = |E|&lt;/code&gt;).&lt;/p&gt;

  &lt;p&gt;The following four steps are executed &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;m²&lt;/code&gt; times, each time using independent
coin tosses.&lt;/p&gt;

  &lt;p&gt;(P1) The prover chooses at random an assignment of three colors to the
three independent sets induced by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;φ&lt;/code&gt;, colors the graph using this 3-coloring,
and places these colors in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;n&lt;/code&gt; locked boxes each bearing the number of the
corresponding vertex. More specifically, the prover chooses a permutation
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;π ∈R S₃&lt;/code&gt;, places &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;π(φ(i))&lt;/code&gt; in a box marked &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;i&lt;/code&gt; (∀ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;i ∈ V&lt;/code&gt;), locks all boxes
and sends them (without the keys) to the verifier.&lt;/p&gt;

  &lt;p&gt;(V1) The verifier chooses at random an edge &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;e ∈R E&lt;/code&gt; and sends it to the
prover. (Intuitively, the verifier asks to examine the colors of the endpoints
of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;e ∈ E&lt;/code&gt;.)&lt;/p&gt;

  &lt;p&gt;(P2) If &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;e = (u, v) ∈ E&lt;/code&gt;, then the prover reveals the colors of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;u&lt;/code&gt; and
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v&lt;/code&gt; by sending the verifier the keys to boxes &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;u&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v&lt;/code&gt;. Otherwise, the
prover does nothing.&lt;/p&gt;

  &lt;p&gt;(V2) The verifier opens boxes &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;u&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v&lt;/code&gt; using the keys received and
checks whether they contain two different elements of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{1, 2, 3}&lt;/code&gt;. If the keys
do not match the boxes, or the contents violate the condition then the
verifier &lt;em&gt;rejects&lt;/em&gt; and stops. Otherwise, the verifier continues to the next
iteration.&lt;/p&gt;

  &lt;p&gt;If the verifier has completed all &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;m²&lt;/code&gt; iterations then it &lt;em&gt;accepts&lt;/em&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We’ll come back to the number of iterations. For now let’s try to just do &lt;em&gt;one&lt;/em&gt;
iteration. For each step, I’ll annotate the code with “Only prover” or “Only
verifier” so that it’s clear who can see what data.&lt;/p&gt;

&lt;h2 id=&quot;one-iteration&quot;&gt;One iteration&lt;/h2&gt;

&lt;p&gt;We’ll start by sketching out what it means to have a graph. For the example
graphviz graph above, we have the following edge list data structure:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# Shared between prover, verifier
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;edges&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Each tuple in the list represents a connection between two numbered nodes.
Fancy stuff. Because it’s an undirected graph, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(0, 1)&lt;/code&gt; means the same as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(1,
0)&lt;/code&gt; so we don’t have to include both. We can also color it:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# Only prover
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;coloring&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;navy&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;darkgreen&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;crimson&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;navy&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;darkgreen&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Each key is a node number and each value is a color.&lt;/p&gt;

&lt;p&gt;Though finding a 3-coloring of a graph is slow, verifying one is fast—linear
in the number of edges. Let’s verify that we have a valid sample coloring:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# For the reader
# Check each edge to make sure no edge has the same color on each node
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;all&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;coloring&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;coloring&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;edges&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# Check that the total number of colors used is 3
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;coloring&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()))&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We’ll now go through the paper’s steps one by one, writing some code to
accompany each step.&lt;/p&gt;

&lt;h3 id=&quot;tips-and-tricks&quot;&gt;Tips and tricks&lt;/h3&gt;

&lt;p&gt;If you are building alongside the blog post, I recommend using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;random.seed(0)&lt;/code&gt;
so your randomness doesn’t change between runs of your program. I also
recommend setting the environment variable &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PYTHONHASHSEED&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt; if you are
using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;hash&lt;/code&gt; for the same stability reasons.&lt;/p&gt;

&lt;h3 id=&quot;step-p1&quot;&gt;Step P1&lt;/h3&gt;

&lt;p&gt;The first thing we need to do is permute the coloring we have. That is, we
should swap around the color values while maintaining the 3-color property.&lt;/p&gt;

&lt;p&gt;Thankfully, this is easier than it might sound: the names of colors are
meaningless to the 3-coloring; they need only be different along the edges. So
if we do a bijective (A maps to one B, and B came from one A) mapping from old
to new name, this will hold.&lt;/p&gt;

&lt;p&gt;I came up with this function that shuffles the colors, lines them up
side-by-side, makes a table, and then uses that to make a new coloring:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;random&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Only prover
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;permute_three_coloring&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;coloring&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;all_colors&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;coloring&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()))&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;new_colors&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sample&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;all_colors&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;all_colors&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;permutation&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;old&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;old&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;zip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;all_colors&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;new_colors&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;node&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;permutation&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;node&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;color&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;coloring&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()}&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# For example,
# {0: &quot;crimson&quot;, 1: &quot;navy&quot;, 2: &quot;darkgreen&quot;, 3: &quot;crimson&quot;, 4: &quot;navy&quot;}
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Then we have to place the colors in “locked boxes”. One way to proverbially
lock a box is to apply a one-way function to it: for example, a hash function.
If we hash each color and then pass only the hashes to the verifier, the
verifier cannot open them.&lt;/p&gt;

&lt;p&gt;This example uses the Python standard library hash function for brevity but it
might be better to use a cryptographic hash function like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;hashlib.sha256&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# Only prover. Wrong!
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;hash_coloring_wrong&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;coloring&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;node&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;hash&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;node&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;color&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;coloring&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()}&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# For example:
# {0: -6789624683659967261, 1: 7846608853949633950, 2: 6009240650600289446,
#  3: -6789624683659967261, 4: 7846608853949633950}
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;There’s just one problem with handing these locked boxes to the verifier: two
boxes locked with the same colors will have the same hashes. The verifier would
know the coloring. Even if the exact colors are now hidden, it’s really the
structure of the coloring for which we want to give up zero knowledge.&lt;/p&gt;

&lt;p&gt;To get around this, we can add what’s called a &lt;em&gt;nonce&lt;/em&gt; to each node and its
coloring. That is, each node gets a little bit of random data packed into the
hash so that different nodes’ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;darkgreen&quot;&lt;/code&gt; hash values look different.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# Only prover
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;nonce&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;randrange&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;box_coloring&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;coloring&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;node&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nonce&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;node&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;coloring&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;hash_values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;coloring&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;hash&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;coloring&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()}&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;permuted_coloring&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;permute_three_coloring&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;coloring&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# For example:
# {0: &quot;crimson&quot;, 1: &quot;navy&quot;, 2: &quot;darkgreen&quot;, 3: &quot;crimson&quot;, 4: &quot;navy&quot;}
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;boxed_coloring&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;box_coloring&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;permuted_coloring&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# For example:
# {0: (&quot;crimson&quot;, 33), 1: (&quot;navy&quot;, 65), 2: (&quot;darkgreen&quot;, 62),
#  3: (&quot;crimson&quot;, 51), 4: (&quot;navy&quot;, 38)}
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hashed_coloring&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hash_values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;boxed_coloring&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# For example:
# {0: -2275004828450249492, 1: 2227921633151400991, 2: -5024343381376265886,
#  3: -5381005702768533635, 4: 1164729608819214729}
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Again, you probably don’t want to use the standard library random number
generator for your nonces. You should consider something like
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;secrets.token_hex()&lt;/code&gt; from the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;secrets&lt;/code&gt; module (Python 3.6+). Maybe even
consider using the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;hmac&lt;/code&gt; module.&lt;/p&gt;

&lt;p&gt;Finally, we can send the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;hashed_coloring&lt;/code&gt; to the verifier and begin step V1.&lt;/p&gt;

&lt;h3 id=&quot;step-v1&quot;&gt;Step V1&lt;/h3&gt;

&lt;p&gt;Since the verifier knows the graph (but not its colors), it can pick an
arbitrary edge to inspect. It wants to verify that the arbitrary edge it picked
satisfies the 3-color conditions. It sends off a request for an arbitrary edge
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;e&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# Only verifier
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;choice&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;edges&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;revealed&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;prover_please_reveal_colors&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This is implicitly relying on some global state (the prover knowing what
“session” is active with the verifier). If you have multiple verifiers or
concurrent sessions or something, you may need to thread through some context
identifier in the communication.&lt;/p&gt;

&lt;h3 id=&quot;step-p2&quot;&gt;Step P2&lt;/h3&gt;

&lt;p&gt;The prover, having received this request, sends over the colors and nonces for
each of the nodes in the edge.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# Only prover
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;prover_please_reveal_colors&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;edge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;edge&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;boxed_coloring&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;boxed_coloring&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]}&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# For example:
# {3: (&apos;crimson&apos;, 51), 4: (&apos;navy&apos;, 38)}
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;You may be suspicious at this point because we’re leaking some information
about the coloring.&lt;/p&gt;

&lt;p&gt;Note that it’s ok for the prover to reveal the color for one edge, because 1)
the colors have been shuffled once per round and 2) we’re going to apply our
box locking protocol &lt;em&gt;each time we reveal an edge&lt;/em&gt; (also once per round), so
the verifier accumulates no information about our colors between iterations.&lt;/p&gt;

&lt;h3 id=&quot;step-v2&quot;&gt;Step V2&lt;/h3&gt;

&lt;p&gt;The verifier can check that the color+nonce hashes to the hash value given for
each node in step P1. This ensures that the prover is not changing colors
around mid-round. This relies on the verifier and the prover using the same
hash function (and the same hash seed if using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;hash&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;The verifier must also check that the colors of the two nodes at each end of
the edge is a member of the publicly agreed on color set. Otherwise, the prover
could color every node a different color.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# Only verifier
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;node&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nonce&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;revealed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hashed_coloring&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;node&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;hash&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nonce&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)),&lt;/span&gt; &lt;span class=&quot;sa&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Hash mismatch!&quot;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;color&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;all_colors&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sa&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Invalid color &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;!&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The verifier can then inspect that the two color values are different. This
gives a small amount of credence (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1/|E|&lt;/code&gt; because you know something about one
edge now) that the graph is 3-colored appropriately because the prover had no
way of knowing which edge the verifier would want to inspect.&lt;/p&gt;

&lt;p&gt;If any of these conditions don’t check out, the verifier &lt;em&gt;rejects&lt;/em&gt;.&lt;/p&gt;

&lt;h2 id=&quot;probabilities&quot;&gt;Probabilities&lt;/h2&gt;

&lt;p&gt;You have to do at least a couple of rounds of this for the verifier to believe
the prover about the 3-coloring.&lt;/p&gt;

&lt;p&gt;The paper goes on to assert that “the probability that the verifier will
accept (i.e., complete all the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;m²&lt;/code&gt; rounds without detecting that “something is
wrong”) is bounded above by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(1 - m⁻¹)^(m²)&lt;/code&gt;” (where &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;m = |E|&lt;/code&gt;). Which is
pretty good. For a large graph (say, 1000 edges), this falls off reasonably
quickly:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4600&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;(1 - m⁻¹)^round = &quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;**-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;At 4600 rounds, you’re at 1% possibility of “cheating”. At 10,000 rounds,
you’re at 0.0045% possibility of “cheating”. At &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;m² = 1,000,000&lt;/code&gt; rounds, it’s
&lt;em&gt;very low&lt;/em&gt;.&lt;/p&gt;

&lt;h2 id=&quot;a-networked-demo&quot;&gt;A networked demo&lt;/h2&gt;

&lt;p&gt;Writing Python code that runs in a single process with comments denoting
“prover” and “verifier” is not very satisfying. It does not preclude accidental
data leaking in the slightest. It would be much more satisfying if there was
some sort of barrier, like a process barrier or a network barrier, between the
prover and the verifier.&lt;/p&gt;

&lt;p&gt;For this reason, Chris and I have prepared a server (prover) and client
(verifier) demo. You can click “Run Round” to run a round (and show the
permuted colors). You can visit &lt;a href=&quot;https://zkp-3color-demo.fly.dev/docs&quot;&gt;the docs
page&lt;/a&gt; to see the API docs and build a
client yourself!&lt;/p&gt;

&lt;div id=&quot;output&quot;&gt;
    &lt;button id=&quot;round&quot;&gt;Run Round&lt;/button&gt;
    &lt;noscript&gt;&lt;p&gt;JavaScript is required to run this demo.&lt;/p&gt;&lt;/noscript&gt;
    &lt;p&gt;&lt;i&gt;(If you don&apos;t see a graph right below this message, please wait a
    moment for the server to wake up.)&lt;/i&gt;&lt;/p&gt;
    &lt;div id=&quot;demoGraph&quot;&gt;&lt;/div&gt;
    &lt;ol reversed=&quot;&quot; style=&quot;max-height: 100px; overflow-y: auto;&quot;&gt;&lt;/ol&gt;
&lt;/div&gt;

&lt;script type=&quot;module&quot;&gt;
import * as Viz from &quot;/assets/js/graphviz.js&quot;;

const BASE_URL = &apos;https://zkp-3color-demo.fly.dev&apos;;

async function requestGraph() {
    const responseText = await fetch(`${BASE_URL}/graph`);
    return responseText.json();
}

var sha256;
if (typeof window === &apos;undefined&apos;) {
    const { createHash } = require(&apos;node:crypto&apos;);
    sha256 = async function(content) {
        return createHash(&apos;sha256&apos;).update(content).digest(&apos;hex&apos;);
    }
} else {
    sha256 = async function(message) {
        const msgBuffer = new TextEncoder().encode(message);
        const hashBuffer = await window.crypto.subtle.digest(&apos;SHA-256&apos;, msgBuffer);
        const hashArray = Array.from(new Uint8Array(hashBuffer));
        return hashArray.map(b =&gt; b.toString(16).padStart(2, &apos;0&apos;)).join(&apos;&apos;);
    }
}

async function requestBoxedEdges() {
    const responseText = await fetch(`${BASE_URL}/boxes`);
    return responseText.json();
}

async function requestUnboxedEdge(roundId, edgeIndex) {
    const responseText = await fetch(`${BASE_URL}/edge?round_id=${roundId}&amp;edge_index=${edgeIndex}`);
    const response = await responseText.json();
    if (typeof response !== &apos;object&apos; || Array.isArray(response)) {
        throw new Error(&apos;Invalid response format&apos;);
    }
    return response;
}

async function round(edges) {
    const boxedEdges = await requestBoxedEdges();
    const hashes = boxedEdges.boxes;
    const roundId = boxedEdges.round_id;

    const randomIndex = Math.floor(Math.random() * edges.length);
    const response = await requestUnboxedEdge(roundId, randomIndex);
    const openedBoxes = response.openings;
    const edge = edges[randomIndex];
    const edge0 = `${edge[0]}`;
    const edge1 = `${edge[1]}`;
    if (!openedBoxes.hasOwnProperty(edge0) || !openedBoxes.hasOwnProperty(edge1)) {
        throw new Error(&apos;Response does not contain expected edge keys&apos;);
    }
    const {color: color0, nonce: nonce0} = openedBoxes[edge0];
    const {color: color1, nonce: nonce1} = openedBoxes[edge1];
    const hash0 = await sha256(`${color0}-${nonce0}`);
    const hash1 = await sha256(`${color1}-${nonce1}`);
    if (hash0 !== hashes[edge0] || hash1 !== hashes[edge1]) {
        throw new Error(&apos;Hash mismatch for edge colors&apos;);
    }
    if (color0 === color1) {
        throw new Error(&apos;Adjacent vertices have the same color&apos;);
    }
    const result = {};
    result[`node${edge0}`] = color0;
    result[`node${edge1}`] = color1;
    console.log(result);
    return result;
}

(async function() {
    const viz = await Viz.instance();
    const outputGraph = document.querySelector(&apos;#demoGraph&apos;);
    const graph = await requestGraph();
    const edges = graph.edges;
    // Graphviz numbers nodes node1, node2, ... in layout order, so give each
    // one an explicit id matching its vertex name.
    const nodes = [...new Set(edges.flat())];
    const dotString = `graph {
        ${nodes.map(node =&gt; `${node} [id=&quot;demo-node${node}&quot;]`).join(&apos;; &apos;)};
        ${edges.map(edge =&gt; `${edge[0]} -- ${edge[1]}`).join(&apos;; &apos;)}
    }`;
    const svgElement = viz.renderSVGElement(dotString);
    outputGraph.appendChild(svgElement);

    const outputList = document.querySelector(&apos;#output ol&apos;);
    const roundButton = document.querySelector(&apos;#round&apos;);

    function paint(nodeId, color) {
        const node = document.querySelector(`#demo-${nodeId}`);
        node.querySelector(&apos;ellipse&apos;).style.fill = color ?? &apos;none&apos;;
        node.querySelector(&apos;text&apos;).style.fill = color ? &apos;white&apos; : &apos;black&apos;;
    }

    function logRound(msg) {
        const item = document.createElement(&apos;li&apos;);
        item.textContent = msg;
        outputList.prepend(item);
    }

    function clearColors() {
        for (const node of nodes) {
            paint(`node${node}`, null);
        }
    }

    let i = 0;
    roundButton.addEventListener(&apos;click&apos;, async () =&gt; {
        roundButton.disabled = true;
        clearColors();
        try {
            const edgeColors = await round(edges);
            for (const [node, color] of Object.entries(edgeColors)) {
                paint(node, color);
            }
            const probability = 1 - Math.pow(1 - 1 / edges.length, i + 1);
            const msg = `Success. Confident ${(probability * 100).toFixed(2)}%`;
            logRound(msg);
            console.log(`Round ${i + 1}:`, msg);
            i += 1;
        } catch (error) {
            const msg = `Failure - ${error.message}`;
            logRound(msg);
            console.error(`Round ${i + 1}:`, msg);
            throw error;
        } finally {
            roundButton.disabled = false;
        }
    });
})();
&lt;/script&gt;

&lt;h2 id=&quot;encoding-other-np-complete-problems&quot;&gt;Encoding other NP-complete problems&lt;/h2&gt;

&lt;p&gt;At this point, we’ve shown how you can use a zero-knowledge interactive proof
to verify that someone has a valid 3-coloring of a graph without learning &lt;em&gt;any&lt;/em&gt;
information about the 3-coloring. So what? Is there anything else we can prove
with zero knowledge? Is the interactive proof of 3-coloring just a contrived
party trick without real applications?&lt;/p&gt;

&lt;p&gt;Well…&lt;/p&gt;

&lt;h3 id=&quot;sudoku&quot;&gt;Sudoku&lt;/h3&gt;

&lt;p&gt;Sudoku puzzles are another example of a problem that’s hard to solve and easy
to verify. It is algorithmically hard to fill in 81 squares to satisfy the
constraints of all rows, columns, and boxes containing the digits 1-9, but the
verifier is extremely quick.&lt;/p&gt;

&lt;p&gt;Let’s say we wanted to prove we’ve finished a Sudoku, but we don’t want to give
up a morsel of information about the solution. We can execute an interactive
proof very analogous to 3-coloring! Instead of shuffling colors, we shuffle
digits. Instead of revealing edges, we reveal rows, columns, and boxes.&lt;/p&gt;

&lt;p&gt;Next time your fellow bus rider leans over asking to see your Sudoku solution,
just ask them to go through 90,000 easy steps first!&lt;/p&gt;

&lt;h3 id=&quot;reduction&quot;&gt;Reduction&lt;/h3&gt;

&lt;p&gt;Let’s say we have a hard problem and we’ve computed a solution it, but we don’t
have an obvious algorithm on hand to execute an interactive proof for it.
Thanks to the authors of the paper above, we know that if the problem is
NP-complete there’s an interactive proof for it!&lt;/p&gt;

&lt;p&gt;We use the power of a polynomial time reduction. We (somehow) convert our
solution to a graph and its 3-coloring, then just follow the steps and code
above! The “somehow” is the tricky part, but much research exists on converting
between different NP-complete problems.&lt;/p&gt;

&lt;p&gt;For example, you may want to create a zero-knowledge proof that you know the
prime factors for a very large composite number. Unfortunately, for only double
digit numbers your graph is thousands of nodes, so reducing to 3-coloring has
its limits in practice and you’re better off with more sophisticated proof
techniques than a reduction to 3-coloring.&lt;/p&gt;

&lt;h2 id=&quot;wrapping-up&quot;&gt;Wrapping up&lt;/h2&gt;

&lt;p&gt;After doing a bit of research, we decided that the most common real world use
cases of zero-knowledge proofs (age verification, crypto, etc) aren’t
particularly interesting to us. We enjoyed the graphs and theory of computation
and networked computing though. We hope you had fun playing around with
interactive proofs too.&lt;/p&gt;
</description>
            <pubDate>Fri, 07 Aug 2026 00:00:00 +0000</pubDate>
            <niceDate>August 7, 2026</niceDate>
            <link>https://bernsteinbear.com/blog/zkp/?utm_source=rss</link>
            <guid isPermaLink="true">https://bernsteinbear.com/blog/zkp/</guid>
        </item>
        
        <item>
            <title>The inliner is yielding benefits for ZJIT</title>
            <description>&lt;p&gt;&lt;em&gt;Originally published on &lt;a href=&quot;https://railsatscale.com/2026-07-28-the-inliner-is-yielding-benefits/&quot;&gt;Rails At Scale&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;We recently enabled a really cool feature in ZJIT that makes it feel like a
Real Compiler™: the inliner! We’ll write more about it soon. In this
post, we’ll talk about one excellent concrete benefit we are already seeing and
how it optimizes blocks in pretty much every Ruby program.&lt;/p&gt;

&lt;p&gt;I’ll start off with a refresher on how blocks work in the Ruby interpreter,
then show you how ZJIT understands and optimizes that bytecode, and then show
you the impact of the inliner.&lt;/p&gt;

&lt;h2 id=&quot;ruby-a-refresher&quot;&gt;Ruby: a refresher&lt;/h2&gt;

&lt;p&gt;In the beginning, there were loops.&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;arr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;sum&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;length&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;sum&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;People used them to navigate and manipulate variable-length structures, like
arrays and strings. This was fine.&lt;/p&gt;

&lt;p&gt;Then, in the 1970s, a small group of computer scientists at Palo Alto Research
Center invented a programming language called Smalltalk. One of the core
features of Smalltalk was that everything was an object and computation was
done by sending messages to objects.&lt;/p&gt;

&lt;p&gt;This meant that iteration wouldn’t do at all. Instead, we would have to send
the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;do:&lt;/code&gt; message to the array object and pass it a block object.&lt;/p&gt;

&lt;div class=&quot;language-smalltalk highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;ss&quot;&gt;#(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;do:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;sum&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;sum&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Then, in the 1990s, Matz, inspired by Smalltalk and Perl, created Ruby. We
still have “normal” loops but we also have a very Smalltalk-y way of doing it,
too:&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;arr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;sum&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;arr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;each&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;sum&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;When this program gets compiled to Ruby bytecode, it ends up looking like a
mostly normal method call to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;each&lt;/code&gt; except that we pass a special kind of
argument to it: a block argument.&lt;/p&gt;

&lt;p&gt;To see how this works inside CRuby, we’re going to look at a listing of YARV
bytecode—CRuby bytecode. For more on YARV, I recommend Kevin Newton’s
excellent &lt;a href=&quot;https://kddnewton.com/2022/11/30/advent-of-yarv-part-0.html&quot;&gt;Advent of
YARV&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Ignore most of the bytecode dump below except for the instruction at &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0009&lt;/code&gt;,
the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;send&lt;/code&gt; instruction. We are &lt;em&gt;send&lt;/em&gt;-ing (see? a message!) &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;each&lt;/code&gt; with the
block argument &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;block in &amp;lt;main&amp;gt;&lt;/code&gt; (passed a different way than “normal”
arguments, hence the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;argc:0&lt;/code&gt;).&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ ruby --dump=insns /tmp/arrayeach.rb
== disasm: #&amp;lt;ISeq:&amp;lt;main&amp;gt;@/tmp/arrayeach.rb:1 (1,0)-(5,3)&amp;gt;
local table (...)
[ 2] arr@0      [ 1] sum@1
0000 duparray                   [1, 2, 3]                 (   1)[Li]
0002 setlocal_WC_0              arr@0
0004 putobject_INT2FIX_0_                                 (   2)[Li]
0005 setlocal_WC_0              sum@1
0007 getlocal_WC_0              arr@0                     (   3)[Li]
0009 send                       &amp;lt;calldata!mid:each, argc:0&amp;gt;, block in &amp;lt;main&amp;gt;
0012 leave
...continued below...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;block in &amp;lt;main&amp;gt;&lt;/code&gt; from the bytecode listing above is the generated name of
the &lt;em&gt;instruction sequence&lt;/em&gt; (bytecode) corresponding to the block we passed to
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;each&lt;/code&gt;. Its code, shown below, is the next thing in the bytecode dump. You can
see the usage of local variables &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sum&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a&lt;/code&gt; via &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;getlocal&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;setlocal&lt;/code&gt;
variants and also &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;opt_plus&lt;/code&gt;, which represents addition (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;+&lt;/code&gt;). Again, the
details are not terribly important:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;...continued from above...
== disasm: #&amp;lt;ISeq:block in &amp;lt;main&amp;gt;@/tmp/arrayeach.rb:3 (3,9)-(5,3)&amp;gt;
local table (...)
[ 1] a@0&amp;lt;AmbiguousArg&amp;gt;
0000 getlocal_WC_1              sum@1                     (   4)[LiBc]
0002 getlocal_WC_0              a@0
0004 opt_plus                   &amp;lt;calldata!mid:+, argc:1, ARGS_SIMPLE&amp;gt;[CcCr]
0006 dup
0007 setlocal_WC_1              sum@1
0009 leave                                                (   5)[Br]
$
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;To make this work, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Array&lt;/code&gt; has an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;each&lt;/code&gt; method. This &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;each&lt;/code&gt; method takes in
its optional block argument and calls it once for every element in the array.&lt;/p&gt;

&lt;p&gt;As with most of the core data structures, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Array&lt;/code&gt;’s methods tend to be written
in C and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;each&lt;/code&gt; is no different. Here is its nice and short definition with
comments added by me:&lt;/p&gt;

&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;VALUE&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;rb_ary_each&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;VALUE&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// Some debug-mode-only checks&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;ary_verify&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// If we didn&apos;t pass a block in, return an enumerator object&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;RETURN_SIZED_ENUMERATOR&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ary_enum_length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// We passed a block in, so call it for every element&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RARRAY_LEN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;rb_yield&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RARRAY_AREF&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;You can’t really see the block parameter to the C code because it’s passed in a
special location on the Ruby VM’s own stack called the &lt;em&gt;block handler&lt;/em&gt;. All you
need to know is that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rb_yield&lt;/code&gt; knows where to find that and how to call it.&lt;/p&gt;

&lt;p&gt;There’s just one more thing, which is… hang on, weren’t we building a JIT to
optimize Ruby code? How are we going to optimize this C code?&lt;/p&gt;

&lt;h2 id=&quot;jits-a-refresher&quot;&gt;JITs: a refresher&lt;/h2&gt;

&lt;p&gt;Rewriting code from C to Ruby means that the JIT compiler gets a chance to
introspect the run-time behavior and code. This means that, over time, as the
compiler and the runtime system grow together, more and more code might get
rewritten in Ruby. This started happening a couple of years ago with YJIT.&lt;/p&gt;

&lt;p&gt;YJIT precipitated some interesting changes to Ruby VM internals. For example,
in 2022, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Array#each&lt;/code&gt; being written in C started to hurt: it was an opaque blob
that the JIT couldn’t reason about. So Kokubun submitted &lt;a href=&quot;https://github.com/ruby/ruby/pull/6687&quot;&gt;a
PR&lt;/a&gt; to rewrite it in Ruby. After some
back and forth, in 2024, Kokubun landed &lt;a href=&quot;https://github.com/ruby/ruby/pull/11955&quot;&gt;a different
PR&lt;/a&gt; that everyone was happy with.&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Array&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;each&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# :nodoc:&lt;/span&gt;
    &lt;span class=&quot;no&quot;&gt;Primitive&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;attr!&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:inline_block&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:c_trace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:without_interrupts&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;unless&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;defined?&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;yield&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Primitive&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;cexpr!&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;SIZED_ENUMERATOR(self, 0, 0, ary_enum_length)&apos;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;until&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Primitive&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;rb_jit_ary_at_end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;yield&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Primitive&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;rb_jit_ary_at&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Primitive&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;rb_jit_fixnum_inc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;self&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Ah, finally. A version that JITs can reason about.&lt;/p&gt;

&lt;p&gt;I keep saying “reason about” and what that means concretely here is a) that
it’s written in a format that the JIT can ingest and optimize: Ruby, and b) mostly a
brief rehashing of the key lessons in the venerable Smalltalk (!) paper
&lt;a href=&quot;https://dl.acm.org/doi/pdf/10.1145/800017.800542&quot;&gt;Efficient Implementation of the Smalltalk-80
System&lt;/a&gt; (PDF):&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Though systems may offer very dynamic behavior, people don’t frequently make
use of wild features all over the place
    &lt;ul&gt;
      &lt;li&gt;Most people pass fewer than 4 types of objects through a given method&lt;/li&gt;
      &lt;li&gt;As a corollary, even if there are many classes in a system, code locality
is super important, and we can take advantage of that&lt;/li&gt;
      &lt;li&gt;Also, most people do not define, re-define, and otherwise continuously
modify method definitions&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;(And if you don’t believe me that the lessons still apply, check out the
excellent paper &lt;a href=&quot;https://dl.acm.org/doi/pdf/10.1145/3563834.3567538&quot;&gt;Who You Gonna
Call&lt;/a&gt; (PDF) by Sophie
Kaleba, Octave Larose, Richard Jones, and Stefan Marr.)&lt;/p&gt;

&lt;p&gt;So JITs like to watch what types of objects flow through methods before
compiling them. And JITs like to, since they know the types of objects, cache
method lookups and specialize method invocations on those objects.&lt;/p&gt;

&lt;p&gt;For example, take a look at this code:&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;next_capacity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;length&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x&lt;/code&gt; could be anything. There is no way of knowing its type by looking at the
code. This means that the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;length&lt;/code&gt; method could be anything. Furthermore, there
is no way of knowing what the return type of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x.length&lt;/code&gt; is, so we can’t
specialize the method lookups or invocations of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;*&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;+&lt;/code&gt; either.&lt;/p&gt;

&lt;p&gt;But! Per our lessons above, likely only a few types flow through this code. Say
the JIT’s profiler notices that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x&lt;/code&gt; has historically been an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Array&lt;/code&gt;. Then we
might reasonably assume that it will continue to be an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Array&lt;/code&gt;, so when we
compile the method, we add a run-time type check: if the type is no longer an
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Array&lt;/code&gt;, jump back into the interpreter.&lt;/p&gt;

&lt;p&gt;Let’s see what optimized code ZJIT can construct by combining profiling
information with the above bytecode. ZJIT operates on its own &lt;em&gt;high-level
intermediate representation&lt;/em&gt; called, uncreatively, HIR. In the following HIR
snippet, we can see this very run-time type check (“guard”) for the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Array&lt;/code&gt;
class:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;fn next_capacity@file.rb:8:
...
bb3(v9:BasicObject, v10:BasicObject):  # v9 is self and v10 is x
  PatchPoint NoSingletonClass(Array@0xdecaf1)
  PatchPoint MethodRedefined(Array@0xdecaf2, length@0xdecaf3, cme:0xdecaf4)
  v35:ArrayExact = GuardType v10, ArrayExact recompile
  v36:CInt64 = ArrayLength v35
  v37:Fixnum = BoxFixnum v36
  # ...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;(with real pointers replaced by fake ones for readability)&lt;/p&gt;

&lt;p&gt;Because classes are, among other things, collections of methods, this type
information tells us what the call target of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x.length&lt;/code&gt; is: it’s
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Array#length&lt;/code&gt;! We have a special fast code snippet to read an array’s length
so we do that instead of a method call.&lt;/p&gt;

&lt;p&gt;And in case the methods ever get changed out from underneath us, we leave
behind these markers called &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PatchPoint&lt;/code&gt; that invalidate the code.&lt;/p&gt;

&lt;p&gt;Finally, because we know that the result of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Array#length&lt;/code&gt; is always a small
integer (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Fixnum&lt;/code&gt;), we can special case the method lookups for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;*&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;+&lt;/code&gt; as
well.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;fn next_capacity@file.rb:8:
...
bb3(v9:BasicObject, v10:BasicObject):  # v9 is self and v10 is x
  PatchPoint NoSingletonClass(Array@0xdecaf)
  PatchPoint MethodRedefined(Array@0xdecaf, length@0xdecaf, cme:0xdecaf)
  v35:ArrayExact = GuardType v10, ArrayExact recompile
  v36:CInt64 = ArrayLength v35
  v37:Fixnum = BoxFixnum v36
  v18:Fixnum[2] = Const Value(2)
  PatchPoint MethodRedefined(Integer@0xdecaf, *@0xdecaf, cme:0xdecaf)
  v41:Fixnum = FixnumMult v37, v18
  v23:Fixnum[1] = Const Value(1)
  PatchPoint MethodRedefined(Integer@0xdecaf, +@0xdecaf, cme:0xdecaf)
  v45:Fixnum = FixnumAdd v41, v23
  CheckInterrupts
  Return v45
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;There you have it. This is how JITs work: observe, assume, specialize. So why
am I telling you all this? How does this circle back to blocks?&lt;/p&gt;

&lt;p&gt;Well, blocks work not quite the same way, but similarly. Instead of having an
object that we call a method on, we just have the target &lt;em&gt;instruction
sequence&lt;/em&gt;&lt;sup id=&quot;fnref:procs-and-such&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:procs-and-such&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;1&lt;/a&gt;&lt;/sup&gt;. But if we observe that the block argument, in its
special location, has been consistently one object, we can specialize the call
to it.&lt;/p&gt;

&lt;p&gt;This is more or less fine for some cases of code. For example, in the following
code snippet, we only have one caller to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;custom_iterator&lt;/code&gt;, so its profiled
block will be &lt;em&gt;monomorphic&lt;/em&gt; (one observed shape).&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;custom_iterator&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;yield&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;yield&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;yield&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;custom_iterator&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;
  &lt;span class=&quot;nb&quot;&gt;puts&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This reinforces what we know and love about the Smalltalk-80 paper: code
locality wins! Yes!&lt;/p&gt;

&lt;p&gt;But unfortunately this falls apart when we start thinking about all of the
core library methods (and potentially the methods and classes you have
stashed away in the grab-bag &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;utils.rb&lt;/code&gt; in your application). Those methods are
probably &lt;em&gt;megamorphic&lt;/em&gt; (many many observed shapes). They probably see all sorts
of stuff because they are general-purpose utilities that everybody needs, all
the time.&lt;/p&gt;

&lt;p&gt;One such example in the Ruby core library is the venerable &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Array#each&lt;/code&gt;
that we saw earlier. Because there are a million different call sites to
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Array#each&lt;/code&gt; across your application and each probably passes a totally
different block, we’re in an unhappy situation. How can we possibly optimize
for so many different blocks? What happened to our code locality? How do we fix
this?&lt;/p&gt;

&lt;h2 id=&quot;the-code-locality-is-in-another-tower&quot;&gt;The code locality is in another tower&lt;/h2&gt;

&lt;p&gt;It’s okay. Code locality still rules. Look at all the various callers of
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Array#each&lt;/code&gt;. They all pass a different, but constant&lt;sup id=&quot;fnref:not-always-constant&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:not-always-constant&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;2&lt;/a&gt;&lt;/sup&gt;,
block at the call-site:&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;method_a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;some_array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;some_array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;each&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;puts&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;method_b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;some_array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;some_array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;each&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;puts&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;method_c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;some_array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;some_array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;each&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;puts&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;So the code locality that we need in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Array#each&lt;/code&gt; to optimize the code is one
level up at the caller. If we can use that &lt;em&gt;call context&lt;/em&gt;, we can specialize
the code.&lt;/p&gt;

&lt;p&gt;YJIT accomplishes this by &lt;em&gt;splitting&lt;/em&gt;, which is a very natural transformation
for basic block versioning and tracing. Such compilers are good at following
code paths as they would be executed and putting together context across method
calls. However, YJIT’s heuristic for splitting blocks is based on
manual annotations: it will only kick in for certain Ruby library functions
specially annotated with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Primitive.attr! :inline_block&lt;/code&gt; (and the name is a bit
of a misnomer).&lt;/p&gt;

&lt;p&gt;The team that builds ZJIT, a method JIT, decided not to add splitting
facilities. We could split methods (and blocks), but we have an easier time
reasoning about larger code units than YJIT does because we optimize an entire
method at once. So instead, ZJIT chooses to get call context by &lt;em&gt;inlining&lt;/em&gt;.&lt;/p&gt;

&lt;h2 id=&quot;inlining&quot;&gt;Inlining&lt;/h2&gt;

&lt;p&gt;Method inlining refers to copying the body of the callee into the caller. In
the above example, it means copying the body of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Array#each&lt;/code&gt; into each of
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;method_a&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;method_b&lt;/code&gt;, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;method_c&lt;/code&gt;. I don’t mean the Ruby code and I don’t
mean the bytecode: I mean the HIR. ZJIT does this by building the HIR of the
callee (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Array#each&lt;/code&gt;) into the existing HIR of the caller (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;method_a&lt;/code&gt;, …).&lt;/p&gt;

&lt;p&gt;The illustrious &lt;a href=&quot;https://railsatscale.com/authors/kevin-menard/&quot;&gt;Kevin Menard&lt;/a&gt; wrote ZJIT’s inliner and
he’ll write a post about all the details soon. It’s pretty interesting stuff.&lt;/p&gt;

&lt;p&gt;For now, we can take a look at the (lightly edited) result of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Array#each&lt;/code&gt;
being inlined into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;method_a&lt;/code&gt;. The details don’t matter, but there are two
things I want to call out:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;What used to be a dynamic &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;invokeblock&lt;/code&gt; is now what we call
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;InvokeBlockIseqDirect&lt;/code&gt; because we know from the call context what block
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;method_a&lt;/code&gt; is passing to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Array#each&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;What used to be a method call is now a loop: the condition check is in
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bb10&lt;/code&gt;, the body is in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bb9&lt;/code&gt;, and the stuff after the loop is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bb13&lt;/code&gt;/&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bb4&lt;/code&gt;.
If you’re interested, try to find the iteration variable and where it gets
incremented. See the footnote&lt;sup id=&quot;fnref:iteration-variable&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:iteration-variable&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;3&lt;/a&gt;&lt;/sup&gt; for the answer.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is a massive improvement over the previous very generic operations.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;fn method_a@/tmp/arrayeach.rb:2:
# ...
bb3(v9:BasicObject, v10:BasicObject):
  # ...
  v55:Fixnum[0] = Const Value(0)
  Jump bb10(v25, v55)
bb10(v68:ArrayExact, v69:Fixnum):
  v73:CInt64 = ArrayLength v68
  v74:Fixnum = BoxFixnum v73
  v75:BoolExact = FixnumGe v69, v74
  v77:CBool = Test v75
  CondBranch v77, bb13(), bb9(v68, v69)
bb13():
  CheckInterrupts
  Jump bb4(v68)
bb9(v90:ArrayExact, v91:Fixnum):
  v96:CInt64 = UnboxFixnum v91
  v97:BasicObject = ArrayAref v90, v96
  v99:CPtr = GetEP 0
  v100:CInt64 = LoadField v99, :VM_ENV_DATA_INDEX_SPECVAL@-0x8
  v101:CInt64[-4] = Const CInt64(-4)
  v102:CInt64 = IntAnd v100, v101
  v103:BasicObject = InvokeBlockIseqDirect (0x16f3d5968), v102, v97
  v107:Fixnum[1] = Const Value(1)
  v108:Fixnum = FixnumAdd v91, v107
  PatchPoint NoEPEscape(each)
  Jump bb10(v90, v108)
bb4(v114:BasicObject):
  PopInlineFrame
  PatchPoint NoEPEscape(method_a)
  CheckInterrupts
  Return v114
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;You may notice that there is still one call in the loop: the call to the block
(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;InvokeBlockIseqDirect&lt;/code&gt;). That’s next on our list to tackle. We are optimistic
that we will soon also be able to inline block calls. Then the whole thing will
&lt;em&gt;really&lt;/em&gt; be just a loop!&lt;/p&gt;

&lt;p&gt;The code that enables us to reason about which block got passed into the
inlined callee (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Array#each&lt;/code&gt;) only landed &lt;a href=&quot;https://github.com/ruby/ruby/commit/c35fbd6fb27a13842299597d46b49a10f018c5b0&quot;&gt;a couple of days
ago&lt;/a&gt;
(July 10, 2026), written by &lt;a href=&quot;https://github.com/luke-gruber&quot;&gt;Luke Gruber&lt;/a&gt;. This
was one of Luke’s first changes to ZJIT.&lt;/p&gt;

&lt;h2 id=&quot;so-what-does-performance-look-like&quot;&gt;So what does performance look like?&lt;/h2&gt;

&lt;p&gt;Well, for starters, the microbenchmarks that we use as “performance unit tests”
for specific aspects of Ruby went wild. Some benchmarks that were using
block-based looping got &lt;em&gt;much&lt;/em&gt; faster; they were previously bounded by ZJIT’s
block invocation performance.&lt;/p&gt;

&lt;p&gt;Take a look at the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cfunc_itself&lt;/code&gt; benchmark, which tests that we can fold away
the call to Ruby’s built-in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;itself&lt;/code&gt; method. ZJIT ends up optimizing the
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;times&lt;/code&gt; method to invoke the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;do&lt;/code&gt; block directly, and the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;do&lt;/code&gt; block gets
optimized to nothing but a guard on the self’s class to make sure it hasn’t
changed.&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;run_benchmark&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;500&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
  &lt;span class=&quot;mi&quot;&gt;500000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;times&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;itself&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;itself&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;itself&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;itself&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;itself&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;itself&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;itself&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;itself&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;itself&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;itself&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Because we had previously optimized the body away, the result of the direct
block invocation is a massive speedup:&lt;/p&gt;

&lt;figure style=&quot;margin: 0 auto; max-width: 80%;&quot;&gt;
  &lt;img src=&quot;/assets/img/inliner_invokeblock_cfunc_itself_benchmark.png&quot; /&gt;
  &lt;figcaption&gt;The yellow line represents ZJIT speedup over the interpreter. Higher is better. 35x!&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;Other benchmarks also kind of stop making sense because of the amount of
inlining. Our bmethod benchmark, which benchmarked how fast we can call methods
defined with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;define_method&lt;/code&gt;, also stopped measuring anything of use. We’re
going to have to rework the benchmark to be more fair…&lt;/p&gt;

&lt;figure style=&quot;margin: 0 auto; max-width: 80%;&quot;&gt;
  &lt;img src=&quot;/assets/img/inliner_invokeblock_send_bmethod_benchmark.png&quot; /&gt;
  &lt;figcaption&gt;The yellow line represents ZJIT speedup over the interpreter. Higher is better. 139x!&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;As expected, larger Rails benchmarks don’t see a ton of change; they exercise a
diffuse set of features so optimizing any one feature bumps the big benchmarks
only a little bit.&lt;/p&gt;

&lt;p&gt;I am excited to see what happens when we can fully turn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;each&lt;/code&gt; and friends into
call-less loops!&lt;/p&gt;

&lt;h2 id=&quot;wrapping-up&quot;&gt;Wrapping up&lt;/h2&gt;

&lt;p&gt;Code locality rules. The inliner helps inject more of it and reason across
method calls. ZJIT, a little over one year old, is growing up! :’)&lt;/p&gt;

&lt;p&gt;We’re still tuning the inliner knobs. Some of the code in this post required
tweaking &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--zjit-inline-threshold&lt;/code&gt; to convince the compiler to inline
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Array#each&lt;/code&gt; into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;method_a&lt;/code&gt; because of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Array#each&lt;/code&gt;’s size. It will take some
time for the ZJIT developers to figure out reasonable defaults.&lt;/p&gt;

&lt;p&gt;Try out our HIR explorer at &lt;a href=&quot;https://tryzjit.fly.dev/&quot;&gt;tryzjit.fly.dev&lt;/a&gt;. Try
out ZJIT in your application by adding the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--zjit&lt;/code&gt; flag to a Ruby over 4.0.
Thanks for reading and see you next time.&lt;/p&gt;
&lt;div class=&quot;footnotes&quot; role=&quot;doc-endnotes&quot;&gt;
  &lt;ol&gt;
    &lt;li id=&quot;fn:procs-and-such&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;Mostly. I am glossing over &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nil&lt;/code&gt;, procs, ifuncs, etc. But
the common path is by and away iseq blocks. &lt;a href=&quot;#fnref:procs-and-such&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:not-always-constant&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;It’s not always the case that these methods are called
with a constant block iseq. Sometimes they are called with the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;amp;:symbol&lt;/code&gt;
form, for example. Or with the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;amp;some_block_variable&lt;/code&gt; form. In that case,
we can use an inline cache to (with a guard) make it constant once more.
But we have not implemented that yet, because it is rarer. &lt;a href=&quot;#fnref:not-always-constant&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:iteration-variable&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;The iteration variable is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v91&lt;/code&gt;! You can see it get used
as an array index when it gets unboxed as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v96&lt;/code&gt; and passed to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ArrayAref&lt;/code&gt;.
You can also see it get incremented with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FixnumAdd&lt;/code&gt; and the constant &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1&lt;/code&gt;.
In &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bb10&lt;/code&gt; it has a different name, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v69&lt;/code&gt;, which gets checked against the
array length. &lt;a href=&quot;#fnref:iteration-variable&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
  &lt;/ol&gt;
&lt;/div&gt;
</description>
            <pubDate>Tue, 28 Jul 2026 00:00:00 +0000</pubDate>
            <niceDate>July 28, 2026</niceDate>
            <link>https://bernsteinbear.com/blog/zjit-inliner-invokeblock/?utm_source=rss</link>
            <guid isPermaLink="true">https://bernsteinbear.com/blog/zjit-inliner-invokeblock/</guid>
        </item>
        
        <item>
            <title>Travel notes: PLDI Boulder</title>
            <description>&lt;p&gt;I had another excellent &lt;a href=&quot;https://www.sigplan.org/Conferences/PLDI/&quot;&gt;PLDI&lt;/a&gt; this
past June. It was my fourth&lt;sup id=&quot;fnref:fifth&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:fifth&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;1&lt;/a&gt;&lt;/sup&gt;. I continued to meet new people and learn
new things!&lt;/p&gt;

&lt;p&gt;Overall: I got to meet a lot of new people, which was exciting. I had some good
chats about research. I asked a question at a talk! I got to show Aaron and
Jacob PLDI and see them enjoy it. I missed hanging out with CF Bolz-Tereick and
Chris Fallin, the usual suspects at conferences I attend. I’m looking forward
to next year.&lt;/p&gt;

&lt;p&gt;This post is more about the conference than the town of Boulder (unlike the
last PLDI post about Seoul) because I didn’t do much Boulder exploring.&lt;/p&gt;

&lt;h2 id=&quot;sunday&quot;&gt;Sunday&lt;/h2&gt;

&lt;p&gt;I got in late on Sunday. Then I had to take a long car ride from Denver airport
to Boulder. I don’t think I had ever flown into Denver with intent to go to
Boulder before so it was a bit of a surprise.&lt;/p&gt;

&lt;p&gt;Jacob offered to have a late dinner with me so we had a tasty meal at Gaia
Masala and Burger. Shout out to Harry, our server.&lt;/p&gt;

&lt;h2 id=&quot;monday&quot;&gt;Monday&lt;/h2&gt;

&lt;p&gt;Monday was a workshop day. I signed up for EGRAPHS and mostly stayed in that
workshop. People kept throwing around the term “Knuth-Bendix”, as they have
been for several years, and only in one of these workshop talks did someone
explain it in a way that made any sense at all. It seems kind of like equality
saturation but for the rewrite rules themselves—no actual expression graphs
involved. I DMed Phil this sketchy explanation during a talk to get his
response and I got to watch him cock his head and think about it in real time.&lt;/p&gt;

&lt;p&gt;At lunch I met Qiantan Hong and we got to talking about Common Lisp and its
object system, CLOS. Seems like a combination of ahead-of-time compilation and
multiple dispatch is really tricky.&lt;/p&gt;

&lt;p&gt;I had dinner with Aaron at Postino and then wandered into a bunch of people
staying at the conference hotel chatting in the lobby. Ben Titzer said “fix my
subtyping bug”, which I interpreted as him saying hi. I ended up just planting
myself at the table as a bunch of interesting people cycled through: Jared
Roesch, Mae Milano, Hila Peleg, Russel Arbore. It was a late evening.&lt;/p&gt;

&lt;h2 id=&quot;tuesday&quot;&gt;Tuesday&lt;/h2&gt;

&lt;p&gt;Back to the workshops! But late because of aforementioned late evening. I saw
Vadym’s talk about Remora. I only understood about 40% of it but it was good to
catch up. I hadn’t seen him since leaving Northeastern.&lt;/p&gt;

&lt;p&gt;Around a break time I joined a little cluster of people talking about e-graphs
and I guessed asked enough basic questions that Pavel convinced Max Willsey to
run a “BYOEG” (build your own e-graph) tutorial. The structure was as follows:
Max would instruct Jacob as to what &lt;em&gt;kind&lt;/em&gt; of thing to build next but not be
prescriptive about exactly how to build it and not look at Jacob’s screen. The
rest of us would sit around a table and try to follow along as best we could. I
hear Pavel has a blog post about this experience coming soon…&lt;/p&gt;

&lt;p&gt;I saw Slava Pestov walking around and introduced myself because we keep liking
one others’ bad jokes on Mastodon. We ended up getting dinner with Aaron and
Jacob that night at Leaf. We learned a lot about monoids, Knuth-Bendix (!),
Factor, and Swift. Slava volunteered to do a similar follow-along “BYOKBC”
(build your own Knuth-Bendix completion) tutorial the next day.&lt;/p&gt;

&lt;h2 id=&quot;wednesday&quot;&gt;Wednesday&lt;/h2&gt;

&lt;p&gt;First day of the conference! I was walking into the hotel in the morning and I
had made it about three feet onto the property when Alexa VanHattum, who was
going the opposite direction, convinced me to instead get coffee elsewhere. We
had a nice catch up and I got to hear about what teaching is like these days.&lt;/p&gt;

&lt;p&gt;Lunch was fun. I got to do another round of “ambush person whose research I
admire” and plopped down with Ben and Christian Wimmer. I’d spent a lot of time
struggling with Christian’s papers on linear scan, then convinced him to chat
about register allocation with us on a video call a couple of months ago. We
continued some of that at lunch but then I (kind of accidentally kind of on
purpose) got Ben started talking about Sea of Nodes and how it is and is not
different between Java and (for example stand-in for dynamic languages)
JavaScript. Apparently he is thinking about a similar thing that he is calling
Sea of Variables.&lt;/p&gt;

&lt;p&gt;We talked about inlining challenges and how to infuse profiles with call
context, which can be a challenge. I feel more inspired to get type-based alias
analysis working in ZJIT.&lt;/p&gt;

&lt;p&gt;I tracked down Christian later in the courtyard and got to hear about what he’s
working on these days. I know very little about ML compilers and ML hardware
and things like that so hearing about the challenges was neat. Yannis
Smaragdakis joined our little standing table chat and we got to learn about
Datalog. Because I had previously written about linear scan register allocation
and about liveness analysis with Datalog, I goaded him into pairing with me on
writing a full linear scan implementation in Datalog.&lt;/p&gt;

&lt;p&gt;This ended up taking the rest of the evening and several beers and then a lot
of the next day! And after the first bit of code I did not manage to contribute
very much at all.&lt;/p&gt;

&lt;h2 id=&quot;thursday&quot;&gt;Thursday&lt;/h2&gt;

&lt;p&gt;I met Hannah Gommerstadt and we got to chatting about bikes and formal methods
(separately). Slava walked by and I got to introduce them. Then Jacob too.&lt;/p&gt;

&lt;p&gt;I continued pairing with Yannis but remained really lost. The only thing I
think I contributed was some familiarity with the core algorithm, which he had
only really seen in passing before. Eventually he got it fully working, but it
needed some deep trickery. More on this soon in its own post.&lt;/p&gt;

&lt;p&gt;I saw a talk about versioned e-graphs and that got me wondering if their
implementation can be used as a persistent e-graph or even just persistent
union-find. Sometimes you want to do backtracking, or have undo-redo in your
compiler.&lt;/p&gt;

&lt;p&gt;Then I went to a talk about streaming byte-pair encoding (BPE). BPE is hard to
do streaming because it definitionally requires looking over the whole input
string. They did some neat trickery to find boundaries in the string that
demarcate regions that don’t interfere with one another and thereby tokenize
on-the-fly. I didn’t understand it fully but I asked my only question of the
conference, which was if this could also be used to implement BPE in parallel.
Seems the answer is “maybe” so I should probably reach out and ask further.&lt;/p&gt;

&lt;p&gt;Slava started showing me and Jacob and Aaron how to implement Knuth-Bendix
completion for strings. I had a lot of tiny little bugs which slowed progress.
Such is life. The banquet and awards ceremony started so we called it a night
and went off to eat dinner. They had good lentils.&lt;/p&gt;

&lt;h2 id=&quot;friday&quot;&gt;Friday&lt;/h2&gt;

&lt;p&gt;I ran into Thalia Archibald and John Regehr and we talked about (really, they talked
about and I tried to learn something) what it might mean to either port Alive2
to another compiler than LLVM, or build “Alive3”, or “Mini-Alive” for some
other IR. John suggested fuzzing the hell out of the thing first, then doing
something more formal later… especially if it’s a dynamic language IR where a
lot of the opcodes end up being “function call that can do anything”.&lt;/p&gt;

&lt;p&gt;I had a nice chat with Steven Holtzen and Zach Tatlock about research and grad
student life. I got some good advice. I meant to talk to Zach about this thing
we keep occasionally chatting about that I call “the big e-graph in the sky”. I
talked a bit about it to Max Willsey and he had some good probing questions
about what would be slow, challenging, or somehow undefined given my problem
statement.&lt;/p&gt;

&lt;p&gt;I continued struggling to implement Knuth-Bendix with significant assistance
from Slava and I think eventually got &lt;em&gt;something&lt;/em&gt; working. I had a really nasty
bug due to string slicing semantics&lt;sup id=&quot;fnref:slicing&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:slicing&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;2&lt;/a&gt;&lt;/sup&gt; in Ruby.&lt;/p&gt;

&lt;p&gt;Aaron went off to learn about deep immutability in Python and then got to
chatting with the authors of the paper. We got to compare notes about language
and language implementation challenges. It’s been a long time since I was in
Python-land.&lt;/p&gt;

&lt;p&gt;Aaron and Slava and I got enchiladas for dinner.&lt;/p&gt;

&lt;h2 id=&quot;saturday&quot;&gt;Saturday&lt;/h2&gt;

&lt;p&gt;I had no reading material for the flight so I went downtown, intending to buy
one book, but got too many books. They barely fit into my bag for the flight
home. I started reading Anathem for the second time. It holds up. It’s a damn
good book.&lt;/p&gt;
&lt;div class=&quot;footnotes&quot; role=&quot;doc-endnotes&quot;&gt;
  &lt;ol&gt;
    &lt;li id=&quot;fn:fifth&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;Actually, it might be my fifth. I just remembered that I attended
PLMW in 2020 and also watched a few online talks at wild hours from my
living room. &lt;a href=&quot;#fnref:fifth&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:slicing&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;The semantics in Ruby are probably globally reasonable but did not
fit the thing I was trying to do: if we have two strings &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;b&lt;/code&gt;, we
want to find the index at which they start to overlap, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;overlap&lt;/code&gt;. Then we
want to grab the bit of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a&lt;/code&gt; that is to the left of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;overlap&lt;/code&gt;. I had
initially written that as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a[..overlap-1]&lt;/code&gt;.&lt;/p&gt;

      &lt;p&gt;However, if &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;b&lt;/code&gt; overlap at the start of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;overlap&lt;/code&gt; is 0. This
generates the range &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;..-1&lt;/code&gt;, which means we’ll slice until the end of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a&lt;/code&gt;.&lt;/p&gt;

      &lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;irb(main):001&amp;gt; &quot;abc&quot;[..-1]
=&amp;gt; &quot;abc&quot;
irb(main):002&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;      &lt;/div&gt;

      &lt;p&gt;Not what we want. Instead, the fix in &lt;a href=&quot;https://github.com/tekknolagi/knuth-bendix/commit/e46b0069767de44622dc116b0b7b51a4392c400d&quot;&gt;the
commit&lt;/a&gt;
shows how I had to add a special slice function called &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;string_upto&lt;/code&gt; that
handles the 0 case. &lt;a href=&quot;#fnref:slicing&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
  &lt;/ol&gt;
&lt;/div&gt;
</description>
            <pubDate>Sun, 05 Jul 2026 00:00:00 +0000</pubDate>
            <niceDate>July 5, 2026</niceDate>
            <link>https://bernsteinbear.com/blog/travel-notes-pldi-boulder/?utm_source=rss</link>
            <guid isPermaLink="true">https://bernsteinbear.com/blog/travel-notes-pldi-boulder/</guid>
        </item>
        
        <item>
            <title>A survey of inlining heuristics</title>
            <description>&lt;p&gt;Compilers, especially method just-in-time compilers, operate on one function at
a time. It is a natural code unit size, especially for a dynamic language JIT:
at a given point in time, what more information can you gather about other
parts of a running, changing system?&lt;/p&gt;

&lt;p&gt;I don’t have any data to back this up—maybe I should go gather some—but on
average, methods are small. Especially in languages such as Ruby that use
method dispatch for everything, even instance variable (attribute, field, …)
lookups, they are &lt;em&gt;small&lt;/em&gt;. And everywhere.&lt;/p&gt;

&lt;p&gt;This makes the compiler sad. If we are to continue to anthropomorphize them,
compilers like having more context so they can optimize better. Consider the
following silly-looking example that is actually representative of a surprising
amount of real-world code:&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Point&lt;/span&gt;
  &lt;span class=&quot;nb&quot;&gt;attr_reader&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:y&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;initialize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;vi&quot;&gt;@x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;
    &lt;span class=&quot;vi&quot;&gt;@y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;distance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;other&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;no&quot;&gt;Math&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;vi&quot;&gt;@x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;other&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;vi&quot;&gt;@y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;other&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;distance_from_origin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;no&quot;&gt;Point&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;distance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Point&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Right now, in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;distance_from_origin&lt;/code&gt; method, I count 8 different method calls:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Point.new&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Point#initialize&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Point.new&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Point#initialize&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Point#distance&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Float#**&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Float#**&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Math.sqrt&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;(Technically more, but the ivar lookups (including &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;attr_reader&lt;/code&gt;!), addition,
and subtraction are generally specialized and don’t push a frame, even in the
interpreter.)&lt;/p&gt;

&lt;p&gt;Furthermore, there are at least two heap allocations: one for each &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Point&lt;/code&gt;
instance.&lt;/p&gt;

&lt;p&gt;Last, there is a bunch of memory traffic to and from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Point&lt;/code&gt; instances.&lt;/p&gt;

&lt;p&gt;This all is a huge bummer! What should be a simple math operation is now
overwhelmed with a bunch of other stuff. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Point&lt;/code&gt; is certainly not a zero-cost
abstraction.&lt;/p&gt;

&lt;p&gt;Even if we had a bunch of other optimizations such as load-store elimination or
escape analysis, they would not be able to do much: pretty much everything
escapes and is effectful. That is, unless we &lt;em&gt;inline&lt;/em&gt;. Inlining is the lever
that enables a bunch of other optimization passes to kick in.&lt;/p&gt;

&lt;h2 id=&quot;inlining-the-easy-part&quot;&gt;Inlining: the “easy” part&lt;/h2&gt;

&lt;p&gt;I wrote about the design and implementation of Cinder’s inliner (&lt;a href=&quot;https://engineering.fb.com/2022/05/02/open-source/cinder-jits-instagram/&quot;&gt;FB
link&lt;/a&gt;,
&lt;a href=&quot;/blog/cinder-jit-inliner/&quot;&gt;personal blog link&lt;/a&gt;) a couple of years ago. I wrote
about arguably the simplest part, which is copying the callee body into the
caller. It took me at least a week to get working. Probably closer to months if
you consider all the plumbing through the rest of the JIT. In February during a
small hackathon, I watched my colleague &lt;a href=&quot;https://github.com/k0kubun&quot;&gt;k0kubun&lt;/a&gt;
prototype that bit of the inliner inside ZJIT in about 30 minutes.&lt;/p&gt;

&lt;p&gt;There is more to do when pretty much every part of the VM is observable from
the guest language: both Python and Ruby allow inspecting the state of the
locals, the call stack, etc from user code. Sampling profilers also expect some
amount of breadcrumbs to work with to inspect the stack. So there’s some more
machinery still required to pretend like the callee function was not inlined. I
talk about this a little bit in the Cinder blog post.&lt;/p&gt;

&lt;p&gt;Even so, all of that can probably be designed and wired together in a couple
of months. Then you will find yourself tuning the inliner for the next 10
years. This is much harder.&lt;/p&gt;

&lt;h2 id=&quot;when-the-harder-part&quot;&gt;When: the harder part&lt;/h2&gt;

&lt;p&gt;The thing that makes inlining difficult, especially in a method JIT, is that
you are trying to make an entire (dynamic!) system faster but you are only
looking through a microscope and only capable of local reasoning&lt;sup id=&quot;fnref:aot-split&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:aot-split&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;1&lt;/a&gt;&lt;/sup&gt;.
Whereas other optimizations such as strength reduction, inline caches, and
value numbering are an un-alloyed good for the generated code, inlining can
have &lt;em&gt;negative effects&lt;/em&gt;. It is also perhaps the first optimization people add
that has non-local impact.&lt;/p&gt;

&lt;p&gt;If you inline wrong, your code size might blow up. This might thrash your CPU’s
caches. Bummer, but happens to the best of us.&lt;/p&gt;

&lt;p&gt;But also, if you inline wrong, you might get in the way of other helpful
optimizations: if you hit some size limit after inlining method A, you might
never get to inline B, which is the key to unlocking the performance of the
method you are trying to optimize.&lt;/p&gt;

&lt;p&gt;Last, inlining might hurt compile time. In situations where latency is
paramount (think: interactive client JavaScript), adding tons more code into
the fray might add noticeable hiccups, even if the long-term throughput
improves. As always, in-band compilation is a trade-off because any time you
spend compiling, you are &lt;em&gt;not executing code&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;You have to write your compiler to reason about all of this stuff. So you have
heuristics. For example, here is Michael Pollan’s inliner heuristic:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;em&gt;Inline methods. Mostly small. Not too many.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I did a survey of a bunch of compilers, mostly JIT compilers, to see what their
inlining heuristics look like. I also read (skimmed) some papers to see what
those folks had to say. I wonder if they agree.&lt;/p&gt;

&lt;p&gt;This post was a long time coming. I started working on it about five years ago
but then when I quit working at Facebook I accidentally left behind all of the
inliner research I did for Cinder’s inliner. So then I kind of just thought
about it aimlessly for a while before redoing it this year. Anyway, here’s
wonderwall.&lt;/p&gt;

&lt;h2 id=&quot;the-heuristics&quot;&gt;The heuristics&lt;/h2&gt;

&lt;p&gt;Spoiler alert: all in all, people tend to look at:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Profiles of call target&lt;/li&gt;
  &lt;li&gt;Cumulative caller size (increasing as callees get inlined)&lt;/li&gt;
  &lt;li&gt;Callee size&lt;/li&gt;
  &lt;li&gt;Inline depth&lt;/li&gt;
  &lt;li&gt;Number of inlined calls at a certain depth&lt;/li&gt;
  &lt;li&gt;If recursion is present&lt;/li&gt;
  &lt;li&gt;Callee/caller call count ratio (if callee only called less than K% of calls
to caller, don’t inline callee)&lt;/li&gt;
  &lt;li&gt;Callee stack usage&lt;/li&gt;
  &lt;li&gt;Polymorphism in callee&lt;/li&gt;
  &lt;li&gt;What mode the compiler is in (baseline vs more aggressive)&lt;/li&gt;
  &lt;li&gt;If the callee looks like it always raises/throws&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And also have different interesting ways to pipe in profile information.&lt;/p&gt;

&lt;p&gt;Last, some newer papers do some wild stuff:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Train neural networks to make inlining decisions&lt;/li&gt;
  &lt;li&gt;Let inlining drive the entire optimization pipeline, treating it as a search
heuristic over a BFS walk of the call graph&lt;/li&gt;
  &lt;li&gt;Use AOT-gathered information to aid in JIT heuristics&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Another thing to consider in inlining is how you gather and interpret profiles.&lt;/p&gt;

&lt;h2 id=&quot;call-context-and-profiles-the-other-harder-part&quot;&gt;Call context and profiles: the other harder part&lt;/h2&gt;

&lt;p&gt;When you compile a function, you tend to specialize it based on the input it
has historically been given. For a monomorphic input, maybe you guard that the
type is still the same and otherwise jump into the interpreter. For a
polymorphic input, maybe you check the top K (~4) common cases and otherwise
jump into the interpreter. Fine.&lt;/p&gt;

&lt;p&gt;But sometimes you can be compiling a polymorphic method &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bar&lt;/code&gt; that is actually
monomorphic in its caller &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;foo&lt;/code&gt;. That is, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;foo&lt;/code&gt; might only ever pass one kind
of input to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bar&lt;/code&gt;, but other callers pass all kinds of stuff. Here is a bit of
a silly example to show what I mean:&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;HashWithIndifferentAccess&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;initialize&lt;/span&gt;
    &lt;span class=&quot;vi&quot;&gt;@hash&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{}&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# Allow reading from the Hash with either a String or a Symbol&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;[]&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;vi&quot;&gt;@hash&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;to_sym&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# ...&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# some method...&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;some_hash&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;HashWithIndifferentAccess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# ...&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;some_hash&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;abc&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# some other method...&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;another_hash&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;HashWithIndifferentAccess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# ...&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;another_hash&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:xyz&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Just kidding, not so silly at all. It’s a super common pattern &lt;a href=&quot;https://github.com/rails/rails/blob/6c75e6d5663afa4278ee593c2d6c20c1ee396e32/activesupport/lib/active_support/hash_with_indifferent_access.rb#L55&quot;&gt;in
Rails&lt;/a&gt;. It makes &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;key&lt;/code&gt; polymorphic in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HashWithIndifferentAccess#[]&lt;/code&gt; even
though for many of its callers, it may well be monomorphic (or even a
constant).&lt;/p&gt;

&lt;p&gt;In order to plumb this information through to the compiler, you have to figure
out this call context relationship. There are a couple of common ways to do it.&lt;/p&gt;

&lt;h3 id=&quot;splitting&quot;&gt;Splitting&lt;/h3&gt;

&lt;p&gt;YJIT, for example, though it does not inline, splits methods based on the types
of the arguments going in. This means that it clones the compiled code,
generating a new version for each context. This does not give &lt;em&gt;call&lt;/em&gt; context
(“A calls B”) but gives type context (“B is called with integers, B’ is called
with strings”).&lt;/p&gt;

&lt;p&gt;A compiler could do type-based splitting in the interpreter or a baseline tier.&lt;/p&gt;

&lt;h3 id=&quot;profile-splitting&quot;&gt;Profile splitting&lt;/h3&gt;

&lt;p&gt;If you don’t fancy duplicating the code, you can instead duplicate the
profiles. You could either do this using type context (as above) or using call
context. SpiderMonkey, for example, does “trial inlining” that allows callers
to pass down a bit of memory for potential inline candidate callees to record
their inline caches. Instead of each function holding its own ICScript, the
caller allocates a unique ICScript for that potential-inline call-site. This
gives each callee function (at least?) one level of call context.&lt;/p&gt;

&lt;p&gt;Later, when inlining the callee into the caller, we don’t have other callers’
type information polluting the IR builder (or whatever reads the profiles).&lt;/p&gt;

&lt;h3 id=&quot;bytecode-inlining&quot;&gt;Bytecode inlining&lt;/h3&gt;

&lt;p&gt;&lt;del&gt;JavaScriptCore handles this by inlining bytecode into other bytecode. This is a
gnarly transformation but gives the interpreter, even (!) access to call
context. On tier-up to the compiler, all the inlining decisions have been made
already.&lt;/del&gt;&lt;/p&gt;

&lt;p&gt;EDIT: Upon reflection, I think think this is wrong and based on a misremembered
conversation.&lt;/p&gt;

&lt;h3 id=&quot;early-tier-with-counters&quot;&gt;Early tier with counters&lt;/h3&gt;

&lt;p&gt;HotSpot handles this with multiple tiers. The interpreter tiers up to the
client compiler, C1. C1 profiles branch and call targets in compiled code. C1
may eventually recompile based on this new information. C1 may eventually tier
up to C2, which copies C1 inlining decisions. This way, we get call context in
profiles via inlining.&lt;/p&gt;

&lt;h3 id=&quot;inline-and-analyze-and-hope&quot;&gt;Inline and analyze and hope&lt;/h3&gt;

&lt;p&gt;One last thing you could do is just trust your type inference and branch
folding in the optimizer. You could inline and do polymorphic specialization in
the callee when building the IR, then hope that your branch pruning
monomorphizes the inlined callee. It’s a little wasteful because the
polymorphic code is built “for nothing”, but it might work fine?&lt;/p&gt;

&lt;!--
### Inline and merge profiles
--&gt;

&lt;p&gt;Okay, onto the collected notes and half-baked commentary. Here’s a survey of a
bunch of JIT compilers and how they reason about inlining heuristics.&lt;/p&gt;

&lt;h3 id=&quot;thanks&quot;&gt;Thanks&lt;/h3&gt;

&lt;p&gt;But before we get into that, thanks to Iain Ireland, CF Bolz-Tereick, and Ian
Rogers for feedback on this blog post!&lt;/p&gt;

&lt;h2 id=&quot;the-survey-bits-and-bobbles&quot;&gt;The survey: bits and bobbles&lt;/h2&gt;

&lt;p&gt;What follows is mostly a “bits and bobbles” section a la &lt;a href=&quot;https://www.philipzucker.com/&quot;&gt;Phil
Zucker&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;We’ll start with &lt;a href=&quot;https://github.com/facebookincubator/cinderx&quot;&gt;Cinder&lt;/a&gt;, because when I wrote Cinder’s inliner I
added only the simplest heuristics, mostly “don’t inline” signals. Over time,
after I left, people tuned more.&lt;/p&gt;

&lt;h3 id=&quot;cinder-new&quot;&gt;Cinder: new&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;Update:&lt;/em&gt; Shortly after writing this post, and completely unrelated to my
writing this post, Alex Malyshev reworked the inlining heuristincs in Cinder. I
left the old section below as &lt;strong&gt;Cinder: old&lt;/strong&gt; and this section covers the newer
design as of July 2026.&lt;/p&gt;

&lt;p&gt;The two PRs: &lt;a href=&quot;https://github.com/facebookincubator/cinderx/commit/069ed87f0771a2dac751afdae8993685792a3e5a&quot;&gt;the
first&lt;/a&gt;
and &lt;a href=&quot;https://github.com/facebookincubator/cinderx/commit/5c918e9d58b6350f6da2095324099a3293220079&quot;&gt;the
second&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The first PR ranks potential callees to inline by call count and avoids
inlining callees that have 5% or less of the call count of the caller
(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;inliner_cold_call_threshold&lt;/code&gt;). It also orders the callees by perceived
importance using this metric: hotter callees get inlined first.&lt;/p&gt;

&lt;p&gt;The second PR starts inlining transitively. That is, if we inline function B
into function A, the calls in function B themselves become considered inlining
candidates. Furthermore, it begins ranking call-sites by cost (measured by
bytecode count) and using a worklist to drive the inlining decision making. It
prohibits recursive inlines, preventing an inline if the code is already found
in the compile-time inline stack. Last, it adds a depth limit for the recursion
(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;inliner_depth_limit&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;The old explicit sorting goes away; the new implementation uses
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::priority_queue&lt;/code&gt; to order by cost and tie-break by the order in which a
call was discovered.&lt;/p&gt;

&lt;h3 id=&quot;cinder-old&quot;&gt;Cinder: old&lt;/h3&gt;

&lt;p&gt;The &lt;a href=&quot;https://github.com/facebookincubator/cinderx/blob/88189ebf4bfd196ac7578c5076efa39bfa11f211/cinderx/Jit/hir/inliner.cpp#L341&quot;&gt;inliner&lt;/a&gt; starts from the caller CFG, walking it to find
suitable inlining candidates. Inlining candidates are only for call targets
that are known—in Cinder’s case, only for monomorphic call targets—and pass
some checks. The callee is only known by it’s function object, which includes
its bytecode. There is no IR available for the callee until we decide to inline.&lt;/p&gt;

&lt;p&gt;Most of the “can’t handle this” checks are related to argument handling. Python
has a pretty complex calling convention, so if the caller/callee have not
agreed on how the arguments should be passed through, the inliner doesn’t care
to try and figure it out on its own. That is the responsibility of &lt;a href=&quot;https://github.com/facebookincubator/cinderx/blob/88189ebf4bfd196ac7578c5076efa39bfa11f211/cinderx/Jit/hir/simplify.cpp#L1765&quot;&gt;other parts
of the compiler&lt;/a&gt;. Things in this &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;canInline&lt;/code&gt;
function could be considered “TODO”.&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;canInline&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Function&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;caller&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;AbstractCall&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;call_instr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// ...&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;BorrowedRef&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PyFunctionObject&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;call_instr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fail&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;](&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;InlineFailureType&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;failure_type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;dlogAndCollectFailureStats&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;caller&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;call_instr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;failure_type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;func&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;func_kwdefaults&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;nullptr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fail&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;InlineFailureType&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kHasKwdefaults&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;BorrowedRef&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PyCodeObject&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;code&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;func&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;func_code&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;JIT_CHECK&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PyCode_Check&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;code&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Expected PyCodeObject&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;code&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;co_kwonlyargcount&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fail&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;InlineFailureType&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kHasKwOnlyArgs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// ...&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Failures are logged so they can be analyzed. If the Cinder team determines that
there is some very frequent case they should handle, they will find out from
the logs.&lt;/p&gt;

&lt;p&gt;The inliner collects all candidate call instructions in one pass over the CFG.
It loads the configurable “cost limit” from the options struct. Then it does
one pass over the inlining candidates vector, inlining until it (maybe) hits
the cost limit.&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// ...&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;size_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cost_limit&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;getConfig&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;inliner_cost_limit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;size_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cost&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;codeCost&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;irfunc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;code&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// Inline as many calls as possible, starting from the top of the function and&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// working down.&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;call&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;to_inline&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;BorrowedRef&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PyCodeObject&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;call_code&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;func&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;func_code&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;size_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;new_cost&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cost&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;codeCost&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;call_code&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;new_cost&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cost_limit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;LOG_INLINER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;&quot;Inliner reached cost limit of {} when trying to inline {} into {}, &quot;&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;&quot;inlining stopping early&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;new_cost&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;funcFullname&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;irfunc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fullname&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;cost&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;new_cost&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;inlineFunctionCall&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;irfunc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// We need to reflow types after every inline to propagate new type&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// information from the callee.&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;reflowTypes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;irfunc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// ...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;It does some graph maintenance work after inlining these calls, but that’s it.&lt;/p&gt;

&lt;p&gt;This approach gets a surprising amount of utility for being so simple: it
inlines constants (quite a few methods look like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;def foo(): return 5&lt;/code&gt;), small
methods, and (at least, as far as I can remember) shrinks the compiled code
size. All for very little compile time overhead.&lt;/p&gt;

&lt;p&gt;There’s one other “standalone” Python JIT out there, PyPy. So we should look at
that too.&lt;/p&gt;

&lt;h3 id=&quot;pypy&quot;&gt;PyPy&lt;/h3&gt;

&lt;p&gt;There are two inliners in PyPy. One is inside the RPython to C translation
pipeline, which acts more like an ahead-of-time compiler&lt;sup id=&quot;fnref:rpython-inliner&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:rpython-inliner&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;2&lt;/a&gt;&lt;/sup&gt;.
Then there is the tracing JIT bit, which has its own optimizer and heuristics.
We’re going to look at the latter.&lt;/p&gt;

&lt;p&gt;I talked to &lt;a href=&quot;https://cfbolz.de/&quot;&gt;CF Bolz-Tereick&lt;/a&gt; about the inliner and their
comment was that PyPy’s inlining heuristic is “yes”. There are a couple of
exceptions, such as not inlining recursive functions or functions with loops.
But the basic idea of tracing includes tracing through call instructions, which
naturally means that you are “inlining”.&lt;/p&gt;

&lt;p&gt;PyPy also does this neat thing where they treat frame pushes like normal
allocation. Frame pushes, frame reads, and frame writes get written to the
trace like normal object memory traffic and can get optimized away like other
field reads and writes. This means that they can “just” use DCE to eliminate
frame pushes and pops, whereas Cinder has some complicated mechanism to do it
(which is my fault).&lt;/p&gt;

&lt;p&gt;TODO get more details here&lt;/p&gt;

&lt;h3 id=&quot;v8&quot;&gt;V8&lt;/h3&gt;

&lt;p&gt;V8 is a JS engine and it has over the years had many execution approaches.
We’ll look at three of them since they all have or had their place in the
history:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Hydrogen was the first real SSA IR and it looks very familiar to me, having
worked on Cinder and now ZJIT. It is now defunct.&lt;/li&gt;
  &lt;li&gt;Turbofan was the replacement, going full Sea of Nodes. In the grand scheme of
things it is a pretty fast compiler, but it does not hold back from doing some
expensive rewrites. This was recently rewritten from Sea of Nodes to a mode
traditional CFG and nicknamed Turboshaft.&lt;/li&gt;
  &lt;li&gt;Maglev is meant to coexist alongside Turbofan, preferring to speculate a little
more eagerly and do fewer incremental rewrites in the name of compile
time.&lt;sup id=&quot;fnref:turbolev&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:turbolev&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;3&lt;/a&gt;&lt;/sup&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;They also each inline at different times in the pipeline, which made for a fun
time trying to understand the different codebases.&lt;/p&gt;

&lt;h4 id=&quot;v8-hydrogen&quot;&gt;V8 Hydrogen&lt;/h4&gt;

&lt;p&gt;Inlining happens during Hydrogen graph building&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/tekknolagi/v8/blob/a969ab67f8e1e7475d9b26468225c3a772890c64/src/crankshaft/hydrogen.cc#L9236&quot;&gt;https://github.com/tekknolagi/v8/blob/a969ab67f8e1e7475d9b26468225c3a772890c64/src/crankshaft/hydrogen.cc#L9236&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Don’t store function bytecode of all functions; need to re-parse callee &lt;em&gt;text
source&lt;/em&gt; to inline&lt;/p&gt;

&lt;p&gt;Heuristics &lt;a href=&quot;https://github.com/tekknolagi/v8/blob/a969ab67f8e1e7475d9b26468225c3a772890c64/src/crankshaft/hydrogen.cc#L7807&quot;&gt;https://github.com/tekknolagi/v8/blob/a969ab67f8e1e7475d9b26468225c3a772890c64/src/crankshaft/hydrogen.cc#L7807&lt;/a&gt;&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;something about native context&lt;/li&gt;
  &lt;li&gt;check callee AST size against configurable limit&lt;/li&gt;
  &lt;li&gt;check inlining depth against configurable limit&lt;/li&gt;
  &lt;li&gt;don’t inline recursive functions&lt;/li&gt;
  &lt;li&gt;check current cumulative method size (as tracked by AST node count) against
configurable limit&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;v8-turbofan&quot;&gt;V8 TurboFan&lt;/h4&gt;

&lt;p&gt;&lt;a href=&quot;https://docs.google.com/document/d/1VoYBhpDhJC4VlqMXCKvae-8IGuheBGxy32EOgC2LnT8/edit&quot;&gt;https://docs.google.com/document/d/1VoYBhpDhJC4VlqMXCKvae-8IGuheBGxy32EOgC2LnT8/edit&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/v8/v8/blob/036842f4841326130a40adfcff38f85a9b4cd30a/src/compiler/js-inlining-heuristic.h#L14&quot;&gt;https://github.com/v8/v8/blob/036842f4841326130a40adfcff38f85a9b4cd30a/src/compiler/js-inlining-heuristic.h#L14&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Find candidates &lt;a href=&quot;https://github.com/v8/v8/blob/036842f4841326130a40adfcff38f85a9b4cd30a/src/compiler/js-inlining-heuristic.cc#L134&quot;&gt;https://github.com/v8/v8/blob/036842f4841326130a40adfcff38f85a9b4cd30a/src/compiler/js-inlining-heuristic.cc#L134&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Can inline &lt;a href=&quot;https://github.com/v8/v8/blob/036842f4841326130a40adfcff38f85a9b4cd30a/src/compiler/js-inlining-heuristic.cc#L75&quot;&gt;https://github.com/v8/v8/blob/036842f4841326130a40adfcff38f85a9b4cd30a/src/compiler/js-inlining-heuristic.cc#L75&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Force inline small functions &lt;a href=&quot;https://github.com/v8/v8/blob/036842f4841326130a40adfcff38f85a9b4cd30a/src/compiler/js-inlining-heuristic.cc#L309&quot;&gt;https://github.com/v8/v8/blob/036842f4841326130a40adfcff38f85a9b4cd30a/src/compiler/js-inlining-heuristic.cc#L309&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Loop over sorted (by comparator) list &lt;a href=&quot;https://github.com/v8/v8/blob/036842f4841326130a40adfcff38f85a9b4cd30a/src/compiler/js-inlining-heuristic.cc#L847&quot;&gt;https://github.com/v8/v8/blob/036842f4841326130a40adfcff38f85a9b4cd30a/src/compiler/js-inlining-heuristic.cc#L847&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;v8-maglev&quot;&gt;V8 Maglev&lt;/h4&gt;

&lt;p&gt;When optimizing, add call instructions to the inline candidates list: &lt;a href=&quot;https://github.com/v8/v8/blob/1a391f98cc7a9196369f2d6cab7df35ffbe92c08/src/maglev/maglev-graph-optimizer.cc#L1271&quot;&gt;https://github.com/v8/v8/blob/1a391f98cc7a9196369f2d6cab7df35ffbe92c08/src/maglev/maglev-graph-optimizer.cc#L1271&lt;/a&gt;&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;ProcessResult&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MaglevGraphOptimizer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;VisitCall&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Call&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;node&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                                              &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ProcessingState&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// ...&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bytecode_length&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;shared&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GetBytecodeArray&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;broker&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;score&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;call_frequency&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bytecode_length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;loop_depth_&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;1.5&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

  &lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is_small_function&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;bytecode_length&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;reducer_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;graph&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;compilation_info&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;flags&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;max_eager_inlined_bytecode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// ...&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;MaglevCallSiteInfo&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;call_site&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;reducer_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;zone&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;New&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MaglevCallSiteInfo&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;MaglevCallerDetails&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
          &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;is_small_function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;call_frequency&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
          &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;score&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bytecode_length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;reducer_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PushInlineCandidate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;call_site&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// ...&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/v8/v8/blob/036842f4841326130a40adfcff38f85a9b4cd30a/src/maglev/maglev-inlining.h#L36&quot;&gt;https://github.com/v8/v8/blob/036842f4841326130a40adfcff38f85a9b4cd30a/src/maglev/maglev-inlining.h#L36&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Unlike for example Cinder, Maglev looks like it does not have a lot of
restrictions about what can get inlined into what, so its “can inline” signal
is about budget. Actually two budgets: small budget and normal budget.&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MaglevInliner&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CanInlineCall&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// We stop inlining entirely if the small budget is exhausted.&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// Inlining decisions after that become bad if we stop inlining small&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// functions, but keep inlining large ones.&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;graph_&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;inlineable_calls&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;empty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt;
         &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;graph_&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;total_inlined_bytecode_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;
              &lt;span class=&quot;n&quot;&gt;max_inlined_bytecode_size_cumulative&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;graph_&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;total_inlined_bytecode_size_small&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;
              &lt;span class=&quot;n&quot;&gt;max_inlined_bytecode_size_small_total&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Then its inlining loop is a greedy walk of the to-inline queue checking
candidate sizes.&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MaglevInliner&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;InlineCallSites&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;DCHECK&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CanInlineCall&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;graph_&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;inlineable_calls&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;empty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// pop from inlineable_calls&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;MaglevCallSiteInfo&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;call_site&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ChooseNextCallSite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;

    &lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is_small_with_heapnum_input_outputs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;IsSmallWithHeapNumberInputsOutputs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;call_site&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;graph_&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;total_inlined_bytecode_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;max_inlined_bytecode_size_cumulative&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;c1&quot;&gt;// We ran out of budget. Checking if this is a small-ish function that we&lt;/span&gt;
      &lt;span class=&quot;c1&quot;&gt;// can still inline.&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;graph_&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;total_inlined_bytecode_size_small&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;max_inlined_bytecode_size_small_total&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;graph_&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;compilation_info&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;set_could_not_inline_all_candidates&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

      &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;is_small_with_heapnum_input_outputs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;graph_&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;compilation_info&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;set_could_not_inline_all_candidates&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;// Not that we don&apos;t break just rather just continue: next candidates&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;// might be inlineable.&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;continue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;InliningResult&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;BuildInlineFunction&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;call_site&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is_small_with_heapnum_input_outputs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// ...&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;It runs this loop (which drains the queue) interleaved with the optimizer
(which populates the queue).&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MaglevInliner&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;graph_&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;inlineable_calls&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;empty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CanInlineCall&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;InlineCallSites&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;RunOptimizer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// ...&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Confusingly, though, the optimizer also calls another function called
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CanInlineCall&lt;/code&gt; which checks if it legally can inline:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;skip recursion&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/v8/v8/blob/1a391f98cc7a9196369f2d6cab7df35ffbe92c08/src/objects/shared-function-info-inl.h#L421&quot;&gt;https://github.com/v8/v8/blob/1a391f98cc7a9196369f2d6cab7df35ffbe92c08/src/objects/shared-function-info-inl.h#L421&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;not called enough (min call frequency)&lt;/li&gt;
  &lt;li&gt;bytecode too big&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bool MaglevGraphBuilder::ShouldEagerInlineCall(&lt;/code&gt; &lt;del&gt;appears unused? / dead
declaration?&lt;/del&gt; maybe src/maglev/maglev-graph-builder.cc is just not working on
github search&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MaybeReduceResult MaglevGraphBuilder::TryBuildCallKnownJSFunction(&lt;/code&gt; &lt;del&gt;also
unused / dead declaration&lt;/del&gt; same&lt;/p&gt;

&lt;h3 id=&quot;javascriptcore&quot;&gt;JavaScriptCore&lt;/h3&gt;

&lt;p&gt;JavaScriptCore is funky! Unlike these other compilers that do inlining in their
neat little SSA IRs, JSC inlines &lt;em&gt;at the bytecode level&lt;/em&gt;&lt;sup id=&quot;fnref:fil&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:fil&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;4&lt;/a&gt;&lt;/sup&gt;. This is their way of
making sure that they get at least one level of call context into their
interpreter inline caches, which will eventually give better information to the
compiler.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Bytecode inlining
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;https://github.com/WebKit/WebKit/blob/709c3895afd71e0836f8c8be7393e44d41fab7e1/Source/JavaScriptCore/bytecode/CodeBlock.cpp#L2453&quot;&gt;https://github.com/WebKit/WebKit/blob/709c3895afd71e0836f8c8be7393e44d41fab7e1/Source/JavaScriptCore/bytecode/CodeBlock.cpp#L2453&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;DFG
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;https://github.com/WebKit/WebKit/blob/709c3895afd71e0836f8c8be7393e44d41fab7e1/Source/JavaScriptCore/dfg/DFGCapabilities.cpp#L76&quot;&gt;https://github.com/WebKit/WebKit/blob/709c3895afd71e0836f8c8be7393e44d41fab7e1/Source/JavaScriptCore/dfg/DFGCapabilities.cpp#L76&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;https://github.com/WebKit/WebKit/blob/917854a9c245b87b333e23ed4b195505d574a333/Source/JavaScriptCore/dfg/DFGByteCodeParser.cpp#L1703&quot;&gt;https://github.com/WebKit/WebKit/blob/917854a9c245b87b333e23ed4b195505d574a333/Source/JavaScriptCore/dfg/DFGByteCodeParser.cpp#L1703&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;https://github.com/WebKit/WebKit/blob/917854a9c245b87b333e23ed4b195505d574a333/Source/JavaScriptCore/bytecode/CallLinkStatus.cpp#L294&quot;&gt;https://github.com/WebKit/WebKit/blob/917854a9c245b87b333e23ed4b195505d574a333/Source/JavaScriptCore/bytecode/CallLinkStatus.cpp#L294&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;https://github.com/WebKit/WebKit/blob/d919344236c47b610930636d3310f00380624d43/Source/JavaScriptCore/bytecode/InlineCallFrame.h&quot;&gt;https://github.com/WebKit/WebKit/blob/d919344236c47b610930636d3310f00380624d43/Source/JavaScriptCore/bytecode/InlineCallFrame.h&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;JSC only inlines based on bytecode profile information, and only inlines
bytecode??&lt;/p&gt;

&lt;p&gt;TODO find better sources for bytecode inlining&lt;/p&gt;

&lt;!--
Compile plan
https://github.com/WebKit/WebKit/blob/709c3895afd71e0836f8c8be7393e44d41fab7e1/Source/JavaScriptCore/dfg/DFGPlan.cpp#L186
--&gt;

&lt;h3 id=&quot;spidermonkey&quot;&gt;SpiderMonkey&lt;/h3&gt;

&lt;p&gt;SpiderMonkey has another way of getting that call context without doing bytecode
inlining: they add call context to their inline caches. Methods can pass down
an &lt;em&gt;ICScript&lt;/em&gt; to their callees where the callee writes its inline cache
information. Then, when compiling, the callee is more likely to be
monomorphized.&lt;/p&gt;

&lt;p&gt;Wasm&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/mozilla-firefox/firefox/blob/438a3ce10eb77fb50d968463b7741117aec5bb4a/js/src/wasm/WasmHeuristics.h#L213&quot;&gt;https://github.com/mozilla-firefox/firefox/blob/438a3ce10eb77fb50d968463b7741117aec5bb4a/js/src/wasm/WasmHeuristics.h#L213&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;SpiderMonkey ICScript&lt;/p&gt;

&lt;h3 id=&quot;wasmtime-and-cranelift&quot;&gt;Wasmtime and Cranelift&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://fitzgen.com/2025/11/19/inliner.html&quot;&gt;https://fitzgen.com/2025/11/19/inliner.html&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;hotspot&quot;&gt;HotSpot&lt;/h3&gt;

&lt;p&gt;Plan: run in interpreter; tier up to C1; profile call targets; inline in C1;
profile branch counts; tier up to C2, which copies C1 inlining decisions in
bytecode parser&lt;/p&gt;

&lt;p&gt;HotSpot C2&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/openjdk/jdk/blob/a05d5d2514c835f2bfeaf7a8c7df0ac241f0177f/src/hotspot/share/opto/bytecodeInfo.cpp#L116&quot;&gt;https://github.com/openjdk/jdk/blob/a05d5d2514c835f2bfeaf7a8c7df0ac241f0177f/src/hotspot/share/opto/bytecodeInfo.cpp#L116&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/openjdk/jdk/blob/497dca2549a9829530670576115bf4b8fab386b3/src/hotspot/share/opto/bytecodeInfo.cpp#L197&quot;&gt;https://github.com/openjdk/jdk/blob/497dca2549a9829530670576115bf4b8fab386b3/src/hotspot/share/opto/bytecodeInfo.cpp#L197&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/openjdk/jdk/blob/497dca2549a9829530670576115bf4b8fab386b3/src/hotspot/share/opto/parse.hpp#L42&quot;&gt;https://github.com/openjdk/jdk/blob/497dca2549a9829530670576115bf4b8fab386b3/src/hotspot/share/opto/parse.hpp#L42&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/openjdk/jdk/blob/497dca2549a9829530670576115bf4b8fab386b3/src/hotspot/share/opto/doCall.cpp#L185&quot;&gt;https://github.com/openjdk/jdk/blob/497dca2549a9829530670576115bf4b8fab386b3/src/hotspot/share/opto/doCall.cpp#L185&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Not too small&lt;/p&gt;

&lt;p&gt;Walk up the call stack to figure out what to compile&lt;/p&gt;

&lt;p&gt;Handling the right thing to inline: def foo(a) = a.each {|x| x }
want to compile &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;foo&lt;/code&gt;, inline each, inline block, not compile block separately
(probably)&lt;/p&gt;

&lt;p&gt;HotSpot C1&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://bernsteinbear.com/assets/img/design-hotspot-client-compiler.pdf&quot;&gt;https://bernsteinbear.com/assets/img/design-hotspot-client-compiler.pdf&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/openjdk/jdk/blob/d854a04231a437a6af36ae65780961f40f336343/src/hotspot/share/c1/c1_GraphBuilder.cpp#L755&quot;&gt;https://github.com/openjdk/jdk/blob/d854a04231a437a6af36ae65780961f40f336343/src/hotspot/share/c1/c1_GraphBuilder.cpp#L755&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/openjdk/jdk/blob/d854a04231a437a6af36ae65780961f40f336343/src/hotspot/share/c1/c1_GraphBuilder.cpp#L3854&quot;&gt;https://github.com/openjdk/jdk/blob/d854a04231a437a6af36ae65780961f40f336343/src/hotspot/share/c1/c1_GraphBuilder.cpp#L3854&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;skip callees with exception handlers (unless explicitly allowed with a CLI flag)&lt;/li&gt;
  &lt;li&gt;skip synchronized callees (unless explicitly allowed with a CLI flag)&lt;/li&gt;
  &lt;li&gt;skip classes with unlinked callees&lt;/li&gt;
  &lt;li&gt;skip uninitialized classes&lt;/li&gt;
  &lt;li&gt;…&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;heuristics:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;max inline level (default 9)&lt;/li&gt;
  &lt;li&gt;max recursive inline level (default 1)&lt;/li&gt;
  &lt;li&gt;callee bytecode size (max for top level is 35 bytecodes, but falls off by 10% per inline level)&lt;/li&gt;
  &lt;li&gt;callee stack usage (max of 10 slots)
    &lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;      &lt;span class=&quot;c1&quot;&gt;// Additional condition to limit stack usage for non-recursive calls.&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;callee_recursive_level&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt;
          &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;callee&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;max_stack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;callee&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;max_locals&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;callee&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size_of_parameters&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;C1InlineStackLimit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;INLINE_BAILOUT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;callee uses too much stack&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
  &lt;li&gt;max total method size (default 8000 bytecodes)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;truffleruby&quot;&gt;TruffleRuby&lt;/h3&gt;

&lt;p&gt;TruffleRuby uses weighted compile queue&lt;/p&gt;

&lt;p&gt;Graal
&lt;a href=&quot;https://ieeexplore.ieee.org/document/8661171&quot;&gt;https://ieeexplore.ieee.org/document/8661171&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;net&quot;&gt;.NET&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/dotnet/runtime/blob/2d638dc1179164a08d9387cbe6354fe2b7e4d823/docs/design/coreclr/jit/inlining-plans.md&quot;&gt;https://github.com/dotnet/runtime/blob/2d638dc1179164a08d9387cbe6354fe2b7e4d823/docs/design/coreclr/jit/inlining-plans.md&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/dotnet/runtime/blob/0b3f3ab1ecf4de06459e5f0e2b7cb3baf70ef981/src/coreclr/jit/inline.def#L94&quot;&gt;https://github.com/dotnet/runtime/blob/0b3f3ab1ecf4de06459e5f0e2b7cb3baf70ef981/src/coreclr/jit/inline.def#L94&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/dotnet/runtime/blob/0b3f3ab1ecf4de06459e5f0e2b7cb3baf70ef981/src/coreclr/jit/inlinepolicy.cpp&quot;&gt;https://github.com/dotnet/runtime/blob/0b3f3ab1ecf4de06459e5f0e2b7cb3baf70ef981/src/coreclr/jit/inlinepolicy.cpp&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/dotnet/runtime/blob/0b3f3ab1ecf4de06459e5f0e2b7cb3baf70ef981/docs/design/coreclr/jit/inline-size-estimates.md?plain=1#L5&quot;&gt;https://github.com/dotnet/runtime/blob/0b3f3ab1ecf4de06459e5f0e2b7cb3baf70ef981/docs/design/coreclr/jit/inline-size-estimates.md?plain=1#L5&lt;/a&gt;
&lt;a href=&quot;https://github.com/dotnet/runtime/blob/0b3f3ab1ecf4de06459e5f0e2b7cb3baf70ef981/src/coreclr/jit/fginline.cpp&quot;&gt;https://github.com/dotnet/runtime/blob/0b3f3ab1ecf4de06459e5f0e2b7cb3baf70ef981/src/coreclr/jit/fginline.cpp&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/dotnet/runtime/issues/10303&quot;&gt;https://github.com/dotnet/runtime/issues/10303&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/AndyAyersMS/PerformanceExplorer/blob/master/notes/notes-aug-2016.md&quot;&gt;https://github.com/AndyAyersMS/PerformanceExplorer/blob/master/notes/notes-aug-2016.md&lt;/a&gt;
&lt;!--
LSRA heuristics
https://github.com/dotnet/runtime/blob/2d638dc1179164a08d9387cbe6354fe2b7e4d823/docs/design/coreclr/jit/lsra-heuristic-tuning.md
--&gt;&lt;/p&gt;

&lt;h3 id=&quot;dart&quot;&gt;Dart&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/dart-lang/sdk/blob/391212f3da8cc0790fc532d367549042216bd5ca/runtime/vm/compiler/backend/inliner.cc#L49&quot;&gt;https://github.com/dart-lang/sdk/blob/391212f3da8cc0790fc532d367549042216bd5ca/runtime/vm/compiler/backend/inliner.cc#L49&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/dart-lang/sdk/blob/391212f3da8cc0790fc532d367549042216bd5ca/runtime/vm/compiler/backend/inliner.cc#L1023&quot;&gt;https://github.com/dart-lang/sdk/blob/391212f3da8cc0790fc532d367549042216bd5ca/runtime/vm/compiler/backend/inliner.cc#L1023&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://web.archive.org/web/20170830093403id_/https://link.springer.com/content/pdf/10.1007/978-3-540-78791-4_5.pdf&quot;&gt;https://web.archive.org/web/20170830093403id_/https://link.springer.com/content/pdf/10.1007/978-3-540-78791-4_5.pdf&lt;/a&gt;&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;DEFINE_FLAG&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;deoptimization_counter_inlining_threshold&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;mi&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;s&quot;&gt;&quot;How many times we allow deoptimization before we stop inlining.&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;DEFINE_FLAG&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;trace_inlining&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Trace inlining&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;DEFINE_FLAG&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;charp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;inlining_filter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;nullptr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Inline only in named function&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// Flags for inlining heuristics.&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;DEFINE_FLAG&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;inline_getters_setters_smaller_than&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;s&quot;&gt;&quot;Always inline getters and setters that have fewer instructions&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;DEFINE_FLAG&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;inlining_depth_threshold&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;s&quot;&gt;&quot;Inline function calls up to threshold nesting depth&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;DEFINE_FLAG&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;inlining_size_threshold&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;mi&quot;&gt;25&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;&quot;Always inline functions that have threshold or fewer instructions&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;DEFINE_FLAG&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;inlining_callee_call_sites_threshold&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;s&quot;&gt;&quot;Always inline functions containing threshold or fewer calls.&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;DEFINE_FLAG&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;inlining_callee_size_threshold&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;mi&quot;&gt;160&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;s&quot;&gt;&quot;Do not inline callees larger than threshold&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;DEFINE_FLAG&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;inlining_small_leaf_size_threshold&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;mi&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;s&quot;&gt;&quot;Do not inline leaf callees larger than threshold&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;DEFINE_FLAG&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;inlining_caller_size_threshold&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;mi&quot;&gt;50000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;s&quot;&gt;&quot;Stop inlining once caller reaches the threshold.&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;DEFINE_FLAG&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;inlining_hotness&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;s&quot;&gt;&quot;Inline only hotter calls, in percents (0 .. 100); &quot;&lt;/span&gt;
            &lt;span class=&quot;s&quot;&gt;&quot;default 10%: calls above-equal 10% of max-count are inlined.&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;DEFINE_FLAG&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;inlining_recursion_depth_threshold&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;s&quot;&gt;&quot;Inline recursive function calls up to threshold recursion depth.&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;DEFINE_FLAG&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;max_inlined_per_depth&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;mi&quot;&gt;500&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;s&quot;&gt;&quot;Max. number of inlined calls per depth&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;a href=&quot;/assets/img/adaptive-inline.pdf&quot;&gt;An adaptive strategy for inline substitution&lt;/a&gt; (PDF)&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  &lt;span class=&quot;c1&quot;&gt;// Inlining heuristics based on Cooper et al. 2008.&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;InliningDecision&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;ShouldWeInline&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Function&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;callee&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                                  &lt;span class=&quot;kt&quot;&gt;intptr_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;instr_count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                                  &lt;span class=&quot;kt&quot;&gt;intptr_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;call_site_count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// Pragma or size heuristics.&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;inliner_&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AlwaysInline&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;callee&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;InliningDecision&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Yes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;AlwaysInline&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;inlined_size_&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;FLAG_inlining_caller_size_threshold&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;c1&quot;&gt;// Prevent caller methods becoming humongous and thus slow to compile.&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;InliningDecision&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;No&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;--inlining-caller-size-threshold&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;instr_count&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;FLAG_inlining_callee_size_threshold&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;c1&quot;&gt;// Prevent inlining of callee methods that exceed certain size.&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;InliningDecision&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;No&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;--inlining-callee-size-threshold&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// Inlining depth.&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;callee_inlining_depth&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;callee&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;inlining_depth&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;callee_inlining_depth&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;callee_inlining_depth&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;inlining_depth_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
         &lt;span class=&quot;n&quot;&gt;FLAG_inlining_depth_threshold&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;InliningDecision&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;No&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;--inlining-depth-threshold&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// Situation instr_count == 0 denotes no counts have been computed yet.&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// In that case, we say ok to the early heuristic and come back with the&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// late heuristic.&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;instr_count&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;InliningDecision&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Yes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;need to count first&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;instr_count&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;FLAG_inlining_size_threshold&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;InliningDecision&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Yes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;--inlining-size-threshold&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;call_site_count&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;FLAG_inlining_callee_call_sites_threshold&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;InliningDecision&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Yes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;--inlining-callee-call-sites-threshold&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;InliningDecision&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;No&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;default&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;!--
CompileType
https://github.com/dart-lang/sdk/blob/d3c0a3768bd4be4a92886e136811b5f748b63ddd/runtime/vm/compiler/backend/compile_type.h#L43
--&gt;

&lt;!--
intrinsics
https://github.com/dart-lang/sdk/blob/d3c0a3768bd4be4a92886e136811b5f748b63ddd/runtime/vm/compiler/call_specializer.cc#L3229
--&gt;

&lt;h3 id=&quot;hhvm&quot;&gt;HHVM&lt;/h3&gt;

&lt;p&gt;tracelet based&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/facebook/hhvm/blob/eeba7ad1ffa372a9b8cc9d1ec7f5295d45627009/hphp/runtime/vm/jit/inlining-decider.h#L89&quot;&gt;https://github.com/facebook/hhvm/blob/eeba7ad1ffa372a9b8cc9d1ec7f5295d45627009/hphp/runtime/vm/jit/inlining-decider.h#L89&lt;/a&gt;&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  &lt;span class=&quot;c1&quot;&gt;// Refuse if the cost exceeds our thresholds.&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// We measure the cost of inlining each callstack and stop when it exceeds a&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// certain threshold.  (Note that we do not measure the total cost of all the&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// inlined calls for a given caller---just the cost of each nested stack.)&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;cost&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;costOfInlining&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;callerSk&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;callee&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;regionAndUnit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;annotationsPtr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cost&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Cfg&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;HHIR&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AlwaysInlineVasmCostLimit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;accept&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;folly&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sformat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;cost={} within always-inline limit&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cost&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;region&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;instrSize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;irgs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;budgetBCInstrs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;refuse&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;folly&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sformat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
      &lt;span class=&quot;s&quot;&gt;&quot;exhausted bytecode budget: budgetBCInstrs={}, regionSize={}&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;irgs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;budgetBCInstrs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;region&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;instrSize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()));&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;maxTotalCost&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;adjustedMaxVasmCost&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;irgs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;region&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;inlineDepth&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;irgs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;maxCost&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;maxTotalCost&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Cfg&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;HHIR&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;InliningUseStackedCost&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;maxCost&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;irgs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;inlineState&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cost&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;baseProfCount&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s_baseProfCount&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;load&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;callerProfCount&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;irgen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;curProfCount&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;irgs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;calleeProfCount&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;irgen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;calleeProfCount&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;irgs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;region&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cost&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;maxCost&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;depth&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;inlineDepth&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;irgs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;refuse&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;folly&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sformat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
      &lt;span class=&quot;s&quot;&gt;&quot;too expensive: cost={} : maxCost={} : &quot;&lt;/span&gt;
      &lt;span class=&quot;s&quot;&gt;&quot;baseProfCount={} : callerProfCount={} : calleeProfCount={} : depth={}&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;cost&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;maxCost&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;baseProfCount&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;callerProfCount&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;calleeProfCount&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;depth&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;accept&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;folly&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sformat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;small region with return: cost={} : &quot;&lt;/span&gt;
                               &lt;span class=&quot;s&quot;&gt;&quot;maxTotalCost={} : maxCost={} : baseProfCount={}&quot;&lt;/span&gt;
                               &lt;span class=&quot;s&quot;&gt;&quot; : callerProfCount={} : calleeProfCount={}&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                               &lt;span class=&quot;n&quot;&gt;cost&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;maxTotalCost&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;maxCost&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;baseProfCount&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                               &lt;span class=&quot;n&quot;&gt;callerProfCount&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;calleeProfCount&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;art&quot;&gt;ART&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/LineageOS/android_art/blob/8ce603e0c68899bdfbc9cd4c50dcc65bbf777982/compiler/optimizing/inliner.h&quot;&gt;https://github.com/LineageOS/android_art/blob/8ce603e0c68899bdfbc9cd4c50dcc65bbf777982/compiler/optimizing/inliner.h&lt;/a&gt;&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// Instruction limit to control memory.&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;constexpr&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;size_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;kMaximumNumberOfTotalInstructions&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1024&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// Maximum number of instructions for considering a method small,&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// which we will always try to inline if the other non-instruction limits&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// are not reached.&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;constexpr&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;size_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;kMaximumNumberOfInstructionsForSmallMethod&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// Limit the number of dex registers that we accumulate while inlining&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// to avoid creating large amount of nested environments.&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;constexpr&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;size_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;kMaximumNumberOfCumulatedDexRegisters&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// Limit recursive call inlining, which do not benefit from too&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// much inlining compared to code locality.&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;constexpr&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;size_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;kMaximumNumberOfRecursiveCalls&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// Limit recursive polymorphic call inlining to prevent code bloat, since it can quickly get out of&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// hand in the presence of multiple Wrapper classes. We set this to 0 to disallow polymorphic&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// recursive calls at all.&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;constexpr&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;size_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;kMaximumNumberOfPolymorphicRecursiveCalls&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// Controls the use of inline caches in AOT mode.&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;constexpr&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;kUseAOTInlineCaches&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// Controls the use of inlining try catches.&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;constexpr&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;kInlineTryCatches&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HInliner&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;UpdateInliningBudget&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;total_number_of_instructions_&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;kMaximumNumberOfTotalInstructions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// Always try to inline small methods.&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;inlining_budget_&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;kMaximumNumberOfInstructionsForSmallMethod&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;inlining_budget_&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;kMaximumNumberOfInstructionsForSmallMethod&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;kMaximumNumberOfTotalInstructions&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;total_number_of_instructions_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HInliner&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IsInliningEncouraged&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HInvoke&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;invoke_instruction&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                                    &lt;span class=&quot;n&quot;&gt;ArtMethod&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;method&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                                    &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CodeItemDataAccessor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;accessor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CountRecursiveCallsOf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;method&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;kMaximumNumberOfRecursiveCalls&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;LOG_FAIL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stats_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MethodCompilationStat&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kNotInlinedRecursiveBudget&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Method &quot;&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;method&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PrettyMethod&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot; is not inlined because it has reached its recursive call budget.&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;kt&quot;&gt;size_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;inline_max_code_units&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;codegen_&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GetCompilerOptions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GetInlineMaxCodeUnits&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;accessor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;InsnsSizeInCodeUnits&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;inline_max_code_units&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;LOG_FAIL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stats_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MethodCompilationStat&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kNotInlinedCodeItem&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Method &quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;method&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PrettyMethod&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot; is not inlined because its code item is too big: &quot;&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;accessor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;InsnsSizeInCodeUnits&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot; &amp;gt; &quot;&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;inline_max_code_units&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;graph_&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IsCompilingBaseline&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;accessor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;InsnsSizeInCodeUnits&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CompilerOptions&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kBaselineInlineMaxCodeUnits&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;LOG_FAIL_NO_STAT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Reached baseline maximum code unit for inlining  &quot;&lt;/span&gt;
                       &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;method&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PrettyMethod&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;outermost_graph_&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SetUsefulOptimizing&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;invoke_instruction&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GetBlock&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GetLastInstruction&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IsThrow&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;LOG_FAIL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stats_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MethodCompilationStat&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kNotInlinedEndsWithThrow&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Method &quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;method&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PrettyMethod&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot; is not inlined because its block ends with a throw&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;outermost_graph_&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IsCompilingBaseline&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;current&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IsInvokeVirtual&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;current&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IsInvokeInterface&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;ProfilingInfoBuilder&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IsInlineCacheUseful&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;current&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AsInvoke&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;codegen_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;uint32_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;maximum_inlining_depth_for_baseline&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;InlineCache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MaxDexPcEncodingDepth&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;outermost_graph_&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GetArtMethod&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;codegen_&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GetCompilerOptions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GetInlineMaxCodeUnits&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;depth_&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;maximum_inlining_depth_for_baseline&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;LOG_FAIL_NO_STAT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Reached maximum depth for inlining in baseline compilation: &quot;&lt;/span&gt;
                       &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;depth_&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot; for &quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;callee_graph&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GetArtMethod&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PrettyMethod&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;outermost_graph_&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SetUsefulOptimizing&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;jikesrvm&quot;&gt;JikesRVM&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/JikesRVM/JikesRVM/blob/5072f19761115d987b6ee162f49a03522d36c697/rvm/src/org/jikesrvm/compilers/opt/inlining/DefaultInlineOracle.java#L55&quot;&gt;https://github.com/JikesRVM/JikesRVM/blob/5072f19761115d987b6ee162f49a03522d36c697/rvm/src/org/jikesrvm/compilers/opt/inlining/DefaultInlineOracle.java#L55&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;otherresearch&quot;&gt;Other/research&lt;/h3&gt;

&lt;p&gt;Partial inlining&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://ethz.ch/content/dam/ethz/special-interest/infk/ast-dam/documents/Theodoridis-ASPLOS22-Inlining-Paper.pdf&quot;&gt;Understanding and Exploiting Optimal Function Inlining&lt;/a&gt; (PDF)&lt;/p&gt;

&lt;p&gt;machine learning&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://ieeexplore.ieee.org/document/6495004&quot;&gt;Automatic construction of inlining heuristics using machine learning&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://ssw.jku.at/Teaching/PhDTheses/Mosaner/Dissertation%20Mosaner.pdf&quot;&gt;Machine-Learning-Based Optimization Heuristics in Dynamic Compilers&lt;/a&gt; (PDF)&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://webdocs.cs.ualberta.ca/~amaral/thesis/ErickOchoaMSc.pdf&quot;&gt;Guiding Inlining Decisions Using Post-Inlining Transformations&lt;/a&gt; (PDF)&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://karimali.ca/resources/papers/ourinliner.pdf&quot;&gt;U Can’t Inline This!&lt;/a&gt; (PDF)&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://dl.acm.org/doi/10.1145/182409.182489&quot;&gt;Towards better inlining decisions using inlining trials&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/chrisseaton/rhizome/blob/main/doc/inlining.md&quot;&gt;RhizomeRuby inlining&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://aleksandar-prokopec.com/resources/docs/prio-inliner-final.pdf&quot;&gt;An Optimization-Driven Incremental Inline Substitution Algorithm for Just-in-Time Compilers&lt;/a&gt; (PDF)&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://www.cresco.enea.it/SC05/schedule/pdf/pap274.pdf&quot;&gt;Automatic Tuning of Inlining Heuristics&lt;/a&gt; (PDF)&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://dl.acm.org/doi/pdf/10.1145/3563838.3567677&quot;&gt;Inlining-Benefit Prediction with Interprocedural Partial Escape Analysis&lt;/a&gt; (PDF)&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;/assets/img/virtual-inlining.pdf&quot;&gt;Inlining of Virtual Methods&lt;/a&gt; (PDF)&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;/assets/img/sable-inlining.pdf&quot;&gt;A Study of Type Analysis for Speculative Method Inlining in a JIT Environment&lt;/a&gt; (PDF)&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://dl.acm.org/doi/epdf/10.1145/351403.351416&quot;&gt;A Comparative Study of Static and Profile-Based Heuristics for Inlining&lt;/a&gt; (PDF)&lt;/p&gt;

&lt;p&gt;clusters from &lt;a href=&quot;https://llvm.org/devmtg/2022-05/slides/2022EuroLLVM-CustomBenefitDrivenInliner-in-FalconJIT.pdf&quot;&gt;Custom benefit-driven inliner in Falcon JIT&lt;/a&gt; (PDF)&lt;/p&gt;

&lt;h3 id=&quot;graal&quot;&gt;Graal&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/oracle/graal/blob/5dde777cba22a99ebe3f19745d03ddfbc35c563c/compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/phases/common/inlining/policy/GreedyInliningPolicy.java&quot;&gt;https://github.com/oracle/graal/blob/5dde777cba22a99ebe3f19745d03ddfbc35c563c/compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/phases/common/inlining/policy/GreedyInliningPolicy.java&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/oracle/graal/blob/5dde777cba22a99ebe3f19745d03ddfbc35c563c/compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/phases/common/inlining/InliningPhase.java&quot;&gt;https://github.com/oracle/graal/blob/5dde777cba22a99ebe3f19745d03ddfbc35c563c/compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/phases/common/inlining/InliningPhase.java&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/oracle/graal/blob/5dde777cba22a99ebe3f19745d03ddfbc35c563c/compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/phases/common/inlining/info/elem/InlineableGraph.java#L148&quot;&gt;https://github.com/oracle/graal/blob/5dde777cba22a99ebe3f19745d03ddfbc35c563c/compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/phases/common/inlining/info/elem/InlineableGraph.java#L148&lt;/a&gt;
&lt;!--
GVN
https://github.com/oracle/graal/blob/5dde777cba22a99ebe3f19745d03ddfbc35c563c/compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/phases/common/DominatorBasedGlobalValueNumberingPhase.java#L132
--&gt;&lt;/p&gt;
&lt;div class=&quot;footnotes&quot; role=&quot;doc-endnotes&quot;&gt;
  &lt;ol&gt;
    &lt;li id=&quot;fn:aot-split&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;There are some newer papers, especially in Java land, that try to
do a lot of analysis ahead-of-time and bundle the resulting information in
.class files. Then the JIT can read it and see more than local context.&lt;/p&gt;

      &lt;p&gt;Or, if you are an AOT compiler, you can probably do a lot more whole system
reasoning—both for time budget reasons and also because you can see more
functions at once. &lt;a href=&quot;#fnref:aot-split&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:rpython-inliner&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;&lt;a href=&quot;https://github.com/pypy/pypy/blob/bab69dca82606f9e4feaf5507f8dd8dfb3e968b2/rpython/translator/backendopt/inline.py#L144&quot;&gt;Check it
out&lt;/a&gt;
if you like. I stumbled across it by accident. &lt;a href=&quot;#fnref:rpython-inliner&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:turbolev&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;See also “Turbolev”, which seems to merge Maglev (CFG) with
Turbofan (Sea of Nodes)… somehow. &lt;a href=&quot;#fnref:turbolev&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:fil&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;Potentially a misunderstanding based on a private conversation. I’m
working on tracking down the implementation… &lt;a href=&quot;#fnref:fil&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
  &lt;/ol&gt;
&lt;/div&gt;
</description>
            <pubDate>Wed, 03 Jun 2026 00:00:00 +0000</pubDate>
            <niceDate>June 3, 2026</niceDate>
            <link>https://bernsteinbear.com/blog/inlining-heuristics/?utm_source=rss</link>
            <guid isPermaLink="true">https://bernsteinbear.com/blog/inlining-heuristics/</guid>
        </item>
        
    </channel>
</rss>
