<rss version="2.0">
    <channel>
        <title>David Bach</title>
        <link>https://david-bach.com</link>
        <description>Essays, projects and thoughts from David Bach</description>
        <language>en</language>
        <lastBuildDate>Mon, 23 Nov 2026 13:08:38 +0000</lastBuildDate>
        
            <item>
                <title>Comptime is funtime: Container-level Mutable State</title>
                <description>&lt;h1&gt;Comptime is funtime: Container-level Mutable State&lt;/h1&gt;&lt;p&gt;In my free time, I have been following Casey Muratori’s excellent &lt;a href=&quot;https://www.computerenhance.com/p/table-of-contents&quot;&gt;Performance-Aware Programming Series&lt;/a&gt;. In Part 2: Basic Profiling, the task is to build a simple profiler. While doing so, I discovered a neat feature of Zig’s comptime and generics.&lt;/p&gt;&lt;p&gt;The profiler is supposed to be easy to use, accurate, and, crucially, have low overhead. It has the three global variables shown below. These are not thread-safe, as we intentionally only want to profile single-threaded programs:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;A buffer of spans with a fixed capacity holding the timings for each profiling block.&lt;/li&gt;&lt;li&gt;The current index.&lt;/li&gt;&lt;li&gt;The current parent index.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;The fixed size means we can only have up to that number of unique spans. That is fine because we are instrumenting our code and it is unlikely we want to time more than &lt;code&gt;4096&lt;/code&gt; &lt;strong&gt;unique&lt;/strong&gt; functions or blocks.&lt;/p&gt;&lt;pre&gt;&lt;code class=&quot;zig&quot;&gt;&lt;span class=&quot;keyword&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;constant variable_builtin type variable&quot;&gt;spans&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;punctuation_bracket&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;number&quot;&gt;4096&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;constant variable_builtin type variable&quot;&gt;Span&lt;/span&gt; &lt;span class=&quot;operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;constant_builtin&quot;&gt;undefined&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;keyword&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;constant variable_builtin type variable&quot;&gt;current_index&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;constant variable_builtin type variable&quot;&gt;SpanIndex&lt;/span&gt; &lt;span class=&quot;operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;punctuation_delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;constant variable_builtin type variable&quot;&gt;reserved&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;keyword&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;constant variable_builtin type variable&quot;&gt;current_parent_index&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;constant variable_builtin type variable&quot;&gt;SpanIndex&lt;/span&gt; &lt;span class=&quot;operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;constant_builtin&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;keyword_modifier&quot;&gt;pub&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;constant variable_builtin type variable&quot;&gt;Span&lt;/span&gt; &lt;span class=&quot;operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;keyword_type&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;punctuation_bracket&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;constant variable_builtin type variable_member variable&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;punctuation_bracket&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;type_builtin&quot;&gt;u8&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;constant variable_builtin type variable_member variable&quot;&gt;elapsed&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;type_builtin&quot;&gt;u64&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;constant variable_builtin type variable_member variable&quot;&gt;elapsed_children&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;type_builtin&quot;&gt;u64&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;constant variable_builtin type variable_member variable&quot;&gt;hit_count&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;type_builtin&quot;&gt;u64&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;punctuation_bracket&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;constant variable_builtin type variable&quot;&gt;SpanIndex&lt;/span&gt; &lt;span class=&quot;operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;keyword_type&quot;&gt;enum&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;type_builtin&quot;&gt;u32&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;punctuation_bracket&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;constant variable_builtin type variable_member variable&quot;&gt;reserved&lt;/span&gt; &lt;span class=&quot;operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;constant variable_builtin type variable_member variable&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;punctuation_bracket&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Each span is identified by its &lt;code&gt;name&lt;/code&gt;, and it tracks how much time has elapsed, how much time its children took, and how many times it has been called. When we instrument our code, spans will be written into the &lt;code&gt;spans&lt;/code&gt; buffer in the order of invocation. If a function that we profile is called twice, we don’t add another span, we increment the existing span’s values.&lt;/p&gt;&lt;p&gt;The API has two functions, &lt;code&gt;beginBlock&lt;/code&gt; and &lt;code&gt;endBlock&lt;/code&gt;, and can be used in the following way:&lt;/p&gt;&lt;pre&gt;&lt;code class=&quot;zig&quot;&gt;&lt;span class=&quot;keyword_modifier&quot;&gt;pub&lt;/span&gt; &lt;span class=&quot;keyword_function&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;constant variable_builtin type variable function&quot;&gt;beginBlock&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;keyword_modifier&quot;&gt;comptime&lt;/span&gt; &lt;span class=&quot;constant variable_builtin variable_parameter variable type&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;punctuation_bracket&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;type_builtin&quot;&gt;u8&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;constant variable_builtin type variable&quot;&gt;SpanBlock&lt;/span&gt; &lt;span class=&quot;punctuation_bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;keyword_modifier&quot;&gt;pub&lt;/span&gt; &lt;span class=&quot;keyword_function&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;constant variable_builtin type variable function&quot;&gt;endBlock&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;constant variable_builtin variable_parameter variable type&quot;&gt;block&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;constant variable_builtin type variable&quot;&gt;SpanBlock&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;type_builtin&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;punctuation_bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;keyword_function&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;constant variable_builtin type variable function&quot;&gt;readFile&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;operator&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;type_builtin&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;punctuation_bracket&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;constant variable_builtin type variable&quot;&gt;block&lt;/span&gt; &lt;span class=&quot;operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;constant variable_builtin type variable&quot;&gt;profile&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;type constant variable_builtin function_call variable_member variable&quot;&gt;beginBlock&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;string&quot;&gt;&amp;quot;readFile&amp;quot;&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;keyword&quot;&gt;defer&lt;/span&gt; &lt;span class=&quot;constant variable_builtin type variable&quot;&gt;profile&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;type constant variable_builtin function_call variable_member variable&quot;&gt;endBlock&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;constant variable_builtin type variable&quot;&gt;block&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;comment_documentation comment spell&quot;&gt;// ... implementation ...&lt;/span&gt;
&lt;span class=&quot;punctuation_bracket&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Note: We could use &lt;code&gt;@src()&lt;/code&gt; instead but I opted for the string variant for now.&lt;/p&gt;&lt;p&gt;When &lt;code&gt;endBlock(block)&lt;/code&gt; runs, we store the elapsed time for this span in the &lt;code&gt;spans&lt;/code&gt; buffer.&lt;/p&gt;&lt;p&gt;The question is: How do we compute the index of a particular span in the &lt;code&gt;spans&lt;/code&gt; buffer &lt;strong&gt;without&lt;/strong&gt; maintaining a &lt;code&gt;HashMap&lt;/code&gt; between block name and index?&lt;/p&gt;&lt;p&gt;In the course, Casey uses &lt;code&gt;__COUNTER__&lt;/code&gt; from C++, which &lt;em&gt;“gives an increasing non-negative integral value each time it is used.”&lt;/em&gt; At each call site, the &lt;code&gt;beginBlock&lt;/code&gt; function would get a unique index for it span &lt;strong&gt;at compile time&lt;/strong&gt;. We don’t have this in Zig. Actually, we could have done this a while ago, but since &lt;a href=&quot;https://github.com/ziglang/zig/pull/19414&quot;&gt;#19414&lt;/a&gt;, global mutable comptime state is no longer allowed.&lt;/p&gt;&lt;p&gt;In Zig, containers are namespaces that can contain top-level mutable declarations. &lt;code&gt;spans&lt;/code&gt;, &lt;code&gt;current_index&lt;/code&gt;, and &lt;code&gt;current_parent_index&lt;/code&gt; are the three mutable top-level declarations of the &lt;code&gt;profile.zig&lt;/code&gt; container (files are structs themselves). In Zig, generics are implemented via &lt;code&gt;comptime&lt;/code&gt;: we write a function that returns a &lt;code&gt;type&lt;/code&gt;. If we combine the two, we get the following:&lt;/p&gt;&lt;pre&gt;&lt;code class=&quot;zig&quot;&gt;&lt;span class=&quot;keyword_function&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;constant variable_builtin type variable function&quot;&gt;SpanSlot&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;keyword_modifier&quot;&gt;comptime&lt;/span&gt; &lt;span class=&quot;constant variable_builtin variable_parameter variable type&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;punctuation_bracket&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;type_builtin&quot;&gt;u8&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;type_builtin&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;punctuation_bracket&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;keyword_return&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;keyword_type&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;punctuation_bracket&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;constant variable_builtin type variable&quot;&gt;span_name&lt;/span&gt; &lt;span class=&quot;operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;constant variable_builtin type variable&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;comment_documentation comment spell&quot;&gt;// This is a mutable top-level declaration in this specialized&lt;/span&gt;
        &lt;span class=&quot;comment_documentation comment spell&quot;&gt;// struct, storing the index the span will have in the&lt;/span&gt;
        &lt;span class=&quot;comment_documentation comment spell&quot;&gt;// `spans` buffer.&lt;/span&gt;
        &lt;span class=&quot;keyword&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;constant variable_builtin type variable&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;constant variable_builtin type variable&quot;&gt;SpanIndex&lt;/span&gt; &lt;span class=&quot;operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;constant_builtin&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;punctuation_bracket&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;punctuation_bracket&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Each time we call &lt;code&gt;beginBlock&lt;/code&gt;, we call &lt;code&gt;SpanSlot&lt;/code&gt; to get the unique container for the given name. Then we look up the &lt;code&gt;index&lt;/code&gt; variable in the container, and if it does not exist, we increment the index counter and store it.&lt;/p&gt;&lt;pre&gt;&lt;code class=&quot;zig&quot;&gt;&lt;span class=&quot;keyword_modifier&quot;&gt;pub&lt;/span&gt; &lt;span class=&quot;keyword_function&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;constant variable_builtin type variable function&quot;&gt;beginBlock&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;keyword_modifier&quot;&gt;comptime&lt;/span&gt; &lt;span class=&quot;constant variable_builtin variable_parameter variable type&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;punctuation_bracket&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;type_builtin&quot;&gt;u8&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;constant variable_builtin type variable&quot;&gt;SpanBlock&lt;/span&gt; &lt;span class=&quot;punctuation_bracket&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;constant variable_builtin type variable&quot;&gt;Slot&lt;/span&gt; &lt;span class=&quot;operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;constant variable_builtin function_call variable type&quot;&gt;SpanSlot&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;constant variable_builtin type variable&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;constant variable_builtin type variable&quot;&gt;span_index&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;constant variable_builtin type variable&quot;&gt;SpanIndex&lt;/span&gt; &lt;span class=&quot;operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;constant variable_builtin type variable&quot;&gt;Slot&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;constant variable_builtin type variable_member variable&quot;&gt;index&lt;/span&gt; &lt;span class=&quot;keyword_operator&quot;&gt;orelse&lt;/span&gt; &lt;span class=&quot;constant label type variable variable_builtin&quot;&gt;blk&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;punctuation_bracket&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;comment_documentation comment spell&quot;&gt;// This is the first time we see a block with this&lt;/span&gt;
        &lt;span class=&quot;comment_documentation comment spell&quot;&gt;// `name`. Increment the index and store it in the container.&lt;/span&gt;
        &lt;span class=&quot;keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;module constant variable_builtin type variable&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;module constant variable_builtin type variable&quot;&gt;SpanIndex&lt;/span&gt; &lt;span class=&quot;operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;function_builtin keyword_import&quot;&gt;@enumFromInt&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;function_builtin&quot;&gt;@intFromEnum&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;constant variable_builtin type variable&quot;&gt;current_index&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;;&lt;/span&gt;

        &lt;span class=&quot;constant variable_builtin type variable&quot;&gt;Slot&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;constant variable_builtin type variable_member variable&quot;&gt;index&lt;/span&gt; &lt;span class=&quot;operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;constant variable_builtin type variable&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;;&lt;/span&gt;

        &lt;span class=&quot;constant variable_builtin type variable&quot;&gt;current_index&lt;/span&gt; &lt;span class=&quot;operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;constant variable_builtin type variable&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;;&lt;/span&gt;

        &lt;span class=&quot;comment_documentation comment spell&quot;&gt;// Initialize the span.&lt;/span&gt;
        &lt;span class=&quot;keyword&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;constant variable_builtin type variable&quot;&gt;span_ptr&lt;/span&gt; &lt;span class=&quot;operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;operator&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;constant variable_builtin type variable&quot;&gt;spans&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;function_builtin&quot;&gt;@intFromEnum&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;constant variable_builtin type variable&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;constant variable_builtin type variable&quot;&gt;span_ptr&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;constant variable_builtin type variable_member variable&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;constant variable_builtin type variable&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;constant variable_builtin type variable&quot;&gt;span_ptr&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;constant variable_builtin type variable_member variable&quot;&gt;hit_count&lt;/span&gt; &lt;span class=&quot;operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;constant variable_builtin type variable&quot;&gt;span_ptr&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;constant variable_builtin type variable_member variable&quot;&gt;elapsed&lt;/span&gt; &lt;span class=&quot;operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;constant variable_builtin type variable&quot;&gt;span_ptr&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;constant variable_builtin type variable_member variable&quot;&gt;elapsed_children&lt;/span&gt; &lt;span class=&quot;operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;;&lt;/span&gt;

        &lt;span class=&quot;keyword_repeat&quot;&gt;break&lt;/span&gt; &lt;span class=&quot;punctuation_delimiter&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;constant label type variable variable_builtin&quot;&gt;blk&lt;/span&gt; &lt;span class=&quot;constant variable_builtin type variable&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;punctuation_bracket&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;comment_documentation comment spell&quot;&gt;/// ...&lt;/span&gt;
