<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Rahul Jha</title>
    <description>The latest articles on DEV Community by Rahul Jha (@rj722).</description>
    <link>https://dev.to/rj722</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F26365%2F1256fde0-6c15-4725-881e-7cb7f3d74d1b.png</url>
      <title>DEV Community: Rahul Jha</title>
      <link>https://dev.to/rj722</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rj722"/>
    <language>en</language>
    <item>
      <title>Daily Log 17</title>
      <dc:creator>Rahul Jha</dc:creator>
      <pubDate>Tue, 06 Apr 2021 20:04:15 +0000</pubDate>
      <link>https://dev.to/rj722/daily-log-17-23fo</link>
      <guid>https://dev.to/rj722/daily-log-17-23fo</guid>
      <description>&lt;p&gt;Started reading the chapter "Python interpreter in Python" by Allison Kaptur.&lt;/p&gt;

</description>
      <category>100daysofcs</category>
    </item>
    <item>
      <title>Daily Log: 13, 14, 15, 16</title>
      <dc:creator>Rahul Jha</dc:creator>
      <pubDate>Mon, 05 Apr 2021 19:22:56 +0000</pubDate>
      <link>https://dev.to/rj722/daily-log-13-14-15-16-11i4</link>
      <guid>https://dev.to/rj722/daily-log-13-14-15-16-11i4</guid>
      <description>&lt;p&gt;From my last post:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;this might have to wait until the weekend.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The weekend came and went away, but this bug did not. In all honesty, I haven't gawked at the screen long enough. You know, sleeves up, pupils dilated, sight narrowed down to a beautiful 8 pixel wide monospace, and then suddenly, it occurs to you. The "flow" as some might call it.&lt;/p&gt;

&lt;p&gt;My excuse, like the 99% of you, is time. This week again, jampacked with the final batch of backlog from the last quarter, which I don't want to carry forward. I'm also taking a little break from work (and everything else) which means a smaller week for me. I don't think that I'd be able to get back on track with "clox" right away.&lt;/p&gt;

&lt;p&gt;The good news is that I've another something to munch on while I wait for "the flow" (I use the term, almost mockingly, despite it being a real, physiological phenomenon; to assert on &lt;em&gt;spending time&lt;/em&gt; and &lt;em&gt;doing the work&lt;/em&gt; and not on waiting for the flow to occur magically). This just arrived in the mailbox:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0l-wPdNB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hzji73sd7czs3s1qhqkr.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0l-wPdNB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hzji73sd7czs3s1qhqkr.jpg" alt="Book: 500 Lines or less"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This book documents the journey of design decisions taken by experienced programmers, the likes of Guido van Rossum and Ned Batchelder, when they start working on something large (complicated) which is still small in size, a limit artificially enforced in the book, ergo 500 lines or less. from "Building a web crawler with asyncio routines" to writing a "Python interpreter in Python" under 500 lines of code.&lt;/p&gt;

&lt;p&gt;As attractive and luring as it is, I still doubt if I'd be able to push another attractive post this week. 🤞&lt;/p&gt;

</description>
      <category>100daysofcs</category>
    </item>
    <item>
      <title>Daily Log: 12</title>
      <dc:creator>Rahul Jha</dc:creator>
      <pubDate>Thu, 01 Apr 2021 18:54:31 +0000</pubDate>
      <link>https://dev.to/rj722/daily-log-11-1edd</link>
      <guid>https://dev.to/rj722/daily-log-11-1edd</guid>
      <description>&lt;p&gt;This bug is turning out quite hard to squish, and I don't see an obvious place to start looking for.&lt;/p&gt;

&lt;p&gt;Guests came over, and as usual, lots of work. New Year. New Quarter. New responsibilities. New Goals. Wish me luck!&lt;/p&gt;

&lt;p&gt;Time crunch tomorrow as well, and I don't quite like the idea of going forward with this buggy implementation, so this might have to wait until the weekend.&lt;/p&gt;

