<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Runtime Checks</title>
    <description>I am a Brazilian Sofware Engineer working remotely from Brazil. I taught myself programming when I was in high school and studied Computer Science in university. I currently work at Voltron Data on the C++ implementation of Apache Arrow. In the past, I lived in Sweden and worked at Spotify on the core C++ libraries powering their family of apps.
</description>
    <link>https://felipe.rs/</link>
    <atom:link href="https://felipe.rs/atom.xml" rel="self" type="application/rss+xml"/>
    <pubDate>Sun, 08 Dec 2024 13:41:15 -0300</pubDate>
    <lastBuildDate>Sun, 08 Dec 2024 13:41:15 -0300</lastBuildDate>
    <generator>Jekyll v4.3.3</generator>
    
      
      <item>
        <title>Compressed Apache Arrow Tables Over HTTP</title>
        <description>&lt;p&gt;Returning &lt;a href=&quot;https://arrow.apache.org&quot;&gt;Arrow&lt;/a&gt; tables over HTTP is relatively straightforward. The
&lt;a href=&quot;https://arrow.apache.org/docs/format/Columnar.html#ipc-streaming-format&quot;&gt;Arrow IPC streaming format&lt;/a&gt;
is repurposed to the HTTP use case and you can return each serialized IPC batch
as a chunk of the HTTP response — &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Transfer-Encoding: chunked&lt;/code&gt; — to enable
streaming clients.&lt;/p&gt;

&lt;p&gt;Arrow IPC streams have a MIME type
&lt;a href=&quot;https://www.iana.org/assignments/media-types/application/vnd.apache.arrow.stream&quot;&gt;registered with IANA&lt;/a&gt;
so you can also set the appropriate &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Content-Type&lt;/code&gt; header on your HTTP responses.&lt;/p&gt;

&lt;figure class=&quot;code&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&quot;line-number&quot;&gt;1&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;code class=&quot;&quot;&gt;&lt;span class=&quot;line&quot;&gt;Content-Type: application/vnd.apache.arrow.stream&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;

&lt;p&gt;You can find Python examples of this in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;arrow-experiments&lt;/code&gt; repository:
&lt;a href=&quot;https://github.com/apache/arrow-experiments/tree/main/http/get_simple/python&quot;&gt;https://github.com/apache/arrow-experiments/tree/main/http/get_simple/python.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Things get trickier when you try to support compression. Arrow IPC streams
are not compressed by default and not all client implementations of the IPC
protocol can parse streams with compressed buffers.&lt;/p&gt;

&lt;p&gt;We will get back to IPC compression in a moment, but first let’s see how we can
achieve compression of the data by compressing the HTTP response body itself.&lt;/p&gt;

&lt;h2 id=&quot;compressing-http-responses&quot;&gt;Compressing HTTP Responses&lt;/h2&gt;

&lt;p&gt;All clients that can handle compressed HTTP responses can benefit from this.
A server can look at the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Accept-Encoding&lt;/code&gt; header of the request and decide to
compress the response body if the client supports it. Chrome, Firefox, and
most modern browsers support &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gzip&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;deflate&lt;/code&gt; compression as well as modern
codecs like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;br&lt;/code&gt; (&lt;a href=&quot;https://github.com/google/brotli&quot;&gt;Brotli&lt;/a&gt;) and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;zstd&lt;/code&gt; (&lt;a href=&quot;https://facebook.github.io/zstd&quot;&gt;Zstandard&lt;/a&gt;)&lt;sup id=&quot;fnref:zstd_on_safari&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:zstd_on_safari&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;1&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;

&lt;p&gt;Web browsers and HTTP client libraries will automatically decompress the
response body if the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Content-Encoding&lt;/code&gt; header is set to the codec used to
compress the response body.&lt;/p&gt;

&lt;figure class=&quot;code&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&quot;line-number&quot;&gt;1&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;code class=&quot;&quot;&gt;&lt;span class=&quot;line&quot;&gt;Content-Encoding: gzip&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;

&lt;p&gt;When Chrome makes an HTTP request, it sets the
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Accept-Encoding: gzip, deflate, br, zstd&lt;/code&gt; header to indicate that it can handle
compressed responses in any of these formats. If you’re using a library to make
HTTP requests, make sure it sets this header correctly so the server can
compress the response body without worrying about compatibility with the client.&lt;/p&gt;

&lt;h2 id=&quot;using-arrow-ipc-compression&quot;&gt;Using Arrow IPC Compression&lt;/h2&gt;

&lt;p&gt;The buffers placed in a serialized Arrow IPC stream can be compressed using the
&lt;a href=&quot;https://lz4.org/&quot;&gt;LZ4&lt;/a&gt; or Zstandard codecs. &lt;a href=&quot;https://arrow.apache.org/docs/dev/format/Columnar.html#compression&quot;&gt;Arrow IPC compression&lt;/a&gt;
has nothing to do with the HTTP protocol — the compression and decompression
is done by the Arrow library itself as it parses the stream. Note, though, that
&lt;a href=&quot;https://arrow.apache.org/docs/dev/status.html#ipc-format&quot;&gt;not all Arrow implementations support compression&lt;/a&gt;.&lt;sup id=&quot;fnref:ipc_compression_support&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:ipc_compression_support&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;2&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;

&lt;p&gt;To maximize the chances of compression without compatibility issues, you can
ask the clients to communicate their compression preferences in the
request headers. This can be achieved by passing parameters to the MIME type in the
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Accept&lt;/code&gt; request header.&lt;sup id=&quot;fnref:accept_codecs&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:accept_codecs&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;3&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;

&lt;figure class=&quot;code&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&quot;line-number&quot;&gt;1&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;code class=&quot;&quot;&gt;&lt;span class=&quot;line&quot;&gt;Accept: application/vnd.apache.arrow.stream; codecs=&quot;lz4, zstd&quot;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;

&lt;p&gt;This way, the server can decide to compress the IPC stream buffers using LZ4 or
Zstandard without fear that client won’t be able to decode it. If the client
doesn’t specify a codec, the server can disable compression altogether, fall back
to HTTP compression (based on the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Accept-Encoding&lt;/code&gt; header), or choose a
compression codec at the risk of incompatibility.&lt;/p&gt;

&lt;p&gt;You can find examples of this in Python at
&lt;a href=&quot;https://github.com/apache/arrow-experiments/pull/35&quot;&gt;https://github.com/apache/arrow-experiments/pull/35.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To make troubleshooting easier, the server may indicate which compression codec
was used to compress the IPC stream, even though the Arrow IPC stream reader
picks up which codec was used from the data stream itself.&lt;/p&gt;

&lt;figure class=&quot;code&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&quot;line-number&quot;&gt;1&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;code class=&quot;&quot;&gt;&lt;span class=&quot;line&quot;&gt;Content-Type: application/vnd.apache.arrow.stream; codecs=lz4&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;

&lt;h3 id=&quot;a-note-on-double-compression&quot;&gt;A Note on Double-Compression&lt;/h3&gt;

&lt;p&gt;Applying both IPC buffer and HTTP compression to the same data is not
recommended. The extra CPU overhead of decompressing the data twice is
not worth any possible gains that double compression might bring. If
compression ratios are unambiguously more important than reducing CPU
overhead, then a different compression algorithm&lt;sup id=&quot;fnref:compression_level&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:compression_level&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;4&lt;/a&gt;&lt;/sup&gt;
that optimizes for that can be chosen.&lt;/p&gt;

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

&lt;p&gt;Returning Arrow tables over HTTP is a powerful way to share data between
applications. Unsurprisingly, columnar formats like Arrow are very friendly
to compression and serialization. This is why Arrow works well as a network
transfer format. As tooling evolves you can expect to see more and more Arrow
tables being shared over HTTP instead of CSV or JSON.&lt;/p&gt;

&lt;div class=&quot;footnotes&quot; role=&quot;doc-endnotes&quot;&gt;
  &lt;ol&gt;
    &lt;li id=&quot;fn:zstd_on_safari&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;&lt;a href=&quot;https://facebook.github.io/zstd&quot;&gt;Zstdandard&lt;/a&gt; is &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Encoding#browser_compatibility&quot;&gt;not supported on Safari as of 2024.&lt;/a&gt; &lt;a href=&quot;#fnref:zstd_on_safari&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:ipc_compression_support&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;Check the &lt;a href=&quot;https://arrow.apache.org/docs/dev/status.html#ipc-format&quot;&gt;implementation status table in the Arrow documentation&lt;/a&gt; to see if the Arrow implementation you’re using supports buffer compression. &lt;a href=&quot;#fnref:ipc_compression_support&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:accept_codecs&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;This is similar to clients requesting video streams by specifying the container format and &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/Media/Formats/codecs_parameter&quot;&gt;the codecs they support&lt;/a&gt; like this &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Accept: video/webm; codecs=&quot;vp8, vorbis&quot;&lt;/code&gt;. &lt;a href=&quot;#fnref:accept_codecs&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:compression_level&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;Compression codecs can be instantiated with different compression levels trading off compression ratio for speed. &lt;a href=&quot;#fnref:compression_level&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
  &lt;/ol&gt;
&lt;/div&gt;
</description>
        <pubDate>Wed, 23 Oct 2024 22:50:34 -0300</pubDate>
        <link>https://felipe.rs/2024/10/23/arrow-over-http/</link>
        <guid isPermaLink="true">https://felipe.rs/2024/10/23/arrow-over-http/</guid>
        
        
        <category>Apache Arrow</category>
        
        <category>Databases</category>
        
        <category>Distributed Systems</category>
        
        <category>In English</category>
        
        <category>Python</category>
        
      </item>
      
    
      
      <item>
        <title>Root Cause vs. Contributing Factors</title>
        <description>&lt;p&gt;In multiplication, to get a non-zero product, all factors must be greater than 0.
If one factor is 0, nothing else matters.&lt;/p&gt;

\[\begin{align}
5 \times 2 \times 229 \times 0.2 &amp;amp;= 458.0\\
5 \times 2 \times 229 \times 0.0 &amp;amp;= 0.0
\end{align}\]

&lt;p&gt;This &lt;a href=&quot;https://en.wikipedia.org/wiki/XZ_Utils_backdoor&quot;&gt;xz/liblzma incident&lt;/a&gt; and
the public post-morten discussion is a great illustration of why incident
post-mortens shouldn’t be carried as a search for &lt;strong&gt;a root cause,&lt;/strong&gt; but as
a comprehensive enumeration of all &lt;strong&gt;contributing factors.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You can model successful attacks as the observed &lt;em&gt;occurrence of an event with
non-zero probability&lt;/em&gt; — a possible incident that occurred.&lt;sup id=&quot;fnref:time&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:time&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;1&lt;/a&gt;&lt;/sup&gt; In that
model, the probability of an incident occurring is the product of all the
contributing factors’ probabilities.&lt;/p&gt;

&lt;p&gt;The nature of multiplication allows anyone to pick one factor and proclaim:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;If the probability of this factor were 0, none of this would have happened,
this must be &lt;strong&gt;the&lt;/strong&gt; root cause.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The contributing factor you elect as the root cause says more about you, than
it says about the shape of the effective solution or mitigating measures.&lt;/p&gt;

&lt;p&gt;An open-source contributor will say the root cause is contributor burnout. An
engineer that never worked on open-source will claim that open-source itself is
the problem. A VP at a big corporation will say the problem is hobbyists not
taking software maintenance seriously enough. A security researcher will blame
the insufficient threat models that software distribution systems assume.&lt;/p&gt;

&lt;p&gt;Getting the chances of that root cause happening down to zero would fully
prevent the incident from ever happening again, except that it might be
impossible to get it down to zero, and reducing all the contributing factors as
much as possible can have a bigger impact on the final product than an imperfect
attempt at reducing the chances of a single contributing factor. Because a
root cause really is just an arbitrarily chosen contributing factor. &lt;sup id=&quot;fnref:hindsight&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:hindsight&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;2&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;

&lt;div class=&quot;footnotes&quot; role=&quot;doc-endnotes&quot;&gt;
  &lt;ol&gt;
    &lt;li id=&quot;fn:time&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;Time factors are necessary for a more advanced model. &lt;a href=&quot;#fnref:time&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:hindsight&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;&lt;a href=&quot;https://how.complexsystems.fail/#8&quot;&gt;“Hindsight biases post-accident assessments of human performance”&lt;/a&gt; &lt;a href=&quot;#fnref:hindsight&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
  &lt;/ol&gt;
&lt;/div&gt;
</description>
        <pubDate>Mon, 08 Apr 2024 22:01:03 -0300</pubDate>
        <link>https://felipe.rs/2024/04/08/root-cause-vs-contributing-factors/</link>
        <guid isPermaLink="true">https://felipe.rs/2024/04/08/root-cause-vs-contributing-factors/</guid>
        
        
        <category>In English</category>
        
        <category>Systems Thinking</category>
        
      </item>
      
    
      
      <item>
        <title>std::optional&lt;T&gt; and non-POD C++ types</title>
        <description>&lt;p&gt;C++ 17 has introduced the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::optional&amp;lt;T&amp;gt;&lt;/code&gt; &lt;a href=&quot;https://en.cppreference.com/w/cpp/utility/optional&quot;&gt;template class&lt;/a&gt; that is
analogous to the &lt;a href=&quot;https://wiki.haskell.org/Maybe&quot;&gt;Maybe/Optional monad&lt;/a&gt; implementation in many other
languages. “Analogous” is doing a lot of work in this statement because the C++
type checker is &lt;a href=&quot;https://en.cppreference.com/w/cpp/utility/optional/operator*&quot;&gt;not going to help you&lt;/a&gt; avoid dereferencing an empty
optional like Rust, Haskell, Scala, Kotlin, TypeScript and many other languages
will do.&lt;/p&gt;

&lt;p&gt;That does not make it useless. As with many things in C++, we
will be careful™ when using it and write only programs that do not dereference
an empty optional.&lt;/p&gt;

&lt;p&gt;In languages that deal mostly with &lt;a href=&quot;https://en.wikipedia.org/wiki/Value_type_and_reference_type&quot;&gt;reference types&lt;/a&gt;, an optional type
can be implemented as an object that wraps a reference and a tag bit that tells
if the optional has some data or nothing.&lt;sup id=&quot;fnref:flowtyping&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:flowtyping&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;1&lt;/a&gt;&lt;/sup&gt; In C++ on the other hand,
the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::optional&amp;lt;T&amp;gt;&lt;/code&gt; will inline the value type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T&lt;/code&gt; onto itself. That means
the general behavior of an optional of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T&lt;/code&gt; depends a lot on the specifics of
the type it’s wrapping.&lt;/p&gt;

&lt;p&gt;For integer, floats, characters, the use of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::optional&amp;lt;T&amp;gt;&lt;/code&gt; doesn’t bring
many surprises. In this post I want to look at what happens when &lt;a href=&quot;http://en.wikipedia.org/wiki/Plain_Old_Data_Structures&quot;&gt;non-POD&lt;/a&gt;
types are wrapped in an optional. For this, I will write a class that prints a
different message on each special function call:&lt;/p&gt;

&lt;figure class=&quot;code&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&quot;line-number&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;3&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;4&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;5&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;6&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;7&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;8&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;9&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;10&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;11&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;12&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;13&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;14&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;15&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;16&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;17&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;18&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;19&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;20&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;21&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;22&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;23&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;24&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;25&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;26&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;code class=&quot;cpp&quot;&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Object&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt; &lt;span class=&quot;k&quot;&gt;private&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt; &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;n&quot;&gt;Object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;puts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;default-constructed&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;o&quot;&gt;~&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;puts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;destroyed&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;k&quot;&gt;explicit&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;puts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;constructed&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;n&quot;&gt;Object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Object&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;puts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;copy-constructed&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;n&quot;&gt;Object&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;operator&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Object&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;    &lt;span class=&quot;n&quot;&gt;puts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;copy-assinged&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;    &lt;span class=&quot;n&quot;&gt;_s&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;n&quot;&gt;Object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Object&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;move&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;puts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;move-constructed&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;n&quot;&gt;Object&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;operator&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Object&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;    &lt;span class=&quot;n&quot;&gt;puts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;move-assigned&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;    &lt;span class=&quot;n&quot;&gt;_s&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;move&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dump&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;puts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c_str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;

&lt;p&gt;And write a function that returns an optional of this class — a common use-case
of optionals. The returned optional will contain a value if the string argument
is non-empty, and be &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::nullopt&lt;/code&gt; otherwise.&lt;/p&gt;

&lt;figure class=&quot;code&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&quot;line-number&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;3&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;4&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;5&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;6&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;code class=&quot;cpp&quot;&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;optional&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Object&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;maybe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;empty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nullopt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;

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

&lt;p&gt;When using a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::optional&amp;lt;Object&amp;gt;&lt;/code&gt;, neither the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Object&lt;/code&gt; constructors or
destructors have to be called if the variable never gets populated with a value.&lt;/p&gt;

&lt;p&gt;To see this in action, consider &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;program1&lt;/code&gt;&lt;/p&gt;

&lt;figure class=&quot;code&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&quot;line-number&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;3&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;4&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;5&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;6&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;7&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;8&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;code class=&quot;cpp&quot;&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;program1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;optional&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Object&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;o&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;maybe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;    &lt;span class=&quot;n&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dump&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;    &lt;span class=&quot;n&quot;&gt;puts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;&amp;lt;empty&amp;gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;

&lt;p&gt;and its output when called with an empty string&lt;/p&gt;

&lt;figure class=&quot;code&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&quot;line-number&quot;&gt;1&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;code class=&quot;cpp&quot;&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;empty&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;

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

&lt;p&gt;Things get more involved when &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;program1&lt;/code&gt; is called with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;Hello!&quot;&lt;/code&gt; and the
optional gets populated&lt;/p&gt;

&lt;figure class=&quot;code&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&quot;line-number&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;3&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;4&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;5&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;code class=&quot;cpp&quot;&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;constructed&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;n&quot;&gt;move&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;constructed&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;n&quot;&gt;destroyed&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;n&quot;&gt;Hello&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;n&quot;&gt;destroyed&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;

&lt;p&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;return Object(s)&lt;/code&gt; line in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;maybe&lt;/code&gt;, calls
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Object::Object(const std::string &amp;amp;)&lt;/code&gt; to create the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Object&lt;/code&gt; that then gets
moved into the storage within the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::optional&amp;lt;Object&amp;gt;&lt;/code&gt;. If &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Object&lt;/code&gt; didn’t
have a move-constructor, it would be copied here. At the end of the scope of
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;maybe&lt;/code&gt;, the “moved-from” temporary &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Object&lt;/code&gt; instance is destroyed, and at the
caller — &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;program1&lt;/code&gt; — &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Object::~Object&lt;/code&gt; has to
be implicitly called again to destroy the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Object&lt;/code&gt; within the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::optional&amp;lt;Object&amp;gt;&lt;/code&gt;
instance.&lt;/p&gt;

&lt;p&gt;This situation can be improved if we tell &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::optional&amp;lt;Object&amp;gt;&lt;/code&gt; to
&lt;a href=&quot;https://eli.thegreenplace.net/2014/perfect-forwarding-and-universal-references-in-c/&quot;&gt;forward&lt;/a&gt; the arguments to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Object::Object&lt;/code&gt; so it can construct
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Object&lt;/code&gt; in the optional’s storage area right away without a temporary&lt;/p&gt;

&lt;figure class=&quot;code&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&quot;line-number&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;3&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;4&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;5&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;code class=&quot;diff&quot;&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;   if (s.empty()) {
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;     return std::nullopt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;   }
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;gd&quot;&gt;-  return return Object(s);&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;gi&quot;&gt;+  return std::optional&amp;lt;Object&amp;gt;(s);&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;

&lt;p&gt;With this change, the output of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;program1(&quot;Hello!&quot;)&lt;/code&gt; becomes&lt;/p&gt;

&lt;figure class=&quot;code&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&quot;line-number&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;3&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;code class=&quot;diff&quot;&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;constructed
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;Hello!
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;destroyed
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;

&lt;p&gt;Only one constructor invocation and one destructor invocation. A win!&lt;/p&gt;

&lt;p&gt;However, most functions returning a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::optional&amp;lt;T&amp;gt;&lt;/code&gt; are calling some function
that returns &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T&lt;/code&gt; in the code path that instantiates and returns the optional.
That takes us back to the same situation of duplicated
constructor/destructor invocations.&lt;/p&gt;

&lt;figure class=&quot;code&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&quot;line-number&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;3&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;4&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;5&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;6&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;7&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;8&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;code class=&quot;cpp&quot;&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Object&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;makeObject&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;optional&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Object&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;maybe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;empty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nullopt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;makeObject&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;

&lt;p&gt;To improve this and keep the logic of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;makeObject&lt;/code&gt; separate from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;maybe&lt;/code&gt;, we
would have to change &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;makeObject&lt;/code&gt; to allow the &lt;a href=&quot;https://eli.thegreenplace.net/2014/perfect-forwarding-and-universal-references-in-c/&quot;&gt;perfect-forwarding&lt;/a&gt;
of the parameters from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;maybe&lt;/code&gt;, to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;makeObject&lt;/code&gt;, to
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::optional&amp;lt;Object&amp;gt;::optional&lt;/code&gt;, to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Object::Object&lt;/code&gt;!&lt;/p&gt;

&lt;p&gt;Another common way of writing these functions is by declaring a variable of type
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T&lt;/code&gt;, performing some operations on it, and then returning it wrapped in an
optional. This has the same problem we started with.&lt;/p&gt;

&lt;figure class=&quot;code&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&quot;line-number&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;3&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;4&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;5&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;6&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;7&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;8&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;code class=&quot;cpp&quot;&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;optional&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Object&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;maybe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;empty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nullopt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;n&quot;&gt;Object&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;n&quot;&gt;doSomething&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;

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

&lt;p&gt;These problems might not be a big deal in most situations, but if you insist on
returning non-PODs wrapped in a optional, make sure that:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;The wrapped type should be cheaply movable, otherwise your program might be
copying it on every function call due to innocent-looking code;&lt;/li&gt;
  &lt;li&gt;Define your destructors outside the class declaration so they don’t get
inlined by the compiler in both functions — the caller and the callee that
returns the optional — to avoid binary size increase.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The specific situation in which I’ve seen this really affect binary size and
possibly performance is when functions are written to return instances of
classes generated by the &lt;a href=&quot;https://developers.google.com/protocol-buffers/docs/reference/cpp-generated&quot;&gt;Google Protocol Buffers&lt;/a&gt; compiler.&lt;/p&gt;

&lt;p&gt;Google Protocol Buffers for C++ was designed before C++11 (i.e. before
move-semantics was added to the language). Its APIs and generated code
are designed to make it possible to use the classes without ever invoking copy
constructors. It’s a good API.&lt;/p&gt;

&lt;p&gt;If you never invoke a function, it doesn’t need to be in the compiled binary.
You can notice a sudden increase in the binary size of your program when a
single call to a big function is added to the codebase. Returning an
optional of a Protocol Buffers object is enough to instantiate a lot of
code that could otherwise never be needed.&lt;/p&gt;

&lt;p&gt;Let’s take a look at the generated code based on a Protocol Buffers message
called &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Date&lt;/code&gt;&lt;/p&gt;

&lt;figure class=&quot;code&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&quot;line-number&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;3&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;4&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;5&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;6&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;7&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;8&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;9&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;10&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;11&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;12&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;13&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;14&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;15&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;16&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;17&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;18&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;19&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;20&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;21&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;22&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;23&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;24&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;code class=&quot;cpp&quot;&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Date&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;PROTOBUF_FINAL&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PROTOBUF_NAMESPACE_ID&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Message&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt; &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;kr&quot;&gt;inline&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Date&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Date&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;nullptr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{}&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;k&quot;&gt;virtual&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;~&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Date&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;n&quot;&gt;Date&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Date&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;n&quot;&gt;Date&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Date&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;noexcept&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;    &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Date&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;    &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;move&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;kr&quot;&gt;inline&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Date&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;operator&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Date&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;    &lt;span class=&quot;n&quot;&gt;CopyFrom&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;kr&quot;&gt;inline&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Date&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;operator&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Date&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;noexcept&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GetArena&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GetArena&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;      &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;InternalSwap&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;      &lt;span class=&quot;n&quot;&gt;CopyFrom&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;

&lt;p&gt;The move-constructor (by calling &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;operator=(Date &amp;amp;&amp;amp;)&lt;/code&gt;) can potentially call
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;InternalSwap&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CopyFrom&lt;/code&gt;. The latter is called when the objects can’t be
swapped because they are allocated in different arenas and have to be to be
copied instead. By using the move-constructor of this object, both the moving
(swapping) and copying functions are instantiated in the binary. This explains
why returning the optional of a Protocol Buffers class increases the binary size
of a program that, before doing that, didn’t have a need for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;InternalSwap&lt;/code&gt; and
the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CopyFrom&lt;/code&gt; function.&lt;/p&gt;

&lt;h2 id=&quot;recommendation&quot;&gt;Recommendation&lt;/h2&gt;

&lt;p&gt;By adopting a more C-like way of initializing structures, the unnecessary use of
move-constructors and extraneous destructor calls can be avoided. This pattern
fits nicely with the code generated by Protocol Buffers.&lt;/p&gt;

&lt;p&gt;Let’s add a new member function to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Object&lt;/code&gt; class&lt;/p&gt;

&lt;figure class=&quot;code&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&quot;line-number&quot;&gt;1&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;code class=&quot;cpp&quot;&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_s&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;

&lt;p&gt;and write an alternative to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;program1&lt;/code&gt; — &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;program2&lt;/code&gt;&lt;/p&gt;

&lt;figure class=&quot;code&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&quot;line-number&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;3&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;4&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;5&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;6&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;7&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;8&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;9&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;10&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;11&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;12&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;13&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;14&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;15&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;16&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;code class=&quot;cpp&quot;&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nodiscard&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]]&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;maybe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Object&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;out_object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;empty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;n&quot;&gt;out_object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;program2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;n&quot;&gt;Object&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;maybe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;    &lt;span class=&quot;n&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dump&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;    &lt;span class=&quot;n&quot;&gt;puts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;&amp;lt;empty&amp;gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;