&lt;span class=&quot;punctuation_bracket&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;When we do &lt;code&gt;const Slot = SpanSlot(name)&lt;/code&gt;, we get back a &lt;code&gt;type&lt;/code&gt;, not a local instance of this &lt;code&gt;type&lt;/code&gt;. This gives us global mutable state &lt;strong&gt;per&lt;/strong&gt; span without needing to explicitly track all possible spans we have, at the expense of compilation time.&lt;/p&gt;&lt;p&gt;Our solution is not entirely identical to using &lt;code&gt;__COUNTER__&lt;/code&gt;. We still increment the index and store it at runtime. But at least this way we don’t need to keep a hash map around.&lt;/p&gt;&lt;h2&gt;Bonus 1&lt;/h2&gt;&lt;p&gt;What are the effects on compilation time when we have a non-trivial number of spans? To simulate this I generate a Zig program that contains increasing number of functions that are timed with unique names and compiled it using &lt;code&gt;ReleaseFast&lt;/code&gt;:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;0 spans : 5 secs&lt;/li&gt;&lt;li&gt;100 spans: 5.2 secs&lt;/li&gt;&lt;li&gt;1000 spans: 6.8 secs&lt;/li&gt;&lt;li&gt;4032 spans: 13.3 secs&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;To further compare this I edited the 4032 spans version and used the same span name:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;4032 spans (identical): 7.3 secs&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Profiling lots of functions does have an effect on compilation time but gets only really noticeable from 1000 spans onward.&lt;/p&gt;&lt;h2&gt;Bonus 2&lt;/h2&gt;&lt;p&gt;If you are wondering how we are tracking time, we are doing something fun. In the course, Casey uses the &lt;code&gt;rdtsc&lt;/code&gt; instruction, which measures something like CPU cycles (invariant cycles in modern CPUs). I’m on a Mac with an M-series chip, and ARM does not have the &lt;code&gt;rdtsc&lt;/code&gt; instruction. But there is another counter we can use for our purposes:&lt;/p&gt;&lt;pre&gt;&lt;code class=&quot;zig&quot;&gt;&lt;span class=&quot;keyword_modifier&quot;&gt;pub&lt;/span&gt; &lt;span class=&quot;keyword_function&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;constant variable_builtin type variable function&quot;&gt;readCounter&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;type_builtin&quot;&gt;u64&lt;/span&gt; &lt;span class=&quot;punctuation_bracket&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;keyword&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;constant variable_builtin type variable&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;type_builtin&quot;&gt;u64&lt;/span&gt; &lt;span class=&quot;operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;constant_builtin&quot;&gt;undefined&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;keyword&quot;&gt;asm&lt;/span&gt; &lt;span class=&quot;keyword_modifier&quot;&gt;volatile&lt;/span&gt; &lt;span class=&quot;punctuation_bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;string&quot;&gt;&amp;quot;mrs %[val], cntpct_el0&amp;quot;&lt;/span&gt;
        &lt;span class=&quot;punctuation_delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;punctuation_bracket&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;constant variable_builtin type variable&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;string&quot;&gt;&amp;quot;=r&amp;quot;&lt;/span&gt; &lt;span class=&quot;punctuation_bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;constant variable_builtin type variable&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;punctuation_bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;keyword_return&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;constant variable_builtin type variable&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;punctuation_bracket&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;mrs&lt;/code&gt; means that we want to read a system register, in our case, &lt;code&gt;cntpct_el0&lt;/code&gt;, which is a hardware counter &lt;a href=&quot;https://developer.arm.com/documentation/ddi0601/2021-12/AArch64-Registers/CNTPCT-EL0--Counter-timer-Physical-Count-register&quot;&gt;(documentation)&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;If we want to know how much time has elapsed, we can use the frequency of this counter:&lt;/p&gt;&lt;pre&gt;&lt;code class=&quot;zig&quot;&gt;&lt;span class=&quot;keyword_modifier&quot;&gt;pub&lt;/span&gt; &lt;span class=&quot;keyword_function&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;constant variable_builtin type variable function&quot;&gt;readCounterFreq&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;type_builtin&quot;&gt;u64&lt;/span&gt; &lt;span class=&quot;punctuation_bracket&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;keyword&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;constant variable_builtin type variable&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;type_builtin&quot;&gt;u64&lt;/span&gt; &lt;span class=&quot;operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;constant_builtin&quot;&gt;undefined&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;keyword&quot;&gt;asm&lt;/span&gt; &lt;span class=&quot;keyword_modifier&quot;&gt;volatile&lt;/span&gt; &lt;span class=&quot;punctuation_bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;string&quot;&gt;&amp;quot;mrs %[val], cntfrq_el0&amp;quot;&lt;/span&gt;
        &lt;span class=&quot;punctuation_delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;punctuation_bracket&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;constant variable_builtin type variable&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;string&quot;&gt;&amp;quot;=r&amp;quot;&lt;/span&gt; &lt;span class=&quot;punctuation_bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;constant variable_builtin type variable&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;punctuation_bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;keyword_return&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;constant variable_builtin type variable&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;punctuation_bracket&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;readCounterFreq&lt;/code&gt; returns 24 MHz on my MacBook. To get the elapsed time in milliseconds, we do:&lt;/p&gt;&lt;pre&gt;&lt;code class=&quot;zig&quot;&gt;&lt;span class=&quot;keyword_function&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;constant variable_builtin type variable function&quot;&gt;msFromElapsed&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;constant variable_builtin variable_parameter variable type&quot;&gt;elapsed&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;type_builtin&quot;&gt;u64&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;type_builtin&quot;&gt;u64&lt;/span&gt; &lt;span class=&quot;punctuation_bracket&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;keyword_return&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;constant variable_builtin type variable&quot;&gt;elapsed&lt;/span&gt; &lt;span class=&quot;operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;number&quot;&gt;1000&lt;/span&gt; &lt;span class=&quot;operator&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;constant variable_builtin function_call variable type&quot;&gt;readCounterFreq&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;punctuation_bracket&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;Bonus 3&lt;/h3&gt;&lt;p&gt;Where does Zig store the mutable declarations of structs? I had a look at the debug executable using &lt;code&gt;lldb&lt;/code&gt; on my Mac. I’m omitting some steps, and I had some help from GPT-5.6. First, I looked for symbols matching one profiling block’s &lt;code&gt;.index&lt;/code&gt; usage. This showed up:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;(lldb)  image lookup -s &amp;apos;profiler.SpanSlot(&amp;quot;readFile&amp;quot;[0..9]).index&amp;apos;
1 symbols match &amp;apos;profiler.SpanSlot(&amp;quot;readFile&amp;quot;[0..9]).index&amp;apos; in ...:
        Address: pap[0x00000001001e8dd8] (pap.__DATA.__bss + 24)
        Summary: pap`profiler.SpanSlot(&amp;quot;readFile&amp;quot;[0..9]).index

&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;code&gt;pap&lt;/code&gt; is the name of the executable. There is already a first hint. The address &lt;code&gt;0x00000001001e8dd8&lt;/code&gt; is in the &lt;code&gt;__DATA,__bss&lt;/code&gt; section of the macOS executable. That is where global mutable state is usually stored. Then I set up a watchpoint around that address, stepping through the program until it is triggered:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;(lldb) continue
Process 61200 resuming

Watchpoint 1 hit:
old value: 0
new value: -6148915419949301757
Process 61200 stopped
* thread #1, queue = &amp;apos;com.apple.main-thread&amp;apos;, stop reason = watchpoint 1
    frame #0: 0x00000001001140c4 pap`profiler.beginBlock__anon_27318 at profiler.zig:53:9
   50
   51           Slot.index = next;
   52
-&amp;gt; 53           current_index = next;
   54
   55           var span_ptr = &amp;amp;spans[@intFromEnum(next)];
   56           span_ptr.name = name;
Target 0: (pap) stopped.
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Now we are in the function that I showed above, &lt;code&gt;beginBlock&lt;/code&gt;, after we set &lt;code&gt;Slot.index = next;&lt;/code&gt;. At this time the index should be stored at that address. So, we inspect it:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;(lldb)  memory read --format x --size 8 --count 1 0x00000001001e8dd8
0x1001e8dd8: 0xaaaaaa0100000003
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;And as you can see, the low 32 bits is the value &lt;code&gt;3&lt;/code&gt;, which I expect because it is the third block I time in the program.  The following &lt;code&gt;01&lt;/code&gt; is the tag indicating that the &lt;code&gt;?SpanIndex&lt;/code&gt; is non-null, and the remaining &lt;code&gt;aa&lt;/code&gt; bytes are padding filled by the debug build.&lt;/p&gt;&lt;p&gt;To summarize: Zig emits &lt;code&gt;Slot.index&lt;/code&gt; as mutable static storage for each specialized &lt;code&gt;SpanSlot&lt;/code&gt; type. That is exactly what we wanted to achieve and allows us to avoid a runtime hash map.&lt;/p&gt;</description>
                <link>https://david-bach.com/pages/posts/2026-07-15-comptime-is-funtime/</link>
                <guid>https://david-bach.com/pages/posts/2026-07-15-comptime-is-funtime/</guid>
                <pubDate>Wed, 15 Jul 2026 00:00:00 +0000</pubDate>
            </item>
        
            <item>
                <title>Comptime is funtime</title>
                <description>&lt;h1&gt;Comptime is fun time&lt;/h1&gt;&lt;p&gt;For a recent project we had to write a columnar CSV parser with a fixed schema and decided to use Zig for this.&lt;/p&gt;&lt;p&gt;The CSV files we were supposed to read were all with a set of compile-time known schemas, about 13 different ones. After parsing we needed to efficiently access the records in a columnar way. We could not use a columnar format like Arrow, we had to read CSV files.&lt;/p&gt;&lt;h2&gt;Schema&lt;/h2&gt;&lt;p&gt;We decided to use &lt;code&gt;MultiArrayList&lt;/code&gt; to represent slices of records in a columnar way. In our case some of the data we dealt with encoded linear constraints we passed to solvers like HiGHs or Gurobi.&lt;/p&gt;&lt;pre&gt;&lt;code class=&quot;zig&quot;&gt;