</description>
      <category>100daysofcs</category>
    </item>
    <item>
      <title>Daily Log: Day 11</title>
      <dc:creator>Rahul Jha</dc:creator>
      <pubDate>Wed, 31 Mar 2021 18:48:41 +0000</pubDate>
      <link>https://dev.to/rj722/daily-log-day-11-i1i</link>
      <guid>https://dev.to/rj722/daily-log-day-11-i1i</guid>
      <description>&lt;p&gt;With trying to fixing a broken sleep cycle, productivity has plummeted quite a lot. Didn't get a whole lot of time to debug the bytecodes-executing-out-of-sequence bug.&lt;/p&gt;

</description>
      <category>100daysofcs</category>
    </item>
    <item>
      <title>Daily Log: Day 9 &amp; 10</title>
      <dc:creator>Rahul Jha</dc:creator>
      <pubDate>Tue, 30 Mar 2021 05:37:50 +0000</pubDate>
      <link>https://dev.to/rj722/log-day-9-10-550o</link>
      <guid>https://dev.to/rj722/log-day-9-10-550o</guid>
      <description>&lt;ul&gt;
&lt;li&gt;The stack we talked about in the last post is to hold intermediary values generated by our interpreter, e.g.:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  fun echo(n):
      print n;
      return n;

  print echo(echo(1) + echo(2)) + (echo(4) + echo(5));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;While our interpreter is computing &lt;code&gt;echo(2)&lt;/code&gt;, it would need some space in memory to hold the output from &lt;code&gt;echo(1)&lt;/code&gt; -- That's where the stack comes in. Similarly, it would need to store the output of &lt;code&gt;echo(1) + echo(2)&lt;/code&gt;, while it computes &lt;code&gt;echo(4) + echo(5)&lt;/code&gt;. These "produced" values are all pushed in a stack.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;After implementing the stack, noticed that the order of execution of opcodes is not correct in the last output:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;== Test Chunk ==
0000  122 OP_CONSANT          0 '1.2'
0002  123 OP_CONSANT          1 '456'
0004  124 OP_RETURN
0001    | OP_CONSANT          0 '1.2'
1.2
0003    | OP_RETURN
456
0005    0 OP_CONSANT          0 '1.2'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;chunk.code&lt;/code&gt; should look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;OP_CONSTANT
0 // Address of 1.2 in chunk.values
OP_CONSTANT
1 // Address of 456 in chunk.values
OP_RETURN
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and &lt;code&gt;chunk.values&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1.2
456
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, notice that &lt;code&gt;OP_RETURN&lt;/code&gt; was executed before the last &lt;code&gt;OP_CONSTANT&lt;/code&gt;. Still debugging why.&lt;/p&gt;

</description>
      <category>100daysofcs</category>
    </item>
    <item>
      <title>Implementing the heart of clox's VM</title>
      <dc:creator>Rahul Jha</dc:creator>
      <pubDate>Sat, 27 Mar 2021 22:13:38 +0000</pubDate>
      <link>https://dev.to/rj722/implementing-a-vm-to-execute-the-instructions-3jcp</link>
      <guid>https://dev.to/rj722/implementing-a-vm-to-execute-the-instructions-3jcp</guid>
      <description>&lt;p&gt;Halfway through Chapter 2 of Crafting Interpreters. We are implementing a VM to "execute" the instructions we had managed to store in chunks in the last post.&lt;/p&gt;