&lt;p&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;maybe&lt;/code&gt; function was rewritten to take an output parameter and return a
boolean. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;out_object&lt;/code&gt; is changed in-place and doesn’t have to be
moved into an optional and then destroyed within &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;maybe&lt;/code&gt;. As expected,
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;program2(&quot;Hello!&quot;)&lt;/code&gt; generates a cleaner output&lt;/p&gt;

&lt;figure class=&quot;code&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&quot;line-number&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;3&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;code class=&quot;cpp&quot;&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;default&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;constructed&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;n&quot;&gt;Hello&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;n&quot;&gt;destroyed&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;program2&lt;/code&gt; does not have to ever call the move-constructor, so it can be
discarded by the linker and the destructor is called only once. If the
destructor was inlined, it would be inlined once in the program, not twice.&lt;/p&gt;

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

&lt;p&gt;Optionals are far from zero-cost abstractions in C++ and if this cost matters to
you, taking output parameters and returning non-discardable booleans is an
advantageous alternative solution to a function returning &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::optional&amp;lt;T&amp;gt;&lt;/code&gt;
when objects of type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T&lt;/code&gt; are expensive&lt;sup id=&quot;fnref:expensive&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:expensive&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;2&lt;/a&gt;&lt;/sup&gt; to move and/or destroy.&lt;/p&gt;

&lt;div class=&quot;footnotes&quot; role=&quot;doc-endnotes&quot;&gt;
  &lt;ol&gt;
    &lt;li id=&quot;fn:flowtyping&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;Languages like TypeScript can statically determine if an object is set &lt;a href=&quot;https://en.wikipedia.org/wiki/Flow-sensitive_typing&quot;&gt;based on its position in the control flow of the program.&lt;/a&gt; &lt;a href=&quot;#fnref:flowtyping&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:expensive&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;In the sense of run-time and size of the code in the binary. &lt;a href=&quot;#fnref:expensive&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
  &lt;/ol&gt;
&lt;/div&gt;
</description>
        <pubDate>Sun, 19 Sep 2021 11:31:34 -0300</pubDate>
        <link>https://felipe.rs/2021/09/19/std-optional-and-non-pod-types-in-cpp/</link>
        <guid isPermaLink="true">https://felipe.rs/2021/09/19/std-optional-and-non-pod-types-in-cpp/</guid>
        
        
        <category>C++</category>
        
        <category>In English</category>
        
        <category>Systems Programming</category>
        
      </item>
      
    
      
      <item>
        <title>Demystifying JOIN Algorithms</title>
        <description>&lt;p&gt;Joins are an important class of operations in databases and data pipelines. There
are &lt;a href=&quot;https://blog.codinghorror.com/a-visual-explanation-of-sql-joins/&quot; title=&quot;A Visual Explanation of SQL Joins&quot;&gt;many kinds of joins&lt;/a&gt; in SQL databases, but this post will
focus only on the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;INNER JOIN&lt;/code&gt;. &lt;sup id=&quot;fnref:innerjoin&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:innerjoin&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;1&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;

&lt;p&gt;It is possible to make the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JOIN&lt;/code&gt; between two large tables very efficient if the
result is going to be small. That being a common scenario in practice, makes
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JOIN&lt;/code&gt;s an interesting topic of research.&lt;/p&gt;

&lt;p&gt;A naive &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JOIN&lt;/code&gt; algorithm can be very slow because &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JOIN&lt;/code&gt;s are essentially nested
loops.&lt;/p&gt;

&lt;figure class=&quot;code&quot;&gt;&lt;figcaption&gt;&lt;span&gt;NestedLoop JOIN based on sequential scans&lt;/span&gt;&lt;/figcaption&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&quot;line-number&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;3&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;4&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;code class=&quot;python&quot;&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;row_i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;table_a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;row_j&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;table_b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;row_i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;row_j&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;      &lt;span class=&quot;n&quot;&gt;emit_join_result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;row_i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;row_j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;

&lt;p&gt;A better alternative to the algorithm above is the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HashJoin&lt;/code&gt;. In the first step
of a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HashJoin&lt;/code&gt;, a &lt;a href=&quot;https://en.wikipedia.org/wiki/Hash_table&quot;&gt;hash table&lt;/a&gt; of one
of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JOIN&lt;/code&gt; operands — &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;table_a&lt;/code&gt; — is built. Then a cheap lookup can
replace the expensive innermost loop doing a sequential scan on the whole
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;table_b&lt;/code&gt; for each row of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;table_a&lt;/code&gt;.&lt;/p&gt;

