<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    
    <title>Rowan Marshall</title>
    <description>Specialisation is for insects</description>
    <link>https://rowanajmarshall.co.uk/</link>
    
    <language>en</language>
    <copyright>Copyright 2026, Calvin Tran</copyright>
    <lastBuildDate>Wed, 04 Mar 2026 00:00:00 +0000</lastBuildDate>
    <generator>Hugo - gohugo.io</generator>
    <docs>http://cyber.harvard.edu/rss/rss.html</docs>
    <atom:link href="https://rowanajmarshall.co.uk//atom.xml" rel="self" type="application/atom+xml"/>
    
    
    <item>
      <title>Every SQLite output mode explained</title>
      <link>https://rowanajmarshall.co.uk/posts/every-sqlite-mode-example/</link>
      <description>&lt;p&gt;SQLite has many viewing modes it can output, and I have difficulty remembering them all and what to use when, so I&amp;rsquo;m posting this as a public memory for myself and anyone else finding themselves googling &amp;ldquo;sqlite all modes example&amp;rdquo; a lot.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Column&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Good for eyeballing data interactively - aligns values into readable columns.&lt;/p&gt;
&lt;div class=&#34;code-block&#34;&gt;
  &lt;div class=&#34;code-block-header&#34;&gt;
    &lt;span class=&#34;code-block-dots&#34;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
    
  &lt;/div&gt;
  &lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;.mode column

id  name   email
--  -----  -------------------
1   Alice  alice@example.com
2   Bob    bob@example.com&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;List&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Useful for piping output into shell scripts or other tools that split on a delimiter.&lt;/p&gt;
&lt;div class=&#34;code-block&#34;&gt;
  &lt;div class=&#34;code-block-header&#34;&gt;
    &lt;span class=&#34;code-block-dots&#34;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
    
  &lt;/div&gt;
  &lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;.mode list

1|Alice|alice@example.com
2|Bob|bob@example.com&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Line&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Handy when you have wide rows and want to read each field on its own line.&lt;/p&gt;
&lt;div class=&#34;code-block&#34;&gt;
  &lt;div class=&#34;code-block-header&#34;&gt;
    &lt;span class=&#34;code-block-dots&#34;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
    
  &lt;/div&gt;
  &lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;.mode line

   id = 1
 name = Alice
email = alice@example.com

   id = 2
 name = Bob
email = bob@example.com&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;CSV&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Use this when you want to export data to a spreadsheet or another tool that accepts CSV.&lt;/p&gt;
&lt;div class=&#34;code-block&#34;&gt;
  &lt;div class=&#34;code-block-header&#34;&gt;
    &lt;span class=&#34;code-block-dots&#34;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
    
  &lt;/div&gt;
  &lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;.mode csv

1,Alice,alice@example.com
2,Bob,bob@example.com&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Tabs&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Like list mode, but tab-separated - easier to paste into Excel or Numbers directly.&lt;/p&gt;
&lt;div class=&#34;code-block&#34;&gt;
  &lt;div class=&#34;code-block-header&#34;&gt;
    &lt;span class=&#34;code-block-dots&#34;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
    
  &lt;/div&gt;
  &lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;.mode tabs

1	Alice	alice@example.com
2	Bob	bob@example.com&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;HTML&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Outputs table rows ready to drop into an HTML page.&lt;/p&gt;
&lt;div class=&#34;code-block&#34;&gt;
  &lt;div class=&#34;code-block-header&#34;&gt;
    &lt;span class=&#34;code-block-dots&#34;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
    
  &lt;/div&gt;
  &lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;.mode html

&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;1&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;Alice&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;alice@example.com&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;2&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;Bob&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;bob@example.com&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Insert&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Generates runnable INSERT statements - good for producing quick migration or seed SQL.&lt;/p&gt;
&lt;div class=&#34;code-block&#34;&gt;
  &lt;div class=&#34;code-block-header&#34;&gt;
    &lt;span class=&#34;code-block-dots&#34;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
    
  &lt;/div&gt;
  &lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;.mode insert users