&lt;p&gt;So far, we've added an &lt;code&gt;interpret&lt;/code&gt; method, which initializes the Instruction Pointer (a pointer which always points to the next instruction to execute) and calls a &lt;code&gt;run&lt;/code&gt; method, which just keeps iterating over the &lt;code&gt;vm.chunk&lt;/code&gt; which consists of  opcodes and their operands, and runs corresponding C code. It looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;InterpretResult&lt;/span&gt; &lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="cp"&gt;#define READ_BYTE() (*vm.ip++)
&lt;/span&gt;    &lt;span class="cp"&gt;#define READ_CONSTANT() (vm.chunk-&amp;gt;constants.values[READ_BYTE()])
&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(;;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;uint8_t&lt;/span&gt; &lt;span class="n"&gt;instruction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;READ_BYTE&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="cp"&gt;#ifdef DEBUG_TRACE_EXECUTION
&lt;/span&gt;            &lt;span class="n"&gt;disassembleInstruction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;vm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="n"&gt;vm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ip&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;vm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;chunk&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;code&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
        &lt;span class="cp"&gt;#endif
&lt;/span&gt;        &lt;span class="k"&gt;switch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;instruction&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;OP_RETURN&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;INTERPRET_OK&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;OP_CONSTANT&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;Value&lt;/span&gt; &lt;span class="n"&gt;constant&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;READ_CONSTANT&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
                &lt;span class="c1"&gt;// For now, let's just print the value&lt;/span&gt;
                &lt;span class="n"&gt;printValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;constant&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
                &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
                &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="cp"&gt;#undef READ_CONSTANT
&lt;/span&gt;    &lt;span class="cp"&gt;#undef READ_BYTE
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Not a whole lot useful, you'd say but hey, we don't have any supporting infrastructure right now. Bob tells me that this is the heart of the interpreter we are building and it will spend some 90% of its time here.&lt;/p&gt;

&lt;p&gt;For the same "program" we embedded in &lt;a href="https://dev.to/rj722/laying-down-ground-work-for-a-bytecode-virtual-machine-5614"&gt;our last post&lt;/a&gt;, this outputs:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;== Test Chunk ==
0000  122 OP_CONSANT          0 '1.2'
0002  123 OP_CONSANT          1 '456'
0004    | OP_RETURN
1.2
456
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here on, I see that we're going to build a stack -- for holding values of local variables? perhaps. Let's find out!&lt;/p&gt;




&lt;p&gt;Housekeeping announcement: I'm not really enjoying writing daily posts. Way too less time to cram the interesting, juicy  stuff. The entire point of this was to learn CS every day, I added daily posts to keep myself accountable. I'll still be pushing a bullet-point-style log every day, and push interesting long form posts only when I have something interesting to write about.&lt;/p&gt;

</description>
      <category>100daysofcs</category>
      <category>computerscience</category>
      <category>c</category>
    </item>
    <item>
      <title>Laying down ground work for a Bytecode Virtual Machine</title>
      <dc:creator>Rahul Jha</dc:creator>
      <pubDate>Fri, 26 Mar 2021 04:13:35 +0000</pubDate>
      <link>https://dev.to/rj722/laying-down-ground-work-for-a-bytecode-virtual-machine-5614</link>
      <guid>https://dev.to/rj722/laying-down-ground-work-for-a-bytecode-virtual-machine-5614</guid>
      <description>&lt;p&gt;Missed writing an entry yesterday, didn't miss coding though. Already done with laying down the foundation of &lt;code&gt;clox&lt;/code&gt;, our Bytecode VM.&lt;/p&gt;

&lt;p&gt;(I'm gonna be making this post very brief, because well, Bob already walks us through the entire code in the book -- &lt;a href="https://craftinginterpreters.com/chunks-of-bytecode.html"&gt;https://craftinginterpreters.com/chunks-of-bytecode.html&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;All we can do with &lt;code&gt;clox&lt;/code&gt; right now is make a chunk and write to it. There is no front-end to the compiler, no interpretation of the code. Heck, we don't even have the full  list of opcodes (each opcode is a unique byte which corresponds to a special instruction of our language, e.g., &lt;code&gt;OP_RETURN&lt;/code&gt; for returning from a function.) yet.&lt;/p&gt;

&lt;h2&gt;
  
  
  Chunk. What chunk?
&lt;/h2&gt;

&lt;p&gt;Consider a chunk as a sequence of codes, each a byte long. Now, these bytes might &lt;em&gt;mean&lt;/em&gt; different things: either opcodes or pointers to the data those opcodes need. But, that's all a chunk is: A sequence of bytes.&lt;/p&gt;

&lt;p&gt;How do we know which is which?&lt;/p&gt;

&lt;p&gt;To interpret the meaning of these bytes, we need to iterate through the entire sequence, and match it against a list of values:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Say, the first value we encounter is &lt;code&gt;0x10&lt;/code&gt;. Let's assume that we had mapped it to &lt;code&gt;OP_RETURN&lt;/code&gt;. (It is very unlikely for the &lt;em&gt;first&lt;/em&gt; opcode to be &lt;code&gt;OP_RETURN&lt;/code&gt;, but bear it for it makes the example easy).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Next byte in the chunk might correspond to &lt;code&gt;OP_CONSTANT&lt;/code&gt;.  Whenever we store a &lt;code&gt;OP_CONSTANT&lt;/code&gt;, we also need to store the value this constant holds. By convention, the next byte holds the address of this value.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You can see a pattern here. Each opcode may have zero or more operands -- so our compiler can easily figure out which is which.&lt;/p&gt;

&lt;p&gt;For now, all it does is exactly that. We hand-write a bunch of values in the chunk, like this, and let it "disassemble" it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;initChunk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;constant&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;addConstant&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// 122 is an arbitrary line number I passed.&lt;/span&gt;
&lt;span class="n"&gt;writeChunk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;OP_CONSTANT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;122&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;writeChunk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;constant&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;122&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;constant2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;addConstant&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;456&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;writeChunk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;OP_CONSTANT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;writeChunk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;constant2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="n"&gt;writeChunk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;OP_RETURN&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="n"&gt;disassembleChunk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Test Chunk"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our disassembler outputs this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;== Test Chunk ==
0000  122 OP_CONSANT          0 '1.2'
0002  123 OP_CONSANT          1 '456'
0004    | OP_RETURN
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>100daysofcs</category>
      <category>c</category>
    </item>
    <item>
      <title>Day 005: Blew off another day</title>
      <dc:creator>Rahul Jha</dc:creator>
      <pubDate>Wed, 24 Mar 2021 00:13:57 +0000</pubDate>
      <link>https://dev.to/rj722/day-005-blew-off-another-day-5ahc</link>
      <guid>https://dev.to/rj722/day-005-blew-off-another-day-5ahc</guid>
      <description>&lt;p&gt;Lots of busy work today. As the quarter is coming to end, I'm trying to wrap up everything at work. Only worked for 10 minutes or so before getting interrupted and going down another rabbit hole.&lt;/p&gt;

&lt;p&gt;But, since you're here: do you know that you can use &lt;code&gt;vars&lt;/code&gt; instead of calling the &lt;code&gt;__dict__&lt;/code&gt; method on an instance/class to get a dictionary of all the attributes? Very nifty.&lt;/p&gt;

</description>
      <category>100daysofcs</category>
    </item>
    <item>
      <title>Day 004: On Showing Up</title>
      <dc:creator>Rahul Jha</dc:creator>
      <pubDate>Tue, 23 Mar 2021 00:07:20 +0000</pubDate>
      <link>https://dev.to/rj722/day-004-on-showing-up-k54</link>
      <guid>https://dev.to/rj722/day-004-on-showing-up-k54</guid>
      <description>&lt;p&gt;This challenge is not about as much about learning or CS, as much it is about discipline and showing up. Showing up everyday, despite everything. The virtue Neil Gaiman talks about when we says:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Husband runs off with politican.&lt;br&gt;
MAKE GOOD ART.&lt;/p&gt;

&lt;p&gt;Leg crushed and then eaten by mutated Boa constrictor?&lt;br&gt;
MAKE GOOD ART.&lt;/p&gt;

&lt;p&gt;IRS on your trail?&lt;br&gt;
MAKE GOOD ART.&lt;/p&gt;

&lt;p&gt;Cat exploded?&lt;br&gt;
MAKE GOOD ART.&lt;/p&gt;

&lt;p&gt;Somebody on the internet thinks what you're doing is stupid, or evil, or has been done before?&lt;br&gt;
MAKE GOOD ART.&lt;/p&gt;

&lt;p&gt;Probably things will work out somehow, and eventually take the sting away, but that doesn't matter, DO ONLY WHAT YOU DO BEST.&lt;br&gt;
MAKE GOOD ART.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So, I'm showing up here, everyday, to make good art. Of course, life gets in it's way, as it did today and I couldn't give a whole lot of time to learning, and towards making that compiler or writing about it. But, I decided to show up nevertheless!&lt;/p&gt;

</description>
      <category>writing</category>
      <category>100daysofcs</category>
    </item>
    <item>
      <title>Day 003: Crafting Interpreters</title>
      <dc:creator>Rahul Jha</dc:creator>
      <pubDate>Sun, 21 Mar 2021 23:00:33 +0000</pubDate>
      <link>https://dev.to/rj722/day-003-crafting-interpreters-mkc</link>
      <guid>https://dev.to/rj722/day-003-crafting-interpreters-mkc</guid>
      <description>&lt;p&gt;In the winter of 2019, protests erupted across my city when an anti-Muslim bill (now act) – CAA – was proposed in the Indian parliament. To stop the protestors from organizing, the city was cut off from the Internet. The shutdown (which would last for 8 days) didn't stop the protestors, but in a strange turn of events, it helped me finally pick up and start reading &lt;a href="https://craftinginterpreters.com/contents.html"&gt;Bob Nystrom's Crafting Interpreters&lt;/a&gt;, wherein he walked us through the design and development of a language he called "Lox".&lt;/p&gt;

&lt;p&gt;In the first half, we created "jlox", a Java implementation of the language. He in fact walked us through &lt;strong&gt;every&lt;/strong&gt; line of Java code required to build the language from the ground up.&lt;/p&gt;

&lt;p&gt;Partly, because I didn't want to do a copy-paste job with the code, but to be engaged with the material, and partly – em, mostly –  because I don't know Java, I decided I would write it in Python. I got started, and one word at a time, I fell in love with it (and Bob and his writing). I got hard at work, borderline maniac, cranking at my computer screen almost every waking moment. And it took me exactly 8 days, and &lt;a href="https://github.com/RJ722/plox"&gt;plox&lt;/a&gt; was finally there. It was very rewarding, seeing it alive and breathing.&lt;/p&gt;

&lt;p&gt;The next half of the book discussed the implementation of the same language in C. It was called, you guessed it .. "clox". It wasn't just jlox written in c instead of Java, but it followed another execution model, the &lt;strong&gt;Bytecode virtual machine&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;You see, jlox and plox were both "interpreters" for Lox. They worked by what's called "walking the tree", that is to say, they parse the user's program into a tree structure (which is very similar to ASTs I talk about in my &lt;a href="https://dev.to/deepsource/static-code-analysis-what-it-is-how-to-use-it-4b79"&gt;other post&lt;/a&gt;) and then executed a block of code in Java/Python corresponding to each node in that tree. This made them heavily reliant on the machinery provided by the source language. &lt;/p&gt;

&lt;p&gt;clox, on the other hand, would compile the program to bytecode (a series of instructions, many of which are one byte long, hence the name) and this bytecode would then be fed to a virtual machine which will execute those instructions and run the program.&lt;/p&gt;

&lt;p&gt;I found this interesting and planned to finish the other half the next week, and that plan never got to &lt;em&gt;C&lt;/em&gt; the light of the day.&lt;/p&gt;

&lt;p&gt;But, I've scratched this itch to go, complete that part enough times for me to commit to it, and what better time than this 100-day challenge. This should hopefully also solve the problem of me spending way more time thinking about &lt;em&gt;what&lt;/em&gt; to write for the challenge, instead of, you know, writing it.&lt;/p&gt;

</description>
      <category>100daysofcs</category>
    </item>
    <item>
      <title>Day 002: Why are hashes cached in dictionaries?</title>
      <dc:creator>Rahul Jha</dc:creator>
      <pubDate>Sat, 20 Mar 2021 23:03:29 +0000</pubDate>
      <link>https://dev.to/rj722/day-002-why-are-hashes-cached-in-dictionaries-27pd</link>
      <guid>https://dev.to/rj722/day-002-why-are-hashes-cached-in-dictionaries-27pd</guid>
      <description>&lt;p&gt;Taking a closer look at &lt;a href="https://dev.to/rj722/day-001-how-do-python-dictionaries-work-5aa2"&gt;how key-value pairs are stored in Python dictionaries&lt;/a&gt; yesterday, I noticed that alongside storing the key+value, the hash of the key is also stored.&lt;/p&gt;

&lt;p&gt;Why would that be?&lt;/p&gt;

&lt;p&gt;It can't be to speed up the lookup, as we cannot get to the data (the tuple where the hash+key+value is stored) until we know it's address, and address can only be known by computing the hash first.&lt;/p&gt;

&lt;p&gt;Why then?&lt;/p&gt;

&lt;p&gt;Turns out that when resizing the &lt;code&gt;addresses&lt;/code&gt; vector, the hash for all the keys in &lt;code&gt;items&lt;/code&gt; needs to be recomputed. This is expensive to calculate, thus slowing down the resize operation. Caching the hash speeds things up:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;resize_to&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;new_addresses&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;entry&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;enumerate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;entries&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
         &lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;perturb&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;entry&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;  &lt;span class="c1"&gt;# No need to compute the hash.
&lt;/span&gt;         &lt;span class="n"&gt;addr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;
         &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;new_addresses&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
             &lt;span class="n"&gt;addr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;addr&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;perturb&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;
             &lt;span class="n"&gt;perturb&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;
         &lt;span class="n"&gt;new_addresses&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;addr&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;new_addresses&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Other than this, storing the hash has another benefit -- during lookup, there's an inequality test required, which ensures that we've arrived at the correct hash+key+value pair, and need not probe any further.&lt;/p&gt;

&lt;p&gt;The lookup code from the earlier post:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;lookup&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;perturb&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;hash&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;address&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;addresses&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;address&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="c1"&gt;# &amp;lt;- inequality test
&lt;/span&gt;
        &lt;span class="n"&gt;address&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;address&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;perturb&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;
        &lt;span class="n"&gt;perturb&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;address&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Comparing two objects for [in]equality can be an expensive operation. The solution here is to rely on the fact that "if two objects have unequal hashes, then the objects must be unequal as well", and use an equality checker like below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;fast_is_equal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;target_key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;key_hash&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;target_key_hash&lt;/span&gt;
&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="n"&gt;target_key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;  &lt;span class="c1"&gt;# Identity implies equality
&lt;/span&gt;    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;hash_key&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;hash_target_key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;hash_key&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And then during the lookup:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# ...
&lt;/span&gt;    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;fast_is_equal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="c1"&gt;# reference: items[address] -&amp;gt; (hash, key, value)
&lt;/span&gt;        &lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;address&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;address&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;h&lt;/span&gt;
    &lt;span class="p"&gt;):&lt;/span&gt;
&lt;span class="c1"&gt;# ...
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, my initial guess was right in a way: it does indeed speeds up the lookup, just not in a way I thought it'd be.&lt;/p&gt;

</description>
      <category>100daysofcs</category>
      <category>python</category>
      <category>computerscience</category>
      <category>todayilearned</category>
    </item>
    <item>
      <title>How do Python Dictionaries work?</title>
      <dc:creator>Rahul Jha</dc:creator>
      <pubDate>Sat, 20 Mar 2021 03:02:40 +0000</pubDate>
      <link>https://dev.to/rj722/day-001-how-do-python-dictionaries-work-5aa2</link>
      <guid>https://dev.to/rj722/day-001-how-do-python-dictionaries-work-5aa2</guid>
      <description>&lt;p&gt;In this post, we'll dissect how a dictionary is represented in Python, and the hash-scheme it uses to achieve constant-time lookup.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="s"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Rahul Jha"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s"&gt;"alias"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"RJ722"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s"&gt;"music"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"country"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s"&gt;"cheat_diet"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"pasta"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When we first create a dictionary, it internally maintains a  list. This list is used to store "data" about the items in the dictionary, and comprises tuples in the following format:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;
   &lt;span class="nb"&gt;hash&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
   &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each tuple referred to as a "row", represents a single key-value pair in the dictionary. When a new pair is added to the dictionary, it is appended to this list. This forms the "database" of the dictionary.&lt;/p&gt;

&lt;p&gt;For our example above, the database will look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="p"&gt;[(&lt;/span&gt;&lt;span class="mi"&gt;7409878836864405608&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'name'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'Rahul Jha'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
 &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2620441572017060194&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'alias'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'RJ722'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
 &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;205288631654089410&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'music'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'country'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
 &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;405166953458871902&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'cheat_diet'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'pasta'&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Along with creating the database, when a new dictionary is created, a chunk of memory is "blocked". This is used for addressing the keys, to say, which row in the database should be returned for a corresponding key. More on it later.&lt;/p&gt;

&lt;p&gt;The size of this chunk determines the number of &lt;em&gt;open addresses&lt;/em&gt; available. If all the open addresses are used up, a new memory chunk (usually double in size) is allocated and any values stored in these addresses are copied over. Generally, this is done when &lt;code&gt;addresses&lt;/code&gt; are two-thirds (or 66%) full.&lt;/p&gt;

&lt;p&gt;For the sake of this example, let's represent the "database" by a list &lt;code&gt;items&lt;/code&gt; and the blocked memory chunk by a list &lt;code&gt;addresses&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;items&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;  &lt;span class="c1"&gt;# number of open addresses available
&lt;/span&gt;&lt;span class="n"&gt;addresses&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Addressing
&lt;/h2&gt;

&lt;p&gt;To lookup a key in a dictionary, one need not search through the entire database. That way it won't be possible to maintain a constant lookup time.&lt;/p&gt;

&lt;p&gt;The pairs are addressed instead. The address/position of each value in the database is computed based on the [hash] of the key.&lt;/p&gt;

&lt;p&gt;To perform a lookup, one can simply hash the key, use it to compute the address, and read the value at the given address.&lt;/p&gt;

&lt;p&gt;If the hashes for two or more keys collide, a technique called &lt;strong&gt;Open Address Multihashing&lt;/strong&gt; helps resolve the address. Before understanding multihashing, let's first look at a simpler technique:&lt;/p&gt;

&lt;h3&gt;
  
  
  Linear Probing
&lt;/h3&gt;

&lt;p&gt;If there's a collision for address &lt;code&gt;i&lt;/code&gt;, the value, check &lt;code&gt;i+1&lt;/code&gt; address. If another value is stored at &lt;code&gt;i+2&lt;/code&gt; check for &lt;code&gt;i+3&lt;/code&gt;, and so on until you find an empty slot:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;insert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;hash&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;append&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="n"&gt;position&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="c1"&gt;# the index of this row
&lt;/span&gt;
    &lt;span class="n"&gt;address&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;addresses&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;address&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;address&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;address&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;

    &lt;span class="c1"&gt;# Store the position at which the tuple can be found
&lt;/span&gt;    &lt;span class="n"&gt;addresses&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;address&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;position&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's try it out:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;insert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Rahul Jha"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;insert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"alias"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"RJ722"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;insert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"music"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"country"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;insert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"cheat_diet"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"pasta"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;pprint&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;addresses&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;#  [(7409878836864405608, 'name', 'Rahul Jha'),
#   (2620441572017060194, 'alias', 'RJ722'),
#   (205288631654089410, 'music', 'country'),
#   (405166953458871902, 'cheat_diet', 'pasta')]
&lt;/span&gt;
&lt;span class="n"&gt;pprint&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;#  [0, None, 1, 2, None, None, 3, None]
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Similarly, when looking up, one needs to probe adjacent addresses until the matching key is found:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;lookup&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;hash&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;address&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;addresses&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;address&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;address&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;address&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;address&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lookup&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# "Rahul Jha"
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, if a large number of addresses collide, linear probing can lead to a &lt;em&gt;catastrophic linear pile-up&lt;/em&gt;, causing long chains of search and slowing down the lookup.&lt;/p&gt;

&lt;p&gt;Note that the implementation above is incomplete: Removing keys can create "holes" which will break the chain, and probing would not be able to find keys that had a collision. The solution is to use mark the slot as &lt;code&gt;DUMMY&lt;/code&gt; to serve as a placeholder. This technique is known as "Knuth – Algorithm D".&lt;/p&gt;

&lt;h3&gt;
  
  
  Open Address Multihashing
&lt;/h3&gt;

&lt;p&gt;Rather than probing adjacent addresses, a new address is generated by re-hashing, based on both the prior hash value and the number of probes so far.&lt;/p&gt;

&lt;p&gt;We add a pseudo-random number to the address probe – generated by a "simple linear-congruential random number generator": &lt;code&gt;i = 5 * i + 1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This, however, again scans &lt;code&gt;addresses&lt;/code&gt; in a fixed order. To avoid this, the original hash is also added to the random number. This hash is then shifted 5 bits on each recurrence. This way the sequence depends (eventually) on every bit in the hash code and the pseudo-scrambling more aggressive:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;insert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;hash&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;append&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="n"&gt;position&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="c1"&gt;# the index of this row
&lt;/span&gt;
    &lt;span class="n"&gt;perturb&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;h&lt;/span&gt;
    &lt;span class="n"&gt;address&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;addresses&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;address&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;address&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;address&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;perturb&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;
        &lt;span class="n"&gt;perturb&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;

    &lt;span class="c1"&gt;# Store the position at which the tuple can be found
&lt;/span&gt;    &lt;span class="n"&gt;addresses&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;address&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;position&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Carrying forward the same example as above, we can generate the addresses:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;insert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Rahul Jha"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;insert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"alias"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"RJ722"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;insert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"music"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"country"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;insert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"cheat_diet"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"pasta"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;pprint&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;#  [(-6473745080427136004, 'name', 'Rahul Jha'),
#   (-1887375983119989115, 'alias', 'RJ722'),
#   (-4671665682772832904, 'music', 'country'),
#   (3463812311448012107, 'cheat_diet', 'pasta')]
&lt;/span&gt;
&lt;span class="n"&gt;pprint&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;addresses&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# [2, None, None, 3, 0, 1, None, None]
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;During lookup, we resolve collisions using the same recurrence pattern:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;lookup&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;perturb&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;hash&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;address&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;addresses&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;address&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;address&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;address&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;perturb&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;
        &lt;span class="n"&gt;perturb&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;address&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lookup&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;  &lt;span class="c1"&gt;# "Rahul Jha"
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;Python uses two tables: one to store hash/key/value densely, and a separate, sparse table of indices that vectors the dense table.&lt;/p&gt;

&lt;p&gt;It wasn't always this way. As Raymond Hettinger explains brilliantly in his talk &lt;a href="https://www.youtube.com/watch?v=p33CVV29OG8"&gt;"Modern Dictionaries"&lt;/a&gt; (which also provided the fodder for this post), dictionaries in Python 2.7 didn't have two tables. There was no table for addressing, only data -- A big sparse table. This lead to a lot of space being wasted in empty holes, and consequently, dictionaries being giant until Python3.6 (wherein this vector scheme was added by Hettinger).&lt;/p&gt;




&lt;p&gt;Fun Question: Why do you think key hashes are also stored along with data?&lt;/p&gt;

</description>
      <category>100daysofcs</category>
      <category>python</category>
      <category>programming</category>
      <category>computerscience</category>
    </item>
  </channel>
</rss>