&lt;figure class=&quot;code&quot;&gt;&lt;figcaption&gt;&lt;span&gt;HashJoin&lt;/span&gt;&lt;/figcaption&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&quot;line-number&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;3&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;4&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;5&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;6&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;7&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;code class=&quot;python&quot;&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;row_i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;table_a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;n&quot;&gt;hash_table&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;row_i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;row_i&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;row_j&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;table_b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;n&quot;&gt;row_i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hash_table&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;row_j&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;row_i&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;AND&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;row_i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;row_j&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;    &lt;span class=&quot;n&quot;&gt;emit_join_result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;row_i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;row_j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;

&lt;p&gt;This code snippet above is very common in hand-written backend and UI code
stitching data together to show something useful to users. So it should be no
surprise that this is what databases commonly do to solve the same problem.&lt;/p&gt;

&lt;h2 id=&quot;a-practical-example&quot;&gt;A Practical Example&lt;/h2&gt;

&lt;p&gt;Let’s look at an e-commerce database containing two tables: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;product&lt;/code&gt; and
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cart_item&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;product&lt;/code&gt; table contains all the product the store sells.&lt;/p&gt;

&lt;figure class=&quot;code&quot;&gt;&lt;figcaption&gt;&lt;span&gt;`product` table&lt;/span&gt;&lt;/figcaption&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&quot;line-number&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;3&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;4&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;5&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;6&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;7&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;8&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;9&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;10&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;11&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;12&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;13&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;14&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;code class=&quot;c&quot;&gt;&lt;span class=&quot;line&quot;&gt; &lt;span class=&quot;n&quot;&gt;id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;o&quot;&gt;----+-------------&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;   &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt; &lt;span class=&quot;mi&quot;&gt;34&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Iogurt&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt; &lt;span class=&quot;mi&quot;&gt;35&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Tea&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt; &lt;span class=&quot;mi&quot;&gt;36&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Sugar&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt; &lt;span class=&quot;mi&quot;&gt;37&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Chocolate&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt; &lt;span class=&quot;mi&quot;&gt;38&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Butter&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt; &lt;span class=&quot;mi&quot;&gt;39&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Detergent&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt; &lt;span class=&quot;mi&quot;&gt;40&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Panettone&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt; &lt;span class=&quot;mi&quot;&gt;41&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Coffee&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt; &lt;span class=&quot;mi&quot;&gt;42&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Shampoo&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt; &lt;span class=&quot;mi&quot;&gt;43&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Toothpaste&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;   &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;

&lt;p&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cart_item&lt;/code&gt; contains products added to the cart of all the users.&lt;/p&gt;

&lt;figure class=&quot;code&quot;&gt;&lt;figcaption&gt;&lt;span&gt;`cart_item` table&lt;/span&gt;&lt;/figcaption&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&quot;line-number&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;3&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;4&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;5&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;6&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;7&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;8&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;9&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;10&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;11&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;12&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;13&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;14&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;15&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;16&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;17&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;code class=&quot;c&quot;&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;  &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;user_id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;product_id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;quantity_added&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;o&quot;&gt;------+---------+------------+----------------&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt; &lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;     &lt;span class=&quot;mi&quot;&gt;776&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;         &lt;span class=&quot;mi&quot;&gt;34&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;              &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt; &lt;span class=&quot;mi&quot;&gt;1001&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;     &lt;span class=&quot;mi&quot;&gt;494&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;         &lt;span class=&quot;mi&quot;&gt;35&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;              &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt; &lt;span class=&quot;mi&quot;&gt;1002&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;     &lt;span class=&quot;mi&quot;&gt;494&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;         &lt;span class=&quot;mi&quot;&gt;34&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;              &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt; &lt;span class=&quot;mi&quot;&gt;1003&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;     &lt;span class=&quot;mi&quot;&gt;494&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;         &lt;span class=&quot;mi&quot;&gt;36&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;              &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt; &lt;span class=&quot;mi&quot;&gt;1004&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;     &lt;span class=&quot;mi&quot;&gt;494&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;         &lt;span class=&quot;mi&quot;&gt;37&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;              &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt; &lt;span class=&quot;mi&quot;&gt;1005&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;     &lt;span class=&quot;mi&quot;&gt;494&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;         &lt;span class=&quot;mi&quot;&gt;38&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;              &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt; &lt;span class=&quot;mi&quot;&gt;1006&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;     &lt;span class=&quot;mi&quot;&gt;494&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;         &lt;span class=&quot;mi&quot;&gt;39&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;              &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt; &lt;span class=&quot;mi&quot;&gt;1007&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;     &lt;span class=&quot;mi&quot;&gt;494&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;         &lt;span class=&quot;mi&quot;&gt;40&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;              &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt; &lt;span class=&quot;mi&quot;&gt;1008&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;     &lt;span class=&quot;mi&quot;&gt;494&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;         &lt;span class=&quot;mi&quot;&gt;41&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;              &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt; &lt;span class=&quot;mi&quot;&gt;1009&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;     &lt;span class=&quot;mi&quot;&gt;494&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;         &lt;span class=&quot;mi&quot;&gt;42&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;              &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt; &lt;span class=&quot;mi&quot;&gt;1010&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;     &lt;span class=&quot;mi&quot;&gt;574&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;         &lt;span class=&quot;mi&quot;&gt;34&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;              &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt; &lt;span class=&quot;mi&quot;&gt;1011&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;     &lt;span class=&quot;mi&quot;&gt;574&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;         &lt;span class=&quot;mi&quot;&gt;36&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;              &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt; &lt;span class=&quot;mi&quot;&gt;1012&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;     &lt;span class=&quot;mi&quot;&gt;574&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;         &lt;span class=&quot;mi&quot;&gt;37&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;              &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt; &lt;span class=&quot;mi&quot;&gt;1013&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;     &lt;span class=&quot;mi&quot;&gt;574&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;         &lt;span class=&quot;mi&quot;&gt;43&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;              &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;                    &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;

&lt;p&gt;An useful view of the cart of a user will need more information about the product
other than just the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;id&lt;/code&gt; (e.g. the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;name&lt;/code&gt; of the product). That is when a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JOIN&lt;/code&gt;
becomes necessary.&lt;/p&gt;

&lt;figure class=&quot;code&quot;&gt;&lt;figcaption&gt;&lt;span&gt;JOIN example&lt;/span&gt;&lt;/figcaption&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&quot;line-number&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;3&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;4&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;5&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;6&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;code class=&quot;sql&quot;&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;k&quot;&gt;SELECT&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cart_item&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;user_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;       &lt;span class=&quot;n&quot;&gt;cart_item&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;product_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;       &lt;span class=&quot;n&quot;&gt;cart_item&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;quantity_added&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;       &lt;span class=&quot;n&quot;&gt;product&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;k&quot;&gt;FROM&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cart_item&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;k&quot;&gt;JOIN&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;product&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;ON&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cart_item&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;product_id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;product&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;

&lt;figure class=&quot;code&quot;&gt;&lt;figcaption&gt;&lt;span&gt;Results of the JOIN example query&lt;/span&gt;&lt;/figcaption&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&quot;line-number&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;3&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;4&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;5&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;6&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;7&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;8&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;9&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;10&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;11&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;12&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;13&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;14&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;15&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;16&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;code class=&quot;c&quot;&gt;&lt;span class=&quot;line&quot;&gt; &lt;span class=&quot;n&quot;&gt;user_id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;product_id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;quantity_added&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;   &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;o&quot;&gt;---------+------------+----------------+------------&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;     &lt;span class=&quot;mi&quot;&gt;776&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;         &lt;span class=&quot;mi&quot;&gt;34&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;              &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Iogurt&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;     &lt;span class=&quot;mi&quot;&gt;574&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;         &lt;span class=&quot;mi&quot;&gt;34&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;              &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Iogurt&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;     &lt;span class=&quot;mi&quot;&gt;494&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;         &lt;span class=&quot;mi&quot;&gt;35&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;              &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Tea&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;     &lt;span class=&quot;mi&quot;&gt;494&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;         &lt;span class=&quot;mi&quot;&gt;36&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;              &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Sugar&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;     &lt;span class=&quot;mi&quot;&gt;574&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;         &lt;span class=&quot;mi&quot;&gt;36&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;              &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Sugar&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;     &lt;span class=&quot;mi&quot;&gt;494&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;         &lt;span class=&quot;mi&quot;&gt;37&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;              &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Chocolate&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;     &lt;span class=&quot;mi&quot;&gt;574&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;         &lt;span class=&quot;mi&quot;&gt;37&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;              &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Chocolate&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;     &lt;span class=&quot;mi&quot;&gt;494&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;         &lt;span class=&quot;mi&quot;&gt;38&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;              &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Butter&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;     &lt;span class=&quot;mi&quot;&gt;494&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;         &lt;span class=&quot;mi&quot;&gt;39&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;              &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Detergent&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;     &lt;span class=&quot;mi&quot;&gt;494&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;         &lt;span class=&quot;mi&quot;&gt;40&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;              &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Panettone&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;     &lt;span class=&quot;mi&quot;&gt;494&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;         &lt;span class=&quot;mi&quot;&gt;41&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;              &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Coffee&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;     &lt;span class=&quot;mi&quot;&gt;494&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;         &lt;span class=&quot;mi&quot;&gt;42&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;              &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Shampoo&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;     &lt;span class=&quot;mi&quot;&gt;574&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;         &lt;span class=&quot;mi&quot;&gt;43&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;              &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Toothpaste&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;                     &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;

&lt;p&gt;PostgreSQL might use a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HashJoin&lt;/code&gt; to execute this query. The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EXPLAIN&lt;/code&gt; command
shows the query plan.&lt;/p&gt;

&lt;figure class=&quot;code&quot;&gt;&lt;figcaption&gt;&lt;span&gt;HashJoin query plan&lt;/span&gt;&lt;/figcaption&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&quot;line-number&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;3&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;4&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;5&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;code class=&quot;c&quot;&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;n&quot;&gt;Hash&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Join&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;n&quot;&gt;Hash&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;Cond&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;product&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cart_item&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;product_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;Seq&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Scan&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;on&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;product&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;Hash&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;        &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;Seq&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Scan&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;on&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cart_item&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;

&lt;p&gt;The sequential scan on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cart_item&lt;/code&gt; is necessary to build the hash table mapping
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cart_item.product_id&lt;/code&gt; to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cart_item&lt;/code&gt; record. That is the first &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;for&lt;/code&gt; loop
in the pseudo-code above. The second &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;for&lt;/code&gt; loop is the sequential scan on
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;product&lt;/code&gt; that queries the hash table with each &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;product.id&lt;/code&gt; it finds.&lt;/p&gt;

&lt;p&gt;If you mess with the PostgreSQL query planner really hard &lt;sup id=&quot;fnref:planner&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:planner&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;2&lt;/a&gt;&lt;/sup&gt;, you can
convince it to use the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NestedLoop&lt;/code&gt; join based on nested sequential scans of
both tables.&lt;/p&gt;

&lt;figure class=&quot;code&quot;&gt;&lt;figcaption&gt;&lt;span&gt;NestedLoop query plan based on sequential scans&lt;/span&gt;&lt;/figcaption&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&quot;line-number&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;3&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;4&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;code class=&quot;c&quot;&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;n&quot;&gt;Nested&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Loop&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;n&quot;&gt;Join&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;Filter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cart_item&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;product_id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;product&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;Seq&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Scan&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;on&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;product&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;Seq&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Scan&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;on&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;open_cart_item&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cart_item&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;

&lt;p&gt;And this is the most naive way to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JOIN&lt;/code&gt; two tables as shown in the first code
snippet — two nested loops doing sequential scans.&lt;/p&gt;

&lt;h3 id=&quot;indexing&quot;&gt;Indexing&lt;/h3&gt;

&lt;p&gt;The hash table built in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HashJoin&lt;/code&gt; is a form of indexing (&lt;em&gt;temporary index&lt;/em&gt;)
that speeds up the second &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;for&lt;/code&gt; loop. Hash tables allow efficient random queries
(i.e. keys used in a sequence of lookups do not obbey any particular order) for
an specific key value.&lt;/p&gt;

&lt;p&gt;An issue with the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HashJoin&lt;/code&gt; query plan above is that the hash table is built
at the time of the query. Indexing in the context of databases more commonly
means that an index is already built when a query comes in (&lt;em&gt;persistent index&lt;/em&gt;).
This requires knowing which index is more suitable for the queries that will
come later.  Creating all possible indexes is not a good strategy because they
would be many and indexes are not free. When the table changes, indexes have to
be updated to reflect the changes. So each index adds an extra cost to writes
and deletes.&lt;/p&gt;

&lt;p&gt;Let’s disable &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HashJoin&lt;/code&gt; &lt;sup id=&quot;fnref:disablehashjoin&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:disablehashjoin&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;3&lt;/a&gt;&lt;/sup&gt; and see how our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JOIN&lt;/code&gt;
query is planned.&lt;/p&gt;

&lt;figure class=&quot;code&quot;&gt;&lt;figcaption&gt;&lt;span&gt;NestedLoop query plan using a single index&lt;/span&gt;&lt;/figcaption&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&quot;line-number&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;3&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;4&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;code class=&quot;c&quot;&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;n&quot;&gt;Nested&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Loop&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;Seq&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Scan&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;on&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cart_item&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;Index&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Scan&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;product_pkey1&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;on&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;product&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;        &lt;span class=&quot;n&quot;&gt;Index&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;Cond&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cart_item&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;product_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;

&lt;p&gt;This query plan execution is similar to the second for loop in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HashJoin&lt;/code&gt;
algorithm, but instead of using a temporary hash table that needs to be built at
the time of the query, it uses the readily available &lt;a href=&quot;https://en.wikipedia.org/wiki/Primary_key&quot;&gt;primary
key&lt;/a&gt; index of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;product&lt;/code&gt; table on
the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;id&lt;/code&gt; column.&lt;/p&gt;

&lt;figure class=&quot;code&quot;&gt;&lt;figcaption&gt;&lt;span&gt;Pseudo-code of the NestedLoop query plan&lt;/span&gt;&lt;/figcaption&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&quot;line-number&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;3&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;4&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;code class=&quot;python&quot;&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cart_item_j&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cart_item&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;n&quot;&gt;product_i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;product_pkey1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lookup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cart_item_j&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;product_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;product_i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cart_item&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;product_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;    &lt;span class=&quot;n&quot;&gt;emit_join_result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;product_i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cart_item_j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;

&lt;h3 id=&quot;indexing-with-b-trees&quot;&gt;Indexing with B-Trees&lt;/h3&gt;

&lt;p&gt;Classical relational databases were designed at a time when RAM was expensive
and small. A lot of effort went into making sure that queries could be performed
without bringing too much data into memory. Optimizing the disk access patterns
is crucial for good performance of the algorithms operating on data stored in
disks. Unlike main memory, disk are very bad at serving random access since the
cost of seeking to the right position is considerable. &lt;sup id=&quot;fnref:numbers&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:numbers&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;4&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;

&lt;p&gt;Sequentially scanning the randomly sorted &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;product_id&lt;/code&gt;s in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cart_item&lt;/code&gt;
table &lt;sup id=&quot;fnref:randomitems&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:randomitems&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;5&lt;/a&gt;&lt;/sup&gt; and querying the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;product_pkey1&lt;/code&gt; index stored in disk every
time is not very good because it might require the disk to seek to random
locations far too often.&lt;/p&gt;

&lt;p&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;product_pkey1&lt;/code&gt; index stored in disk is not a hash table, but a B-Tree.
B-Trees (and other types of ordered indexes) allow more interesting queries than
the specific key lookup provided by hash tables. In addition to key lookups,
B-trees allow range queries.&lt;/p&gt;