&lt;span class=&quot;comment_documentation comment spell&quot;&gt;// Each row is in COO (coordinate list).&lt;/span&gt;
&lt;span class=&quot;keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;constant variable_builtin type variable&quot;&gt;SparseMatrixRow&lt;/span&gt; &lt;span class=&quot;operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;keyword_type&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;punctuation_bracket&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;constant variable_builtin type variable_member variable&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;type_builtin&quot;&gt;u64&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;constant variable_builtin type variable_member variable&quot;&gt;col&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;type_builtin&quot;&gt;u64&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;constant variable_builtin type variable_member variable&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;type_builtin&quot;&gt;f64&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;punctuation_bracket&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;constant variable_builtin type variable&quot;&gt;SparseMatrix&lt;/span&gt; &lt;span class=&quot;operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;constant variable_builtin function_call variable type&quot;&gt;MultiArrayList&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;constant variable_builtin type variable&quot;&gt;SparseMatrixRow&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The record type is our schema. We want to compile a specialized reader for this particular schema, parsing CSV records and returning or filling a &lt;code&gt;MultiArrayList&lt;/code&gt;.&lt;/p&gt;&lt;p&gt;We want to support a subset of primitive types in Zig, notably also optionals.&lt;/p&gt;&lt;h2&gt;Tokenizer&lt;/h2&gt;&lt;p&gt;We implement a simple tokenizer (heavily inspired by the Zig languages compiler tokenizer).&lt;/p&gt;&lt;pre&gt;&lt;code class=&quot;zig&quot;&gt;
&lt;span class=&quot;keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;constant variable_builtin type variable&quot;&gt;Tokenizer&lt;/span&gt; &lt;span class=&quot;operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;keyword_type&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;punctuation_bracket&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;constant variable_builtin type variable_member variable&quot;&gt;buffer&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;punctuation_bracket&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;type_builtin&quot;&gt;u8&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;constant variable_builtin type variable_member variable&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;type_builtin&quot;&gt;usize&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;,&lt;/span&gt;

    &lt;span class=&quot;comment_documentation comment spell&quot;&gt;// Each token has a tag and a location in the source.&lt;/span&gt;
    &lt;span class=&quot;keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;constant variable_builtin type variable&quot;&gt;Token&lt;/span&gt; &lt;span class=&quot;operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;keyword_type&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;punctuation_bracket&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;constant variable_builtin type variable_member variable&quot;&gt;tag&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;constant variable_builtin type variable&quot;&gt;Tag&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;constant variable_builtin type variable_member variable&quot;&gt;loc&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;constant variable_builtin type variable&quot;&gt;Loc&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;,&lt;/span&gt;

        &lt;span class=&quot;comment_documentation comment spell&quot;&gt;// Index points into the source where the value of this token&lt;/span&gt;
        &lt;span class=&quot;comment_documentation comment spell&quot;&gt;// starts.&lt;/span&gt;
        &lt;span class=&quot;keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;constant variable_builtin type variable&quot;&gt;Loc&lt;/span&gt; &lt;span class=&quot;operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;keyword_type&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;punctuation_bracket&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;constant variable_builtin type variable_member variable&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;type_builtin&quot;&gt;usize&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;constant variable_builtin type variable_member variable&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;type_builtin&quot;&gt;usize&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;punctuation_bracket&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;;&lt;/span&gt;

        &lt;span class=&quot;comment_documentation comment spell&quot;&gt;// Kind of token.&lt;/span&gt;
        &lt;span class=&quot;keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;constant variable_builtin type variable&quot;&gt;Tag&lt;/span&gt; &lt;span class=&quot;operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;keyword_type&quot;&gt;enum&lt;/span&gt; &lt;span class=&quot;punctuation_bracket&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;constant variable_builtin type variable_member variable&quot;&gt;field&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;constant variable_builtin type variable_member variable&quot;&gt;delimiter&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;constant variable_builtin type variable_member variable&quot;&gt;clrf&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;constant variable_builtin type variable_member variable&quot;&gt;invalid&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;constant variable_builtin type variable_member variable&quot;&gt;eof&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;punctuation_bracket&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;punctuation_bracket&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The tokznier contains a zero-terminated slice, the CSV file we are reading, and it produces tokens. Each token points to some data in the buffer and has a tag. When looking at &lt;a href=&quot;https://www.rfc-editor.org/rfc/rfc4180.html&quot;&gt;rfc4180&lt;/a&gt; we know that a valid CSV file does not have so many different tokens.&lt;/p&gt;&lt;p&gt;The tokenizer reads each byte from the source and we have a simple state machine to decide what kind of token we are dealing with:&lt;/p&gt;&lt;pre&gt;&lt;code class=&quot;zig&quot;&gt;    &lt;span class=&quot;comment_documentation comment spell&quot;&gt;// State of the tokenizer.&lt;/span&gt;
    &lt;span class=&quot;keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;constant variable_builtin type variable&quot;&gt;State&lt;/span&gt; &lt;span class=&quot;operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;keyword_type&quot;&gt;enum&lt;/span&gt; &lt;span class=&quot;punctuation_bracket&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;constant variable_builtin type variable_member variable&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;constant variable_builtin type variable_member variable&quot;&gt;eof&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;constant variable_builtin type variable_member variable&quot;&gt;quote&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;constant variable_builtin type variable_member variable&quot;&gt;field&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;constant variable_builtin type variable_member variable&quot;&gt;delimiter&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;constant variable_builtin type variable_member variable&quot;&gt;clrf&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;constant variable_builtin type variable_member variable&quot;&gt;invalid&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;punctuation_bracket&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;comment_documentation comment spell&quot;&gt;// Produce the next token.&lt;/span&gt;
    &lt;span class=&quot;keyword_modifier&quot;&gt;pub&lt;/span&gt; &lt;span class=&quot;keyword_function&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;constant variable_builtin type variable&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;constant variable_builtin variable_parameter variable type&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;constant variable_builtin type variable&quot;&gt;Tokenizer&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;constant variable_builtin type variable&quot;&gt;Token&lt;/span&gt; &lt;span class=&quot;punctuation_bracket&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;keyword&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;constant variable_builtin type variable&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;constant variable_builtin type variable&quot;&gt;Token&lt;/span&gt; &lt;span class=&quot;operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;punctuation_delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;punctuation_delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;constant variable_builtin type variable_member variable&quot;&gt;tag&lt;/span&gt; &lt;span class=&quot;operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;punctuation_delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;constant variable_builtin type variable&quot;&gt;field&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;punctuation_delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;constant variable_builtin type variable_member variable&quot;&gt;loc&lt;/span&gt; &lt;span class=&quot;operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;punctuation_delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;punctuation_delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;constant variable_builtin type variable_member variable&quot;&gt;start&lt;/span&gt; &lt;span class=&quot;operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;constant variable_builtin type variable&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;constant variable_builtin type variable_member variable&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;punctuation_delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;constant variable_builtin type variable_member variable&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;constant_builtin&quot;&gt;undefined&lt;/span&gt; &lt;span class=&quot;punctuation_bracket&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;punctuation_bracket&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;;&lt;/span&gt;

        &lt;span class=&quot;constant label type variable variable_builtin&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;keyword_conditional&quot;&gt;switch&lt;/span&gt; &lt;span class=&quot;punctuation_bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;constant variable_builtin type variable&quot;&gt;State&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;constant variable_builtin type variable_member variable&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;punctuation_bracket&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;punctuation_delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;constant variable_builtin type variable&quot;&gt;start&lt;/span&gt; &lt;span class=&quot;punctuation_delimiter&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;punctuation_bracket&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;keyword_conditional&quot;&gt;switch&lt;/span&gt; &lt;span class=&quot;punctuation_bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;constant variable_builtin type variable&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;constant variable_builtin type variable_member variable&quot;&gt;buffer&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;constant variable_builtin type variable&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;constant variable_builtin type variable_member variable&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;punctuation_bracket&quot;&gt;{&lt;/span&gt;
                    &lt;span class=&quot;character&quot;&gt;&amp;apos;&amp;quot;&amp;apos;&lt;/span&gt; &lt;span class=&quot;punctuation_delimiter&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;punctuation_bracket&quot;&gt;{&lt;/span&gt;
                        &lt;span class=&quot;comment_documentation comment spell&quot;&gt;// Quoted fields should not contain the quote&lt;/span&gt;
                        &lt;span class=&quot;comment_documentation comment spell&quot;&gt;// itself.&lt;/span&gt;
                        &lt;span class=&quot;constant variable_builtin type variable&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;constant variable_builtin type variable_member variable&quot;&gt;loc&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;constant variable_builtin type variable_member variable&quot;&gt;start&lt;/span&gt; &lt;span class=&quot;operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;constant variable_builtin type variable&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;constant variable_builtin type variable_member variable&quot;&gt;index&lt;/span&gt; &lt;span class=&quot;operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;;&lt;/span&gt;
                        &lt;span class=&quot;keyword_repeat&quot;&gt;continue&lt;/span&gt; &lt;span class=&quot;punctuation_delimiter&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;constant label type variable variable_builtin&quot;&gt;state&lt;/span&gt; &lt;span class=&quot;punctuation_delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;constant variable_builtin type variable&quot;&gt;quote&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;;&lt;/span&gt;
                    &lt;span class=&quot;punctuation_bracket&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;,&lt;/span&gt;
                    &lt;span class=&quot;comment_documentation comment spell&quot;&gt;// ... other cases ...&lt;/span&gt;
            &lt;span class=&quot;punctuation_bracket&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;comment_documentation comment spell&quot;&gt;// ... other states ...&lt;/span&gt;
        &lt;span class=&quot;punctuation_bracket&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;keyword_return&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;constant variable_builtin type variable&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;punctuation_bracket&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We read on byte after the other and use Zigs new &lt;code&gt;continue :state&lt;/code&gt; to do a state transition. The statemachine encodes the CSV syntax. The only way for us to go into the &lt;code&gt;.invalid&lt;/code&gt; state is when a plain &lt;code&gt;.field&lt;/code&gt; contains a quote. That is not allowed.&lt;/p&gt;&lt;h2&gt;Reader&lt;/h2&gt;&lt;p&gt;Now the reader is a generic type specialized for the record type.&lt;/p&gt;&lt;pre&gt;&lt;code class=&quot;zig&quot;&gt;
&lt;span class=&quot;comment_documentation comment spell&quot;&gt;///  A ReaderType parses CSVs and returns a MultiArrayList of records.&lt;/span&gt;
&lt;span class=&quot;keyword_modifier&quot;&gt;pub&lt;/span&gt; &lt;span class=&quot;keyword_function&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;constant variable_builtin type variable&quot;&gt;ReaderType&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;keyword_modifier&quot;&gt;comptime&lt;/span&gt; &lt;span class=&quot;constant variable_builtin variable_parameter variable type&quot;&gt;Record&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;type_builtin&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;type_builtin&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;punctuation_bracket&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;keyword_return&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;keyword_type&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;punctuation_bracket&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;constant variable_builtin type variable_member variable&quot;&gt;inner&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;constant variable_builtin type variable&quot;&gt;Io&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;constant variable_builtin type variable_member variable&quot;&gt;Reader&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;,&lt;/span&gt;

        &lt;span class=&quot;keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;module constant variable_builtin type variable&quot;&gt;Reader&lt;/span&gt; &lt;span class=&quot;operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;function_builtin keyword_import&quot;&gt;@This&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;constant variable_builtin type variable&quot;&gt;RecordList&lt;/span&gt; &lt;span class=&quot;operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;constant variable_builtin type variable&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;type constant variable_builtin function_call variable_member variable&quot;&gt;MultiArrayList&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;constant variable_builtin type variable&quot;&gt;Record&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;comment_documentation comment spell&quot;&gt;// ... implementation ...&lt;/span&gt;
        
        &lt;span class=&quot;comment_documentation comment spell&quot;&gt;/// Parses a row into the Record struct.&lt;/span&gt;
        &lt;span class=&quot;keyword_function&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;constant variable_builtin type variable function&quot;&gt;parseRecord&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;constant variable_builtin variable_parameter variable type&quot;&gt;tokenizer&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;constant variable_builtin type variable&quot;&gt;Tokenizer&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;constant variable_builtin variable_parameter variable type&quot;&gt;source&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;punctuation_bracket&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;type_builtin&quot;&gt;u8&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;operator&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;constant variable_builtin type variable&quot;&gt;Record&lt;/span&gt; &lt;span class=&quot;punctuation_bracket&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;punctuation_bracket&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;parseRecord&lt;/code&gt; is where the comptime starts. From the provided record type we statically know how many fields we expect and what types. We can use &lt;code&gt;comptime&lt;/code&gt; to unroll the parsing loop, creating an optimal but also maintainable functionality.&lt;/p&gt;&lt;pre&gt;&lt;code class=&quot;zig&quot;&gt;&lt;span class=&quot;comment_documentation comment spell&quot;&gt;/// Parses a row into the Record struct.&lt;/span&gt;
&lt;span class=&quot;keyword_function&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;constant variable_builtin type variable&quot;&gt;parseRecord&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;constant variable_builtin variable_parameter variable type&quot;&gt;tokenizer&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;constant variable_builtin type variable&quot;&gt;Tokenizer&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;constant variable_builtin variable_parameter variable type&quot;&gt;source&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;punctuation_bracket&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;type_builtin&quot;&gt;u8&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;operator&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;constant variable_builtin type variable&quot;&gt;Record&lt;/span&gt; &lt;span class=&quot;punctuation_bracket&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;keyword&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;constant variable_builtin type variable&quot;&gt;record&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;constant variable_builtin type variable&quot;&gt;Record&lt;/span&gt; &lt;span class=&quot;operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;constant_builtin&quot;&gt;undefined&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;constant variable_builtin type variable&quot;&gt;columns&lt;/span&gt; &lt;span class=&quot;operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;function_builtin&quot;&gt;@typeInfo&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;constant variable_builtin type variable&quot;&gt;Record&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;constant variable_builtin type variable_member variable&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;string constant variable_builtin type variable_member variable&quot;&gt;&amp;quot;struct&amp;quot;&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;constant variable_builtin type variable_member variable&quot;&gt;fields&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;keyword_modifier&quot;&gt;inline&lt;/span&gt; &lt;span class=&quot;keyword_repeat&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;punctuation_bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;constant variable_builtin type variable&quot;&gt;columns&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;operator&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;punctuation_bracket operator&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;constant variable_builtin variable_parameter variable type&quot;&gt;column&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;constant variable_builtin variable_parameter variable type&quot;&gt;idx&lt;/span&gt;&lt;span class=&quot;punctuation_bracket operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;punctuation_bracket&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;constant variable_builtin type variable&quot;&gt;token&lt;/span&gt; &lt;span class=&quot;operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;constant variable_builtin type variable&quot;&gt;tokenizer&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;type constant variable_builtin function_call variable_member variable&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;keyword_conditional&quot;&gt;switch&lt;/span&gt; &lt;span class=&quot;punctuation_bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;constant variable_builtin type variable&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;constant variable_builtin type variable_member variable&quot;&gt;tag&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;punctuation_bracket&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;punctuation_delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;constant variable_builtin type variable&quot;&gt;eof&lt;/span&gt; &lt;span class=&quot;punctuation_delimiter&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;keyword_return&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;constant_builtin&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;punctuation_delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;constant variable_builtin type variable&quot;&gt;field&lt;/span&gt; &lt;span class=&quot;punctuation_delimiter&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;punctuation_bracket&quot;&gt;{&lt;/span&gt;
                
            &lt;span class=&quot;punctuation_bracket&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;punctuation_bracket&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;punctuation_bracket&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;inline for&lt;/code&gt; instruct the compiler to unroll this loop at compile time. If&lt;/p&gt;</description>
                <link>https://david-bach.com/pages/posts/2026-06-15-fixed-csv/</link>
                <guid>https://david-bach.com/pages/posts/2026-06-15-fixed-csv/</guid>
                <pubDate>Mon, 15 Jun 2026 00:00:00 +0000</pubDate>
            </item>
        
            <item>
                <title>Reading Source Code</title>
                <description>&lt;h1&gt;Reading Source Code&lt;/h1&gt;&lt;p&gt;One of the things I like about the &lt;a href=&quot;https://ziglang.org/&quot;&gt;Zig&lt;/a&gt; programming language is how readable it is. This applies not only to projects written in it, but also to the standard library, compiler, and build system themselves. Whenever I need to look up how to use something in &lt;code&gt;std&lt;/code&gt;, I open the Zig source code in my editor and search for it.&lt;/p&gt;&lt;p&gt;The language design makes this quite easy and enjoyable. Files are structs, which are namespaces that contain definitions such as functions, types, or literals. There are no generated functions, no macros, and no inheritance. If I see a function, I can easily find it.&lt;/p&gt;&lt;p&gt;Over time, reading source code compounds. I am exposed to a lot of high-quality code from maintainers and creators of the language. I develop a mental map of it. It starts to fit into my head.&lt;/p&gt;&lt;p&gt;Let’s take an example from a recent project I was working on. It required working with columnar data for a certain schema. I thought about using &lt;code&gt;std.MultiArrayList&lt;/code&gt; for this. It is much more efficient to store records column-wise rather than row-wise; see &lt;a href=&quot;https://en.wikipedia.org/wiki/AoS_and_SoA&quot;&gt;AoS and SoA&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;Searching for this term leads me to the implementation in the standard library, under &lt;code&gt;multi_array_list.zig&lt;/code&gt;, as shown below:&lt;/p&gt;&lt;pre&gt;&lt;code class=&quot;zig&quot;&gt;&lt;span class=&quot;keyword_modifier&quot;&gt;pub&lt;/span&gt; &lt;span class=&quot;keyword_function&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;constant variable_builtin type variable&quot;&gt;MultiArrayList&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;keyword_modifier&quot;&gt;comptime&lt;/span&gt; &lt;span class=&quot;constant variable_builtin variable_parameter variable type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;type_builtin&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;type_builtin&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;punctuation_bracket&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;keyword_return&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;keyword_type&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;punctuation_bracket&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;comment_documentation comment spell&quot;&gt;/// ... comment ...&lt;/span&gt;
        &lt;span class=&quot;constant variable_builtin type variable_member variable&quot;&gt;bytes&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;punctuation_bracket&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;type_builtin&quot;&gt;u8&lt;/span&gt; &lt;span class=&quot;operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;constant_builtin&quot;&gt;undefined&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;constant variable_builtin type variable_member variable&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;type_builtin&quot;&gt;usize&lt;/span&gt; &lt;span class=&quot;operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;constant variable_builtin type variable_member variable&quot;&gt;capacity&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;type_builtin&quot;&gt;usize&lt;/span&gt; &lt;span class=&quot;operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;comment_documentation comment spell&quot;&gt;/// ... implementation ...&lt;/span&gt;
&lt;span class=&quot;punctuation_bracket&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A &lt;code&gt;MultiArrayList&lt;/code&gt; is a generic type in Zig. It takes a struct or union, here the &lt;code&gt;comptime T: type&lt;/code&gt; argument, and returns a data structure specialized to my element type. Internally, it stores the arrays as a single, contiguous block of memory. An &lt;code&gt;ArrayList&lt;/code&gt; would instead store my elements directly as a &lt;code&gt;[]T&lt;/code&gt;.&lt;/p&gt;&lt;p&gt;This representation is more memory efficient because no padding is needed, and it is more cache- and SIMD-friendly without losing ergonomics.&lt;/p&gt;&lt;p&gt;&lt;code&gt;MultiArrayList&lt;/code&gt; has the usual array API: &lt;code&gt;get&lt;/code&gt;, &lt;code&gt;set&lt;/code&gt;, and &lt;code&gt;append&lt;/code&gt;, which operate on the element type, &lt;code&gt;T: type&lt;/code&gt; above. I can also access the arrays directly via &lt;code&gt;.slice()&lt;/code&gt;, which computes and caches start pointers for each field. &lt;code&gt;.items(.&amp;lt;field&amp;gt;)&lt;/code&gt; on that gives me the array for a single field. Under the hood, that does a &lt;code&gt;@ptrCast(@alignCast(byte_ptr))&lt;/code&gt; on the bytes and returns a slice of the original field. The compiler guarantees that both the field name and slice type are valid.&lt;/p&gt;&lt;p&gt;These days, with Claude Code and friends, reading how &lt;code&gt;MultiArrayList&lt;/code&gt; is implemented is likely not what most people would do. That Zig naturally leads me to it is refreshing and, in my opinion, one of the great features of Zig.&lt;/p&gt;</description>
                <link>https://david-bach.com/pages/posts/2026-06-08-read-the-code/</link>
                <guid>https://david-bach.com/pages/posts/2026-06-08-read-the-code/</guid>
                <pubDate>Mon, 08 Jun 2026 00:00:00 +0000</pubDate>
            </item>
        
            <item>
                <title>A Computational Mind</title>
                <description>&lt;h1&gt;A Computational Mind&lt;/h1&gt;&lt;p&gt;How can a computational approach help us gain insights into the workings of the mind?&lt;/p&gt;&lt;p&gt;It is apparent that the human mind can act in a way a computer does, by following rules, such as manipulating symbols, to compute arithmetic expressions. Importantly the human executing the list of instructions does not need to comprehend what these symbols mean, he simply needs to obey the rules. The important analogy between human reasoning and execution of a computer program is that both need to continuously reduce a problem to more and more fundamental truths, which then can be understood in isolation.  These then are iteratively combined until the initial statement is understood/computed.&lt;/p&gt;&lt;p&gt;These hints lead to the questions, if we could not simply assume that the nervous system is a computing system, consisting of individual computing units, the neurons. Even though neurons are physically analog, they follow a “all or nothing” rule.&lt;/p&gt;&lt;p&gt;Modeling these neurons as building blocks in logic networks, &lt;a href=&quot;#mcculloch90_logic_calcul_ideas_imman_nervous_activ&quot;&gt;McCulloch, W. S. &amp; Pitts, W. (1990)&lt;/a&gt; pioneered the idea of the brain as a logical inference engine. The usage of AND &lt;script type=&quot;math/tex&quot;&gt;\wedge&lt;/script&gt; , OR &lt;script type=&quot;math/tex&quot;&gt;\vee&lt;/script&gt; or NOT &lt;script type=&quot;math/tex&quot;&gt;\neg&lt;/script&gt; in the interaction of neurons can model the response of any one neuron given its neighbors.&lt;/p&gt;&lt;p&gt;One now could assume that the mind is but a series of computed logical functions. &lt;a href=&quot;#putnam67_natur_mental_states&quot;&gt;Putnam, H. (1967)&lt;/a&gt; provides a different view. The mind and its mental states are functions of the computational process. Direct one-to-one mappings between physiological brain states and mental states would then not exist. This view is revised by &lt;a href=&quot;#fodor74_special_scien_or_disun_scien&quot;&gt;Fodor, J. A. (1974)&lt;/a&gt;. He argues that it is highly unlikely that we will find a decomposition of the brain that is a direct analog of the psychology of the brain. He makes the argument that even if that would be the case,&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;… then there are only epistemological reasons for studying the former instead of the latter.&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;In a extreme view, the functional state would be completely decoupled from the physical state of the underlying structure. There would be no hope to understand the mind by observing the structure it is implemented on. &lt;a href=&quot;#chalmers94_comput_found_study_cognit&quot;&gt;Chalmers, D. J. (1994)&lt;/a&gt; loosens this assumption by arguing the the causal structure of the physical system must mirror the formal structure of the computation.  To whatever degree we assume the decouplement of function and structure, it opens the door to different realizations of the same mental state from different physical states. This in turn leads to the idea of machines possessing the same mental states as we humans do.&lt;/p&gt;&lt;p&gt;Following this functional paradigm we need to ask ourselves, what are these functions and what do they compute.  We can view functions from a mathematical point of view. As mappings between in- and outputs. The inputs are perceptions of the physical world, proprioception and exteroception, and the outputs are meaning or models. This in essences means that perceptive functions apply meaning to what is being experienced and thus relate to it.  From an evolutionary point of view this makes sense, as the better organisms can relate to or model what is being experienced the better they can act upon it. &lt;a href=&quot;#dennett17_from_bach&quot;&gt;Dennett, D. C. (2017)&lt;/a&gt; put it the following way:&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;Brains are designed by natural selection to have, or reliably develop, equipment that can extract the semantic information needed for this control task.&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;Perception thus seems to be the first encounter that a mental state and its associated functions have. The act of perception is one of categorization. It a an impressive feat on its own, to decode raw sensory data and then infer on it.  Models for this process of inferring meaning from data has their roots in Bayesian probability theory. Different hypothesis and their prio probability exist and by observing information/evidence the posterior distribution is inferred. The most likely explanation is then assigned as the meaning of the perception.&lt;/p&gt;&lt;p&gt;As an example we can take visual categorization. We start with a set of given categories, in a sense the prio distributions above, and a perception on our retina. The function the brain needs to implement maps this image into one of the given categories.  This function needs to map a enormous amount of visual input to a small set of categories reliable and fast. We seem to do it effortless, but thinking about programming such a function in a imperative manner is almost incomprehensible. Nowadays this problem is solved using machine learning and exemplified data.  A trained neural network can reach high amounts of fidelity in categorizing images.  This process of training an algorithm seems familiar in the evolutionary context. The brain is a “living computational system” continuously learning from data and programming itself, on the level of individuals.  In the machine learning space Deep Neural Networks are the frontier of computing such complex task, and they do with great precision. They are modeled after the neurons in the brain. In general the techniques use gradient descend methods to optimize a given loss function, e.g. categorizing cat pictures.  The rise of machine learning in the last decades has given us the opportunity to create functions that compute a task the brain does and thus given us access to study those functions and systems they are implemented on.&lt;/p&gt;&lt;p&gt;There exist different opinions about neural networks as merely another specific implementation of a Turing machine, and as such not providing any more insight about mental representations, or a more fundamental process of extracting and reducing to relevant information, which is needed for mental representations. Staying at the example of categorizing images, we can also think of it as learning representations. Can deep neural networks also learn meaningful intermediate representations? In other words, the process of reducing the high dimensional data leads to the representation of features along the way to the actual task at hand.  Fundamentally representations are created by reduction. The functions performing this information reduction have been acquired by organism for a purpose, usually to improve its chances of survival.&lt;/p&gt;&lt;p&gt;Another interesting area to investigate the creation and processing of representations is in the space of language.  How can we acquire semantic representations?  There are different opinions, from a “mental encyclopedia” in which the meaning of words is following the reference into the encyclopedia to find it, like a look-up in a table &lt;a href=&quot;#pinker11_words_rules&quot;&gt;Pinker, S. (2011)&lt;/a&gt;, to an innate existing of lexical concepts that are atomic and as such cannot be learned.  Criticism for both exists, the former needs a preexisting table and the second one leads to absurd consequences of all conceivable meaning already innate in every human.  Following a more deductive approach we can model the meaning of words as a point in a “vector space”, where relating concept are “close” in a distant measure sense and together build a connected graph.  These structures again can be learned, such that points correspond to probability distribution over relating concepts. New concepts can be acquired by locating it in the space and appropriately altering the connections and distributions.  We can show that semantic and syntactic representations can be learned through these intermediate representations.  This intermediate representation can again be used to translate between, e.g. visual and audio information, as shown by algorithms that can categorize an image and create language describing them.  From language it is not far fetching to describe thought via an algorithm. We think in a mental language and thoughts are but a continuous stream of related mental representations.&lt;/p&gt;&lt;p&gt;&lt;a href=&quot;#chalmers94_comput_found_study_cognit&quot;&gt;Chalmers, D. J. (1994)&lt;/a&gt; argues that most mental properties and processes are organizational invariant, in that we might change and substitute the underlying structure but as long as we maintain its causal properties, we preserve its mental properties. The idea can be reformulated from a physics point of view. The first thing we can argue is that information is substrate independent. We may represent bits of information via transistors, neurons, magnetic states or light. The underlying, physical representation does not change the containing information. Otherwise we would need to physically move the matter to transmit information, a ghastly image. If information is substrate independent, so must be information processing. There exists an abstract notion of processing information, which does not rely on the physical implementation executing it. Furthermore we can argue that any computational process is merely one of information processing and thus we arrive at Chalmers statement.  If we assume that there exist not such a thing as conscious or mind matter, a special sort of matter that, if information processing would be implemented on it, would expose of mental processes, we inevitably arrive at the notion of computational sufficiency, describe again in &lt;a href=&quot;#chalmers94_comput_found_study_cognit&quot;&gt;Chalmers, D. J. (1994)&lt;/a&gt;.  If a system implements the very same function as mental processes do, it possesses mental properties.&lt;/p&gt;&lt;p&gt;Chalmers proposes the next step in this reasoning, what he coins Computational Explanation, that the computational framework can be used to explain mental phenomena.  In other words: If we understand the function that is being computed we understand the mental process and vice versa. It a mathematical description:&lt;/p&gt;&lt;script type=&quot;math/tex&quot;&gt;\text{Computational Properties} \Longleftrightarrow \text{Mental
Properties}
&lt;/script&gt;&lt;p&gt;This does of course not answer the question which functions lead to mental states and which not, but it gives us a general framework in which we can investigate such properties.&lt;/p&gt;&lt;h1&gt;Conclusion&lt;/h1&gt;&lt;p&gt;Over the last years improvement in the artificial intelligence space shows that more and more, prior unthinkable, computational tasks have been solved. Learning is the prominent reason for that. Through this the evidence that mental state are describable via computation seems apparent. On the other hand neural networks are often black boxes and its not clear if any actual meaning is inferred. Adding to this are notorious robustness issues. &lt;a href=&quot;#carlini16_towar_evaluat_robus_neural_networ&quot;&gt;Carlini, N. &amp; Wagner, D. A. (2016)&lt;/a&gt; show that a small change in an input image can lead to completely different classifications, even though for a human observer nothing has changed. These and similar results, where neural networks are duped in such subtle ways, show that inside the neural network there exists no such thing like a mental representation of what has been learned. Or at least not yet.&lt;/p&gt;&lt;p&gt;To conclude, the basic premise that mental states are despicable in a functional and computational way is a very pervasive model and allows us to build and reason about what mental processes are, with a toolkit that has been developed over the last 50 years.&lt;/p&gt;&lt;h1&gt;References&lt;/h1&gt;&lt;ul&gt;&lt;li&gt;&lt;span id=&quot;mcculloch90_logic_calcul_ideas_imman_nervous_activ&quot;&gt;McCulloch, W. S. &amp; Pitts, W. (1990)&lt;/span&gt; &lt;strong&gt;McCulloch&lt;/strong&gt;, &lt;strong&gt;Pitts&lt;/strong&gt;, A logical calculus of the ideas immanent in nervous activity. 1943. Bulletin of mathematical biology, 52 1-2(), 99–115–73–97.&lt;/li&gt;&lt;li&gt;&lt;span id=&quot;putnam67_natur_mental_states&quot;&gt;Putnam, H. (1967)&lt;/span&gt; &lt;strong&gt;Putnam&lt;/strong&gt;, The nature of mental states. In W. Capitan, &amp; D. Merrill (Eds.), Art, Mind, and Religion (pp. 1–223). Pittsburgh University Press.&lt;/li&gt;&lt;li&gt;&lt;span id=&quot;fodor74_special_scien_or_disun_scien&quot;&gt;Fodor, J. A. (1974)&lt;/span&gt; &lt;strong&gt;Fodor&lt;/strong&gt;, Special sciences, or disunity of science as a working hypothesis. Synthese, 28(2), 97–115.&lt;/li&gt;&lt;li&gt;&lt;span id=&quot;chalmers94_comput_found_study_cognit&quot;&gt;Chalmers, D. J. (1994)&lt;/span&gt; &lt;strong&gt;Chalmers&lt;/strong&gt;, A computational foundation for the study of cognition. Minds And Machines. 1994.&lt;/li&gt;&lt;li&gt;&lt;span id=&quot;dennett17_from_bach&quot;&gt;Dennett, D. C. (2017)&lt;/span&gt; &lt;strong&gt;Dennett&lt;/strong&gt;, From Bacteria to Bach and Back : The Evolution of Minds.&lt;/li&gt;&lt;li&gt;&lt;span id=&quot;pinker11_words_rules&quot;&gt;Pinker, S. (2011)&lt;/span&gt; &lt;strong&gt;Pinker&lt;/strong&gt;, Words and rules (1999/2011). New York, Harper Perennial.&lt;/li&gt;&lt;li&gt;&lt;span id=&quot;carlini16_towar_evaluat_robus_neural_networ&quot;&gt;Carlini, N. &amp; Wagner, D. A. (2016)&lt;/span&gt; &lt;strong&gt;Carlini&lt;/strong&gt;, &lt;strong&gt;Wagner&lt;/strong&gt;, Towards evaluating the robustness of neural networks. CoRR, abs/1608.04644().&lt;/li&gt;&lt;/ul&gt;</description>
                <link>https://david-bach.com/pages/posts/2020-08-03-a-computational-mind/</link>
                <guid>https://david-bach.com/pages/posts/2020-08-03-a-computational-mind/</guid>
                <pubDate>Mon, 03 Aug 2020 00:00:00 +0000</pubDate>
            </item>
        
            <item>
                <title>Incremental Functional Aggregate Queries</title>
                <description>&lt;h1&gt;Incremental Functional Aggregate Queries&lt;/h1&gt;&lt;p&gt;This blog post concludes a research project I’ve done together with Frank McSherry &lt;sup class=&quot;footnote-ref&quot;&gt;&lt;a href=&quot;#fn-1&quot; id=&quot;fn-1-ref-1&quot;&gt;1&lt;/a&gt;&lt;/sup&gt; earlier this year at ETH Zurich.&lt;br&gt;In 2015 Khamis, Ngo and Rudra presented their paper, somewhat confusingly named, FAQ: Questions asked frequently &lt;sup class=&quot;footnote-ref&quot;&gt;&lt;a href=&quot;#fn-2&quot; id=&quot;fn-2-ref-1&quot;&gt;2&lt;/a&gt;&lt;/sup&gt; (that by the way makes it quite hard to google it).&lt;br&gt;It introduces a general framework to solve a variety of computational problems following ideas from database and relational algebra research.&lt;br&gt;That framework is called &lt;strong&gt;F&lt;/strong&gt;unctional &lt;strong&gt;A&lt;/strong&gt;ggregate &lt;strong&gt;Q&lt;/strong&gt;uery.&lt;br&gt;I implemented a simple version of it using the excellent differential dataflow &lt;sup class=&quot;footnote-ref&quot;&gt;&lt;a href=&quot;#fn-3&quot; id=&quot;fn-3-ref-1&quot;&gt;3&lt;/a&gt;&lt;/sup&gt; library in the Rust programming language.&lt;/p&gt;&lt;p&gt;This blog post is structured in the following way:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;An intuitive motivation and explanation of FAQ&lt;/li&gt;&lt;li&gt;A bit of formalism&lt;/li&gt;&lt;li&gt;Differential dataflow and how we can use its internals to implement FAQ&lt;/li&gt;&lt;li&gt;How we then go on and incrementalize FAQ’s&lt;/li&gt;&lt;/ul&gt;&lt;h1&gt;Introduction&lt;/h1&gt;&lt;p&gt;What’s this paper about?&lt;/p&gt;&lt;p&gt;Its major contribution is the insight that there is a common algebraic structure that seemingly unrelated problems share and that there is a general framework to efficiently solve these.&lt;/p&gt;&lt;p&gt;So what &lt;strong&gt;do&lt;/strong&gt; problems like matrix chain multiplication, triangle counting or constrain satisfaction share? They are all special instances of what the authors refer to as a Functional Aggregate Query (FAQ) problem.&lt;/p&gt;&lt;p&gt;Let’s have a look at two of them:&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Matrix Chain Multiplication&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;Given a series of matrices &lt;script type=&quot;math/tex&quot;&gt;\boldsymbol{A}_{1},...,\boldsymbol{A}_{n}&lt;/script&gt; what is the product of these? We can reformulate this problem by identifying the matrices &lt;script type=&quot;math/tex&quot;&gt;\boldsymbol{A}_{n}&lt;/script&gt; as functions of two variables:&lt;/p&gt;&lt;script type=&quot;math/tex&quot;&gt;\psi_{i. i+1}: \text{Dom}(X_{i}) \, \times \, \text{Dom}(X_{i+1}) \rightarrow \mathcal{F}
&lt;/script&gt;&lt;p&gt;where &lt;script type=&quot;math/tex&quot;&gt;\psi_{i, i+1}(x,y) = (\boldsymbol{A}_{i})_{xy}&lt;/script&gt; and &lt;script type=&quot;math/tex&quot;&gt;\mathcal{F}&lt;/script&gt; is a Field&lt;br&gt;The problem then is to compute:&lt;/p&gt;&lt;script type=&quot;math/tex&quot;&gt;\phi (x_1, x_{n+1}) = \sum_{x_2 \in \text{Dom}(X_2)} ... \sum_{x_{n} \in \text{Dom}(X_{n})} \prod_{i=1}^{n}\psi_{i, i+1}(x_{i}, x_{i+1})
&lt;/script&gt;&lt;p&gt;&lt;strong&gt;Triangle Counting&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;We want to compute the number of triangles in a given graph &lt;script type=&quot;math/tex&quot;&gt;G = (V, E)&lt;/script&gt;.  If we choose to represent edges in the graph &lt;script type=&quot;math/tex&quot;&gt;G&lt;/script&gt; in the following way:&lt;/p&gt;&lt;script type=&quot;math/tex&quot;&gt;\psi(u,v) =
	\begin{cases}
		1  &amp; \text{if} \, \{u,v\} \in E, u &lt; v \\
		0  &amp; \text{otherwise}.
	\end{cases}
&lt;/script&gt;&lt;p&gt;The problem then becomes to compute:&lt;/p&gt;&lt;script type=&quot;math/tex&quot;&gt;\phi = \sum_{x_{1} \in V} \sum_{x_{2} \in V} \sum_{x_{3} \in V} \psi(x_{1}, x_{2}) \cdot \psi(x_{1}, x_{3}) \cdot \psi(x_{2}, x_{3})
&lt;/script&gt;&lt;p&gt;Generally speaking these functions &lt;script type=&quot;math/tex&quot;&gt;\psi_{S} : \prod_{i \in S} \text{Dom}(X_{i}) \rightarrow \boldsymbol{D}&lt;/script&gt; also referred to as &lt;em&gt;factors&lt;/em&gt;, map specific values of their input variables to some domain. For our purpose, we can think of these functions as relations over their variables. But instead of their Domain &lt;script type=&quot;math/tex&quot;&gt;\boldsymbol{D}&lt;/script&gt; simply being an identifier if there exists an instance of this specific relation we may choose a different one. Like the real numbers, sets, and so on.&lt;br&gt;We represent the values our factors take as a table, also called &lt;em&gt;listing representation&lt;/em&gt;. We list all non-zero instances of our relation, &lt;script type=&quot;math/tex&quot;&gt;\psi_{S}&lt;/script&gt;:&lt;/p&gt;&lt;script type=&quot;math/tex&quot;&gt;\langle \boldsymbol{x}_{S}, \psi_{S}(\boldsymbol{x}_{S})\rangle
&lt;/script&gt;&lt;p&gt;Whereas &lt;script type=&quot;math/tex&quot;&gt;\boldsymbol{x}_{S}&lt;/script&gt; represent the support of the variable &lt;script type=&quot;math/tex&quot;&gt;\boldsymbol{x}&lt;/script&gt;. In other words a specific tuple of variables and the corresponding value of the factor. The listing representation of a matrix factor for example looks like that:&lt;/p&gt;&lt;script type=&quot;math/tex&quot;&gt;\begin{gathered}
		\langle (1, 1), 1\rangle \langle (1, 2), 2\rangle \\
		\langle (2, 1), 3\rangle \langle (2, 2), 4\rangle
	\end{gathered}
&lt;/script&gt;&lt;script type=&quot;math/tex&quot;&gt;\begin{bmatrix} 
		1 &amp; 2 \\
		3 &amp; 4 
	\end{bmatrix}
&lt;/script&gt;&lt;p&gt;The listing representation of our triangle example is simply a table of all edges in our graph, with their value being 1.&lt;/p&gt;&lt;p&gt;The solution to those examples follows the following formula:&lt;/p&gt;&lt;ol&gt;&lt;li&gt;Choose a variable order&lt;/li&gt;&lt;li&gt;Collect the set of factors that intersect with the first variable in our order&lt;/li&gt;&lt;li&gt;Multiply all values from the listing representation&lt;/li&gt;&lt;li&gt;Eliminate the given variable with the provided monoid operation&lt;/li&gt;&lt;li&gt;Push the resulting new factor into the next iteration&lt;/li&gt;&lt;/ol&gt;&lt;p&gt;How can we represent these steps? First a few thoughts about the monoid operations that are part of the semiring.&lt;/p&gt;&lt;p&gt;If we identify factors as relations, multiplication might be naturally expressed as a join operation. We can join two relations along one of their attributes and produce a new one. In the relational algebra land, a tuple of a given relation only has two possible states. It exists or it does not. If we go on and extend this to allow values to be other then a boolean identifier, say the rational numbers, we can represent the multiplication monoid in the FAQ setting via a join operation.&lt;/p&gt;&lt;p&gt;Aggregations appear to be a fold or reduce over joined relations along a specific symbol.&lt;/p&gt;&lt;p&gt;Now given we have our factor in listing representation how do we go on and solve e.g. the Triangle Counting task?  By writing the equation in the way we did, we naturally imposed a variable order in the aggregations.&lt;/p&gt;&lt;p&gt;We take the first variable, in this case &lt;script type=&quot;math/tex&quot;&gt;x_{3}&lt;/script&gt;, and join all factors that contain this specific one. Joining in the sense that we multiply their respective values given their input variable instances intersect. We then apply the specified aggregation to eliminate the variable and end up with a new factor that includes the union of the variable of the two prior factors, minus the one we just aggregated. And then we continue on with the next variable in order until we are done.&lt;/p&gt;&lt;p&gt;In essence we reformulate our task into a dynamic programming problem and use variable elimination to solve it. This dynamic programming problem is best thought of as a hypergraph.&lt;/p&gt;&lt;p&gt;The induced hypergraph can be seen here. &lt;img src=&quot;https://david-bach.com/assets/pages/2019-07-06-Incremental-Functional-Aggregate-Queries/faq_triangle_query.png&quot; alt=&quot;Triangle query&quot;&gt;&lt;/p&gt;&lt;p&gt;(I gave &lt;code&gt;tikz&lt;/code&gt; a shot and only nearly cried twice).&lt;/p&gt;&lt;h1&gt;Functional Aggregate Queries&lt;/h1&gt;&lt;p&gt;In general the FAQ problem is to compute:&lt;/p&gt;&lt;script type=&quot;math/tex&quot;&gt;\varphi(\boldsymbol{\text{x}}_{[f]}) \, = \, \underset{x_{f+1} \in \text{Dom}(X_{f+1})}{\bigoplus^{(f+1)}} \quad ... \quad \underset{x_{n} \in \text{Dom}(X_{n})}{\bigoplus^{(n)}} \bigotimes_{S \in \mathcal{E}} \psi_{S}(\boldsymbol{\text{x}}_{S})
&lt;/script&gt;&lt;p&gt;where:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;script type=&quot;math/tex&quot;&gt;\mathcal{H} = (\mathcal{V}, \mathcal{E})&lt;/script&gt; is a multi-hypergraph&lt;/li&gt;&lt;li&gt;For every hypergraph edge &lt;script type=&quot;math/tex&quot;&gt;S\in\mathcal{E},\psi_{S} : \prod_{i \in       S} \text{Dom}(X_{i}) \rightarrow \boldsymbol{D}&lt;/script&gt;&lt;/li&gt;&lt;li&gt;For every bound variable &lt;script type=&quot;math/tex&quot;&gt;i&gt;f, \oplus^{(i)}&lt;/script&gt; is a binary aggregate operator on Domain &lt;script type=&quot;math/tex&quot;&gt;\boldsymbol{D}&lt;/script&gt;&lt;/li&gt;&lt;li&gt;For every bound variable &lt;script type=&quot;math/tex&quot;&gt;(\boldsymbol{D}, \oplus^{(i)}, \otimes)&lt;/script&gt; forms a commutative semiring&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;A semiring is made up of a Domain &lt;script type=&quot;math/tex&quot;&gt;\boldsymbol{D}&lt;/script&gt; and two monoids &lt;script type=&quot;math/tex&quot;&gt;\oplus, \otimes&lt;/script&gt;, wheres the &lt;script type=&quot;math/tex&quot;&gt;\oplus&lt;/script&gt; distributes over the &lt;script type=&quot;math/tex&quot;&gt;\otimes&lt;/script&gt;. A monoid consists of a binary operator and a neutral element, but no inverse. Instances of the FAQ problem induce an underlying multi-hypergraph, which represents the algebraic structure.&lt;/p&gt;&lt;p&gt;How do we go on and solve a FAQ?&lt;/p&gt;&lt;p&gt;The paper describes a general solution scheme called &lt;code&gt;InsideOut&lt;/code&gt;, that at least for our purpose does what I intuitively explained above. We iteratively solve a given FAQ instance in the order given by starting with the most inner variable, eliminating it by applying its corresponding aggregation and moving on the next one. From the inside out.&lt;/p&gt;&lt;p&gt;The authors then go on and prove optimal run times given we have the best variable order.&lt;/p&gt;&lt;p&gt;That leaves us with a general solution to all problems that can be phrased as a FAQ.&lt;/p&gt;&lt;p&gt;For a more formal and thorough analysis please do read the paper. What I also am not at all concerned about, and what the authors take a big portion of time taking care of is finding the optimal variable order to guarantee worst-case optimal run time and such things.&lt;/p&gt;&lt;h1&gt;Differential Dataflow&lt;/h1&gt;&lt;p&gt;Okay the basics of FAQ’s are set, but why should we choose differential dataflow to implement it? First a few words about the system itself: The GitHub reads:&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;Differential dataflow is a data-parallel programming framework designed to efficiently process large volumes of data and to quickly respond to arbitrary changes in input collections&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;So, in essence, it is a distributed stream processor whose operators are incrementally implemented.&lt;/p&gt;&lt;p&gt;If we take a look at the way we model computations in the FAQ setting we can find proper analogs in differential dataflow, that is why we started this after all!&lt;/p&gt;&lt;p&gt;Multiplication is done as a &lt;code&gt;join&lt;/code&gt;, aggregation via the &lt;code&gt;reduce&lt;/code&gt; operator and factors are modeled as differential dataflow collections.&lt;/p&gt;&lt;p&gt;The reason why we can take a system like differential dataflow and implement a setting like FAQ in it, is its generality and ability to directly use the type system to encode mathematical invariants and guarantees.&lt;br&gt;Let us see that in action.&lt;/p&gt;&lt;p&gt;The collection is differential dataflows main data structure.&lt;/p&gt;&lt;p&gt;This is its signature:&lt;/p&gt;&lt;pre&gt;&lt;code class=&quot;rust&quot;&gt;&lt;span class=&quot;keyword_modifier&quot;&gt;pub&lt;/span&gt; &lt;span class=&quot;keyword_type&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;type&quot;&gt;Collection&lt;/span&gt;&lt;span class=&quot;punctuation_bracket operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;type&quot;&gt;G&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;type&quot;&gt;Scope&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;type&quot;&gt;D&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;type&quot;&gt;R&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;type&quot;&gt;Monoid&lt;/span&gt; &lt;span class=&quot;operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;type_builtin&quot;&gt;isize&lt;/span&gt;&lt;span class=&quot;punctuation_bracket operator&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;punctuation_bracket&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;keyword_modifier&quot;&gt;pub&lt;/span&gt; &lt;span class=&quot;constant variable_member&quot;&gt;inner&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;type&quot;&gt;Stream&lt;/span&gt;&lt;span class=&quot;punctuation_bracket operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;type&quot;&gt;G&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;punctuation_bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;type&quot;&gt;D&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;module constant type variable constant_builtin&quot;&gt;G&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;constant type&quot;&gt;Timestamp&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;type&quot;&gt;R&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;punctuation_bracket operator&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;punctuation_bracket&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A collection internally has a timely dataflow &lt;sup class=&quot;footnote-ref&quot;&gt;&lt;a href=&quot;#fn-4&quot; id=&quot;fn-4-ref-1&quot;&gt;4&lt;/a&gt;&lt;/sup&gt; stream that consists of elements that are triples of type &lt;code&gt;D&lt;/code&gt;, a timestamp and a monoid &lt;code&gt;R&lt;/code&gt; which defaults to &lt;code&gt;isize&lt;/code&gt;.&lt;/p&gt;&lt;p&gt;We can best think of a &lt;code&gt;Collection&lt;/code&gt; as a sequence of immutable tuples at distinct logical timestamps while the &lt;code&gt;Diff&lt;/code&gt; represents their multiplicity. If we add a tuple to the collection it is &lt;code&gt;+1&lt;/code&gt; if we retract one it is &lt;code&gt;-1&lt;/code&gt;.&lt;/p&gt;&lt;p&gt;The interesting part is that we can provide our own type there.&lt;/p&gt;&lt;p&gt;Lets go and build our own &lt;code&gt;Diff&lt;/code&gt; type encoding a &lt;code&gt;MaxProd&lt;/code&gt; semiring.&lt;/p&gt;&lt;pre&gt;&lt;code class=&quot;rust&quot;&gt;&lt;span class=&quot;keyword_modifier&quot;&gt;pub&lt;/span&gt; &lt;span class=&quot;keyword_type&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;type&quot;&gt;MaxProd&lt;/span&gt; &lt;span class=&quot;punctuation_bracket&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;keyword_modifier&quot;&gt;pub&lt;/span&gt; &lt;span class=&quot;constant variable_member&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;type_builtin&quot;&gt;u32&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;punctuation_bracket&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If we take a look at the signature of the monoid trait:&lt;/p&gt;&lt;pre&gt;&lt;code class=&quot;rust&quot;&gt;&lt;span class=&quot;keyword_modifier&quot;&gt;pub&lt;/span&gt; &lt;span class=&quot;keyword_type&quot;&gt;trait&lt;/span&gt; &lt;span class=&quot;type&quot;&gt;Monoid&lt;/span&gt; &lt;span class=&quot;punctuation_delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;punctuation_bracket operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;keyword_modifier&quot;&gt;&amp;apos;&lt;/span&gt;&lt;span class=&quot;constant_builtin attribute_builtin constant type attribute variable&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;punctuation_bracket operator&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;type&quot;&gt;AddAssign&lt;/span&gt;&lt;span class=&quot;punctuation_bracket operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;operator&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;keyword_modifier&quot;&gt;&amp;apos;&lt;/span&gt;&lt;span class=&quot;constant_builtin attribute_builtin constant type attribute variable&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;type&quot;&gt;Self&lt;/span&gt;&lt;span class=&quot;punctuation_bracket operator&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;punctuation_delimiter&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;module constant type variable constant_builtin&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;module constant type variable constant_builtin&quot;&gt;marker&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;type&quot;&gt;Sized&lt;/span&gt; &lt;span class=&quot;operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;type&quot;&gt;Data&lt;/span&gt; &lt;span class=&quot;operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;type&quot;&gt;Clone&lt;/span&gt; &lt;span class=&quot;punctuation_bracket&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;comment spell&quot;&gt;/* snip */&lt;/span&gt;
&lt;span class=&quot;punctuation_bracket&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;we see that we basically only need to implement Rusts &lt;code&gt;AddAssign&lt;/code&gt; trait. It is only a monoid after all. To make it a semiring we need a multiplication operator. When we provide it we can use our newly defined type as a differences type for a collection.&lt;/p&gt;&lt;pre&gt;&lt;code class=&quot;rust&quot;&gt;&lt;span class=&quot;keyword&quot;&gt;impl&lt;/span&gt;&lt;span class=&quot;punctuation_bracket operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;keyword_modifier&quot;&gt;&amp;apos;&lt;/span&gt;&lt;span class=&quot;constant_builtin attribute_builtin constant type attribute variable&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;punctuation_bracket operator&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;type&quot;&gt;AddAssign&lt;/span&gt;&lt;span class=&quot;punctuation_bracket operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;operator&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;keyword_modifier&quot;&gt;&amp;apos;&lt;/span&gt;&lt;span class=&quot;constant_builtin attribute_builtin constant type attribute variable&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;type&quot;&gt;Self&lt;/span&gt;&lt;span class=&quot;punctuation_bracket operator&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;type&quot;&gt;MaxProd&lt;/span&gt; &lt;span class=&quot;punctuation_bracket&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;keyword_function&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;constant_builtin constant type variable function&quot;&gt;add_assign&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;operator&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;keyword_modifier&quot;&gt;mut&lt;/span&gt; &lt;span class=&quot;variable_builtin&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;constant_builtin constant variable_parameter variable type&quot;&gt;rhs&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;operator&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;type&quot;&gt;MaxProd&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;punctuation_bracket&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;variable_builtin&quot;&gt;self&lt;/span&gt; &lt;span class=&quot;operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;type&quot;&gt;MaxProd&lt;/span&gt; &lt;span class=&quot;punctuation_bracket&quot;&gt;{&lt;/span&gt;
                        &lt;span class=&quot;constant variable_member&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;module constant type variable constant_builtin&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;module constant type variable constant_builtin&quot;&gt;cmp&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;constant_builtin constant function_call type variable&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;variable_builtin&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;constant variable_member&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;constant type variable constant_builtin&quot;&gt;rhs&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;constant variable_member&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;,&lt;/span&gt;
                &lt;span class=&quot;punctuation_bracket&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;punctuation_bracket&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;punctuation_bracket&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;keyword&quot;&gt;impl&lt;/span&gt; &lt;span class=&quot;type&quot;&gt;Mul&lt;/span&gt;&lt;span class=&quot;punctuation_bracket operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;type&quot;&gt;Self&lt;/span&gt;&lt;span class=&quot;punctuation_bracket operator&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;type&quot;&gt;MaxProd&lt;/span&gt; &lt;span class=&quot;punctuation_bracket&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;keyword_type&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;type&quot;&gt;Output&lt;/span&gt; &lt;span class=&quot;operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;type&quot;&gt;Self&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;keyword_function&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;constant_builtin constant type variable function&quot;&gt;mul&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;variable_builtin&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;constant_builtin constant variable_parameter variable type&quot;&gt;rhs&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;type&quot;&gt;Self&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;punctuation_delimiter&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;type&quot;&gt;Self&lt;/span&gt; &lt;span class=&quot;punctuation_bracket&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;type&quot;&gt;MaxProd&lt;/span&gt; &lt;span class=&quot;punctuation_bracket&quot;&gt;{&lt;/span&gt;
                        &lt;span class=&quot;constant variable_member&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;variable_builtin&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;constant variable_member&quot;&gt;value&lt;/span&gt; &lt;span class=&quot;operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;constant type variable constant_builtin&quot;&gt;rhs&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;constant variable_member&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;,&lt;/span&gt;
                &lt;span class=&quot;punctuation_bracket&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;punctuation_bracket&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;punctuation_bracket&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We can implement the &lt;code&gt;Monoid&lt;/code&gt; trait that only consists of two methods &lt;code&gt;zero()&lt;/code&gt; and &lt;code&gt;is_zero()&lt;/code&gt;. These tell differential dataflow when accumulations of updates equal zero (That is e.g. important to know if we are doing fix-point iteration and want to know when to stop).&lt;/p&gt;&lt;pre&gt;&lt;code class=&quot;rust&quot;&gt;&lt;span class=&quot;keyword&quot;&gt;impl&lt;/span&gt; &lt;span class=&quot;type&quot;&gt;Monoid&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;type&quot;&gt;MaxProd&lt;/span&gt; &lt;span class=&quot;punctuation_bracket&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;keyword_function&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;constant_builtin constant type variable function&quot;&gt;zero&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;punctuation_delimiter&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;type&quot;&gt;MaxProd&lt;/span&gt; &lt;span class=&quot;punctuation_bracket&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;type&quot;&gt;MaxProd&lt;/span&gt; &lt;span class=&quot;punctuation_bracket&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;constant variable_member&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;punctuation_bracket&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;punctuation_bracket&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;punctuation_bracket&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And that is it. &lt;code&gt;is_zero()&lt;/code&gt; has a default implementation.&lt;/p&gt;&lt;p&gt;Now, why is it interesting to encode this semiring directly as the differences type?&lt;br&gt;Because the join operator applies the &lt;code&gt;Mul&lt;/code&gt; operation whilst joining and when we accumulate updates, e.g. with the &lt;code&gt;consolidate&lt;/code&gt; operator or in &lt;code&gt;reduce&lt;/code&gt;, we apply &lt;code&gt;AddAssign&lt;/code&gt;.&lt;br&gt;The idea behind accumulation and consolidation is that we merge triples whose data part and timestamp are identical. In our case, the &lt;code&gt;Data&lt;/code&gt; part of our collection only contains indexing variables (edges in the case of the triangle computation or matrix indices for the chain multiplication) but no actual values. In the process of eliminating a variable, we may produce a multiplicity of triples with the same indices. These are &lt;code&gt;consolidated&lt;/code&gt; by applying the &lt;code&gt;AddAssign&lt;/code&gt; operator, one of our monoids.&lt;/p&gt;&lt;p&gt;We can evaluate our FAQ simply by joining collections and consolidating the result.&lt;/p&gt;&lt;h2&gt;Tying things together&lt;/h2&gt;&lt;p&gt;We implement the FAQ semiring as our &lt;code&gt;Diff&lt;/code&gt; type and move the variables to the data plane. That means every listing in the listing representation is one triple in the collection.  Now &lt;code&gt;insideOut&lt;/code&gt; is simply the repeated application of the &lt;code&gt;join&lt;/code&gt; operator and a &lt;code&gt;reduce&lt;/code&gt; to eliminate variables. I did a naive and simple implementation of that and a few semi rings over here:&lt;sup class=&quot;footnote-ref&quot;&gt;&lt;a href=&quot;#fn-5&quot; id=&quot;fn-5-ref-1&quot;&gt;5&lt;/a&gt;&lt;/sup&gt; (That is partly broken right now though. I promise better)&lt;/p&gt;&lt;pre&gt;&lt;code class=&quot;rust&quot;&gt;&lt;span class=&quot;keyword_function&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;constant_builtin constant type variable function&quot;&gt;inside_out&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;variable_builtin&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;punctuation_delimiter&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;type&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;punctuation_bracket&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;comment spell&quot;&gt;// Reduce over factors, variables and aggregates to return a faq instance over only free variables&lt;/span&gt;
        &lt;span class=&quot;keyword&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;constant type variable constant_builtin&quot;&gt;faq&lt;/span&gt; &lt;span class=&quot;operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;variable_builtin&quot;&gt;self&lt;/span&gt;
                &lt;span class=&quot;punctuation_delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;constant variable_member&quot;&gt;variable_order&lt;/span&gt;
                &lt;span class=&quot;punctuation_delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;constant function_call variable_member&quot;&gt;into_iter&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;)&lt;/span&gt;
                &lt;span class=&quot;punctuation_delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;constant function_call variable_member&quot;&gt;fold&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;variable_builtin&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;constant variable_member&quot;&gt;factors&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;punctuation_bracket operator&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;keyword_modifier variable_parameter&quot;&gt;mut&lt;/span&gt;&lt;span class=&quot;variable_parameter&quot;&gt; &lt;/span&gt;&lt;span class=&quot;constant_builtin constant variable_parameter variable type&quot;&gt;factors&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;constant_builtin constant variable_parameter variable type&quot;&gt;var&lt;/span&gt;&lt;span class=&quot;punctuation_bracket operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;punctuation_bracket&quot;&gt;{&lt;/span&gt;
                        &lt;span class=&quot;keyword&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;constant type variable constant_builtin&quot;&gt;hyper_edges&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;type&quot;&gt;Vec&lt;/span&gt;&lt;span class=&quot;punctuation_bracket operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;punctuation_bracket operator&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;constant type variable constant_builtin&quot;&gt;factors&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;constant function_call variable_member&quot;&gt;drain_filter&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;punctuation_bracket operator&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;constant_builtin constant variable_parameter variable type&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;punctuation_bracket operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;constant type variable constant_builtin&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;constant function_call variable_member&quot;&gt;participate&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;operator&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;constant type variable constant_builtin&quot;&gt;var&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;constant function_call variable_member&quot;&gt;collect&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;;&lt;/span&gt;
                        &lt;span class=&quot;keyword&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;constant type variable constant_builtin&quot;&gt;factor_prime&lt;/span&gt; &lt;span class=&quot;operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;constant_builtin constant function_call variable type&quot;&gt;eliminate&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;constant_builtin constant function_call variable type&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;constant type variable constant_builtin&quot;&gt;hyper_edges&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;constant type variable constant_builtin&quot;&gt;var&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;;&lt;/span&gt;
                        &lt;span class=&quot;constant type variable constant_builtin&quot;&gt;factors&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;constant function_call variable_member&quot;&gt;push&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;constant type variable constant_builtin&quot;&gt;factor_prime&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;;&lt;/span&gt;
                        &lt;span class=&quot;constant type variable constant_builtin&quot;&gt;factors&lt;/span&gt;
                &lt;span class=&quot;punctuation_bracket&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;comment spell&quot;&gt;// Join the remaining factors to produce the output representation&lt;/span&gt;
        &lt;span class=&quot;comment spell&quot;&gt;/* snip */&lt;/span&gt;
&lt;span class=&quot;punctuation_bracket&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Internally &lt;code&gt;eliminate&lt;/code&gt; and &lt;code&gt;join&lt;/code&gt; apply the differential dataflow operators.&lt;/p&gt;&lt;p&gt;Currently this only works for single semiring applications of the FAQ setting.&lt;/p&gt;&lt;p&gt;The output coming from &lt;code&gt;inside_out&lt;/code&gt; is simply another collection whose output is the evaluation of the FAQ.&lt;/p&gt;&lt;h1&gt;Incrementalize it!&lt;/h1&gt;&lt;p&gt;Now that we expressed, at least a rather naive, FAQ framework in differential dataflow, we can turn to the interesting things.&lt;/p&gt;&lt;p&gt;Does this incrementalize functional aggregate queries, or some instances of it?&lt;/p&gt;&lt;p&gt;For this, we need to do a bit of math.&lt;/p&gt;&lt;p&gt;Single Semiring FAQ’s represent all these instances that only have a single semiring. The examples shown at the beginning are SS-FAQ instances. To show that it is possible to incrementalize a FAQ instance we need commutativity between all aggregations. This is naturally only given, without loss of generality, when there is only one semiring.&lt;/p&gt;&lt;script type=&quot;math/tex&quot;&gt;\begin{aligned}
\overline{\varphi}(\boldsymbol{\text{x}}_{[f]}) \, &amp;= \, \underset{x_{f+1}}{\bigoplus^{(f+1)}} \, ... \, \underset{x_{n}}{\bigoplus^{(n)}} \bigotimes_{S \in \mathcal{E}} \psi_{S}(\boldsymbol{\text{x}}_{S}) \\
&amp;= \, \underset{x_{f+1}}{\bigoplus} \, ... \, \underset{x_{n}}{\bigoplus} \bigotimes_{S \in \mathcal{E} - S^{&apos;}} \psi_{S}(\boldsymbol{\text{x}}_{S}) \otimes \left( \psi_{S^{&apos;}}(\boldsymbol{\text{x}}_{S^{&apos;}}) \bigoplus^{(n)} \delta_{\boldsymbol{\text{x}}_{S^{&apos;}}}\right) \\
&amp;= \, \varphi(\boldsymbol{\text{x}}_{[f]}) \bigoplus^{(n)} \delta \varphi(\boldsymbol{\text{x}}_{[f]})
\end{aligned}
&lt;/script&gt;&lt;p&gt;The equation above shows that incremental properties hold. We start with a given FAQ instance. Then we assume an update occurs to factor &lt;script type=&quot;math/tex&quot;&gt;\psi_{S^{&apos;}}(\boldsymbol{\text{x}}_{S^{&apos;}})&lt;/script&gt; that regards variable &lt;script type=&quot;math/tex&quot;&gt;n&lt;/script&gt;, namely &lt;script type=&quot;math/tex&quot;&gt;\delta_{\boldsymbol{\text{x}}_{S^{&apos;}}}&lt;/script&gt;. Observe that we distribute the &lt;script type=&quot;math/tex&quot;&gt;\bigoplus^{(n)}&lt;/script&gt; over the multiplication. Now commutativity is important as we push the remaining &lt;script type=&quot;math/tex&quot;&gt;n-1&lt;/script&gt; aggregations through &lt;script type=&quot;math/tex&quot;&gt;\bigoplus^{(n)}&lt;/script&gt;.  We can then compute the original part of the FAQ that leads to the left result. The right part: &lt;script type=&quot;math/tex&quot;&gt;\delta \varphi(\boldsymbol{\text{x}}_{[f]})&lt;/script&gt; represents the incremental update. Here we only need to compute the result with the single update &lt;script type=&quot;math/tex&quot;&gt;\delta_{\boldsymbol{\text{x}}_{S^{&apos;}}}&lt;/script&gt;. To get the new result to our FAQ instance, we only need to take the already computed value: &lt;script type=&quot;math/tex&quot;&gt;\varphi(\boldsymbol{\text{x}}_{[f]})&lt;/script&gt; and apply the aggregate to the update.&lt;/p&gt;&lt;p&gt;Incrementality in the sense of the above statement holds.&lt;/p&gt;&lt;p&gt;The beauty of implementing the FAQ setting in differential dataflow is that operators like &lt;code&gt;join&lt;/code&gt; are already internally incrementally implemented (that is the whole point). That means by phrasing our computation with differential dataflow &lt;code&gt;join&lt;/code&gt;s and monoidal &lt;code&gt;reduce&lt;/code&gt;es we incrementalized it.&lt;/p&gt;&lt;p&gt;We may start with a batch of inputs for our factors and evaluate the FAQ computation for a specific time, say &lt;script type=&quot;math/tex&quot;&gt;t_{0}&lt;/script&gt;. Sometime later, say &lt;script type=&quot;math/tex&quot;&gt;t_{5}&lt;/script&gt; there are novel inputs and we incrementally update the evaluation, because internally it is just a bunch of joins. These novel inputs might even be retractions (for that our semiring needs to be an abelian group to allow for inverse elements). Updating the result just takes a fraction of the time.&lt;/p&gt;&lt;h1&gt;Conclusion&lt;/h1&gt;&lt;p&gt;Well, this was quite a fun project.&lt;/p&gt;&lt;p&gt;I think there are a few very interesting things to point out.&lt;/p&gt;&lt;ul&gt;&lt;li&gt;The power of rusts type system to let us enforce mathematical&lt;/li&gt;&lt;li&gt;invariants statically and sanely.&lt;/li&gt;&lt;li&gt;The surprising generality of differential dataflow.&lt;/li&gt;&lt;li&gt;How amazing differential dataflows &lt;code&gt;join&lt;/code&gt; is.&lt;/li&gt;&lt;li&gt;The power and versatility of relational algebra and the FAQ framework to express a lot of common problems.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;And I haven’t even started talking about running this in a distributed setting.&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Next Time&lt;/strong&gt;:&lt;/p&gt;&lt;p&gt;Until now we did not see the whole thing in action. That must wait for another blog post though. I’ll implement a few more FAQ’s in this incremental setting and we will have a look at the run times.&lt;/p&gt;</description>
                <link>https://david-bach.com/pages/posts/2019-07-06-Incremental-Functional-Aggregate-Queries/</link>
                <guid>https://david-bach.com/pages/posts/2019-07-06-Incremental-Functional-Aggregate-Queries/</guid>
                <pubDate>Sat, 06 Jul 2019 00:00:00 +0000</pubDate>
            </item>
        
            <item>
                <title>Reactive Datalog with Vega</title>
                <description>&lt;h1&gt;Reactive Datalog with Vega&lt;/h1&gt;&lt;p&gt;Reactive Datalog?&lt;/p&gt;&lt;p&gt;Yes exactly! We are working on a system, dubbed &lt;strong&gt;3DF&lt;/strong&gt; for &lt;em&gt;Declarative Differential Dataflow&lt;/em&gt; &lt;sup class=&quot;footnote-ref&quot;&gt;&lt;a href=&quot;#fn-1&quot; id=&quot;fn-1-ref-1&quot;&gt;1&lt;/a&gt;&lt;/sup&gt; , that has the ability to compile Datalog queries into Differential Dataflows.&lt;/p&gt;&lt;p&gt;Differential Dataflow &lt;sup class=&quot;footnote-ref&quot;&gt;&lt;a href=&quot;#fn-2&quot; id=&quot;fn-2-ref-1&quot;&gt;2&lt;/a&gt;&lt;/sup&gt; data-parallel programming framework designed to efficiently process large volumes of data and to quickly respond to arbitrary changes in input collections”. It is written and maintained by Frank McSherry.&lt;/p&gt;&lt;p&gt;A unique property of this framework is its ability to incrementally update the state of various operators (think &lt;code&gt;join&lt;/code&gt;, &lt;code&gt;group&lt;/code&gt;, …). In other words: A Differential Dataflow is a computation that reacts to incoming changes in a smart and efficient way and propagates any new information correctly. There is quite a lot to it and we defer to &lt;a href=&quot;http://frankmcsherry.org/&quot;&gt;Frank’s blog&lt;/a&gt; for a trove of information on Differential itself.&lt;/p&gt;&lt;p&gt;The system consists of a server written in Rust that takes commands via WebSocket and internally constructs the appropriate dataflows. We’ve build a client that can compile Datalog queries into query plans that the backend understands.&lt;/p&gt;&lt;p&gt;To clarify, here is a small example: We are interested in all people with a certain name and a certain age. As a Datalog query that may look like this:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;[:find ?name ?age
 :where
 [?e :name ?name]
 [?e :age ?age]
 [(&amp;gt; ?age 18)]]
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Now we input some new information (In general that may come from some file, a stream, kafka, datomic, … you name it):&lt;/p&gt;&lt;pre&gt;&lt;code&gt;{:name &amp;quot;Mabel&amp;quot; :age 19}
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;3DF will update us promptly (For how see below):&lt;/p&gt;&lt;pre&gt;&lt;code&gt;[[[&amp;quot;Mabel&amp;quot; 19] 1]]
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;It tells us that there is new information in the result set of our query. The tuple consisting of &lt;code&gt;&amp;quot;Mabel&amp;quot; 19&lt;/code&gt;is the result of our query and the &lt;code&gt;1&lt;/code&gt;is the difference. That means this information is added.&lt;/p&gt;&lt;p&gt;Maybe we realized, that we made a typo and we retract the first information and add a new one: &lt;code&gt;:name &amp;quot;Mabel :age 21&amp;quot;&lt;/code&gt;. 3DF will promptly tell us&lt;/p&gt;&lt;pre&gt;&lt;code&gt;[[[&amp;quot;Mabel&amp;quot; 19] -1] [[&amp;quot;Mabel&amp;quot; 21] 1]]
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;indicating that now there are new facts in the system. The person with name “Mabel” is not 19 but actually 21. This represents a fact, a statement about our world that is true at a given point in time. Given such differences, Differential Dataflow will only do work proportional to the change itself.&lt;/p&gt;&lt;p&gt;Having 3DF keep track of our changes, we can start looking at ClojureScript and Vega.&lt;/p&gt;&lt;h2&gt;Communication&lt;/h2&gt;&lt;p&gt;3DF exposes a WebSocket connection over which we can register new Datalog expressions, manage input parameters, send data, and receive updates to our queries, as new data arrives in the system.&lt;/p&gt;&lt;p&gt;Any further communication is build around Clojure’s amazing async library. The client code &lt;sup class=&quot;footnote-ref&quot;&gt;&lt;a href=&quot;#fn-3&quot; id=&quot;fn-3-ref-1&quot;&gt;3&lt;/a&gt;&lt;/sup&gt; is available for both Clojure &amp; ClojureScript. When connecting to the server, we get a single connection handle:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;(def conn (create-conn &amp;quot;ws://127.0.0.1:6262&amp;quot;))
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;It contains one async channel were all outputs are put and one sink to which we can write commands.  As we likely will be running more than one query, we want dedicated subscribers for every one of them. For that purpose we use pub/sub from the async library.  We partition our results by the name of the query.&lt;/p&gt;&lt;p&gt;A registration will look like the following, with the former query:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;(exec! conn (register-query &amp;quot;people-over-18&amp;quot; query))
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Here &lt;code&gt;people-over-18&lt;/code&gt; is our globally named relation and &lt;code&gt;exec!&lt;/code&gt; is a macro that will serialize the output from &lt;code&gt;register-query&lt;/code&gt; and write it into the WebSocket.&lt;/p&gt;&lt;h2&gt;Vega&lt;/h2&gt;&lt;p&gt;Alright. Communication is sorted and we have 3DF running. Now a short look at Vega.&lt;/p&gt;&lt;p&gt;Vega is high-level grammar for visualizations &lt;sup class=&quot;footnote-ref&quot;&gt;&lt;a href=&quot;#fn-4&quot; id=&quot;fn-4-ref-1&quot;&gt;4&lt;/a&gt;&lt;/sup&gt;. That means we describe, in a declarative manner, what we want Vega to visualize. Here is an example specification &lt;sup class=&quot;footnote-ref&quot;&gt;&lt;a href=&quot;#fn-5&quot; id=&quot;fn-5-ref-1&quot;&gt;5&lt;/a&gt;&lt;/sup&gt; for a bar chart with x values taken from the &lt;code&gt;hour&lt;/code&gt; field and y values from &lt;code&gt;sum_passenger&lt;/code&gt;:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;{:data     {:name &amp;quot;passenger&amp;quot;}
 :width    1000
 :heigt    1500
 :mark     &amp;quot;bar&amp;quot;
 :encoding {:x {:field &amp;quot;hour&amp;quot;
                :type  &amp;quot;quantitative&amp;quot;
                :title &amp;quot;Hour&amp;quot;}
            :y {:field &amp;quot;sum_passenger&amp;quot;
                :type  &amp;quot;quantitative&amp;quot;
                :title &amp;quot;# passengers&amp;quot;}}}
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;In contrast to normal Vega specs, we do not specify any data. We will stream it!&lt;/p&gt;&lt;p&gt;Vega offers an &lt;code&gt;embed&lt;/code&gt; functionality, which can run arbitrary JavaScript in its callbacks. Well thats exactly the place we will put our communication logic to get new results.&lt;/p&gt;&lt;pre&gt;&lt;code&gt;(go-loop [] 
  (when-let [[_ results] (!&amp;lt; source)]
    (.. res -view
      (change data-name
              (.. js/vega changeset
              (insert (vega-insert encoding (:insert diffmap)))
              (remove (vega-remove encoding (:remove diffmap)))))
    (run))
  (recur)))
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;As soon as some data arrives in the channel, we take it, process it and call two Vega methods.  We create a &lt;code&gt;changeset&lt;/code&gt; via the &lt;code&gt;change&lt;/code&gt; method, expressing what changes Vega should visualize. The &lt;code&gt;insert&lt;/code&gt; takes a vector of tuples indicating new data to insert and the &lt;code&gt;remove&lt;/code&gt; takes a function, which will be called on the existing data tuples and returns &lt;code&gt;true&lt;/code&gt; for all that are supposed to be removed.&lt;/p&gt;&lt;p&gt;In the end we call &lt;code&gt;run&lt;/code&gt; which will execute and render the changes. More on that can be read here &lt;sup class=&quot;footnote-ref&quot;&gt;&lt;a href=&quot;#fn-6&quot; id=&quot;fn-6-ref-1&quot;&gt;6&lt;/a&gt;&lt;/sup&gt;. Finally we call the &lt;code&gt;embed&lt;/code&gt; method and mount into the DOM.&lt;/p&gt;&lt;h2&gt;Running Analytics&lt;/h2&gt;&lt;p&gt;Our data set is the publicly available data of NYC cab rides &lt;sup class=&quot;footnote-ref&quot;&gt;&lt;a href=&quot;#fn-7&quot; id=&quot;fn-7-ref-1&quot;&gt;7&lt;/a&gt;&lt;/sup&gt;.  The dataset contains one single day, these are around 9.000.000 lines of uncompressed csv with a size of 800 MB. We are running 3DF in one thread on my MBP.&lt;/p&gt;&lt;p&gt;Every line describes one cab ride with the following attributes: &lt;code&gt;VendorID&lt;/code&gt;, &lt;code&gt;pickup_time&lt;/code&gt;, &lt;code&gt;passenger_count&lt;/code&gt;, &lt;code&gt;trip_distance&lt;/code&gt;. The remaining ones are omitted, as we are not using them. We will simulate streaming this data at 1024 lines per batch.&lt;/p&gt;&lt;pre&gt;&lt;code&gt;(exec! conn
    (register-source
     [:cab/vendor :cab/hour :cab/passenger :cab/distance]
     {:CsvFile {:path      &amp;quot;DATA_PATH&amp;quot;
                :separator &amp;quot;,&amp;quot;
                :schema    [[0 {:Number 0}][1 {:Number 0}] [3 {:Number 0}] [4 {:Number 0}]]}}))
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Columns from this dataset will always have the namespace &lt;code&gt;cab/&lt;/code&gt; such that we can distinguish them from different sources. The reading of the whole dataset takes around 70 seconds with a batch size of 1024 lines. That is quite long, compared to 25 seconds when we do not batch them with that high of a granularity. That is because we increment the logical timestamp every 1024 lines, causing Differential to do a lot more progress tracking work.&lt;/p&gt;&lt;p&gt;The first thing we look at is quite simple: &lt;em&gt;Number of transported people per hour&lt;/em&gt;. Here is the Datalog query:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;(def passenger
  &amp;quot;Number of transported people per hour&amp;quot;
  &amp;apos;[:find ?hour (sum ?passenger)
    :with ?e
    :where
    [?e :cab/passenger ?passenger]
    [?e :cab/hour ?hour]])
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The Vega specs for all queries are here: &lt;a href=&quot;https://gist.github.com/eoxxs/cca4b9d83b1eece79e4ce4ea22b16509&quot;&gt;vega-specs&lt;/a&gt;&lt;/p&gt;&lt;p&gt;You’ll only see parts of the whole day. 3DF will update us with a quite high frequency. Every update consists of values to be removed and values to be added.&lt;/p&gt;&lt;p&gt;&lt;a href=&quot;https://david-bach.com/assets/pages/2018-11-25-reactive-datalog-with-vega/passengers.gif&quot;&gt;Passenger.gif&lt;/a&gt;&lt;/p&gt;&lt;p&gt;On the x-axis you see the different hours and the y-axis represent the number of transported passengers. You can see how neatly those bars are growing.&lt;/p&gt;&lt;h2&gt;Interactive Queries&lt;/h2&gt;&lt;p&gt;Well that has been nice and fun. We can stream results of our computations into Vega and get updated visualizations whenever the result set of our query changes. But this is just the beginning. How about creating an interactive visualization just with 3DF?&lt;/p&gt;&lt;p&gt;For this we will create a new query with an interactive input, allowing our users to see results for specific hours of the day. We will be looking at a query that asks for the number of rides aggregated by trip distance, with all distances rounded to integers.&lt;/p&gt;&lt;pre&gt;&lt;code&gt;(def distance-distribution
  &amp;quot;Aggregate number of rides for a given distance&amp;quot;
  &amp;apos;[:find ?distance (count ?e)
    :where
    [?e :cab/distance ?distance]
    [?e :cab/hour ?hour]]) ; &amp;lt;- we want to control this
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;We introduce a new interactive input called &lt;code&gt;:filter/hour&lt;/code&gt;.&lt;/p&gt;&lt;pre&gt;&lt;code&gt;(exec! conn (create-input :filter/hour))
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Now we have to adapt the query to make use of this new input. For that we bind input values to the symbol &lt;code&gt;?hour&lt;/code&gt; and then join them with the &lt;code&gt;:cab/hour&lt;/code&gt;. This ends up looking like the following:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;(def distance-distribution
  &amp;quot;Aggregate number of rides for a given distance&amp;quot;
  &amp;apos;[:find ?distance (count ?e)
    :where
    [_ :filter/hour ?hour] ; &amp;lt;- the new input
    [?e :cab/distance ?distance]
    [?e :cab/hour ?hour]])
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Now we can start transacting into that input and observe changes to our visualization. In Emacs on the right, we update the hour input.&lt;/p&gt;&lt;p&gt;&lt;a href=&quot;https://david-bach.com/assets/pages/2018-11-25-reactive-datalog-with-vega/interactive_2.gif&quot;&gt;Interactivity.gif&lt;/a&gt;&lt;/p&gt;&lt;p&gt;We start with the distribution at 9pm, move on to 3am, 6pm, and finally to 11pm. You could also add several different hours to see the distribution for all of them combined. Every addition into &lt;code&gt;:filter/hour&lt;/code&gt; input leads the &lt;code&gt;?hour&lt;/code&gt;-join in the &lt;code&gt;distance-distribution&lt;/code&gt; query to match all those rows that have the respective &lt;code&gt;:cab/hour&lt;/code&gt; value. Retractions work the same but with negative differences and as such lead to data being removed.&lt;/p&gt;&lt;h2&gt;Embrace The Change&lt;/h2&gt;&lt;p&gt;After spending all day building this wonderful dashboard and computing all these aggregates your boss storms into your office. He informs you that one of the vendors has retracted their data usage consent. We need to remove all of their information asap.&lt;/p&gt;&lt;p&gt;3DF to the rescue. We have a system that is designed to propagate changes efficiently. So lets see how we can solve this.&lt;/p&gt;&lt;p&gt;We’ll assume that we have a separate database containing information about the different vendors. Every line in our analytics files contains an id, uniquely identifying these vendors. In our query we would match on these ids, in order to incorporate information from the vendor database &lt;sup class=&quot;footnote-ref&quot;&gt;&lt;a href=&quot;#fn-8&quot; id=&quot;fn-8-ref-1&quot;&gt;8&lt;/a&gt;&lt;/sup&gt;:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;(def passenger
  &amp;quot;Number of transported people per hour&amp;quot;
  &amp;apos;[:find ?hour (sum ?passenger)
    :with ?e
    :where
    [?e :cab/passenger ?passenger]
    [?e :cab/hour ?hour]
    [?e :cab/vendor ?id]
    [_  :vendor/id ?id]])
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Now observe what happens to our results as we remove this one vendor from the vendor database.&lt;/p&gt;&lt;pre&gt;&lt;code&gt;(exec! conn (transact db [[:db/retract :vendor/id 1]]))
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;a href=&quot;https://david-bach.com/assets/pages/2018-11-25-reactive-datalog-with-vega/gdpr_1.gif&quot;&gt;Number of passenger.gif&lt;/a&gt;, &lt;a href=&quot;https://david-bach.com/assets/pages/2018-11-25-reactive-datalog-with-vega/gdpr_2.gif&quot;&gt;Distribution of distances.gif&lt;/a&gt;&lt;/p&gt;&lt;p&gt;You see how all the values drop down as we remove the vendor. This one retraction led to a change touching more than half of all cab rides and these changes are propagated through all dataflows.&lt;/p&gt;&lt;p&gt;All our results got updated without us changing our queries or filtering the input dataset. It’s also important to note that we didn’t have to re-run our computations from scratch, as we can still use the data from all other vendors. 3DF efficiently computed just the differences.&lt;/p&gt;&lt;p&gt;Imagine we had been feeding these results into even more computations, such as price forecasting, movement analytics, or predicting traffic bottlenecks. All those downstream computations might’ve also used information affected by this retraction, and would now be updated accordingly and in an efficient manner.&lt;/p&gt;&lt;p&gt;Well this has been a lot of fun. Hopefully you’ve made it through this lengthy piece. Next time we will take a deeper dive into the performance characteristics of working with changes, run computations in a distributed setting, and see how 3DF performs at scale.&lt;/p&gt;&lt;p&gt;Until then cheers and goodbye.&lt;/p&gt;&lt;h2&gt;Resources&lt;/h2&gt;</description>
                <link>https://david-bach.com/pages/posts/2018-11-25-reactive-datalog-with-vega/</link>
                <guid>https://david-bach.com/pages/posts/2018-11-25-reactive-datalog-with-vega/</guid>
                <pubDate>Sun, 25 Nov 2018 00:00:00 +0000</pubDate>
            </item>
        
    </channel>
</rss>