INSERT INTO users VALUES(1,&amp;#39;Alice&amp;#39;,&amp;#39;alice@example.com&amp;#39;);
INSERT INTO users VALUES(2,&amp;#39;Bob&amp;#39;,&amp;#39;bob@example.com&amp;#39;);&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;JSON&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Use when you need to hand the results off to something that consumes JSON (note: some older JSON parsers error or warn on a top-level list).&lt;/p&gt;
&lt;div class=&#34;code-block&#34;&gt;
  &lt;div class=&#34;code-block-header&#34;&gt;
    &lt;span class=&#34;code-block-dots&#34;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
    
  &lt;/div&gt;
  &lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;.mode json

[
  {&amp;#34;id&amp;#34;:1,&amp;#34;name&amp;#34;:&amp;#34;Alice&amp;#34;,&amp;#34;email&amp;#34;:&amp;#34;alice@example.com&amp;#34;},
  {&amp;#34;id&amp;#34;:2,&amp;#34;name&amp;#34;:&amp;#34;Bob&amp;#34;,&amp;#34;email&amp;#34;:&amp;#34;bob@example.com&amp;#34;}
]&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Markdown&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Outputs a GitHub-flavoured markdown table, good for pasting into docs or READMEs.&lt;/p&gt;
&lt;div class=&#34;code-block&#34;&gt;
  &lt;div class=&#34;code-block-header&#34;&gt;
    &lt;span class=&#34;code-block-dots&#34;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
    
  &lt;/div&gt;
  &lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;.mode markdown

| id | name  | email               |
|----|-------|---------------------|
| 1  | Alice | alice@example.com   |
| 2  | Bob   | bob@example.com     |&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Box&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;A nicer-looking version of table mode using Unicode box-drawing characters.&lt;/p&gt;
&lt;div class=&#34;code-block&#34;&gt;
  &lt;div class=&#34;code-block-header&#34;&gt;
    &lt;span class=&#34;code-block-dots&#34;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
    
  &lt;/div&gt;
  &lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;.mode box

┌────┬───────┬────────────────────┐
│ id │ name  │ email              │
├────┼───────┼────────────────────┤
│ 1  │ Alice │ alice@example.com  │
│ 2  │ Bob   │ bob@example.com    │
└────┴───────┴────────────────────┘&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Quote&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Similar to list mode but strings are single-quoted, matching SQL literal syntax.&lt;/p&gt;
&lt;div class=&#34;code-block&#34;&gt;
  &lt;div class=&#34;code-block-header&#34;&gt;
    &lt;span class=&#34;code-block-dots&#34;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
    
  &lt;/div&gt;
  &lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;.mode quote

1,&amp;#39;Alice&amp;#39;,&amp;#39;alice@example.com&amp;#39;
2,&amp;#39;Bob&amp;#39;,&amp;#39;bob@example.com&amp;#39;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;ASCII&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Uses ASCII control characters (unit separator &lt;code&gt;0x1F&lt;/code&gt;, record separator &lt;code&gt;0x1E&lt;/code&gt;) as delimiters - safe for data that might contain commas or pipes.&lt;/p&gt;
&lt;div class=&#34;code-block&#34;&gt;
  &lt;div class=&#34;code-block-header&#34;&gt;
    &lt;span class=&#34;code-block-dots&#34;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
    
  &lt;/div&gt;
  &lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;.mode ascii

1␟Alice␟alice@example.com␞
2␟Bob␟bob@example.com␞&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Table&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;A plain ASCII bordered table, like box mode but without Unicode - useful in terminals that don&amp;rsquo;t support it.&lt;/p&gt;
&lt;div class=&#34;code-block&#34;&gt;
  &lt;div class=&#34;code-block-header&#34;&gt;
    &lt;span class=&#34;code-block-dots&#34;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
    
  &lt;/div&gt;
  &lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;.mode table

+----+-------+-------------------+
| id | name  | email             |
+----+-------+-------------------+
| 1  | Alice | alice@example.com |
| 2  | Bob   | bob@example.com   |
+----+-------+-------------------+&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;TCL&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Outputs values as a TCL list - mainly useful if you&amp;rsquo;re scripting with TCL or tclsh.&lt;/p&gt;
&lt;div class=&#34;code-block&#34;&gt;
  &lt;div class=&#34;code-block-header&#34;&gt;
    &lt;span class=&#34;code-block-dots&#34;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
    
  &lt;/div&gt;
  &lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;.mode tcl

1 Alice alice@example.com
2 Bob bob@example.com&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
</description>
      <author>Rowan Marshall</author>
      <guid>https://rowanajmarshall.co.uk/posts/every-sqlite-mode-example/</guid>
      <pubDate>Wed, 04 Mar 2026 00:00:00 +0000</pubDate>
    </item>
    
    <item>
      <title>Demand for Software is Different</title>
      <link>https://rowanajmarshall.co.uk/posts/demand-for-software-is-different/</link>
      <description>&lt;p&gt;Most things humans produce have a recognisable demand ceiling - the world will only eat so much bread, regardless of how cheap it is, and we&amp;rsquo;ll only upgrade our phones so often, no matter how nice the new models are.&lt;/p&gt;
&lt;p&gt;If we get 10x times better at growing wheat, we don&amp;rsquo;t grow 10x more wheat, because we&amp;rsquo;re pretty close to that upper limit now - instead, we get 10x less wheat farmers, producing a similar quantity of wheat.&lt;/p&gt;
&lt;p&gt;Software is a bit different.&lt;/p&gt;
&lt;p&gt;Software doesn&amp;rsquo;t really have a recognisable demand ceiling. User interfaces can always be better, a server more performant, or more options added. There are whole industries left largely undigitalised and countless problems we haven&amp;rsquo;t even realised software can solve yet.&lt;/p&gt;
&lt;p&gt;In a way, this is a retelling of Jevons Paradox: when a resource becomes cheaper or more efficient to produce, demand increases as we find new ways to use it at that lower price point.&lt;/p&gt;
&lt;p&gt;Whenever we’ve made software development more efficient—whether through better programming languages, IDEs, libraries, frameworks, low-code tools, or AI—the result hasn’t been long-term unemployment for developers. Quite the opposite. We’ve seen more developers, paid even more, building bigger, faster, and more complex systems.&lt;/p&gt;
&lt;p&gt;That’s why I’m bullish on AI programming tools &lt;em&gt;and&lt;/em&gt; the future of software development jobs. Sure, the role will evolve, but software hasn’t finished &lt;a href=&#34;https://a16z.com/why-software-is-eating-the-world/&#34;&gt;eating the world&lt;/a&gt; yet. And until it does, there’s going to be high demand for people who specialise in solving problems with code.&lt;/p&gt;
</description>
      <author>Rowan Marshall</author>
      <guid>https://rowanajmarshall.co.uk/posts/demand-for-software-is-different/</guid>
      <pubDate>Wed, 08 Jan 2025 00:00:00 +0000</pubDate>
    </item>
    
    <item>
      <title>Modifying a compiled executable</title>
      <link>https://rowanajmarshall.co.uk/posts/modifying-a-compiled-executable/</link>
      <description>&lt;p&gt;Oh no!&lt;/p&gt;
&lt;p&gt;Your boss, Harry, is leaving the company. He was a good guy and a good boss, who appreciated the simple things in life. One of those simple things was a little program called &amp;ldquo;who-the-boss&amp;rdquo; which, when run, printed out &amp;ldquo;Harry the boss!&amp;rdquo;.&lt;/p&gt;
&lt;p&gt;His replacement is called Larry. Larry is a short-tempered man with little patience for anything he considers wrong, which is everything.&lt;/p&gt;
&lt;p&gt;One day, he finds Harry&amp;rsquo;s program on a machine everyone forgot existed. He runs it, expecting it to say he, Larry, is the boss. When it prints out &amp;ldquo;Harry&amp;rdquo;, Larry is furious. He marches down past the espresso machine, fresh fruit and pool tables to the dev office and demand they change it now!. The thing is, no-one knows where the source code is for this program. You know your hexadecimals from your stack machines, so the team beg you to help.&lt;/p&gt;
&lt;p&gt;First things first, you run the program to validate it&amp;rsquo;s output:&lt;/p&gt;
&lt;p&gt;&lt;img
  src=&#34;https://rowanajmarshall.co.uk/1.png&#34;
  alt=&#34;&amp;amp;ldquo;The who-the-boss program is run, printing out &amp;amp;lsquo;Harry the boss!&amp;amp;rsquo;&amp;amp;rdquo;&#34;
  loading=&#34;lazy&#34;
  decoding=&#34;async&#34;
  class=&#34;full-width&#34;
/&gt;

&lt;/p&gt;
&lt;p&gt;You open up Vim, making sure to use the &lt;code&gt;:%!xxd&lt;/code&gt; command to open in hex mode.&lt;/p&gt;
&lt;p&gt;&lt;img
  src=&#34;https://rowanajmarshall.co.uk/2.png&#34;
  alt=&#34;&amp;amp;ldquo;Vim has opened a hex-editor view of the who-the-boss binary&amp;amp;rdquo;&#34;
  loading=&#34;lazy&#34;
  decoding=&#34;async&#34;
  class=&#34;full-width&#34;
/&gt;

&lt;/p&gt;
&lt;p&gt;Hmm, how to find Harry&amp;rsquo;s name&amp;hellip; What&amp;rsquo;s the hex representation of Harry?&lt;/p&gt;
&lt;p&gt;You quickly look up a table of UTF-8 character codes and find that &amp;ldquo;Harry&amp;rdquo; is &amp;ldquo;48 61 72 72 79&amp;rdquo; in hexadecimal UTF-8.&lt;/p&gt;
&lt;p&gt;Right, lets search for that in Vim.&lt;/p&gt;
&lt;p&gt;&lt;img
  src=&#34;https://rowanajmarshall.co.uk/3.png&#34;
  alt=&#34;&amp;amp;ldquo;We search for a string of hexadecimal digits 48 61 72 72 79 in Vim&amp;amp;rdquo;&#34;
  loading=&#34;lazy&#34;
  decoding=&#34;async&#34;
  class=&#34;full-width&#34;
/&gt;

&lt;/p&gt;
&lt;p&gt;Right, we&amp;rsquo;ve isolated the &amp;ldquo;Harry&amp;rdquo; string. Now to change that H to an L.&lt;/p&gt;
&lt;p&gt;&lt;img
  src=&#34;https://rowanajmarshall.co.uk/4.png&#34;
  alt=&#34;&amp;amp;ldquo;The hex digit 48 is changed to 4e&amp;amp;rdquo;&#34;
  loading=&#34;lazy&#34;
  decoding=&#34;async&#34;
  class=&#34;full-width&#34;
/&gt;

&lt;/p&gt;
&lt;p&gt;There we are, done! Lets save that making sure to use the &lt;code&gt;:%!xxd -r&lt;/code&gt; command to correctly switch back.&lt;/p&gt;
&lt;p&gt;&lt;img
  src=&#34;https://rowanajmarshall.co.uk/5.png&#34;
  alt=&#34;&amp;amp;ldquo;The program who-the-boss now prints out &amp;amp;lsquo;Larry the boss!&amp;amp;rsquo;&#34;
  loading=&#34;lazy&#34;
  decoding=&#34;async&#34;
  class=&#34;full-width&#34;
/&gt;

&lt;/p&gt;
&lt;p&gt;Perfect! Larry is content and the team owes you one.&lt;/p&gt;
</description>
      <author>Rowan Marshall</author>
      <guid>https://rowanajmarshall.co.uk/posts/modifying-a-compiled-executable/</guid>
      <pubDate>Thu, 14 Feb 2019 00:00:00 +0000</pubDate>
    </item>
    
    <item>
      <title>Calling Nim from Python</title>
      <link>https://rowanajmarshall.co.uk/posts/calling-nim-from-python/</link>
      <description>&lt;p&gt;In this post I&amp;rsquo;ll show you how to write a function in &lt;a href=&#34;https://nim-lang.org/&#34;&gt;Nim&lt;/a&gt; and call it in &lt;a href=&#34;https://www.python.org/&#34;&gt;Python&lt;/a&gt;.&lt;/p&gt;
&lt;h3 id=&#34;nim&#34;&gt;Nim&lt;/h3&gt;
&lt;p&gt;To start with we&amp;rsquo;ll need a Nim function. Here&amp;rsquo;s a one that takes 2 integers, adds them and returns the result:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;called.nim&lt;/em&gt;&lt;/p&gt;
&lt;div class=&#34;code-block&#34;&gt;
  &lt;div class=&#34;code-block-header&#34;&gt;
    &lt;span class=&#34;code-block-dots&#34;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&#34;code-block-lang&#34;&gt;nim&lt;/span&gt;
  &lt;/div&gt;
  &lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-nim&#34; data-lang=&#34;nim&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;proc &lt;/span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;nim_add&lt;/span&gt;(num1: &lt;span style=&#34;color:#66d9ef&#34;&gt;int&lt;/span&gt;, num2: &lt;span style=&#34;color:#66d9ef&#34;&gt;int&lt;/span&gt;): &lt;span style=&#34;color:#66d9ef&#34;&gt;int&lt;/span&gt; {.exportc.} &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;return&lt;/span&gt; num1 &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt; num2&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Most of this is pretty ordinary, except for the &lt;code&gt;{.exportc.}&lt;/code&gt; in the function signature. This is a &lt;em&gt;pragma&lt;/em&gt;, which provides metadata about a function to the Nim compiler. Here, &lt;code&gt;{.exportc.}&lt;/code&gt; mainly tells the compiler to keep the function name as it is in the &lt;code&gt;.nim&lt;/code&gt; file instead of mangling it, so we know our function will still be called &lt;code&gt;nim_add&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Now we need to compile this into a shared object to let Python call into it:&lt;/p&gt;
&lt;div class=&#34;code-block&#34;&gt;
  &lt;div class=&#34;code-block-header&#34;&gt;
    &lt;span class=&#34;code-block-dots&#34;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&#34;code-block-lang&#34;&gt;bash&lt;/span&gt;
  &lt;/div&gt;
  &lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;nim compile --app:lib called.nim&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;This will create a shared object file called &lt;code&gt;libcalled.so&lt;/code&gt; in your current directory.&lt;/p&gt;
&lt;h3 id=&#34;python&#34;&gt;Python&lt;/h3&gt;
&lt;p&gt;If you&amp;rsquo;ve used &lt;code&gt;cffi&lt;/code&gt; before this will be very familiar.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;calling.py&lt;/em&gt;&lt;/p&gt;
&lt;div class=&#34;code-block&#34;&gt;
  &lt;div class=&#34;code-block-header&#34;&gt;
    &lt;span class=&#34;code-block-dots&#34;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&#34;code-block-lang&#34;&gt;python&lt;/span&gt;
  &lt;/div&gt;
  &lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;from&lt;/span&gt; cffi &lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; FFI
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;ffi &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; FFI()
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;ffi&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;cdef(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&amp;#34;&amp;#34;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;    int nim_add(int num1, int num2);
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&amp;#34;&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;lib &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; ffi&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;dlopen(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;./libcalled.so&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;res &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; lib&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;nim_add(&lt;span style=&#34;color:#ae81ff&#34;&gt;5&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;10&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;print(res)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Here we create an FFI object and load in the function definition of &lt;code&gt;nim_add&lt;/code&gt; using the C function declaration syntax. There is an explanation on converting Nim types to C types in the Nim manual &lt;a href=&#34;https://nim-lang.org/docs/manual.html&#34;&gt;here&lt;/a&gt;. We then open our shared library from the current directory, and can call methods on the library object like any Python namespace.&lt;/p&gt;
&lt;h3 id=&#34;running-it&#34;&gt;Running it&lt;/h3&gt;
&lt;p&gt;Now we can just run the Python file like normal, and if the shared object file is in the expected place, &lt;code&gt;cffi&lt;/code&gt; will call into it and we can add two numbers together, but in Nim!&lt;/p&gt;
&lt;div class=&#34;code-block&#34;&gt;
  &lt;div class=&#34;code-block-header&#34;&gt;
    &lt;span class=&#34;code-block-dots&#34;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&#34;code-block-lang&#34;&gt;bash&lt;/span&gt;
  &lt;/div&gt;
  &lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;➜ python3 calling.py
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;15&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
</description>
      <author>Rowan Marshall</author>
      <guid>https://rowanajmarshall.co.uk/posts/calling-nim-from-python/</guid>
      <pubDate>Sun, 27 Jan 2019 00:00:00 +0000</pubDate>
    </item>
    
    <item>
      <title>Automatic memoization of function calls in Python</title>
      <link>https://rowanajmarshall.co.uk/posts/automatic-momoization/</link>
      <description>&lt;p&gt;The Fibonacci sequence is the integer sequence where each number is the sum of the previous two numbers, starting with zero and one:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;0, 1, 1, 2, 3, 5, 8, 13, 21, 34&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Writing a function to calculate an arbitrary fibonacci number is a common problem in interviews and tests, that has an elegant if inefficient way of being solved via &lt;a href=&#34;https://en.wikipedia.org/wiki/Recursion_(computer_science)&#34;&gt;recursion&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Here&amp;rsquo;s a simple solution in Python:&lt;/p&gt;
&lt;div class=&#34;code-block&#34;&gt;
  &lt;div class=&#34;code-block-header&#34;&gt;
    &lt;span class=&#34;code-block-dots&#34;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&#34;code-block-lang&#34;&gt;python&lt;/span&gt;
  &lt;/div&gt;
  &lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;def&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;fib&lt;/span&gt;(n):
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;if&lt;/span&gt; n &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;2&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#66d9ef&#34;&gt;return&lt;/span&gt; n
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;else&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#66d9ef&#34;&gt;return&lt;/span&gt; fib(n&lt;span style=&#34;color:#f92672&#34;&gt;-&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;) &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt; fib(n&lt;span style=&#34;color:#f92672&#34;&gt;-&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;2&lt;/span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Given an integer &lt;em&gt;n&lt;/em&gt;, this function will calculate the &lt;em&gt;n&amp;rsquo;th&lt;/em&gt; number in the fibonacci sequence. If we try benchmarking this using the ever-useful &lt;code&gt;timeout&lt;/code&gt; tool, using a value of &lt;em&gt;28&lt;/em&gt;:&lt;/p&gt;
&lt;div class=&#34;code-block&#34;&gt;
  &lt;div class=&#34;code-block-header&#34;&gt;
    &lt;span class=&#34;code-block-dots&#34;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&#34;code-block-lang&#34;&gt;bash&lt;/span&gt;
  &lt;/div&gt;
  &lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;➜ python3 -m timeit -s &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;import fib&amp;#39;&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;fib.fib(28)&amp;#39;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;2&lt;/span&gt; loops, best of 5: &lt;span style=&#34;color:#ae81ff&#34;&gt;109&lt;/span&gt; msec per loop&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Reasonably fast. But if we up this to &lt;em&gt;30&lt;/em&gt;&amp;hellip;&lt;/p&gt;
&lt;div class=&#34;code-block&#34;&gt;
  &lt;div class=&#34;code-block-header&#34;&gt;
    &lt;span class=&#34;code-block-dots&#34;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&#34;code-block-lang&#34;&gt;bash&lt;/span&gt;
  &lt;/div&gt;
  &lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;➜ python3 -m timeit -s &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;import fib&amp;#39;&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;fib.fib(30)&amp;#39;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt; loop, best of 5: &lt;span style=&#34;color:#ae81ff&#34;&gt;291&lt;/span&gt; msec per loop&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;A small increase almost tripled the algorithm&amp;rsquo;s runtime. If we increase this much more it will get exponentially slower to calculate, this being an &lt;code&gt;O(2^n)&lt;/code&gt; algorithm. Why is this so inefficient?&lt;/p&gt;
&lt;p&gt;To solve this, we need to look at how the function is being called. Here&amp;rsquo;s a diagram stolen from Stack Overflow showing the recursive tree of &lt;em&gt;fib(n)&lt;/em&gt; calls being made:&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;https://stackoverflow.com/questions/35959100/explanation-on-fibonacci-recursion&#34;&gt;&lt;img
  src=&#34;https://rowanajmarshall.co.uk/fib.png&#34;
  alt=&#34;https://stackoverflow.com/questions/35959100/explanation-on-fibonacci-recursion&#34;
  loading=&#34;lazy&#34;
  decoding=&#34;async&#34;
  class=&#34;full-width&#34;
/&gt;

&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;25 seperate calls altogether, with 18 of those being repeats. The percentage of repeated calls gets much worse as &lt;em&gt;n&lt;/em&gt; increases, meaning a lot of work is being done unnecessarily.&lt;/p&gt;
&lt;p&gt;How can we fix this, without resorting to changing our algorithm?&lt;/p&gt;
&lt;h3 id=&#34;introducing-the-lru-cache-decorator&#34;&gt;Introducing the LRU Cache decorator&lt;/h3&gt;
&lt;p&gt;Inside the &lt;code&gt;functools&lt;/code&gt; standard library package, there&amp;rsquo;s a decorator called &lt;code&gt;@lru_cache&lt;/code&gt;. This decorator records the arguments a function is called with and the value the function returned. If that function is called twice with the same arguments, the lru_cache will return the same value it got the first time, saving us the overhead of calling it twice. Since our issue is repeated function calls, we should expect a significant speedup.&lt;/p&gt;
&lt;p&gt;Lets try timing that again after adding the decorator:&lt;/p&gt;
&lt;div class=&#34;code-block&#34;&gt;
  &lt;div class=&#34;code-block-header&#34;&gt;
    &lt;span class=&#34;code-block-dots&#34;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&#34;code-block-lang&#34;&gt;python&lt;/span&gt;
  &lt;/div&gt;
  &lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;from&lt;/span&gt; functools &lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; lru_cache
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;@lru_cache&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;def&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;fib&lt;/span&gt;(n):
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;if&lt;/span&gt; n &lt;span style=&#34;color:#f92672&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#66d9ef&#34;&gt;return&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;elif&lt;/span&gt; n &lt;span style=&#34;color:#f92672&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#66d9ef&#34;&gt;return&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;else&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#66d9ef&#34;&gt;return&lt;/span&gt; fib(n&lt;span style=&#34;color:#f92672&#34;&gt;-&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;) &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt; fib(n&lt;span style=&#34;color:#f92672&#34;&gt;-&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;2&lt;/span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&#34;code-block&#34;&gt;
  &lt;div class=&#34;code-block-header&#34;&gt;
    &lt;span class=&#34;code-block-dots&#34;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&#34;code-block-lang&#34;&gt;bash&lt;/span&gt;
  &lt;/div&gt;
  &lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;➜ python3 -m timeit -s &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;import fib&amp;#39;&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;fib.fib(30)&amp;#39;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;2000000&lt;/span&gt; loops, best of 5: &lt;span style=&#34;color:#ae81ff&#34;&gt;105&lt;/span&gt; nsec per loop&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;That sped up our algorithm by about a million times. Not bad for one line of code.&lt;/p&gt;
</description>
      <author>Rowan Marshall</author>
      <guid>https://rowanajmarshall.co.uk/posts/automatic-momoization/</guid>
      <pubDate>Sat, 20 Jan 2018 00:00:00 +0000</pubDate>
    </item>
    
  </channel>
</rss>