&lt;figure class=&quot;code&quot;&gt;&lt;figcaption&gt;&lt;span&gt;Queries a B-Tree can efficiently resolve&lt;/span&gt;&lt;/figcaption&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&quot;line-number&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;3&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;4&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;5&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;6&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;7&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;8&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;9&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;10&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;code class=&quot;c&quot;&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;n&quot;&gt;Specific&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;key&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lookup&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;n&quot;&gt;key&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;?&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;queries&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;n&quot;&gt;key&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;  &lt;span class=&quot;o&quot;&gt;?&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;n&quot;&gt;key&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;?&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;n&quot;&gt;key&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;?&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;n&quot;&gt;key&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;  &lt;span class=&quot;o&quot;&gt;?&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;

&lt;p&gt;B-Trees shine when a range of keys is read sequentially. This access pattern
minimizes the number of disk seeks and number of page loads into memory (storage
blocks containing sorted keys).&lt;/p&gt;

&lt;p&gt;Let’s create a B-Tree index for the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;product_id&lt;/code&gt; column in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cart_item&lt;/code&gt; table
and see what happens to the query plan. &lt;sup id=&quot;fnref:disablesort&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:disablesort&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;6&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;

&lt;figure class=&quot;code&quot;&gt;&lt;figcaption&gt;&lt;span&gt;NestedLoop query plan using two indexes&lt;/span&gt;&lt;/figcaption&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&quot;line-number&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;3&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;4&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;code class=&quot;c&quot;&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;n&quot;&gt;Nested&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Loop&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;Index&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Scan&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cart_product_id_idx&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;on&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cart_item&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;Index&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Scan&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;product_pkey1&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;on&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;product&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;        &lt;span class=&quot;n&quot;&gt;Index&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;Cond&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cart_item&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;product_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;

&lt;p&gt;Now the two indexes are being used. This is very good, because the scan for
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;product_id&lt;/code&gt; on the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cart_product_id_idx&lt;/code&gt; index will not be in the random order
they appear in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cart_item&lt;/code&gt; table. The lookups for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;product&lt;/code&gt; by the product
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;id&lt;/code&gt; will happen in the order of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;product_pkey1&lt;/code&gt; index. This will maximize
the use of the page cache. Entry &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;43&lt;/code&gt; of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;product_pkey1&lt;/code&gt; B-Tree is very
likely to be in the same page of the entry &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;42&lt;/code&gt; and so on.  This locality is
what guarantees the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Nested Loop&lt;/code&gt; will not be
&lt;a href=&quot;https://en.wikipedia.org/wiki/Time_complexity#Table_of_common_time_complexities&quot;&gt;quadratic&lt;/a&gt;
in time.  The cost of the innermost lookup will be amortized. Some will be
slower than others when a page has to be loaded, but on average they will take a
constant time. This is how the algorithm looks like in pseudo-code.&lt;/p&gt;

&lt;figure class=&quot;code&quot;&gt;&lt;figcaption&gt;&lt;span&gt;Pseudo-code of the NestedLoop query plan using two indexes&lt;/span&gt;&lt;/figcaption&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&quot;line-number&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;3&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;code class=&quot;python&quot;&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;product_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cart_item_row_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cart_product_id_idx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;n&quot;&gt;product_i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;product_pkey1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lookup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;product_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;n&quot;&gt;emit_join_result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cart_item&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cart_item_row_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;product_i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;

&lt;h2 id=&quot;joining-sorted-relations&quot;&gt;Joining Sorted Relations&lt;/h2&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MergeJoin&lt;/code&gt; can take advantage of the two &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JOIN&lt;/code&gt; operands (&lt;em&gt;relations&lt;/em&gt;) being
sorted to efficiently produce output. The pseudo-code for the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MergeJoin&lt;/code&gt; is not
as straightforward as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NestedLoop&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HashJoin&lt;/code&gt;, but is not very complicated.&lt;/p&gt;

&lt;figure class=&quot;code&quot;&gt;&lt;figcaption&gt;&lt;span&gt;Pseudo-code for a simple MergeJoin&lt;/span&gt;&lt;/figcaption&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&quot;line-number&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;3&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;4&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;5&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;6&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;7&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;8&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;9&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;10&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;11&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;12&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;13&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;code class=&quot;python&quot;&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;o&quot;&gt;//&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Cursors&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sequential&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;scanning&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;of&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;table_a&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;table_b&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;n&quot;&gt;cursor_a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;n&quot;&gt;cursor_b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cursor_a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hasNext&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cursor_b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hasNext&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cursor_a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cursor_b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;    &lt;span class=&quot;n&quot;&gt;cursor_a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;next&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;k&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cursor_a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cursor_b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;    &lt;span class=&quot;n&quot;&gt;emit_join_result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cursor_a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cursor_b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;    &lt;span class=&quot;n&quot;&gt;cursor_a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;    &lt;span class=&quot;n&quot;&gt;cursor_b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;    &lt;span class=&quot;n&quot;&gt;cursor_b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;

&lt;p&gt;On every loop iteration at least one record is consumed from one of the two
operands. This guarantees that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MergeJoin&lt;/code&gt; will take time proportional to the
sum of the size of the two sorted &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JOIN&lt;/code&gt; operands.&lt;/p&gt;

&lt;p&gt;Since B-Tree indexes are sorted, they lend themselves very well to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MergeJoin&lt;/code&gt;.
This is a possible query plan to our example query. The query planner is taking
advantage of two sorted indexes for the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MergeJoin&lt;/code&gt; – &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cart_product_id_idx&lt;/code&gt; and
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;product_pkey1&lt;/code&gt;.&lt;/p&gt;

&lt;figure class=&quot;code&quot;&gt;&lt;figcaption&gt;&lt;span&gt;Pseudo-code for a simple MergeJoin&lt;/span&gt;&lt;/figcaption&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&quot;line-number&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;3&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;4&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;code class=&quot;python&quot;&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;n&quot;&gt;Merge&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Join&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;n&quot;&gt;Merge&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Cond&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;Index&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Scan&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cart_product_id_idx&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;on&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;open_cart_item&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;Index&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Scan&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;product_pkey1&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;on&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;product&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;

&lt;p&gt;If a persistent index is not available (or data is small enough to be completely
in-memory), PostgreSQL might sort one or both operands to be able to run a
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MergeJoin&lt;/code&gt; on two sorted relations. This is possible query plan when the
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cart_product_id_idx&lt;/code&gt; index is not available.&lt;/p&gt;

&lt;figure class=&quot;code&quot;&gt;&lt;figcaption&gt;&lt;span&gt;Pseudo-code for a simple MergeJoin&lt;/span&gt;&lt;/figcaption&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&quot;line-number&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;3&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;4&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;5&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;6&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;code class=&quot;python&quot;&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;n&quot;&gt;Merge&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Join&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;n&quot;&gt;Merge&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Cond&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;Sort&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;        &lt;span class=&quot;n&quot;&gt;Sort&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;open_cart_item&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;product_id&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;        &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;Seq&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Scan&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;on&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;open_cart_item&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;Index&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Scan&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;product_pkey1&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;on&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;product&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;

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

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JOIN&lt;/code&gt;s are essentially nested loops.&lt;/li&gt;
  &lt;li&gt;Multiple techniques are used to improve the performance of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JOIN&lt;/code&gt;s.&lt;/li&gt;
  &lt;li&gt;When datasets are small or no appropriate index is being maintained, query
planners will likely decide to use memory to make queries run faster. The
query executor can build hash tables (or sort records in memory, or…) and
use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HashJoin&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MergeJoin&lt;/code&gt; to more efficiently join the two operands
according to the join-condition in the query.&lt;/li&gt;
  &lt;li&gt;Maintaining index data structures like B-Trees up to date allows &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Nested
Loop&lt;/code&gt; algorithm to run much faster even when the dataset doesn’t conveniently
fit in memory. Indexed &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JOIN&lt;/code&gt;s take time proportional to the size of the
result and are not affected by the size of the tables.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There’s much more about &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JOIN&lt;/code&gt;s and techniques to speed them up, but these are the
basics that will allow you to start thinking about how data layout and choice of
algorithms can greatly affect query performance.&lt;/p&gt;

&lt;p&gt;Understanding the three classical &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JOIN&lt;/code&gt; algorithms – &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NestedLoop&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HashJoin&lt;/code&gt;,
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MergeJoin&lt;/code&gt; – how they take advantage of different indexes and how they behave
when there is no index can give you a lot of insight on how databases run
queries.&lt;/p&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further Reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://dl.acm.org/citation.cfm?id=152611&quot;&gt;Query evaluation techniques for large databases&lt;/a&gt; by Goetz Graefe, 1993.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.nowpublishers.com/article/Details/DBS-028&quot;&gt;Modern B-Tree Techniques&lt;/a&gt; by Goetz Graefe, 2011.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://dl.acm.org/citation.cfm?id=2124913&quot;&gt;New algorithms for join and grouping operations&lt;/a&gt; by Goetz Graefe, 2012.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://www.redbook.io/&quot;&gt;Readings in Database Systems, 5th Edition&lt;/a&gt; by Peter Bailis, Joseph M. Hellerstein, and Michael Stonebraker, 2015.&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;div class=&quot;footnotes&quot; role=&quot;doc-endnotes&quot;&gt;
  &lt;ol&gt;
    &lt;li id=&quot;fn:innerjoin&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;INNER&lt;/code&gt; keyword is optional in SQL queries. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JOIN&lt;/code&gt; alone means &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;INNER JOIN&lt;/code&gt;. &lt;a href=&quot;#fnref:innerjoin&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:planner&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;Set &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;enable_material&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;enable_mergejoin&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;enable_bitmapscan&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;enable_indexscan&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;enable_hashjoin&lt;/code&gt; &lt;a href=&quot;https://www.postgresql.org/docs/10/runtime-config-query.html&quot;&gt;query planner options&lt;/a&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;off&lt;/code&gt;. &lt;a href=&quot;#fnref:planner&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:disablehashjoin&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HashJoin&lt;/code&gt; can be disabled with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;set enable_hashjoin = off;&lt;/code&gt; in PostgreSQL. By default, the query planner is smart enough to realize that our example table is so small that it is better to build a temporary in-memory hash table than to load the, possibly not cached, disk-persistent primary key index. So &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HashJoin&lt;/code&gt; is disabled to illustrate the behavior when tables do not fit in RAM. &lt;a href=&quot;#fnref:disablehashjoin&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:numbers&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;&lt;a href=&quot;https://people.eecs.berkeley.edu/~rcs/research/interactive_latency.html&quot;&gt;Latency Numbers Every Programmer Should Know&lt;/a&gt; &lt;a href=&quot;#fnref:numbers&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:randomitems&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;The rows in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cart_item&lt;/code&gt; table follow the order at which users added products to cart. &lt;a href=&quot;#fnref:randomitems&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:disablesort&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;To force the query planner to not use memory for everything, I had to also &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;set enable_sort = off;&lt;/code&gt;. This wouldn’t be necessary if the tables didn’t fit in RAM. &lt;a href=&quot;#fnref:disablesort&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
  &lt;/ol&gt;
&lt;/div&gt;
</description>
        <pubDate>Tue, 29 Jan 2019 03:10:53 -0300</pubDate>
        <link>https://felipe.rs/2019/01/29/demystifying-join-algorithms/</link>
        <guid isPermaLink="true">https://felipe.rs/2019/01/29/demystifying-join-algorithms/</guid>
        
        
        <category>Computer Science</category>
        
        <category>Databases</category>
        
        <category>In English</category>
        
        <category>Software Engineering</category>
        
        <category>Systems Programming</category>
        
      </item>
      
    
      
      <item>
        <title>Papers I've Read in 2017</title>
        <description>&lt;p&gt;In 2017 I decided to print some PDFs to read instead of just have them
in an ever-growing folder of Papers to Read. Here is the list of the ones I
managed to read this year.&lt;/p&gt;

&lt;h3 id=&quot;out-of-the-tar-pit--ben-moseley-peter-marks--2006&quot;&gt;&lt;a href=&quot;https://github.com/papers-we-love/papers-we-love/blob/master/design/out-of-the-tar-pit.pdf&quot;&gt;Out of the Tar Pit&lt;/a&gt; / Ben Moseley, Peter Marks / 2006&lt;/h3&gt;

&lt;p&gt;This is my favorite paper. It is a bit long (around 60 pages), but well
worth reading. It is very thorough in defining what software complexity is.
The authors make the distinction between essential and accidental complexity
(following Fred Brook’s ideas on the topic) and postulate that, to this day, most
complexity in software is still accidental. To make things more concrete, they
give an outline for a potential complexity-minimizing approach. The outline
might throw some readers off. Some reviewers criticize the paper focusing only
on this outline. I think that is a mistake. The given outline is one of
the many possible solutions and many applicable insights can be found in the
less concrete part of the paper.&lt;/p&gt;

&lt;p&gt;See &lt;a href=&quot;https://blog.acolyer.org/2015/03/20/out-of-the-tar-pit/&quot;&gt;The Morning Paper’s review of Out of the Tar Pit&lt;/a&gt;.&lt;/p&gt;

&lt;h3 id=&quot;some-thoughts-on-security-after-ten-years-of-qmail-10--daniel-j-bernstein--2007&quot;&gt;&lt;a href=&quot;https://cr.yp.to/qmail/qmailsec-20071101.pdf&quot;&gt;Some thoughts on security after ten years of qmail 1.0&lt;/a&gt; / &lt;a href=&quot;https://en.wikipedia.org/wiki/Daniel_J._Bernstein&quot;&gt;Daniel J. Bernstein&lt;/a&gt; / 2007&lt;/h3&gt;

&lt;p&gt;In this paper, &lt;a href=&quot;https://twitter.com/hashbreaker&quot;&gt;djb&lt;/a&gt;
describes his approaches to writing secure and bug-free software.&lt;/p&gt;

&lt;h3 id=&quot;kqueue-a-generic-and-scalable-event-notification-facility--jonathan-lemon--2001&quot;&gt;&lt;a href=&quot;https://people.freebsd.org/~jlemon/papers/kqueue.pdf&quot;&gt;Kqueue: A generic and scalable event notification facility&lt;/a&gt; / Jonathan Lemon / 2001&lt;/h3&gt;

&lt;p&gt;This paper describes the FreeBSD event notification facility: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kqueue&lt;/code&gt;.
The main application of such facility is in servers that rely on asynchronous
I/O to be able to handle many clients concurrently (think Java NIO, Netty,
Node.js, Nginx) without a thread/process per connection. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kqueue&lt;/code&gt; is very
elegant and can do much more than just asynchronous I/O. It is a pity that the
&lt;a href=&quot;http://man7.org/linux/man-pages/man7/epoll.7.html&quot;&gt;Linux equivalent&lt;/a&gt; is
&lt;a href=&quot;https://idea.popcount.org/2017-02-20-epoll-is-fundamentally-broken-12/&quot;&gt;much less nicer than &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kqueue&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;h3 id=&quot;real-world-concurrency--bryan-cantrill-jeff-bonwick--2008&quot;&gt;&lt;a href=&quot;http://queue.acm.org/detail.cfm?id=1454462&quot;&gt;Real-world Concurrency&lt;/a&gt; / &lt;a href=&quot;https://twitter.com/bcantrill&quot;&gt;Bryan Cantrill&lt;/a&gt;, Jeff Bonwick / 2008&lt;/h3&gt;

