<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>python on Kai Striega</title><link>http://kaistriega.com/tags/python/</link><description>Recent content in python on Kai Striega</description><generator>Hugo</generator><language>en-us</language><lastBuildDate>Sun, 26 Jul 2026 11:20:00 +1000</lastBuildDate><atom:link href="http://kaistriega.com/tags/python/index.xml" rel="self" type="application/rss+xml"/><item><title>Pointer Chasing</title><link>http://kaistriega.com/blog/mechanical-sympathy/pointer-chasing/</link><pubDate>Sun, 26 Jul 2026 11:20:00 +1000</pubDate><guid>http://kaistriega.com/blog/mechanical-sympathy/pointer-chasing/</guid><description>&lt;p&gt;Everyone who writes Python for a living has been handed the same rule: don&amp;rsquo;t use a &lt;code&gt;list&lt;/code&gt;, use an array.
It&amp;rsquo;s a good rule. I&amp;rsquo;ve given it as advice myself, probably to someone who deserved a better explanation than
the one I gave.&lt;/p&gt;
&lt;p&gt;Here is the rule being followed, on two million integers:&lt;/p&gt;


&lt;div class="highlight"&gt;
 &lt;pre class="chroma"&gt;&lt;code&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;sum(values) &lt;span style="color:#75715e"&gt;# a plain list 10.0 ms&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;sum(values) &lt;span style="color:#75715e"&gt;# array(&amp;#39;q&amp;#39;) 19.2 ms&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Swapping the list for a compact array of machine integers made it &lt;strong&gt;twice as slow&lt;/strong&gt;. Nothing was measured