&lt;blockquote&gt;
  &lt;p&gt;Although we assert that less—much less—code needs to be parallel than some
might fear, it is nonetheless true that writing parallel code remains
something of a black art. We also therefore give specific implementation
techniques for developing a highly parallel system.  As such, this article is
somewhat dichotomous: we try both to argue that most code can (and should)
achieve concurrency without explicit parallelism, and at the same time to
elucidate techniques for those who must write explicitly parallel code.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3 id=&quot;solving-linear-arithmetic-constraints-for-user-interface-applications--alan-borning-kim-marriott-peter-stuckey-yi-xiao--1997&quot;&gt;&lt;a href=&quot;https://dl.acm.org/citation.cfm?id=263518&quot;&gt;Solving Linear Arithmetic Constraints for User Interface Applications&lt;/a&gt; / Alan Borning, Kim Marriott, Peter Stuckey, Yi Xiao / 1997&lt;/h3&gt;

&lt;p&gt;This paper describes an incremental algorithm to efficiently solve linear
constraints. This is the kind of algorithm behind systems like
&lt;a href=&quot;https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/AutolayoutPG/index.html&quot;&gt;AutoLayout&lt;/a&gt;
— a layout system that is widely used by iOS developers.&lt;/p&gt;

&lt;h3 id=&quot;software-engineering-at-google--fergus-henderson--2017&quot;&gt;&lt;a href=&quot;https://arxiv.org/abs/1702.01715&quot;&gt;Software Engineering at Google&lt;/a&gt; / Fergus Henderson / 2017&lt;/h3&gt;

&lt;p&gt;A description of Google’s software engineering practices.&lt;/p&gt;

&lt;h3 id=&quot;mosh-an-interactive-remote-shell-for-mobile-clients--keith-winstein-hari-balakrishnan--2012&quot;&gt;&lt;a href=&quot;https://mosh.org/mosh-paper.pdf&quot;&gt;Mosh: An Interactive Remote Shell for Mobile Clients&lt;/a&gt; / Keith Winstein, Hari Balakrishnan / 2012&lt;/h3&gt;

&lt;p&gt;This paper describes how &lt;a href=&quot;http://mosh.org&quot;&gt;Mosh&lt;/a&gt; works. Great read for anyone
building interactive networking software that needs to work in the presence of
intermittent connection and keep perceived latency to a minimum.&lt;/p&gt;

&lt;h3 id=&quot;on-the-criteria-to-be-used-in-decomposing-systems-into-modules--david-parnas--1972&quot;&gt;&lt;a href=&quot;https://dl.acm.org/citation.cfm?id=361623&quot;&gt;On the Criteria to be Used in Decomposing Systems into Modules&lt;/a&gt; / &lt;a href=&quot;https://en.wikipedia.org/wiki/David_Parnas&quot;&gt;David Parnas&lt;/a&gt; / 1972&lt;/h3&gt;

&lt;p&gt;A classical CS paper from the 70s. I think what it describes is known to
anyone developing software in 2017. It still is a valuable reading for the
historical value.&lt;/p&gt;

&lt;h3 id=&quot;spanner-truetime--the-cap-theorem--eric-brewer--2017&quot;&gt;&lt;a href=&quot;https://research.google.com/pubs/pub45855.html&quot;&gt;Spanner, TrueTime &amp;amp; The CAP Theorem&lt;/a&gt; / &lt;a href=&quot;https://en.wikipedia.org/wiki/Eric_Brewer_(scientist)&quot;&gt;Eric Brewer&lt;/a&gt; / 2017&lt;/h3&gt;

&lt;p&gt;This paper describes how &lt;a href=&quot;https://cloud.google.com/spanner/&quot;&gt;Spanner&lt;/a&gt; can exist
if the &lt;a href=&quot;https://en.wikipedia.org/wiki/CAP_theorem&quot;&gt;CAP theorem&lt;/a&gt; is still true.
Spanner is Google’s highly available global-scale distributed database that
provides strong consistency for all transactions.&lt;/p&gt;

&lt;h3 id=&quot;the-triumph-of-types-principia-mathematicas-impact-on-computer-science--robert-constable--2011&quot;&gt;&lt;a href=&quot;http://www.nuprl.org/documents/Constable/PrincipiaArticle.html&quot;&gt;The Triumph of Types: Principia Mathematica’s Impact on Computer Science&lt;/a&gt; / Robert Constable / 2011&lt;/h3&gt;

&lt;p&gt;I read this when I was writing the
&lt;a href=&quot;https://felipe.rs/2017/07/07/where-do-type-systems-come-from/&quot;&gt;Where do Type Systems Come From&lt;/a&gt;
post which I obviously recommend you to read.&lt;/p&gt;

&lt;h3 id=&quot;an-axiomatic-basis-for-computer-programming--tony-hoare--1969&quot;&gt;&lt;a href=&quot;https://dl.acm.org/citation.cfm?id=363259&quot;&gt;An Axiomatic Basis for Computer Programming&lt;/a&gt; / &lt;a href=&quot;https://en.wikipedia.org/wiki/Tony_Hoare&quot;&gt;Tony Hoare&lt;/a&gt; / 1969&lt;/h3&gt;

&lt;p&gt;Another classical CS paper. Reading it will give you an initial idea of what
software verification is about.&lt;/p&gt;

&lt;h3 id=&quot;state-the-problem-before-describing-the-solution--leslie-lamport--1978&quot;&gt;&lt;a href=&quot;https://lamport.azurewebsites.net/pubs/state-the-problem.pdf&quot;&gt;State the Problem Before Describing the Solution&lt;/a&gt; / Leslie Lamport / 1978&lt;/h3&gt;

&lt;p&gt;This one-page note was recommended by
&lt;a href=&quot;https://twitter.com/rygorous&quot;&gt;Fabian Giesen&lt;/a&gt; in his
&lt;a href=&quot;https://fgiesen.wordpress.com/category/papers/&quot;&gt;Papers I like&lt;/a&gt;
series.&lt;/p&gt;

&lt;h3 id=&quot;how-complex-systems-fail--richard-irvin-cook--2002&quot;&gt;&lt;a href=&quot;https://www.researchgate.net/publication/228797158_How_complex_systems_fail&quot;&gt;How Complex Systems Fail&lt;/a&gt; / Richard Irvin Cook / 2002&lt;/h3&gt;

&lt;p&gt;This paper is also from Fabian Giesen’s
&lt;a href=&quot;https://fgiesen.wordpress.com/category/papers/&quot;&gt;Papers I like&lt;/a&gt; series.&lt;/p&gt;

&lt;h3 id=&quot;time-clocks-and-the-ordering-of-events--leslie-lamport--1978&quot;&gt;&lt;a href=&quot;https://lamport.azurewebsites.net/pubs/time-clocks.pdf&quot;&gt;Time, Clocks and the Ordering of Events&lt;/a&gt; / Leslie Lamport / 1978&lt;/h3&gt;

&lt;p&gt;Another paper from &lt;a href=&quot;http://lamport.azurewebsites.net&quot;&gt;Leslie Lamport&lt;/a&gt;. Probably
his most cited paper.&lt;/p&gt;

&lt;p&gt;It describes how to build distributed state machines (AKA distributed systems)
given that only partial ordering of events is knowable in such systems (i.e. it
is not possible to know for every event, what events happened before it).&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;In a distributed system, it is sometimes impossible to
say that one of two events occurred first. The relation
“happened before” is therefore only a partial ordering
of the events in the system. We have found that problems
often arise because people are not fully aware of this fact
and its implications.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To this day, many systems are built as if it was possible to have complete
ordering of events in distributed systems. This causes many subtle bugs that
annoy users and puzzle programmers.&lt;/p&gt;

&lt;h3 id=&quot;holistic-configuration-management-at-facebook--chunqiang-cq-tang-thawan-kooburat-pradeep-venkat-akshay-chander-zhe-wen-aravind-narayanan-patrick-dowell-robert-karl--2015&quot;&gt;&lt;a href=&quot;https://research.fb.com/publications/holistic-configuration-management-at-facebook/&quot;&gt;Holistic Configuration Management at Facebook&lt;/a&gt; / Chunqiang (CQ) Tang, Thawan Kooburat, Pradeep Venkat, Akshay Chander, Zhe Wen, Aravind Narayanan, Patrick Dowell, Robert Karl / 2015&lt;/h3&gt;

&lt;p&gt;This paper describes how Facebook manages configuration (changing, testing,
deploying…) of its backend services and mobile clients. My interest in
reading this paper is in how they manage configuration of mobile clients in a
reliable, scalable and backwards-compatible way. That is described in part 4 —
Gatekeeper.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://blog.acolyer.org/2015/10/16/holistic-configuration-management-at-facebook/&quot;&gt;The Morning Paper also has a review of this paper.&lt;/a&gt;&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;If you like reading lists of papers people like with quick and short reviews,
check out &lt;a href=&quot;https://blog.acolyer.org/&quot;&gt;The Morning Paper&lt;/a&gt; by
&lt;a href=&quot;https://twitter.com/adriancolyer&quot;&gt;Adrian Colyer&lt;/a&gt; and
&lt;a href=&quot;https://twitter.com/rygorous&quot;&gt;Fabian Giesen’s&lt;/a&gt; excellent
&lt;a href=&quot;https://fgiesen.wordpress.com/category/papers/&quot;&gt;Papers I Like series&lt;/a&gt;.&lt;/p&gt;
</description>
        <pubDate>Sat, 30 Dec 2017 18:30:53 -0300</pubDate>
        <link>https://felipe.rs/2017/12/30/papers-ive-read-in-2017/</link>
        <guid isPermaLink="true">https://felipe.rs/2017/12/30/papers-ive-read-in-2017/</guid>
        
        
        <category>BSD</category>
        
        <category>Computer Science</category>
        
        <category>Distributed Systems</category>
        
        <category>Functional Programming</category>
        
        <category>In English</category>
        
        <category>Papers</category>
        
        <category>Software Engineering</category>
        
        <category>Systems Programming</category>
        
        <category>Unix</category>
        
      </item>
      
    
      
      <item>
        <title>Monorepo First, Submodules Later</title>
        <description>&lt;!-- My story --&gt;

&lt;p&gt;When starting a new side-project I always wonder if parts of the codebase can
be shared as an open-source project. A library extracted from the project might
be of interest to other developers and it’s nice to have good stuff on your
Github profile.&lt;/p&gt;

&lt;!-- Problem --&gt;

&lt;p&gt;That possibility may compel you to spend a lot of time thinking about the
repository structure. You may create a &lt;em&gt;git submodule&lt;/em&gt; for every library that
doesn’t exist yet but might be useful to you in other projects or to other
people and you don’t want to lose the git history of that code. Trying to
predict what is shareable before anything is done is almost impossible.
When starting a project we have to try to do the minimum amount of metawork
possible.  There’s a solution to this problem that won’t require that much of
upfront planning.&lt;/p&gt;

&lt;!-- Solution --&gt;

&lt;center&gt;
&lt;blockquote class=&quot;twitter-tweet&quot; data-conversation=&quot;none&quot; data-lang=&quot;en&quot;&gt;&lt;p lang=&quot;en&quot; dir=&quot;ltr&quot;&gt;I start everything as monorepo and use git filter-branch to extract folders as separate repos if necessary (e.g. I want to extract a lib)&lt;/p&gt;&amp;mdash; Felipe O. Carvalho (@_Felipe) &lt;a href=&quot;https://twitter.com/_Felipe/status/884326394498568193&quot;&gt;July 10, 2017&lt;/a&gt;&lt;/blockquote&gt;
&lt;script async=&quot;&quot; src=&quot;//platform.twitter.com/widgets.js&quot; charset=&quot;utf-8&quot;&gt;&lt;/script&gt;
&lt;/center&gt;

&lt;p&gt;Don’t introduce any first-party git submodule in the beginning. Make sure things
are cleanly separated in directories and get started.&lt;/p&gt;

&lt;figure class=&quot;code&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&quot;line-number&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;3&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;4&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;5&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;6&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;7&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;8&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;9&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;10&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;11&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;12&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;13&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;14&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;15&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;16&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;17&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;18&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;19&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;20&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;21&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;22&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;23&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;24&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;25&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;26&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;27&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;28&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;29&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;30&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;31&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;32&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;33&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;code class=&quot;&quot;&gt;&lt;span class=&quot;line&quot;&gt;[...]
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;* 493fcbc HAMT: Clone and operator=
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;* a33bf67 HAMT: Add destructor for the HAMT
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;* 9604360 HAMT: Start passing entries around instead of key and values
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;* 578fcbf HAMT: Move BitmatTrie and Node out of the HAMT class
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;* c8a6e1b HAMT: Start making it look like std::unordered_map
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;* 591df2a HAMT: Use KeyEqual to compare keys
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;* 16d3bb0 HAMT: Little pedantic tweaks
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;* fe8bec4 HAMT: Get rid of the fancy allocators
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;* 9295225 HAMT: Use std::{pair,hash} and move allocators out of the HAMT class
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;* df1a923 HAMT: Simplify alloc size calculation logic
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;* 07520de HAMT: Collect Fibonacci statistics correctly
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;* 9b3e627 HAMT: Make insert return the entry node pointer
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;* 960e5db HAMT: Rename some things
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;* 7d3993e HAMT: Make some stuff private
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;* bbde709 HAMT: Super wasteful allocation
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;* 429f897 HAMT: Fibonacci allocation and free lists
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;* 3799eca HAMT: Basic implementation
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;* 9777aa4 chronos: Fix lint errors in chronos.cpp
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;* e8d15f4 sqlite: Fix lint errors
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;* 4b796be chronos: Fix indentation
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;* ad7902f chronos: Use tuple for queries using the SQLite wrapper
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;* d04407e chronos: Add projects to the database through the UI
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;* 575ad64 chronos: Prototype with User and projects on the left sidebar
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;* 6bf5612 sqlite: Initial commit to the SQLite wrapper
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;* c67fe58 foc: SmallVector from LLVM (and tests!)
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;* fbecc32 cpplint.py
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;* 0750734 chronos: SQLite database schema
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;* 29eb494 sqlite: SQLite amalgamation source files
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;* 92c376a Copy examples/opengl3_example to chronos
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;* 3a369f6 Add googletest as a submodule
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;* bf1da9a Add imgui as a submodule
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;* bc3e92e Initial commit: .gitignore&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;

&lt;p&gt;Now you may notice that you have a directory with cool data structures that you
want to extract and publish as a library. You can create a new repository that
contains only that directory and commits that touch it using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git
filter-branch&lt;/code&gt;. Here’s how I did it for a project of mine.&lt;/p&gt;

&lt;p&gt;To start, I made a copy of the repository to a new directory called
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;foc_libraries&lt;/code&gt;.&lt;/p&gt;

&lt;figure class=&quot;code&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&quot;line-number&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;3&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;code class=&quot;sh&quot;&gt;&lt;span class=&quot;line&quot;&gt;~/code &lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;ls chronos/
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;CMakeLists.txt  chronos         cpplint.py      foc             sqlite_wrapper  vendor
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;~/code &lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;cp -r chronos foc_libraries
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;

&lt;p&gt;This new repository will be the repository of the library based on the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;foc&lt;/code&gt;
in &lt;em&gt;chronos.&lt;/em&gt; I use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git filter-branch&lt;/code&gt; on master to filter out anything
that’s not in the original &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;foc&lt;/code&gt; directory.&lt;/p&gt;

&lt;figure class=&quot;code&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&quot;line-number&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;3&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;code class=&quot;sh&quot;&gt;&lt;span class=&quot;line&quot;&gt;~/code/foc_libraries &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;master&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;git filter-branch --prune-empty --subdirectory-filter foc/ master
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;Rewrite 1299f7e3c0e2ab9f469a72e2880700a301386639 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;51/51&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;Ref &lt;span class=&quot;s1&quot;&gt;&amp;#39;refs/heads/master&amp;#39;&lt;/span&gt; was rewritten
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git filter-branch&lt;/code&gt; removed all the commits that weren’t related to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;foc&lt;/code&gt;
directory and the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;foc_libraries&lt;/code&gt; repository now contains only the files from
the original &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;foc&lt;/code&gt; directory in &lt;em&gt;chronos.&lt;/em&gt;&lt;/p&gt;

&lt;figure class=&quot;code&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&quot;line-number&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;3&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;4&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;5&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;6&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;7&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;8&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;9&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;10&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;code class=&quot;sh&quot;&gt;&lt;span class=&quot;line&quot;&gt;~/code/foc_libraries &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;master&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;git ls-files
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;allocator.h
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;array_ref.h
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;hash_array_mapped_trie.h
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;hash_array_mapped_trie_test.cpp
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;hash_array_mapped_trie_test_helpers.h
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;none.h
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;small_vector.h
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;small_vector_test.cpp
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;support.h
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;

&lt;p&gt;The commit log of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;foc_libraries&lt;/code&gt; doesn’t have any commit related to the
original application.&lt;/p&gt;

&lt;figure class=&quot;code&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&quot;line-number&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;3&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;4&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;5&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;6&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;7&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;8&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;9&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;10&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;11&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;12&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;13&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;14&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;15&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;16&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;code class=&quot;sh&quot;&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;...&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;* 8c46e22 - HAMT: Move BitmatTrie and Node out of the HAMT class
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;* e2fd8cd - HAMT: Start making it look like std::unordered_map
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;* 67896cc - HAMT: Use KeyEqual to compare keys
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;* 1237a32 - HAMT: Little pedantic tweaks
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;* &lt;span class=&quot;m&quot;&gt;1868375&lt;/span&gt; - HAMT: Get rid of the fancy allocators
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;* 8259c1d - HAMT: Use std::&lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;pair,hash&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt; and move allocators out of the HAMT class
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;* 90d5771 - HAMT: Simplify alloc size calculation logic
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;* ae69ffa - HAMT: Collect Fibonacci statistics correctly
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;* adf5651 - HAMT: Make insert &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; the entry node pointer
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;* 2e8dfaa - HAMT: Rename some things
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;* 1addd89 - HAMT: Make some stuff private
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;* 11156b8 - HAMT: Super wasteful allocation
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;* 85b2d73 - HAMT: Fibonacci allocation and free lists
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;* 9c6dc21 - HAMT: Basic implementation
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;* 9f0a6c5 - foc: SmallVector from LLVM &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;and tests!&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;

&lt;p&gt;Now I can upload &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;foc_libraries&lt;/code&gt; to
&lt;a href=&quot;https://github.com/felipecrv/foc_libraries&quot;&gt;Github&lt;/a&gt;.&lt;/p&gt;

&lt;figure class=&quot;code&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&quot;line-number&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;3&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;4&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;5&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;6&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;7&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;8&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;9&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;10&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;11&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;code class=&quot;sh&quot;&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;~/code/foc_libraries &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;master&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;$ git remote add origin git@github.com:felipecrv/foc_libraries.git
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;~/code/foc_libraries &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;master&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;$ git push -u origin master
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;Counting objects: &lt;span class=&quot;m&quot;&gt;186&lt;/span&gt;, &lt;span class=&quot;k&quot;&gt;done&lt;/span&gt;.
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;Delta compression using up to &lt;span class=&quot;m&quot;&gt;4&lt;/span&gt; threads.
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;Compressing objects: &lt;span class=&quot;m&quot;&gt;100&lt;/span&gt;% &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;86&lt;/span&gt;/86&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;, &lt;span class=&quot;k&quot;&gt;done&lt;/span&gt;.
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;Writing objects: &lt;span class=&quot;m&quot;&gt;100&lt;/span&gt;% &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;186&lt;/span&gt;/186&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;, &lt;span class=&quot;m&quot;&gt;67&lt;/span&gt;.43 KiB &lt;span class=&quot;p&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt; bytes/s, &lt;span class=&quot;k&quot;&gt;done&lt;/span&gt;.
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;Total &lt;span class=&quot;m&quot;&gt;186&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;delta &lt;span class=&quot;m&quot;&gt;124&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;, reused &lt;span class=&quot;m&quot;&gt;106&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;delta &lt;span class=&quot;m&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;remote: Resolving deltas: &lt;span class=&quot;m&quot;&gt;100&lt;/span&gt;% &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;124&lt;/span&gt;/124&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;, &lt;span class=&quot;k&quot;&gt;done&lt;/span&gt;.
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;To git@github.com:felipecrv/foc_libraries.git
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt; * &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;new branch&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;      master -&amp;gt; master
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;Branch master &lt;span class=&quot;nb&quot;&gt;set&lt;/span&gt; up to track remote branch master from origin
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;

&lt;p&gt;After publishing the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;foc_libraries&lt;/code&gt; repository I can go back to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;chronos&lt;/code&gt; and
replace the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;foc&lt;/code&gt; directory with a git submodule.&lt;/p&gt;

&lt;figure class=&quot;code&quot;&gt;&lt;figcaption&gt;&lt;span&gt;Remove the foc/ directory&lt;/span&gt;&lt;/figcaption&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&quot;line-number&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;3&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;4&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;5&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;6&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;7&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;8&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;9&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;10&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;code class=&quot;sh&quot;&gt;&lt;span class=&quot;line&quot;&gt;~/code/chronos &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;master&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;git rm -r foc
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;rm &lt;span class=&quot;s1&quot;&gt;&amp;#39;foc/allocator.h&amp;#39;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;rm &lt;span class=&quot;s1&quot;&gt;&amp;#39;foc/array_ref.h&amp;#39;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;rm &lt;span class=&quot;s1&quot;&gt;&amp;#39;foc/hash_array_mapped_trie.h&amp;#39;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;rm &lt;span class=&quot;s1&quot;&gt;&amp;#39;foc/hash_array_mapped_trie_test.cpp&amp;#39;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;rm &lt;span class=&quot;s1&quot;&gt;&amp;#39;foc/hash_array_mapped_trie_test_helpers.h&amp;#39;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;rm &lt;span class=&quot;s1&quot;&gt;&amp;#39;foc/none.h&amp;#39;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;rm &lt;span class=&quot;s1&quot;&gt;&amp;#39;foc/small_vector.h&amp;#39;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;rm &lt;span class=&quot;s1&quot;&gt;&amp;#39;foc/small_vector_test.cpp&amp;#39;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;rm &lt;span class=&quot;s1&quot;&gt;&amp;#39;foc/support.h&amp;#39;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;

&lt;figure class=&quot;code&quot;&gt;&lt;figcaption&gt;&lt;span&gt;Add the submodule as foc/&lt;/span&gt;&lt;/figcaption&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&quot;line-number&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;3&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;4&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;5&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;6&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;7&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;8&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;code class=&quot;sh&quot;&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;~/code/chronos &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;master&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;$ git submodule add https://github.com/felipecrv/foc_libraries.git foc
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;Cloning into &lt;span class=&quot;s1&quot;&gt;&amp;#39;foc&amp;#39;&lt;/span&gt;...
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;remote: Counting objects: &lt;span class=&quot;m&quot;&gt;186&lt;/span&gt;, &lt;span class=&quot;k&quot;&gt;done&lt;/span&gt;.
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;remote: Compressing objects: &lt;span class=&quot;m&quot;&gt;100&lt;/span&gt;% &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;62&lt;/span&gt;/62&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;, &lt;span class=&quot;k&quot;&gt;done&lt;/span&gt;.
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;remote: Total &lt;span class=&quot;m&quot;&gt;186&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;delta &lt;span class=&quot;m&quot;&gt;124&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;, reused &lt;span class=&quot;m&quot;&gt;186&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;delta &lt;span class=&quot;m&quot;&gt;124&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;, pack-reused &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;Receiving objects: &lt;span class=&quot;m&quot;&gt;100&lt;/span&gt;% &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;186&lt;/span&gt;/186&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;, &lt;span class=&quot;m&quot;&gt;67&lt;/span&gt;.43 KiB &lt;span class=&quot;p&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt; bytes/s, &lt;span class=&quot;k&quot;&gt;done&lt;/span&gt;.
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;Resolving deltas: &lt;span class=&quot;m&quot;&gt;100&lt;/span&gt;% &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;124&lt;/span&gt;/124&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;, &lt;span class=&quot;k&quot;&gt;done&lt;/span&gt;.
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;Checking connectivity... &lt;span class=&quot;k&quot;&gt;done&lt;/span&gt;.
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;

&lt;figure class=&quot;code&quot;&gt;&lt;figcaption&gt;&lt;span&gt;Review the changes&lt;/span&gt;&lt;/figcaption&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&quot;line-number&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;3&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;4&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;5&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;6&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;7&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;8&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;9&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;10&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;11&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;12&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;13&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;14&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;15&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;16&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;17&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;18&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;19&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;code class=&quot;sh&quot;&gt;&lt;span class=&quot;line&quot;&gt;~/code/chronos &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;master&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;git status
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;On branch master
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;Your branch and &lt;span class=&quot;s1&quot;&gt;&amp;#39;origin/master&amp;#39;&lt;/span&gt; have diverged,
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;and have &lt;span class=&quot;m&quot;&gt;28&lt;/span&gt; and &lt;span class=&quot;m&quot;&gt;1&lt;/span&gt; different commit each, respectively.
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;use &lt;span class=&quot;s2&quot;&gt;&amp;quot;git pull&amp;quot;&lt;/span&gt; to merge the remote branch into yours&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;Changes to be committed:
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;use &lt;span class=&quot;s2&quot;&gt;&amp;quot;git reset HEAD &amp;lt;file&amp;gt;...&amp;quot;&lt;/span&gt; to unstage&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;        modified:   .gitmodules
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;        new file:   foc
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;        deleted:    foc/allocator.h
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;        deleted:    foc/array_ref.h
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;        deleted:    foc/hash_array_mapped_trie.h
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;        deleted:    foc/hash_array_mapped_trie_test.cpp
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;        deleted:    foc/hash_array_mapped_trie_test_helpers.h
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;        deleted:    foc/none.h
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;        deleted:    foc/small_vector.h
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;        deleted:    foc/small_vector_test.cpp
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;        deleted:    foc/support.h
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;

&lt;figure class=&quot;code&quot;&gt;&lt;figcaption&gt;&lt;span&gt;Commit the changes&lt;/span&gt;&lt;/figcaption&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&quot;line-number&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;3&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;4&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;5&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;6&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;7&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;8&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;9&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;10&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;11&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;12&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;13&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;code class=&quot;sh&quot;&gt;&lt;span class=&quot;line&quot;&gt;~/code/chronos &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;master&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;git commit -m &lt;span class=&quot;s2&quot;&gt;&amp;quot;Extract foc/ as a git submodule&amp;quot;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;master b2d4ce0&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; Extract foc/ as a git submodule
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt; &lt;span class=&quot;m&quot;&gt;11&lt;/span&gt; files changed, &lt;span class=&quot;m&quot;&gt;4&lt;/span&gt; insertions&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;+&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;, &lt;span class=&quot;m&quot;&gt;3925&lt;/span&gt; deletions&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;-&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt; create mode &lt;span class=&quot;m&quot;&gt;160000&lt;/span&gt; foc
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt; delete mode &lt;span class=&quot;m&quot;&gt;100644&lt;/span&gt; foc/allocator.h
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt; delete mode &lt;span class=&quot;m&quot;&gt;100644&lt;/span&gt; foc/array_ref.h
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt; delete mode &lt;span class=&quot;m&quot;&gt;100644&lt;/span&gt; foc/hash_array_mapped_trie.h
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt; delete mode &lt;span class=&quot;m&quot;&gt;100644&lt;/span&gt; foc/hash_array_mapped_trie_test.cpp
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt; delete mode &lt;span class=&quot;m&quot;&gt;100644&lt;/span&gt; foc/hash_array_mapped_trie_test_helpers.h
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt; delete mode &lt;span class=&quot;m&quot;&gt;100644&lt;/span&gt; foc/none.h
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt; delete mode &lt;span class=&quot;m&quot;&gt;100644&lt;/span&gt; foc/small_vector.h
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt; delete mode &lt;span class=&quot;m&quot;&gt;100644&lt;/span&gt; foc/small_vector_test.cpp
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt; delete mode &lt;span class=&quot;m&quot;&gt;100644&lt;/span&gt; foc/support.h
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;

&lt;p&gt;Some adaptations might be needed on both repositories, but that’s pretty much
all there is to it. Happy gitting!&lt;/p&gt;
</description>
        <pubDate>Tue, 11 Jul 2017 17:17:48 -0300</pubDate>
        <link>https://felipe.rs/2017/07/11/monorepo-first-submodules-later/</link>
        <guid isPermaLink="true">https://felipe.rs/2017/07/11/monorepo-first-submodules-later/</guid>
        
        
        <category>In English</category>
        
        <category>git</category>
        
      </item>
      
    
      
      <item>
        <title>Where do Type Systems Come From?</title>
        <description>&lt;!-- My story --&gt;

&lt;p&gt;Every time I want to quickly understand something about an advanced type system or
programming language concept I get lost when I see something like this
on Wikipedia:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Linear type systems are the internal language of closed symmetric monoidal
categories, much in the same way that simply typed lambda calculus is the
language of Cartesian closed categories. More precisely, one may construct
functors between the category of linear type systems and the category of closed
symmetric monoidal categories.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;!-- Problem --&gt;

&lt;p&gt;Why there’s so much research around types if perfectly applying them to
programming languages is impractical? &lt;sup id=&quot;fnref:impractical&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:impractical&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;1&lt;/a&gt;&lt;/sup&gt; It turns out mathematicians
are the ones behind the development of most of the work related to type
systems – type theory.&lt;sup id=&quot;fnref:distinction&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:distinction&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;2&lt;/a&gt;&lt;/sup&gt; One might think they do this to support
programming languages. After all, it’s part of the job of mathematicians to
formalize what other disciplines developed with a more practical mindset.
Infinitesimal calculus was greatly advanced by Isaac Newton to solve immediate
problems in physics for example. But the development and study of type theory
pre-dates programming languages!  What problems were mathematicians trying to
solve when they developed type theory, a curious programmer might ask.&lt;/p&gt;

&lt;!-- Origin of types --&gt;

&lt;p&gt;The development of a theory of types started with Bertrand Russell in the
beginning of the 20th century in Appendix B of &lt;a href=&quot;http://a.co/2p9cuJG&quot;&gt;The Principles of Mathematics
(1903).&lt;/a&gt; It was developed as part of the effort to to
describe new foundations for mathematics from which &lt;a href=&quot;https://en.wikipedia.org/wiki/Hilbert%27s_program&quot;&gt;all mathematical truths
could in principle be proven using symbolic
logic&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;
  &lt;center&gt;
    &lt;figure&gt;
      &lt;img src=&quot;/assets/russell.jpg&quot; alt=&quot;Bertrand Russell&quot; /&gt;
      &lt;figcaption&gt;
        Bertrand Russell – Widely held to be one of the 20th century's premier logicians.
      &lt;/figcaption&gt;
    &lt;/figure&gt;
  &lt;/center&gt;