wrongly. That is what the machine does.&lt;/p&gt;</description></item><item><title>False Sharing</title><link>http://kaistriega.com/blog/mechanical-sympathy/false-sharing/</link><pubDate>Sat, 25 Jul 2026 14:01:54 +1000</pubDate><guid>http://kaistriega.com/blog/mechanical-sympathy/false-sharing/</guid><description>&lt;p&gt;&lt;a href="https://kaistriega.com/blog/mechanical-sympathy/array-of-structs-vs-struct-of-arrays/"&gt;Part one&lt;/a&gt; of this series had one central fact and one piece of advice. The fact was that memory moves
in 64 byte cache lines, so the cost of a loop is the number of lines it touches. The advice that fell out of
it was to pack your data tightly, because packing more useful bytes into each line means fetching fewer lines.&lt;/p&gt;
&lt;p&gt;This post is about a case where packing tightly is the entire bug, and the fix is to deliberately waste 56
bytes out of every 64.&lt;/p&gt;</description></item><item><title>Array of Structs vs Struct of Arrays</title><link>http://kaistriega.com/blog/mechanical-sympathy/array-of-structs-vs-struct-of-arrays/</link><pubDate>Sat, 25 Jul 2026 08:50:30 +1000</pubDate><guid>http://kaistriega.com/blog/mechanical-sympathy/array-of-structs-vs-struct-of-arrays/</guid><description>&lt;p&gt;I&amp;rsquo;ve been putting together a talk about &lt;a href="https://github.com/Kai-Striega/speeches/tree/main/things-I-dont-worry-about-as-NumPy-does-them-for-me"&gt;the things I don&amp;rsquo;t worry about, because NumPy does them for me&lt;/a&gt;:
the performance patterns I kept running into over seven years of reviewing NumPy code written by people far
smarter than me. What an array actually is. When &lt;code&gt;reshape&lt;/code&gt; quietly copies six gigabytes. What broadcasting
promises not to allocate. I did a practice run of it at &lt;a href="https://python.sydney/"&gt;SydPy&lt;/a&gt; recently.&lt;/p&gt;
&lt;p&gt;The NumPy-specific parts landed fine. Then I got to a slide which says that a copy isn&amp;rsquo;t slow because the CPU
is busy, it&amp;rsquo;s slow because the bytes have to physically &lt;em&gt;move&lt;/em&gt;; that main memory will only hand them over at
something like 20 GB/s; and that while that&amp;rsquo;s happening your cache is filling up with data you will never look
at again.&lt;/p&gt;</description></item><item><title>Curiously Related Words Preprocessing Our Data</title><link>http://kaistriega.com/blog/curiously-related-words/curiously-related-words-preprocessing-our-data/</link><pubDate>Sat, 07 Dec 2024 15:32:41 +1100</pubDate><guid>http://kaistriega.com/blog/curiously-related-words/curiously-related-words-preprocessing-our-data/</guid><description>&lt;p&gt;&lt;a href="https://kaistriega.com/blog/curiously-related-words/what-is-a-curiously-related-word/"&gt;Previously&lt;/a&gt; I&amp;rsquo;ve made up the
concept of a curiously connected word and a high level plan for finding such words. This post outlines the
interesting parts of how I parse EtymDB. For those who are interested in all the code, it is available on my
&lt;a href="https://github.com/Kai-Striega/curiously-connected-words/tree/main/src/neo4j_helper"&gt;GitHub&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id="the-data-we-have-and-why-thats-not-enough"&gt;The data we have, and why that&amp;rsquo;s not enough&lt;a class="headerlink" href="#the-data-we-have-and-why-thats-not-enough" title="Link to this heading"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;As outlined previously we have two sources of data:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;a href="https://paperswithcode.com/dataset/etymdb-2-0"&gt;EtymDB&lt;/a&gt; a database of words and their etymological relationships&lt;/li&gt;
&lt;li&gt;&lt;a href="https://radimrehurek.com/gensim/"&gt;gensim&lt;/a&gt; a library of &lt;a href="https://en.wikipedia.org/wiki/Word2vec"&gt;Word2vec&lt;/a&gt;
models that model the semantic relationship between words&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Our goal is to combine these two datasets into something nerdy. But just &lt;strong&gt;having&lt;/strong&gt; data isn&amp;rsquo;t enough. We need to be
able to retrieve, analyse and work with our datasets efficiently. Furthermore, our data isn&amp;rsquo;t really tabular. What
we have are words, with some associated properties, and relationships between them. When you hear &amp;ldquo;relationships&amp;rdquo;
you may be tempted to think of a relational database. While these are often the right choice, our data is a
&lt;a href="https://en.wikipedia.org/wiki/Graph_(discrete_mathematics)"&gt;graph&lt;/a&gt; which can be difficult to work with in relational
databases &lt;sup id="fnref:1"&gt;&lt;a href="#fn:1" class="footnote-ref" role="doc-noteref"&gt;1&lt;/a&gt;&lt;/sup&gt;. &lt;a href="https://neo4j.com/https://neo4j.com/"&gt;Neo4j&lt;/a&gt; provides a database that is specialised on graphs.
This seems like the right tool for the job. Plus I want to try a new tool&lt;sup id="fnref:2"&gt;&lt;a href="#fn:2" class="footnote-ref" role="doc-noteref"&gt;2&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;</description></item><item><title>Processes, Threads, Oh My!</title><link>http://kaistriega.com/blog/low-level-fundamentals/processes-threads-oh-my/</link><pubDate>Sun, 27 Oct 2024 13:51:52 +1100</pubDate><guid>http://kaistriega.com/blog/low-level-fundamentals/processes-threads-oh-my/</guid><description>&lt;h2 id="tldr"&gt;TLDR&lt;a class="headerlink" href="#tldr" title="Link to this heading"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Processes and threads are an integral part of programming&lt;/li&gt;
&lt;li&gt;They are an essential tool in any developer&amp;rsquo;s toolbox&lt;/li&gt;
&lt;li&gt;Processes is how the OS represents a running program.&lt;/li&gt;
&lt;li&gt;Threads are how the computer groups together instructions from your program and executes them&lt;/li&gt;
&lt;li&gt;You can have multiple processes and threads&lt;/li&gt;
&lt;li&gt;At least one thread runs &lt;em&gt;inside&lt;/em&gt; each process&lt;/li&gt;
&lt;li&gt;Tradeoffs of processes vs threads are:
&lt;ul&gt;
&lt;li&gt;Processes are slower to create than threads&lt;/li&gt;
&lt;li&gt;Processes own their memory, threads share memory between them&lt;/li&gt;
&lt;li&gt;It is easy to make very difficult to debug errors with threads&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="introduction"&gt;Introduction&lt;a class="headerlink" href="#introduction" title="Link to this heading"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;I have a friend who is currently trying to transition into software engineering. She is currently completing her masters
in IT. While nerding out together about tech, I mentioned multiprocessing. She said she&amp;rsquo;d never heard of it. That&amp;rsquo;s not
great, so I tried to explain it, but feel that I didn&amp;rsquo;t do a great job of it. This blog post will be my attempt to
clarify some of the essential concepts that a developer should know about processes and threads, focusing on how they
work in Linux. I&amp;rsquo;ll do this in a couple of parts:&lt;/p&gt;</description></item></channel></rss>