&lt;/p&gt;

&lt;!-- What problem types solve --&gt;

&lt;p&gt;&lt;strong&gt;In short, type theory was developed to be an alternative to set theory
as the foundation of mathematical proofs in symbolic logic due to its ability to
solve some contradictions stemming from naive set theory.&lt;/strong&gt; &lt;sup id=&quot;fnref:paradoxes&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:paradoxes&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;3&lt;/a&gt;&lt;/sup&gt; Later,
type theory referred to a whole class of formal systems. There are many
type theories.&lt;/p&gt;

&lt;p&gt;In part I of his &lt;em&gt;Mathematical Logic as Based on the Theory of Types&lt;/em&gt; paper
published in 1908, Russell lists many of such contradictions and finds a
commonality in them.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;In all the above contradictions (which are merely selections from an
indefinite number) there is a common characteristic, which we may describe as
&lt;em&gt;self-reference&lt;/em&gt; or &lt;em&gt;reflexiveness&lt;/em&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The simplest form of this kind of contradiction is the person who says “I am
lying;” if she is lying, she is speaking the truth and if she is speaking the
truth, she is lying. Russell rewrites “I am lying” many times to find the root
cause of the contradiction.&lt;/p&gt;

&lt;p&gt;“I am lying” becomes “There is a proposition which I am affirming and which is false”.
All propositions that say “there is so-and-so” may be regarded as saying that the
negation of “so-and-so” is not always true. We can now rewrite “I am lying” to “It
is not true of all propositions that either I am not affirming them or they are
true” in other words “It is not true for all propositions &lt;em&gt;p&lt;/em&gt; that if I affirm
&lt;em&gt;p&lt;/em&gt;, &lt;em&gt;p&lt;/em&gt; is true.” The “I am lying” paradox results, according to Russell, from
regarding that as a proposition. This makes it evident that the notion of “all
propositions” is illegitimate; for otherwise, there must be propositions (such
as the above) which are about all propositions, and yet can not, without
contradiction, be included among the propositions they are about. Whatever we
suppose to be “all propositions,” statements about them generate new
propositions which must lie outside “all propositions” to avoid a
contradiction. Regarding “all propositions” as a meaningless phrase will avoid
us many contradictions. Similarly, in set theory, we should not consider “the
set of all sets” to be a legitimate set.&lt;/p&gt;

&lt;!-- The hierarchy of types --&gt;

&lt;p&gt;In part IV, Russell proposes the &lt;em&gt;hierarchy of types&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;A &lt;em&gt;type&lt;/em&gt; is defined as the range of significance of a propositional function,
i.e., as the collection of arguments for which the said function has values.
Whenever an apparent variable occurs in a proposition, the range of values of
the apparent variable is a type, the type being fixed by the function of which
“all values” are concerned. The division of objects into types is necessitated
by the reflexive fallacies which otherwise arise.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That’s the equivalent of writing type annotations for programming functions. And
the goal is avoiding bugs instead of logical contradictions.&lt;/p&gt;

&lt;p&gt;It’s called &lt;em&gt;hierarchy of types&lt;/em&gt; because it separates propositions into orders:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;A proposition containing no apparent variable we will call an &lt;em&gt;elementary&lt;/em&gt;
proposition. […] The terms of elementary propositions we will call
&lt;em&gt;individuals&lt;/em&gt;; these form the first or lowest type.&lt;/p&gt;

  &lt;p&gt;[…]&lt;/p&gt;

  &lt;p&gt;Elementary propositions together with such as contain only individuals as
apparent variables we will call &lt;em&gt;first-order propositions.&lt;/em&gt; These form the
second logical type.&lt;/p&gt;

  &lt;p&gt;[…] We can thus form new propositions in which first-order propositions
occur as apparent variables. These we will call &lt;em&gt;second-order propositions&lt;/em&gt;;
these form the third logical type. Thus, e.g, if Epimenides asserts that “all
first-order propositions affirmed by me are false,” he asserts a second-order
proposition; he may assert this truly, without asserting truly any first-order
proposition, and thus no contradiction arises.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;!-- Programming analogy --&gt;

&lt;p&gt;For a programming analogy, let’s try to create a function capable of running &lt;em&gt;all
functions&lt;/em&gt;&lt;/p&gt;

&lt;figure class=&quot;code&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&quot;line-number&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;3&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;4&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;5&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;6&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;7&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;8&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;9&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;10&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;11&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;12&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;13&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;14&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;15&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;runFunction&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;addTwo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;param&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;param&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;multiplyByTwo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;param&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;param&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;program1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;runFunction&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;addTwo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;program2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;runFunction&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;multiplyByTwo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;program3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;runFunction&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;runFunction&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;

&lt;p&gt;All programs above are syntactically valid. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;program1&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;program2&lt;/code&gt; will finish
(they &lt;em&gt;halt&lt;/em&gt;). &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;program3&lt;/code&gt; also finishes, but will be due to an error caught by
JavaScript interpreter checks.&lt;/p&gt;

&lt;figure class=&quot;code&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&quot;line-number&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;3&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;4&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;5&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;6&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;program1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;program2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;program3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;nx&quot;&gt;TypeError&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;program3&lt;/code&gt; fails because &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;runFunction&lt;/code&gt; can only run first-order functions and
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;runFunction&lt;/code&gt; is a second-order function – a function that takes a first-order
function as a parameter.&lt;/p&gt;

&lt;p&gt;If we redefine &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;runFunction&lt;/code&gt; in Typescript as&lt;/p&gt;

&lt;figure class=&quot;code&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&quot;line-number&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;3&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;runFunction&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;func&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;param&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;any&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;

&lt;p&gt;The type-checker will yell at us about &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;program3&lt;/code&gt;&lt;/p&gt;

&lt;figure class=&quot;code&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&quot;line-number&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;3&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;4&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;5&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;6&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;program3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;runFunction&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;runFunction&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;                                         &lt;span class=&quot;o&quot;&gt;^&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;nx&quot;&gt;Argument&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;of&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;(func: (param: number) =&amp;gt; any) =&amp;gt; any&amp;#39;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;assignable&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;to&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;parameter&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;of&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;(param: number) =&amp;gt; any&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;nx&quot;&gt;Types&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;of&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;parameters&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;func&amp;#39;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;param&amp;#39;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;are&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;incompatible&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;    &lt;span class=&quot;nx&quot;&gt;Type&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;number&amp;#39;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;assignable&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;to&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;(param: number) =&amp;gt; any&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;runFunction&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;func&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;param&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;any&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;any&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;

&lt;!-- Conclusion --&gt;

&lt;p&gt;Every programmer knows that type systems can’t prevent all bugs. Similarly,
type theory wasn’t enough to describe new foundations for mathematics from
which all mathematical truths could in principle be proven using symbolic
logic. It wasn’t enough because
&lt;a href=&quot;https://en.wikipedia.org/wiki/G%C3%B6del%27s_incompleteness_theorems&quot;&gt;this goal in its full extent is unattainable.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Even though theoretically, &lt;em&gt;type theories&lt;/em&gt; and &lt;em&gt;type systems&lt;/em&gt; are not enough to
prevent all the problems in logic and programming, they can be improved and
refined to prevent an increasingly number of problems in the practice of logic
and programming. That means the research for better practical type systems is
far from over!&lt;/p&gt;

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

&lt;p&gt;Russell, Bertrand, &lt;a href=&quot;http://a.co/2p9cuJG&quot;&gt;“The Principles of Mathematics”&lt;/a&gt;, 1903.&lt;/p&gt;

&lt;p&gt;Russell, Bertrand, &lt;a href=&quot;https://www.jstor.org/stable/2369948&quot;&gt;“Mathematical Logic as Based on the Theory of Types”&lt;/a&gt;, 1908, &lt;em&gt;American Journal of Mathematics.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Coquand, Thierry, &lt;a href=&quot;https://plato.stanford.edu/entries/type-theory/&quot;&gt;“Type Theory”,&lt;/a&gt; &lt;em&gt;The Stanford Encyclopedia of Philosophy.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Constable, Robert, &lt;a href=&quot;https://ecommons.cornell.edu/handle/1813/28696&quot;&gt;The Triumph of Types: Principia Mathematica’s Impact on Computer Science,&lt;/a&gt;, 2011, &lt;em&gt;Proceedings of 10th Annual Oregon Programming Languages Summer School.&lt;/em&gt;&lt;/p&gt;

&lt;h2 id=&quot;notes&quot;&gt;Notes&lt;/h2&gt;

&lt;div class=&quot;footnotes&quot; role=&quot;doc-endnotes&quot;&gt;
  &lt;ol&gt;
    &lt;li id=&quot;fn:impractical&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;In fact, automatic proof assistants rely on type theories to achieve correctness (e.g. &lt;a href=&quot;https://coq.inria.fr/&quot;&gt;Coq&lt;/a&gt;). &lt;a href=&quot;#fnref:impractical&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:distinction&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;Making a distinction between &lt;em&gt;type theory&lt;/em&gt; and &lt;em&gt;type system&lt;/em&gt; is necessary. A programming language type system is analogous to a type theory. A &lt;em&gt;type system&lt;/em&gt; is a feature of a programming language. It’s concerned with how to represent types in a machine and other things. &lt;a href=&quot;#fnref:distinction&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:paradoxes&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;A quick explanation of these paradoxes can be found in the &lt;a href=&quot;https://plato.stanford.edu/entries/type-theory/#1&quot;&gt;Stanford Encyclopedia of Phylosophy&lt;/a&gt;. &lt;a href=&quot;#fnref:paradoxes&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
  &lt;/ol&gt;
&lt;/div&gt;
</description>
        <pubDate>Fri, 07 Jul 2017 18:32:02 -0300</pubDate>
        <link>https://felipe.rs/2017/07/07/where-do-type-systems-come-from/</link>
        <guid isPermaLink="true">https://felipe.rs/2017/07/07/where-do-type-systems-come-from/</guid>
        
        
        <category>Computer Science</category>
        
        <category>Functional Programming</category>
        
        <category>In English</category>
        
      </item>
      
    
      
      <item>
        <title>id Software Programming Principles</title>
        <description>&lt;p&gt;id Software co-founder John Romero tells the early story of the game company in
this GDC 2016 talk and lists the programming principles that guided them towards
the rapid development of many games including Doom and Quake with a very small
team.&lt;/p&gt;

&lt;center&gt;
  &lt;iframe width=&quot;560&quot; height=&quot;315&quot; src=&quot;https://www.youtube-nocookie.com/embed/E2MIpi8pIvY?ecver=1&quot; frameborder=&quot;0&quot; allowfullscreen=&quot;&quot;&gt;&lt;/iframe&gt;
&lt;/center&gt;

&lt;p&gt;Some of these principles resembles today’s common Agile practices while
others do not. I like them and id definitely had good results, so the principles
should at least be considered and adapted to different contexts.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;No prototypes. Just make the game. Polish as you go. Don’t depend on polish
happening later. &lt;strong&gt;Always maintain constantly shippable code.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Continuous delivery and integration?&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;It’s incredibly important that &lt;strong&gt;your game can always be run by your team.&lt;/strong&gt;
Bulletproof your engine by providing defaults upon load failure.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Don’t break the build.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;Keep your code absolutely simple.&lt;/strong&gt; Keep looking at your functions and figure
out how you simplify further.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Spend time simplifying code.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Great tools help make great games. &lt;strong&gt;Spend as much time on tools as
possible.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;p&gt;We are our own best testing team and should never allow anyone else to
experience bugs or see the game crash. &lt;strong&gt;Don’t waste others’ time. Test
thoroughly before checking in your code.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;As soon as you see a bug, you fix it.&lt;/strong&gt; Do not continue on. If you don’t fix
your bugs your new code will be built on a buggy codebase and ensure an
unstable foundation.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;p&gt;Use a superior development system than your target.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This was very true in the 90s when consumer computers could be so much slower
than high-end ones.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;Write your code for this game only - not for a future game.&lt;/strong&gt; You’re going to
be writing new code later because you’ll be smarter.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Don’t write so many libraries.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;Encapsulate functionality to ensure design consistency.&lt;/strong&gt; This minimizes
mistakes and saves design time.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Less moving parts.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;Try to code transparently.&lt;/strong&gt; Tell your lead and peers exactly how you are going
to solve your current task and get feedback and advice. Do not treat game
programming like each coder is a black box. The project could go off the rails
cause delays.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Communicate.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Programming is a creative art form based in logic. &lt;strong&gt;Every programmer is
different and will code differently.&lt;/strong&gt; It’s the output that matters.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Respect differences and focus on the results.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/quake_header.png&quot; alt=&quot;Quake source copyright ingo&quot; /&gt;&lt;/p&gt;
</description>
        <pubDate>Sat, 25 Feb 2017 11:00:02 -0300</pubDate>
        <link>https://felipe.rs/2017/02/25/id-software-programming-principles/</link>
        <guid isPermaLink="true">https://felipe.rs/2017/02/25/id-software-programming-principles/</guid>
        
        
        <category>In English</category>
        
        <category>Software Engineering</category>
        
      </item>
      
    
      
      <item>
        <title>Por que 0.4 - 0.5 = 0.09999999999999998 em Javascript?</title>
        <description>&lt;p&gt;Ontem o &lt;a href=&quot;https://www.facebook.com/photo.php?fbid=10154938566093373&quot;&gt;Marco Gomes perguntou&lt;/a&gt;
por que 0.4 - 0.5 é 0.09999999999999998 em vez de 0.1 em Javascript. Esse post é
uma tentativa de resposta à essa pergunta.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/marco_gomes_post.png&quot; alt=&quot;Por que 0.4 - 0.5 é 0.09999999999999998?&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Existe um número infinito de números reais entre 0.0 e 1.0 então não dá pra
representar todos os números reais com precisão infinita já que a memória do
computador é finita. Ao contrário do que disseram nos comentários, dá sim pra
representar 0.4, 0.5 e 0.1 em 64 bits (tamanho do float no Javascript) e em até
muito menos que 64 bits o que garante mais 9’s na aproximação de 0.1, mas não
está livre dos mesmos problemas.&lt;/p&gt;

&lt;p&gt;Vou usar representação de 32 bits para todos os exemplos abaixo para não ter que
lidar com números muito longos. O Javascript usa 64 bits.&lt;/p&gt;

&lt;p&gt;Quando você digita “0.4” solicitando que o computador represente o número real
0.4 em um número finito de bits no padrão IEEE 754 (padrão usado por CPUs e
linguagens modernas) o computador decide codificar 0.4 como
\(2^{-2} \times 1.600000023841858\) (é o que cabe em 32 bits seguindo as regras do IEEE 754).&lt;/p&gt;

&lt;p&gt;Essa representação no formato \(2^{expoente} \times mantissa\) é chamada de ponto
flutuante e o padrão IEEE 754 define como esses números são transformados em
operações.&lt;/p&gt;

&lt;p&gt;Se você usar uma calculadora de alta precisão vai ver que&lt;/p&gt;

\[2^{-2} \times 1.600000023841858 = \\0.4000000059604645\]

&lt;p&gt;Se você pedir para imprimir esse número de novo verá “0.4” porque a rotina que
converte pontos flutuantes de 32 bits para texto seguindo as regras do IEEE 754
decide que “0.4” é o número mais próximo daquilo que está tentando-se
representar com expoente igual a -2 e mantissa igual a 1.600000023841858. Mesmo
assim, dá pra dizer que existe um erro de 0.0000000059604645 nessa
representação.&lt;/p&gt;

&lt;p&gt;Representar “0.5” é muito menos problemático porque computadores são muito bons
em dividir por 2 e 0.5 é 1 dividido por 2.&lt;/p&gt;

\[0.5 = 2^{-1} \times 1.0\]

&lt;p&gt;Fazendo a subtração usando as representações em ponto flutuante de 32 bits de
0.4 e 0.5 em uma calculadora de alta precisão:&lt;/p&gt;

\[0.4000000059604645 - 0.5 = \\-0.0999999940395355\]

&lt;p&gt;Esse resultado contém o erro que vem da representação do 0.4. Como a cada
operação com floats IEEE 754, os números são arredondados para o máximo de
precisão possível com um número finito de bits
(32 nesses exemplos), então o resultado visível será -0.099999994.&lt;/p&gt;

&lt;p&gt;Por que esse pequeno erro agora se tornou “visível”? Porque a precisão de pontos
flutuante é variável! Dá pra representar -0.0999999940395355 com um pouco mais
de precisão do que 0.4000000059604645 “movendo o ponto” e o IEEE 754 aproveita
isso. -0.099999994 é representado como \(2^{-4} \times 1.5999999046325684\). Esse -4
é a “flutuação do ponto” e dá a possibilidade de retornar mais uma casa decimal
com confiança. -0.099999994 é mais próximo de -0.1 do que -0.09999999, mas
0.400000005 (9 casas) é menos próximo de 0.4 do que 0.40000000 (8 casas).&lt;/p&gt;

&lt;center style=&quot;margin: 1em 0&quot;&gt;&lt;img src=&quot;/assets/point_floated.png&quot; /&gt;&lt;/center&gt;

&lt;p&gt;Um exemplo de número que é muito amigável à representação de ponto flutuante é
0.0000000000009094947017729282379150390625. Ao contrário de 0.4 pode ser
representado sem erro algum por \(2^{-40} \times 1.0\).&lt;/p&gt;

&lt;p&gt;Em ponto flutuante decimal (em vez de binário comum em computadores) é muito
fácil representar 0.0000000000000000000000000000000000000001 como
\(10^{-40} \times 1.0\) mas é bem mais problemático de se representar com ponto
flutuante binário:&lt;/p&gt;

\[2^{-126} \times 0.008507013320922852 = \\
0.0000000000000000000000000000\\
000000000000999994610111476009\\
580469753702428423236444030435\\
167589607897177528261778434170\\
992113649845123291015625\]

&lt;p&gt;Representar números reais em computadores não é fácil e é necessário entender as
limitações de cada representação (pontos flutuantes, ponto fixo, frações,
fórmulas simbólicas…) para minimizar problemas em aplicações práticas.&lt;/p&gt;
</description>
        <pubDate>Sat, 04 Feb 2017 13:02:02 -0300</pubDate>
        <link>https://felipe.rs/2017/02/04/porque-04-05-0-09999999999999998-em-javascript/</link>
        <guid isPermaLink="true">https://felipe.rs/2017/02/04/porque-04-05-0-09999999999999998-em-javascript/</guid>
        
        
        <category>Computer Science</category>
        
        <category>In Portuguese</category>
        
        <category>Javascript</category>
        
        <category>Math</category>
        
      </item>
      
    
      
      <item>
        <title>Running the Xv6 Operating System</title>
        <description>&lt;p&gt;Being able to easily run and debug a simple operating system can be really
useful when you want to learn how low level components are implemented.
&lt;a href=&quot;https://pdos.csail.mit.edu/6.828/2012/xv6/book-rev7.pdf&quot;&gt;Xv6&lt;/a&gt; is a
very simple Unix-like operating system that allows you to do just that.&lt;/p&gt;

&lt;p&gt;sillysaurus2 exemplified
this in the &lt;a href=&quot;https://news.ycombinator.com/item?id=6971127&quot;&gt;Hacker News’ thread on Xv6&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Have you ever:&lt;/p&gt;

  &lt;ul&gt;
    &lt;li&gt;Wondered how a filesystem can survive a power outage?&lt;/li&gt;
    &lt;li&gt;Wondered how to organize C code?&lt;/li&gt;
    &lt;li&gt;Wondered how memory allocation works?&lt;/li&gt;
    &lt;li&gt;Wondered how memory paging works?&lt;/li&gt;
    &lt;li&gt;Wondered about the difference between a kernel function and a userspace function?&lt;/li&gt;
    &lt;li&gt;Wondered how a shell works? (How it parses your commands, or how to write your own, etc)&lt;/li&gt;
    &lt;li&gt;Wondered how a mutex can be implemented? Or how to have multiple threads executing safely?&lt;/li&gt;
    &lt;li&gt;How multiple processes are scheduled by the OS? Priority, etc?&lt;/li&gt;
    &lt;li&gt;How permissions are enforced by the OS? Security model? Why Unix won while Multics didn’t (simplicity)?&lt;/li&gt;
    &lt;li&gt;How piping works? Stdin/stdout and how to compose them together to build complicated systems without drowning in complexity?&lt;/li&gt;
    &lt;li&gt;So much more!&lt;/li&gt;
  &lt;/ul&gt;

  &lt;p&gt;I credit studying xv6 as being one of the most important decisions I’ve made;
up there with learning vim or emacs, or touch typing. This is foundational
knowledge which will serve you the rest of your life in a thousand ways, both
subtle and overt. Spend a weekend or two dissecting xv6 and you’ll love
yourself for it later. (Be sure to study the book, not just the source code.
It’s freely available online. The source code is also distributed as a PDF,
which seems strange till you start reading the book. Both PDFs are meant to be
read simultaneously, rather than each alone.)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2 id=&quot;download-compile-and-run&quot;&gt;Download, Compile, and Run&lt;/h2&gt;

&lt;p&gt;You can download Xv6’s source code, compile, and run under QEMU using the
following commands:&lt;/p&gt;

&lt;figure class=&quot;code&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&quot;line-number&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;3&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;4&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;5&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;6&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;7&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;8&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;9&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;10&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;11&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;12&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;code class=&quot;sh&quot;&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;c&quot;&gt;# Download the code&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;git clone https://github.com/mit-pdos/xv6-public.git xv6
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;nb&quot;&gt;cd &lt;/span&gt;xv6
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;c&quot;&gt;# Compile&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;make
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;c&quot;&gt;# Install QEMU&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;sudo apt-get install qemu
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;c&quot;&gt;# Run under QEMU&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;make qemu
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;

&lt;p&gt;It looks like this:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/xv6_qemu.png&quot; alt=&quot;Xv6 Under QEMU&quot; title=&quot;Xv6 Under QEMU&quot; /&gt;&lt;/p&gt;

&lt;p&gt;There are very few commands available but you can see UNIX here:&lt;/p&gt;

&lt;figure class=&quot;code&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&quot;line-number&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;3&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;4&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;5&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;6&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;7&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;8&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;9&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;10&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;11&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;12&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;13&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;14&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;15&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;16&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;17&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;18&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;19&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;20&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;21&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;22&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;23&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;24&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;25&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;26&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;code class=&quot;sh&quot;&gt;&lt;span class=&quot;line&quot;&gt;xv6...
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;cpu1: starting
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;cpu0: starting
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;sb: size 1000 nblocks 941 ninodes 200 nlog 30 logstart 2 inodestart 32 bmap start 58
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;init: starting sh
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;ls
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;.              1 1 512
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;..             1 1 512
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;README         2 2 1973
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;cat            2 3 13320
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;nb&quot;&gt;echo           &lt;/span&gt;2 4 12428
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;forktest       2 5 8144
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;grep           2 6 15020
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;init           2 7 13084
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;nb&quot;&gt;kill           &lt;/span&gt;2 8 12580
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;ln             2 9 12424
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;ls             2 10 14812
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;mkdir          2 11 12572
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;rm             2 12 12560
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;sh             2 13 23564
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;stressfs       2 14 13280
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;usertests      2 15 58584
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;wc             2 16 13848
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;zombie         2 17 12196
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;console        3 18 0
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;err&quot;&gt;$&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;

&lt;h2 id=&quot;remote-debug-xv6-under-qemu-using-gdb&quot;&gt;Remote Debug Xv6 Under QEMU using GDB&lt;/h2&gt;

&lt;p&gt;Xv6’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Makefile&lt;/code&gt; has a rule to make this very easy (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;qemu-gdb&lt;/code&gt;):&lt;/p&gt;

&lt;figure class=&quot;code&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&quot;line-number&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;3&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;4&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;5&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;code class=&quot;sh&quot;&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;make qemu-gdb
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;...
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;sed &lt;span class=&quot;s2&quot;&gt;&amp;quot;s/localhost:1234/localhost:26000/&amp;quot;&lt;/span&gt; &amp;lt; .gdbinit.tmpl &amp;gt; .gdbinit
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;*** Now run &lt;span class=&quot;s1&quot;&gt;&amp;#39;gdb&amp;#39;&lt;/span&gt;.
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;qemu-system-i386 -serial mon:stdio -hdb fs.img xv6.img -smp 2 -m 512  -S -gdb tcp::26000
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;

&lt;p&gt;Execution stopped before the first instruction and is now waiting  for GDB to
connect and supervise the execution (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;breakpoint&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;continue&lt;/code&gt;…). Open GDB from
another shell:&lt;/p&gt;

&lt;figure class=&quot;code&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&quot;line-number&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;3&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;4&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;5&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;6&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;7&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;8&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;9&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;10&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;11&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;12&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;code class=&quot;sh&quot;&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;gdb kernel
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;...
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;Reading symbols from kernel...done.
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;+ target remote localhost:26000
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;warning: A handler &lt;span class=&quot;k&quot;&gt;for &lt;/span&gt;the OS ABI &lt;span class=&quot;s2&quot;&gt;&amp;quot;GNU/Linux&amp;quot;&lt;/span&gt; is not built into this configuration
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;of GDB.  Attempting to &lt;span class=&quot;k&quot;&gt;continue &lt;/span&gt;with the default i8086 settings.
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;The target architecture is assumed to be i8086
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;f000:fff0&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;    0xffff0: ljmp   &lt;span class=&quot;nv&quot;&gt;$0xf000&lt;/span&gt;,&lt;span class=&quot;nv&quot;&gt;$0xe05b&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;0x0000fff0 in ?? &lt;span class=&quot;o&quot;&gt;()&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;+ symbol-file kernel
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;gdb&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;

&lt;p&gt;Now you can set breakpoints, resume execution, and do whatever you want. Here I
set a breakpoint at the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;exec&lt;/code&gt; function:&lt;/p&gt;

&lt;figure class=&quot;code&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&quot;line-number&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;3&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;4&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;5&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;6&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;7&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;8&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;9&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;10&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;11&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;12&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;13&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;14&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;15&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;16&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;17&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;18&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;19&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;20&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;21&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;22&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;23&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;24&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;25&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;26&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;27&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;28&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;29&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;30&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;31&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;32&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;33&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;34&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;35&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;36&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;37&lt;/span&gt;
&lt;span class=&quot;line-number&quot;&gt;38&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;code class=&quot;sh&quot;&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;gdb&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; breakpoint &lt;span class=&quot;nb&quot;&gt;exec&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;Breakpoint 1 at 0x80100b5f: file exec.c, line 12.
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;gdb&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;continue&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;Continuing.
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;New Thread 2&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;Switching to Thread 2&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;The target architecture is assumed to be &lt;span class=&quot;nv&quot;&gt;i386&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&amp;gt; 0x80100b5f &amp;lt;&lt;span class=&quot;nb&quot;&gt;exec&lt;/span&gt;&amp;gt;:   push   %ebp
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;Breakpoint 1, &lt;span class=&quot;nb&quot;&gt;exec&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;0x1c &lt;span class=&quot;s2&quot;&gt;&amp;quot;/init&amp;quot;&lt;/span&gt;, &lt;span class=&quot;nv&quot;&gt;argv&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;0x8dfffec8&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; at exec.c:12
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;12      &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;gdb&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;continue&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;Continuing.
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&amp;gt; 0x80100b5f &amp;lt;&lt;span class=&quot;nb&quot;&gt;exec&lt;/span&gt;&amp;gt;:   push   %ebp
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;Breakpoint 1, &lt;span class=&quot;nb&quot;&gt;exec&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;0x87d &lt;span class=&quot;s2&quot;&gt;&amp;quot;sh&amp;quot;&lt;/span&gt;, &lt;span class=&quot;nv&quot;&gt;argv&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;0x8dffeec8&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; at exec.c:12
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;12      &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;gdb&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;continue&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;Continuing.
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;Switching to Thread 1&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&amp;gt; 0x80100b5f &amp;lt;&lt;span class=&quot;nb&quot;&gt;exec&lt;/span&gt;&amp;gt;:   push   %ebp
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;Breakpoint 1, &lt;span class=&quot;nb&quot;&gt;exec&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;0x19c0 &lt;span class=&quot;s2&quot;&gt;&amp;quot;ls&amp;quot;&lt;/span&gt;, &lt;span class=&quot;nv&quot;&gt;argv&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;0x8dfbeec8&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; at exec.c:12
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;12      &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;gdb&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; p argv&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;0&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$5&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x19c0 &lt;span class=&quot;s2&quot;&gt;&amp;quot;ls&amp;quot;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;gdb&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; p argv&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;1&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$6&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x0
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;gdb&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; backtrace
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;c&quot;&gt;#0  exec (path=0x19c0 &amp;quot;ls&amp;quot;, argv=0x8df2bec8) at exec.c:12&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;c&quot;&gt;#1  0x801062ba in sys_exec () at sysfile.c:417&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;c&quot;&gt;#2  0x80105619 in syscall () at syscall.c:133&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;c&quot;&gt;#3  0x801067d7 in trap (tf=0x8df2bfb4) at trap.c:43&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;c&quot;&gt;#4  0x801065cc in alltraps () at trapasm.S:23&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;c&quot;&gt;#5  0x8df2bfb4 in ?? ()&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;Backtrace stopped: previous frame inner to this frame &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;corrupt stack?&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;gdb&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;continue&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;Continuing.
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;

&lt;p&gt;You can see that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/init&lt;/code&gt; was the first process, it spawned &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sh&lt;/code&gt; and I entered
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ls&lt;/code&gt; in the console myself. Things are magically simple!&lt;/p&gt;

&lt;h2 id=&quot;useful-resources&quot;&gt;Useful Resources&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://pdos.csail.mit.edu/6.828/2012/xv6.html&quot;&gt;Xv6 Home Page&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://pdos.csail.mit.edu/6.828/2012/xv6/book-rev7.pdf&quot;&gt;The Xv6 book&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</description>
        <pubDate>Mon, 22 Feb 2016 17:02:02 -0300</pubDate>
        <link>https://felipe.rs/2016/02/22/running-the-xv6-operating-system/</link>
        <guid isPermaLink="true">https://felipe.rs/2016/02/22/running-the-xv6-operating-system/</guid>
        
        
        <category>Assembly</category>
        
        <category>C</category>
        
        <category>Systems Programming</category>
        
        <category>Unix</category>
        
        <category>X86</category>
        
      </item>
      
    
  </channel>
</rss>
