<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
    <title>Boyd Johnson</title>
    <subtitle>Systems software, machine learning compilers, and heterogeneous computing.</subtitle>
    <link rel="self" type="application/atom+xml" href="https://boydjohnson.dev/atom.xml"/>
    <link rel="alternate" type="text/html" href="https://boydjohnson.dev"/>
    <generator uri="https://www.getzola.org/">Zola</generator>
    <updated>2026-08-22T00:00:00+00:00</updated>
    <id>https://boydjohnson.dev/atom.xml</id>
    <entry xml:lang="en">
        <title>Exploring Machine Learning with a toy American football dataset</title>
        <published>2026-08-22T00:00:00+00:00</published>
        <updated>2026-08-22T00:00:00+00:00</updated>
        
        <author>
          <name>Boyd Johnson</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://boydjohnson.dev/blog/exploring-machine-learning-toy-dataset/"/>
        <id>https://boydjohnson.dev/blog/exploring-machine-learning-toy-dataset/</id>
        
        <content type="html" xml:base="https://boydjohnson.dev/blog/exploring-machine-learning-toy-dataset/">&lt;h2 id=&quot;intro&quot;&gt;Intro&lt;a class=&quot;zola-anchor&quot; href=&quot;#intro&quot; aria-label=&quot;Anchor link for: intro&quot;&gt;🔗&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;I got the idea to see how small of a machine learning model could understand American football diagrams (Xs and Os). I have only generated the offensive diagrams (the Os). The interesting things to predict are how many eligible receivers there are and if the formation is valid.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://boydjohnson.dev/blog/exploring-machine-learning-toy-dataset/./image-1.png&quot; alt=&quot;image&quot; /&gt;&lt;/p&gt;
&lt;h2 id=&quot;details&quot;&gt;Details&lt;a class=&quot;zola-anchor&quot; href=&quot;#details&quot; aria-label=&quot;Anchor link for: details&quot;&gt;🔗&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Outputs from this task:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;dataset &lt;a rel=&quot;external&quot; href=&quot;https://huggingface.co/datasets/boydjohnson/american-football-diagrams&quot;&gt;huggingface&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;generator and models &lt;a rel=&quot;external&quot; href=&quot;https://github.com/boydjohnson/american-football-diagrams&quot;&gt;github&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Some of the diagrams are similar to what you might see in a football game, and others are not – but both contain invalid and valid diagrams. The diagrams aren’t all the possible valid formations.&lt;/p&gt;
&lt;p&gt;The valid vs invalid formation is the easier task and relies on counting the number of Os that there are at the line of scrimmage: there should be 7+. The diagrams always put the line of scrimmage at the same vertical location&lt;/p&gt;
&lt;p&gt;The number of eligible receivers is a slightly harder task, and relies on if there is another player to the further outside on the line of scrimmage. The two outside on the line of scrimmage players and then every backfield position excluding the quarterback (for this dataset). For this dataset, I made the quarterback red and not a counted eligible receiver. There is a considerable amount of jitter in the placement of players in the diagrams and some of the linemen could be counted as not on the line of scrimmage in a real game.&lt;/p&gt;
&lt;h2 id=&quot;models&quot;&gt;Models&lt;a class=&quot;zola-anchor&quot; href=&quot;#models&quot; aria-label=&quot;Anchor link for: models&quot;&gt;🔗&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;I tried two different models on the same task. The models had two output heads during training: a binary classification head (was the formation valid or not) and a multi-class classification head (1 - 5 eligible receivers).&lt;/p&gt;
&lt;p&gt;The first model was a pure convolutional model common in image classification tasks, MobileNetV2, that I trained for 20 epochs, choosing the model with the best accuracy on the number of eligible receivers task. I trained MobileNetV2 without the pretrained weights as those weights caused the training to be unstable.&lt;/p&gt;
&lt;p&gt;The model chosen:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;epoch 18/20 train_loss=0.0112 train_formation_acc=0.9994 train_receiver_acc=0.9977 test_loss=0.0008 test_formation_acc=1.0000 test_receiver_acc=0.9996&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;=== formation_invalid (binary) ===&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;confusion matrix (rows=actual, cols=predicted):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                pred_valid  pred_invalid&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  actual_valid        9591             0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;actual_invalid           0           409&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;accuracy:  1.0000&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;precision: 1.0000&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;recall:    1.0000&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;f1:        1.0000&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;type I error rate  (false positive rate, valid called invalid): 0.0000&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;type II error rate (false negative rate, invalid called valid): 0.0000&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;=== eligible_receiver_count (multi-class) ===&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; actual\pred         1         2         3         4         5&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;           1         0         0         0         0         0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;           2         0         0         0         0         0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;           3         0         0       704         0         0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;           4         0         0         1      3625         0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;           5         0         0         0         3      5667&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;   class   precision    recall        f1   support&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;       1      0.0000    0.0000    0.0000         0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;       2      0.0000    0.0000    0.0000         0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;       3      0.9986    1.0000    0.9993       704&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;       4      0.9992    0.9997    0.9994      3626&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;       5      1.0000    0.9995    0.9997      5670&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The second model has Axial attention – attention along the width and height of the image separately – and Convolutions with skip connections. This model is in the train-tinyaxialmobilenet.py file in the Github repo.&lt;/p&gt;
&lt;p&gt;The best training epoch:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;epoch 17/20 train_loss=0.0050 train_formation_acc=1.0000 train_receiver_acc=0.9986 test_loss=0.0004 test_formation_acc=1.0000 test_receiver_acc=0.9999&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;=== formation_invalid (binary) ===&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;confusion matrix (rows=actual, cols=predicted):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                pred_valid  pred_invalid&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  actual_valid        9591             0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;actual_invalid           0           409&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;accuracy:  1.0000&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;precision: 1.0000&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;recall:    1.0000&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;f1:        1.0000&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;type I error rate  (false positive rate, valid called invalid): 0.0000&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;type II error rate (false negative rate, invalid called valid): 0.0000&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;=== eligible_receiver_count (multi-class) ===&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; actual\pred         1         2         3         4         5&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;           1         0         0         0         0         0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;           2         0         0         0         0         0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;           3         0         0       704         0         0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;           4         0         0         1      3625         0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;           5         0         0         0         0      5670&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;   class   precision    recall        f1   support&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;       1      0.0000    0.0000    0.0000         0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;       2      0.0000    0.0000    0.0000         0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;       3      0.9986    1.0000    0.9993       704&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;       4      1.0000    0.9997    0.9999      3626&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;       5      1.0000    1.0000    1.0000      5670&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;a class=&quot;zola-anchor&quot; href=&quot;#conclusion&quot; aria-label=&quot;Anchor link for: conclusion&quot;&gt;🔗&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Both models did well on the task. MobileNetV2 has ~3.5M parameters. The custom model had ~250k parameters, and did better then the pure vision model.&lt;/p&gt;
&lt;p&gt;I had thought the problem was more difficult than it actually proved to be. You can find people asking on Reddit and other places whether a formation was valid, and was a receiver eligible, but the actual task as posed in this dataset is easier than I imagined.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Using Hurl instead of Postman for information sharing within teams</title>
        <published>2023-01-06T00:00:00+00:00</published>
        <updated>2023-01-06T00:00:00+00:00</updated>
        
        <author>
          <name>Boyd Johnson</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://boydjohnson.dev/blog/using-cli-tools-for-postman/"/>
        <id>https://boydjohnson.dev/blog/using-cli-tools-for-postman/</id>
        
        <content type="html" xml:base="https://boydjohnson.dev/blog/using-cli-tools-for-postman/">&lt;h1 id=&quot;intro&quot;&gt;Intro&lt;a class=&quot;zola-anchor&quot; href=&quot;#intro&quot; aria-label=&quot;Anchor link for: intro&quot;&gt;🔗&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;Often in software development, a team has to write API clients, make API requests in application code, and troubleshoot APIs that are necessary for the team’s operation. Often these HTTP requests rely on multiple requests – maybe to an Auth server first – and multiple secrets. I have seen teams share Postman collections to share information about how to get the required information from the API when it isn’t always obvious how to do so. I will show you how to use Hurl to do these tasks.&lt;/p&gt;
&lt;h2 id=&quot;the-basics&quot;&gt;The Basics&lt;a class=&quot;zola-anchor&quot; href=&quot;#the-basics&quot; aria-label=&quot;Anchor link for: the-basics&quot;&gt;🔗&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;a rel=&quot;external&quot; href=&quot;https://hurl.dev/&quot;&gt;Hurl&lt;/a&gt; gives you two command-line tools, &lt;code&gt;hurl&lt;/code&gt; and &lt;code&gt;hurlfmt&lt;/code&gt;. You will be producing Hurl files (.hurl extensions)
that you can share with your team to give information on how to use HTTP servers (REST, GraphQL, HTML) to get important information.&lt;/p&gt;
&lt;p&gt;You can use &lt;code&gt;hurlfmt&lt;/code&gt; to make sure the file parses, as well as to produce simple documentation.&lt;/p&gt;
&lt;h2 id=&quot;documenting-complex-request-flows&quot;&gt;Documenting Complex Request Flows&lt;a class=&quot;zola-anchor&quot; href=&quot;#documenting-complex-request-flows&quot; aria-label=&quot;Anchor link for: documenting-complex-request-flows&quot;&gt;🔗&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Documenting complex request flows with multiple pieces of information (OAUTH client IDs and secrets, Basic Auth usernames and passwords, API Keys) and multiple requests is a perfect use-case for &lt;a rel=&quot;external&quot; href=&quot;https://hurl.dev/&quot;&gt;Hurl&lt;/a&gt;. For this blog post, I am using the Hurl version (hurl 4.2.0-SNAPSHOT) to have support for variables in keys &lt;a rel=&quot;external&quot; href=&quot;https://github.com/Orange-OpenSource/hurl/pull/2012&quot;&gt;PR #2012&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Suppose you are making requests to API #1, which requires you to authenticate against another endpoint and then use a bearer token from that endpoint to make the requests against API #1. Here is what that looks like in a Hurl file:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;POST https://auth.example.com/authenticate/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;X-API-REASON: &amp;quot;authentication&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[Options]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;verbose: true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &amp;quot;OAUTH_CLIENT_ID&amp;quot;: &amp;quot;{{OAUTH_CLIENT_ID}}&amp;quot;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &amp;quot;OAUTH_CLIENT_SECRET&amp;quot;: &amp;quot;{{OAUTH_CLIENT_SECRET}}&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;HTTP 200&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[Captures]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;token: jsonpath &amp;quot;$[&amp;#39;token&amp;#39;]&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;GET https://api1.example.com/v1/items&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Authorization: &amp;quot;Bearer {{token}}&amp;quot; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[Options]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;verbose: true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To understand the Hurl file, look for a Request section and an optional Response section. The file has two Request sections and one Response section. What is nice about Hurl files is that you can add comments to call out pieces of information that are especially important.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;POST https://auth.example.com/authenticate/    # POST to the Auth server&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;X-API-REASON: &amp;quot;authentication&amp;quot; # Header that is specific to this Auth server&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[Options]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;verbose: true  # Turn off verbose if you don&amp;#39;t want to see Request/Response information&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &amp;quot;OAUTH_CLIENT_ID&amp;quot;: &amp;quot;{{OAUTH_CLIENT_ID}}&amp;quot;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &amp;quot;OAUTH_CLIENT_SECRET&amp;quot;: &amp;quot;{{OAUTH_CLIENT_SECRET}}&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;#^^^^^ The JSON body to the POST request&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;HTTP 200   # A HTTP Status code that you are asserting/expecting&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[Captures]  # What you are capturing from the Response&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;token: jsonpath &amp;quot;$[&amp;#39;token&amp;#39;]&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;GET https://api1.example.com/v1/items   # The GET to API #1 to find your items&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Authorization: &amp;quot;Bearer {{token}}&amp;quot;    # Using the captured variable in a header &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[Options]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;verbose: true    # Turn off if you don&amp;#39;t want to see the Request/Response information&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This file requires two pieces of information: the OAUTH_CLIENT_ID and the OAUTH_CLIENT_SECRET. These can be handled in many different ways.&lt;/p&gt;
&lt;h3 id=&quot;some-ways-to-handle-variables-in-hurl-files&quot;&gt;Some ways to handle variables in Hurl files&lt;a class=&quot;zola-anchor&quot; href=&quot;#some-ways-to-handle-variables-in-hurl-files&quot; aria-label=&quot;Anchor link for: some-ways-to-handle-variables-in-hurl-files&quot;&gt;🔗&lt;/a&gt;&lt;/h3&gt;
&lt;h4 id=&quot;variable&quot;&gt;–variable&lt;a class=&quot;zola-anchor&quot; href=&quot;#variable&quot; aria-label=&quot;Anchor link for: variable&quot;&gt;🔗&lt;/a&gt;&lt;/h4&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;hurl --variable OAUTH_CLIENT_ID=foo --variable OAUTH_CLIENT_SECRET=bar file.hurl&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h4 id=&quot;variables-file&quot;&gt;–variables-file&lt;a class=&quot;zola-anchor&quot; href=&quot;#variables-file&quot; aria-label=&quot;Anchor link for: variables-file&quot;&gt;🔗&lt;/a&gt;&lt;/h4&gt;
&lt;p&gt;create an env file (prod.env):&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;OAUTH_CLIENT_ID=foo&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;OAUTH_CLIENT_SECRET=bar&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;hurl --variables-file prod.env file.hurl&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h4 id=&quot;from-environment-variables&quot;&gt;From Environment Variables&lt;a class=&quot;zola-anchor&quot; href=&quot;#from-environment-variables&quot; aria-label=&quot;Anchor link for: from-environment-variables&quot;&gt;🔗&lt;/a&gt;&lt;/h4&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;export HURL_OAUTH_CLIENT_ID=foo&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;export HURL_OAUTH_CLIENT_SECRET=bar&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;hurl file.hurl&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&quot;sharing-a-hurl-file-with-a-teammate&quot;&gt;Sharing a Hurl file with a teammate&lt;a class=&quot;zola-anchor&quot; href=&quot;#sharing-a-hurl-file-with-a-teammate&quot; aria-label=&quot;Anchor link for: sharing-a-hurl-file-with-a-teammate&quot;&gt;🔗&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;If someone within the team runs the file without including the variables that are needed &lt;code&gt;hurl&lt;/code&gt; will give them a helpful error message.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;❯&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; hurl&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; file.hurl&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;*&lt;/span&gt;&lt;span&gt; ------------------------------------------------------------------------------&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;*&lt;/span&gt;&lt;span&gt; Executing entry 1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;*&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;*&lt;/span&gt;&lt;span&gt; Entry options:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;*&lt;/span&gt;&lt;span&gt; verbose: true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;error:&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; Undefined&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; variable&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;  --&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; session5.hurl:6:27&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;   |&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt; 6&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt; |&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;     &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;OAUTH_CLIENT_ID&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;{{OAUTH_CLIENT_ID}}&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;   |&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;                           ^^^^^^^^^^^^^^^&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; you&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; must&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; set&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; the&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; variable&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; OAUTH_CLIENT_ID&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&quot;adhoc-use-of-an-api&quot;&gt;Adhoc use of an API&lt;a class=&quot;zola-anchor&quot; href=&quot;#adhoc-use-of-an-api&quot; aria-label=&quot;Anchor link for: adhoc-use-of-an-api&quot;&gt;🔗&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Sometimes team members want one piece of information from a server – maybe to determine what a probable value would look like, maybe because the API doesn’t have an OpenAPI schema and so they are unsure what the output should be – and so they want to make one or two HTTP requests.&lt;/p&gt;
&lt;p&gt;For this example, I’m going to use the &lt;a rel=&quot;external&quot; href=&quot;https://api.nobelprize.org&quot;&gt;Nobel Prize public API&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Create a file sess.hurl:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;GET https://api.nobelprize.org/v1/laureate.{{format}}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[QueryStringParams]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;id: {{ NOBEL_WINNER_ID }}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then to run this file:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;hurl --variable NOBEL_WINNER_ID=5 --variable format=json sess.hurl | jq&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The result is the JSON object representing Pierre Curie.&lt;/p&gt;
&lt;h1 id=&quot;conclusion&quot;&gt;Conclusion&lt;a class=&quot;zola-anchor&quot; href=&quot;#conclusion&quot; aria-label=&quot;Anchor link for: conclusion&quot;&gt;🔗&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;Hurl can be used for information sharing within teams around complex API workflows.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Assert on local instance of a mocked class - Python</title>
        <published>2021-01-27T00:00:00+00:00</published>
        <updated>2021-01-27T00:00:00+00:00</updated>
        
        <author>
          <name>Boyd Johnson</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://boydjohnson.dev/blog/mock-instance-of-mock-class/"/>
        <id>https://boydjohnson.dev/blog/mock-instance-of-mock-class/</id>
        
        <content type="html" xml:base="https://boydjohnson.dev/blog/mock-instance-of-mock-class/">&lt;p&gt;I recently ran into the situation where I wanted to test something that was similar to this function:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://boydjohnson.dev/blog/mock-instance-of-mock-class/mod1.png&quot; alt=&quot;Python function with requests.Session usage&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Now, on the internet you can read plenty of text that says “you can’t mock a local variable”. While technically correct, this doesn’t help a person who is trying to make assertions about the local instance of a mocked class.&lt;/p&gt;
&lt;p&gt;Here is how to get the local instance of the mocked class: &lt;code&gt;mock_session.return_value&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://boydjohnson.dev/blog/mock-instance-of-mock-class/mod1_test.png&quot; alt=&quot;Python test of function with requests.Session usage&quot; /&gt;&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Pandas GroupBy and Top by Group</title>
        <published>2021-01-24T00:00:00+00:00</published>
        <updated>2021-01-24T00:00:00+00:00</updated>
        
        <author>
          <name>Boyd Johnson</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://boydjohnson.dev/blog/top-of-group-by-different-metrics/"/>
        <id>https://boydjohnson.dev/blog/top-of-group-by-different-metrics/</id>
        
        <content type="html" xml:base="https://boydjohnson.dev/blog/top-of-group-by-different-metrics/">&lt;p&gt;So, often working on a data project you will have to report the top items in a group, based on some value. This could be states (group), cities (item), and poverty rate (value). This could be applications (group), metrics (item), and count of occurrence (value). The point is we want to find items in a group that are interesting based on a value.&lt;/p&gt;
&lt;h2 id=&quot;the-setup&quot;&gt;The setup&lt;a class=&quot;zola-anchor&quot; href=&quot;#the-setup&quot; aria-label=&quot;Anchor link for: the-setup&quot;&gt;🔗&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;I will use a random dataframe with 3 columns: category, item, and value. Some of the problem domains fit the random data frame, and for some you will have to imagine that the items are completely different names. So imagine that foo_baz is a stand-in for any value.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://boydjohnson.dev/blog/top-of-group-by-different-metrics/creation.png&quot; alt=&quot;Random Dataframe Creation&quot; /&gt;&lt;/p&gt;
&lt;p&gt;df&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://boydjohnson.dev/blog/top-of-group-by-different-metrics/creation-output.png&quot; alt=&quot;Random Dataframe Display&quot; /&gt;&lt;/p&gt;
&lt;h2 id=&quot;a-simple-top-n-by-group&quot;&gt;A simple Top N by group&lt;a class=&quot;zola-anchor&quot; href=&quot;#a-simple-top-n-by-group&quot; aria-label=&quot;Anchor link for: a-simple-top-n-by-group&quot;&gt;🔗&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Suppose you want to show someone the Top 1 or Top 10 items by group. Here is what we do: sort the values in a descending order, groupby, and then take the head(N).&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://boydjohnson.dev/blog/top-of-group-by-different-metrics/top_in_category.png&quot; alt=&quot;Top N by group&quot; /&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://boydjohnson.dev/blog/top-of-group-by-different-metrics/top_in_category-output.png&quot; alt=&quot;Top N by group output&quot; /&gt;&lt;/p&gt;
&lt;h2 id=&quot;top-as-a-percentage-of-the-group-s-value&quot;&gt;Top as a percentage of the group’s value&lt;a class=&quot;zola-anchor&quot; href=&quot;#top-as-a-percentage-of-the-group-s-value&quot; aria-label=&quot;Anchor link for: top-as-a-percentage-of-the-group-s-value&quot;&gt;🔗&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Suppose the dataset is count (value) of votes for a mayoral candidate (item) by city (group). We might be interested in the count of votes if it is above a certain percentage of the total votes cast in that city. We first have to calculate the total votes cast in the city.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://boydjohnson.dev/blog/top-of-group-by-different-metrics/category.png&quot; alt=&quot;Groupby and Sum in Pandas&quot; /&gt;&lt;/p&gt;
&lt;p&gt;df&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://boydjohnson.dev/blog/top-of-group-by-different-metrics/category-output.png&quot; alt=&quot;Groupby and Sum in Pandas output&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Then we use this grouped-by sum to compute the pct of the item’s value in each group. So that is which candidates got over 2.5% of the vote in their city.&lt;/p&gt;
&lt;p&gt;We use apply on the dataframe and use axis=1 to apply over the columns, for each row.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://boydjohnson.dev/blog/top-of-group-by-different-metrics/above_pct_of_category.png&quot; alt=&quot;Above 2.5% of the group’s total output&quot; /&gt;&lt;/p&gt;
&lt;p&gt;df&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://boydjohnson.dev/blog/top-of-group-by-different-metrics/above_pct_of_category-output.png&quot; alt=&quot;Above 2.5% of the group’s total output&quot; /&gt;&lt;/p&gt;
&lt;h2 id=&quot;top-ntile-for-the-group&quot;&gt;Top Ntile for the group&lt;a class=&quot;zola-anchor&quot; href=&quot;#top-ntile-for-the-group&quot; aria-label=&quot;Anchor link for: top-ntile-for-the-group&quot;&gt;🔗&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Suppose the dataset is counts (value) of people living in poverty by county (item) and by state (group). We might want to see the # of counties that are in the top 10% of counties in that state, for count of people living in poverty.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://boydjohnson.dev/blog/top-of-group-by-different-metrics/above_quantile_of_category.png&quot; alt=&quot;Top 10% ntile by group&quot; /&gt;&lt;/p&gt;
&lt;p&gt;The important part is the lambda with the quantile function and the apply with &amp;gt; and choosing the right ntile for each group.&lt;/p&gt;
&lt;p&gt;df&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://boydjohnson.dev/blog/top-of-group-by-different-metrics/above_quantile_of_category-output.png&quot; alt=&quot;Top 10% ntile by group&quot; /&gt;&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;a class=&quot;zola-anchor&quot; href=&quot;#conclusion&quot; aria-label=&quot;Anchor link for: conclusion&quot;&gt;🔗&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;We just went through three ways to give the top items in a group based on a value. This applies to many situations and is generalized in three techniques here: Top N, Top above percentage, and top above an ntile.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>3.3B Taxi Trips and the Unreasonable Effectiveness of Duckdb and Parquet</title>
        <published>2021-01-21T00:00:00+00:00</published>
        <updated>2021-01-21T00:00:00+00:00</updated>
        
        <author>
          <name>Boyd Johnson</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://boydjohnson.dev/blog/unreasonable-effectiveness-duckdb/"/>
        <id>https://boydjohnson.dev/blog/unreasonable-effectiveness-duckdb/</id>
        
        <content type="html" xml:base="https://boydjohnson.dev/blog/unreasonable-effectiveness-duckdb/">&lt;h1 id=&quot;in-an-instant&quot;&gt;In an instant&lt;a class=&quot;zola-anchor&quot; href=&quot;#in-an-instant&quot; aria-label=&quot;Anchor link for: in-an-instant&quot;&gt;🔗&lt;/a&gt;&lt;/h1&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;num_rows&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;3367004170&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;I was going to write a blog post comparing &lt;a rel=&quot;external&quot; href=&quot;https://github.com/apache/arrow-ballista/&quot;&gt;arrow-ballista&lt;/a&gt; and &lt;a rel=&quot;external&quot; href=&quot;https://duckdb.org/&quot;&gt;duckdb&lt;/a&gt; on a large task, but duckdb was so fast it was alarming. So I decided to write down some of my observations of using duckdb on the &lt;a rel=&quot;external&quot; href=&quot;https://aws.amazon.com/marketplace/pp/prodview-okyonroqg5b2u#resources&quot;&gt;NYC Taxi Dataset&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&quot;context&quot;&gt;Context&lt;a class=&quot;zola-anchor&quot; href=&quot;#context&quot; aria-label=&quot;Anchor link for: context&quot;&gt;🔗&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;I used Duckdb v0.9.2 on a 96-core Dual Xeon workstation with ~5TB of disk space. I had good results on my 16-core workstation, too, but markedly faster results on the big workstation. There were 439 parquet files as of January 2024. I put the parquet files in one directory and used &lt;code&gt;read_parquet(&#39;taxi/*.parquet&#39;, filename=true, union_by_name=true)&lt;/code&gt;, allowing duckdb to read the files in parallel. I used the duckdb cli, and gave it a database on disk, &lt;code&gt;duckdb taxidb&lt;/code&gt;, but read from the parquet files throughout.&lt;/p&gt;
&lt;h1 id=&quot;interesting-results&quot;&gt;Interesting Results&lt;a class=&quot;zola-anchor&quot; href=&quot;#interesting-results&quot; aria-label=&quot;Anchor link for: interesting-results&quot;&gt;🔗&lt;/a&gt;&lt;/h1&gt;
&lt;h2 id=&quot;messy-data-example&quot;&gt;Messy data example&lt;a class=&quot;zola-anchor&quot; href=&quot;#messy-data-example&quot; aria-label=&quot;Anchor link for: messy-data-example&quot;&gt;🔗&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;This is the record with &lt;code&gt;max(total_amount)&lt;/code&gt;. It seems like an outlier, where it should be 15.0 but there was an accidental 10000000 added in.&lt;/p&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;fare_amount&lt;/th&gt;&lt;th&gt;extra&lt;/th&gt;&lt;th&gt;mta_tax&lt;/th&gt;&lt;th&gt;tip_amount&lt;/th&gt;&lt;th&gt;tolls_amount&lt;/th&gt;&lt;th&gt;total_amount&lt;/th&gt;&lt;th&gt;filename&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;14.0&lt;/td&gt;&lt;td&gt;0.5&lt;/td&gt;&lt;td&gt;0.5&lt;/td&gt;&lt;td&gt;0.0&lt;/td&gt;&lt;td&gt;0.0&lt;/td&gt;&lt;td&gt;10000015.0&lt;/td&gt;&lt;td&gt;taxi/yellow_tripdata_2014-08.parquet&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;trips-by-year&quot;&gt;Trips by year&lt;a class=&quot;zola-anchor&quot; href=&quot;#trips-by-year&quot; aria-label=&quot;Anchor link for: trips-by-year&quot;&gt;🔗&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Peak trip count dropped in 2020 with Covid-19 but has increased some since.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://boydjohnson.dev/blog/unreasonable-effectiveness-duckdb/sql1.png&quot; alt=&quot;SQL for trip count by year calculated from filename&quot; /&gt;&lt;/p&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;file_year&lt;/th&gt;&lt;th&gt;trip_count&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;2009&lt;/td&gt;&lt;td&gt;170896055&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;2010&lt;/td&gt;&lt;td&gt;111529714&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;2011&lt;/td&gt;&lt;td&gt;176887259&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;2012&lt;/td&gt;&lt;td&gt;171359007&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;2013&lt;/td&gt;&lt;td&gt;171816340&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;2014&lt;/td&gt;&lt;td&gt;181284588&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;2015&lt;/td&gt;&lt;td&gt;228661528&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;2016&lt;/td&gt;&lt;td&gt;279631429&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;2017&lt;/td&gt;&lt;td&gt;317546944&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;2018&lt;/td&gt;&lt;td&gt;372645858&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;2019&lt;/td&gt;&lt;td&gt;368790969&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;2020&lt;/td&gt;&lt;td&gt;184638604&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;2021&lt;/td&gt;&lt;td&gt;221374980&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;2022&lt;/td&gt;&lt;td&gt;267424247&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;2023&lt;/td&gt;&lt;td&gt;142516648&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;trips-by-hour-of-day&quot;&gt;Trips by hour of day&lt;a class=&quot;zola-anchor&quot; href=&quot;#trips-by-hour-of-day&quot; aria-label=&quot;Anchor link for: trips-by-hour-of-day&quot;&gt;🔗&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The # of trips varies by hour of day, but many trips didn’t have this information.&lt;/p&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;hour_of_pickup&lt;/th&gt;&lt;th&gt;count_star()&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;50136451&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;36511245&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;26713761&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;3&lt;/td&gt;&lt;td&gt;19394015&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;4&lt;/td&gt;&lt;td&gt;14414029&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;5&lt;/td&gt;&lt;td&gt;13478009&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;6&lt;/td&gt;&lt;td&gt;29142824&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;7&lt;/td&gt;&lt;td&gt;49853450&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;8&lt;/td&gt;&lt;td&gt;61836553&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;9&lt;/td&gt;&lt;td&gt;63869199&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;10&lt;/td&gt;&lt;td&gt;63211677&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;11&lt;/td&gt;&lt;td&gt;65778781&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;12&lt;/td&gt;&lt;td&gt;69481912&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;13&lt;/td&gt;&lt;td&gt;69504611&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;14&lt;/td&gt;&lt;td&gt;72311337&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;15&lt;/td&gt;&lt;td&gt;69914744&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;16&lt;/td&gt;&lt;td&gt;62170489&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;17&lt;/td&gt;&lt;td&gt;72704518&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;18&lt;/td&gt;&lt;td&gt;85003150&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;19&lt;/td&gt;&lt;td&gt;85608774&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;20&lt;/td&gt;&lt;td&gt;79561917&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;21&lt;/td&gt;&lt;td&gt;78185955&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;22&lt;/td&gt;&lt;td&gt;74891862&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;23&lt;/td&gt;&lt;td&gt;64675234&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;1988649673&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;h1 id=&quot;thoughts-on-duckdb&quot;&gt;Thoughts on duckdb&lt;a class=&quot;zola-anchor&quot; href=&quot;#thoughts-on-duckdb&quot; aria-label=&quot;Anchor link for: thoughts-on-duckdb&quot;&gt;🔗&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;In the past with a dataset with long strings coming from a ndjson file, I had segfaults with Duckdb. That was several minor versions of Duckdb ago. I was really impressed with the stability while throwing a huge amount of parquet at it. The difference in time it took for a query between the 16-core workstation and the 96-core workstation is exactly as you want it to be: duckdb can use all of the compute resources that you can throw at it.&lt;/p&gt;
&lt;h2 id=&quot;locality-of-compute-memory-and-disk&quot;&gt;Locality of compute, memory, and disk&lt;a class=&quot;zola-anchor&quot; href=&quot;#locality-of-compute-memory-and-disk&quot; aria-label=&quot;Anchor link for: locality-of-compute-memory-and-disk&quot;&gt;🔗&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Instead of throwing many (relatively)small computers at a problem, duckdb is a software that can use the resources of one big computer. The duckdb python package can be used for writing processes that run automatically, but I love the duckdb cli for exploring a dataset.&lt;/p&gt;
&lt;h1 id=&quot;go-forth-and-crunch-data&quot;&gt;Go Forth and Crunch data&lt;a class=&quot;zola-anchor&quot; href=&quot;#go-forth-and-crunch-data&quot; aria-label=&quot;Anchor link for: go-forth-and-crunch-data&quot;&gt;🔗&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;Try out duckdb with the knowledge that you can throw huge amounts of data at it and it will perform.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Tools to profile Rust CLI applications</title>
        <published>2021-01-05T00:00:00+00:00</published>
        <updated>2021-01-05T00:00:00+00:00</updated>
        
        <author>
          <name>Boyd Johnson</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://boydjohnson.dev/blog/profile-rust-cli-applications/"/>
        <id>https://boydjohnson.dev/blog/profile-rust-cli-applications/</id>
        
        <content type="html" xml:base="https://boydjohnson.dev/blog/profile-rust-cli-applications/">&lt;p&gt;This blog post shows how to profile running time with Hyperfine, memory usage with Valgrind and Massif, application function calls with Valgrind and Callgrind, and system calls with strace. I will show examples of how to use these tools on a command line application written in Rust on Ubuntu Linux even though &lt;strong&gt;all&lt;/strong&gt; of these tools work on many types of application binaries.&lt;/p&gt;
&lt;h2 id=&quot;table-of-contents&quot;&gt;Table of Contents&lt;a class=&quot;zola-anchor&quot; href=&quot;#table-of-contents&quot; aria-label=&quot;Anchor link for: table-of-contents&quot;&gt;🔗&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href=&quot;https://boydjohnson.dev/blog/profile-rust-cli-applications/#profiling-running-time-with-hyperfine&quot;&gt;Profiling Running Time with Hyperfine&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href=&quot;https://boydjohnson.dev/blog/profile-rust-cli-applications/#profiling-application-function-calls-with-valgrind-and-callgrind&quot;&gt;Profiling Application Function Calls with Valgrind and Callgrind&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href=&quot;https://boydjohnson.dev/blog/profile-rust-cli-applications/#profiling-memory-usage-with-valgrind-and-massif&quot;&gt;Profiling Memory Usage with Valgrind and Massif&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href=&quot;https://boydjohnson.dev/blog/profile-rust-cli-applications/#profiling-system-calls-with-strace&quot;&gt;Profiling System Calls with strace&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Info&lt;/strong&gt;: If you want to follow along with the examples, use the following:&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;To have a Rust CLI tool to profile, I’m going to use &lt;a href=&quot;https://boydjohnson.dev/blog/tool-for-processing-large-json/&quot;&gt;&lt;code&gt;ndjson&lt;/code&gt; the JSON/NDJSON processing tool&lt;/a&gt;. Some of the commands need to be run on a debug build, so we should make debug and release builds.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;git&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; clone&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; https://github.com/boydjohnson/ndjson-spatial&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;cd&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; ndjson-spatial&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;cargo&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; build&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;cargo&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; build&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;-release&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To have some JSON files to use the tool on, I am using a JSON file of &lt;a rel=&quot;external&quot; href=&quot;https://www.govtrack.us/api/v2/role?current=true&amp;amp;role_type=senator&quot;&gt;current US Senators&lt;/a&gt; and the &lt;a rel=&quot;external&quot; href=&quot;https://catalog.data.gov/dataset/consumer-complaint-database&quot;&gt;US CFPB Complaints dataset&lt;/a&gt; on &lt;a rel=&quot;external&quot; href=&quot;https://data.gov&quot;&gt;data.gov&lt;/a&gt;.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;wget&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;O&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; senators.json&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; https://www.govtrack.us/api/v2/role?current=&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;\&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;role_type=senator&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;wget&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; https://files.consumerfinance.gov/ccdb/complaints.json.zip&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;unzip&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; complaints.json.zip&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;ls&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;lh&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; complaints.json&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; senators.json&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;complaints.json is 1.8 GiB and senators.json is 139 KiB.&lt;/p&gt;
&lt;h1 id=&quot;profiling-running-time-with-hyperfine&quot;&gt;Profiling Running Time with Hyperfine&lt;a class=&quot;zola-anchor&quot; href=&quot;#profiling-running-time-with-hyperfine&quot; aria-label=&quot;Anchor link for: profiling-running-time-with-hyperfine&quot;&gt;🔗&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;&lt;a rel=&quot;external&quot; href=&quot;https://github.com/sharkdp/hyperfine&quot;&gt;Hyperfine&lt;/a&gt; (from its README.md) “is a command-line benchmarking tool”. I’m going to install it with &lt;code&gt;cargo&lt;/code&gt;, but there are instructions for many operating systems and package managers (&lt;a rel=&quot;external&quot; href=&quot;https://github.com/sharkdp/hyperfine#installation&quot;&gt;hyperfine installation&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;To install with &lt;code&gt;cargo&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;cargo&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; install&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; hyperfine&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Hyperfine can run multiple commands at once and it will run a benchmarking session on each command.&lt;/p&gt;
&lt;p&gt;With one command this looks like:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;hyperfine&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;-warmup&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; 5&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;cat senators.json | ./target/release/ndjson from-json d.objects &amp;gt;&amp;gt; /dev/null&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;&amp;#39;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;Benchmark&lt;/span&gt;&lt;span class=&quot;z-l-4 z-l-i z-d-3 z-d-i&quot;&gt; #&lt;/span&gt;&lt;span class=&quot;z-l-4 z-l-i z-d-3 z-d-i&quot;&gt;1: cat senators.json | ndjson from-json d.objects &amp;gt;&amp;gt; /dev/null&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;  Time&lt;/span&gt;&lt;span&gt; (mean&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; ±&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; σ&lt;/span&gt;&lt;span&gt;):       6.9 ms ±   1.0 ms    &lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;User: &lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;6.8&lt;/span&gt;&lt;span&gt; ms, System: &lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;1.6&lt;/span&gt;&lt;span&gt; ms&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;  Range&lt;/span&gt;&lt;span&gt; (min&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; …&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; max&lt;/span&gt;&lt;span&gt;):     4.4 ms …   9.0 ms    333 runs&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;  Warning:&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; Command&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; took&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; less&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; than&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; 5&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; ms&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; to&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; complete.&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; Results&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; might&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; be&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; inaccurate.&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Or on the larger file:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;hyperfine&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;-warmup&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; 5&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;cat complaints.json | ./target/release/ndjson from-json d &amp;gt;&amp;gt; /dev/null&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;&amp;#39;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;Benchmark&lt;/span&gt;&lt;span class=&quot;z-l-4 z-l-i z-d-3 z-d-i&quot;&gt; #&lt;/span&gt;&lt;span class=&quot;z-l-4 z-l-i z-d-3 z-d-i&quot;&gt;1: cat complaints.json | ndjson from-json d &amp;gt;&amp;gt; /dev/null&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;  Time&lt;/span&gt;&lt;span&gt; (mean&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; ±&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; σ&lt;/span&gt;&lt;span&gt;):     25.936 s ±  0.205 s    &lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;User: &lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;24.937&lt;/span&gt;&lt;span&gt; s, System: &lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;1.877&lt;/span&gt;&lt;span&gt; s&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;  Range&lt;/span&gt;&lt;span&gt; (min&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; …&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; max&lt;/span&gt;&lt;span&gt;):   25.688 s … 26.433 s    10 runs&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;What seems notable is that the system time only increased by .277 seconds. You may be wondering how many calls to the &lt;code&gt;read&lt;/code&gt; &lt;a rel=&quot;external&quot; href=&quot;https://en.wikipedia.org/wiki/System_call#Categories_of_system_calls&quot;&gt;Linux system call&lt;/a&gt; there were. Wait until the &lt;code&gt;strace&lt;/code&gt; section, as &lt;code&gt;strace&lt;/code&gt; will tell us that information.&lt;/p&gt;
&lt;p&gt;Calculating how many MB/s the &lt;code&gt;ndjson from-json&lt;/code&gt; command can process can be done by 1892823525 (bytes of the complaints.json file) / 25.936 s (average from Hyperfine) / 1_000_000 = 72.89 MB/s.&lt;/p&gt;
&lt;p&gt;Running a comparison between two commands looks like this:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;WARNING&lt;/strong&gt;: the following command takes 1.5+ hours&lt;/p&gt;
&lt;/blockquote&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;hyperfine&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;-warmup&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; 5&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;cat complaints.json | ./target/release/ndjson from-json d &amp;gt;&amp;gt; /dev/null&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;cat complaints.json | jq -cn --stream &amp;quot;fromstream(1|truncate_stream(inputs))&amp;quot; &amp;gt;&amp;gt; /dev/null&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;&amp;#39;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;Benchmark&lt;/span&gt;&lt;span class=&quot;z-l-4 z-l-i z-d-3 z-d-i&quot;&gt; #&lt;/span&gt;&lt;span class=&quot;z-l-4 z-l-i z-d-3 z-d-i&quot;&gt;1: cat complaints.json | ./target/release/ndjson from-json d &amp;gt;&amp;gt; /dev/null&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;  Time&lt;/span&gt;&lt;span&gt; (mean&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; ±&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; σ&lt;/span&gt;&lt;span&gt;):     26.013 s ±  0.172 s    &lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;User: &lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;25.005&lt;/span&gt;&lt;span&gt; s, System: &lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;1.887&lt;/span&gt;&lt;span&gt; s&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;  Range&lt;/span&gt;&lt;span&gt; (min&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; …&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; max&lt;/span&gt;&lt;span&gt;):   25.704 s … 26.242 s    10 runs&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;Benchmark&lt;/span&gt;&lt;span class=&quot;z-l-4 z-l-i z-d-3 z-d-i&quot;&gt; #&lt;/span&gt;&lt;span class=&quot;z-l-4 z-l-i z-d-3 z-d-i&quot;&gt;2: cat complaints.json | jq -cn --stream &amp;quot;fromstream(1|truncate_stream(inputs))&amp;quot; &amp;gt;&amp;gt; /dev/null&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;  Time&lt;/span&gt;&lt;span&gt; (mean&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; ±&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; σ&lt;/span&gt;&lt;span&gt;):     437.069 s ±  1.034 s    &lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;User: &lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;436.352&lt;/span&gt;&lt;span&gt; s, System: &lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;13.421&lt;/span&gt;&lt;span&gt; s&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;  Range&lt;/span&gt;&lt;span&gt; (min&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; …&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; max&lt;/span&gt;&lt;span&gt;):   435.606 s … 439.567 s    10 runs&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;Summary&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;  &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;cat complaints.json | ./target/release/ndjson from-json d &amp;gt;&amp;gt; /dev/null&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; ran&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;   16.80&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; ±&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; 0.12&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; times&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; faster&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; than&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;cat complaints.json | jq -cn --stream &amp;quot;fromstream(1|truncate_stream(inputs))&amp;quot; &amp;gt;&amp;gt; /dev/null&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;&amp;#39;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;figure class=&quot;image&quot;&gt;
&lt;p&gt;&lt;img src=&quot;https://boydjohnson.dev/blog/profile-rust-cli-applications/hyperfine-ndjson.gif&quot; alt=&quot;hyperfine command run on ndjson command&quot; /&gt;&lt;/p&gt;
&lt;/figure&gt;
&lt;h3 id=&quot;summary&quot;&gt;Summary&lt;a class=&quot;zola-anchor&quot; href=&quot;#summary&quot; aria-label=&quot;Anchor link for: summary&quot;&gt;🔗&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Hyperfine can give us an average running time that is averaged over many runs so that the average is more accurate. The running time is broken down between user time and system time. Running our command on different sizes of input produced different results in user time but barely changed system time.&lt;/p&gt;
&lt;h1 id=&quot;profiling-application-function-calls-with-valgrind-and-callgrind&quot;&gt;Profiling Application Function Calls with Valgrind and Callgrind&lt;a class=&quot;zola-anchor&quot; href=&quot;#profiling-application-function-calls-with-valgrind-and-callgrind&quot; aria-label=&quot;Anchor link for: profiling-application-function-calls-with-valgrind-and-callgrind&quot;&gt;🔗&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;&lt;a rel=&quot;external&quot; href=&quot;https://valgrind.org/&quot;&gt;Valgrind&lt;/a&gt; “is an instrumentation framework for building dynamic analysis tools”. In my understanding, valgrind creates an environment where everything the program does goes through valgrind and can then be counted.&lt;/p&gt;
&lt;p&gt;To install on Ubuntu:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;sudo&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; apt&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; install&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; valgrind&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Valgrind can pass standard input to the command so it can be run like:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;cat&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; senators.json&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt; |&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt; valgrind&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; ./target/release/ndjson&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; from-json&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; d.objects&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt; &amp;gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; /dev/null&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; HEAP&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; SUMMARY:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;     in&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; use&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; at&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; exit:&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; 61&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; bytes&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; in&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; 2&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; blocks&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;   total&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; heap&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; usage:&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; 19,144&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; allocs,&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; 19,142&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; frees,&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; 6,452,908&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; bytes&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; allocated&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;=&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; LEAK&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; SUMMARY:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;    definitely&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; lost:&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; bytes&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; in&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; blocks&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;    indirectly&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; lost:&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; bytes&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; in&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; blocks&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;      possibly&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; lost:&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; bytes&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; in&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; blocks&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;    still&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; reachable:&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; 61&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; bytes&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; in&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; 2&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; blocks&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;         suppressed:&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; bytes&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; in&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; blocks&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; Rerun&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; with&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;-leak-check=full&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; to&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; see&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; details&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; of&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; leaked&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; memory&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The command had more allocs then frees, so it has a benign memory leak.&lt;/p&gt;
&lt;p&gt;Callgrind is the tool that we will use in this section and it is installed with the valgrind package. Callgrind gives the order and timing of function calls within your program. Callgrind produces a file that can be viewed with a viewer, kcachegrind.&lt;/p&gt;
&lt;p&gt;For the callgrind information to be understandable we need to run it on a debug build of a Rust library.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;cat&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; senators.json&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt; |&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt; valgrind&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;-tool=callgrind&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; ./target/debug/ndjson&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; from-json&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; d.objects&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt; &amp;gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; /dev/null&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This outputs a file &lt;code&gt;callgrind.out.&amp;lt;number&amp;gt;&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;To view the results with a GUI, we need to install kcachegrind.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;sudo&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; apt&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; install&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; kcachegrind&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This next command will bring up a GUI.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;kcachegrind&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; callgrind.out.&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;numbe&lt;/span&gt;&lt;span&gt;r&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For each function call, callgrind output has the percentage of application time that is spent in that function &lt;strong&gt;or&lt;/strong&gt; functions called by the named function, as well as percentage of time spent in that function not including called functions, and also the total number of times that function is called.&lt;/p&gt;
&lt;p&gt;For the image below, &lt;code&gt;&amp;lt;yajlish::parser::Wrapper as core::iter::traits::Iterator&amp;gt;::next&lt;/code&gt; (the highlighted function) takes 35.12% of the total time when its callees (the functions it calls) are included. The named function takes 3.68% of the time when its callees are not included. The named function is called 141,335 times.&lt;/p&gt;
&lt;div class=&quot;columns&quot;&gt;
&lt;div class=&quot;column is-8&quot;&gt;
&lt;figure class=&quot;image&quot;&gt;
&lt;p&gt;&lt;img src=&quot;https://boydjohnson.dev/blog/profile-rust-cli-applications/callgrind-calls-percentage-of-total-time.png&quot; alt=&quot;kcachegrind view of callgrind output&quot; /&gt;&lt;/p&gt;
&lt;/figure&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The image below is in a screen called &lt;code&gt;callgraph&lt;/code&gt;. It shows the function call times, number of calls, and which function calls which.&lt;/p&gt;
&lt;div class=&quot;columns&quot;&gt;
&lt;div class=&quot;column&quot;&gt;
&lt;figure class=&quot;image&quot;&gt;
&lt;p&gt;&lt;img src=&quot;https://boydjohnson.dev/blog/profile-rust-cli-applications/callgraph-kcachegrind.png&quot; alt=&quot;kcachegrind view of callgraph&quot; /&gt;&lt;/p&gt;
&lt;/figure&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;h3 id=&quot;summary-1&quot;&gt;Summary&lt;a class=&quot;zola-anchor&quot; href=&quot;#summary-1&quot; aria-label=&quot;Anchor link for: summary-1&quot;&gt;🔗&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Valgrind with callgrind can give information about which parts of the call graph are taking the most time and how many times functions are being called.&lt;/p&gt;
&lt;h1 id=&quot;profiling-memory-usage-with-valgrind-and-massif&quot;&gt;Profiling Memory Usage with Valgrind and Massif&lt;a class=&quot;zola-anchor&quot; href=&quot;#profiling-memory-usage-with-valgrind-and-massif&quot; aria-label=&quot;Anchor link for: profiling-memory-usage-with-valgrind-and-massif&quot;&gt;🔗&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;Valgrind with tool &lt;code&gt;massif&lt;/code&gt; will show what memory has been allocated at different times during the running of the application.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;cat&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; senators.json&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt; |&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt; valgrind&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;-tool=massif&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; ./target/debug/ndjson&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; from-json&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; d.objects&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt; &amp;gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; /dev/null&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This produces a file called &lt;code&gt;massif.out.&amp;lt;number&amp;gt;&lt;/code&gt;. To view it with a GUI we need &lt;code&gt;massif-visualizer&lt;/code&gt;.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;sudo&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; apt&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; install&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; massif-visualizer&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The next command opens a GUI.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;massif-visualizer&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; massif.out.&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;numbe&lt;/span&gt;&lt;span&gt;r&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;From the GUI, the following image shows that most of the memory allocated was allocated in the function &lt;code&gt;core::alloc::Vec::with_capacity&lt;/code&gt;. The heap allocation is pretty constant throughout the running time of the application.&lt;/p&gt;
&lt;div class=&quot;columns&quot;&gt;
&lt;div class=&quot;column&quot;&gt;
&lt;figure class=&quot;image&quot;&gt;
&lt;p&gt;&lt;img src=&quot;https://boydjohnson.dev/blog/profile-rust-cli-applications/massif-visualizer.png&quot; alt=&quot;massif visualizer showing heap allocations&quot; /&gt;&lt;/p&gt;
&lt;/figure&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;h1 id=&quot;profiling-system-calls-with-strace&quot;&gt;Profiling System Calls with strace&lt;a class=&quot;zola-anchor&quot; href=&quot;#profiling-system-calls-with-strace&quot; aria-label=&quot;Anchor link for: profiling-system-calls-with-strace&quot;&gt;🔗&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;&lt;code&gt;strace&lt;/code&gt; can show the types, number, and timing of system calls. It can even inject faults into system calls so that you can see how your application responds to an error, but I don’t have examples of this usage.&lt;/p&gt;
&lt;p&gt;Like valgrind, strace hands standard input to the command it is running.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;cat&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; senators.json&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt; |&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt; strace&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; ./target/release/ndjson&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; from-json&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; d.objects&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt; &amp;gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; /dev/null&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; time&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;     seconds&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;  usecs/call&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;     calls&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;    errors&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; syscall&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;------&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;----------&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;----------&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;--------&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;--------&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;---------------&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt; 23.25&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;    0.000286&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;          19&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;        15&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;           read&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt; 19.19&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;    0.000236&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;           9&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;        27&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;           mmap&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt; 16.50&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;    0.000203&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;          11&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;        19&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;           mprotect&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;  9.59&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;    0.000118&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;          12&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;        10&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;           openat&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;  6.59&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;    0.000081&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;           8&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;        10&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;        10&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; access&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;  4.55&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;    0.000056&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;           6&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;        10&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;           close&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;  4.47&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;    0.000055&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;           6&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;        10&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;           fstat&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;  4.47&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;    0.000055&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;          14&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;         4&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;           munmap&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;  3.33&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;    0.000041&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;           6&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;         7&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;           rt_sigaction&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;  1.54&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;    0.000019&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;           6&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;         3&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;           brk&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;  0.89&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;    0.000011&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;           4&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;         3&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;           sigaltstack&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;  0.89&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;    0.000011&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;           6&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;         2&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;           futex&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;  0.89&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;    0.000011&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;           6&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;         2&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;           prlimit64&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;  0.65&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;    0.000008&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;           8&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;         1&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;           execve&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;  0.57&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;    0.000007&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;           7&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;         1&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;           getrandom&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;  0.49&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;    0.000006&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;           6&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;         1&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;           write&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;  0.49&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;    0.000006&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;           6&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;         1&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;           sched_getaffinity&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;  0.41&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;    0.000005&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;           5&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;         1&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;           rt_sigprocmask&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;  0.41&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;    0.000005&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;           5&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;         1&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;           arch_prctl&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;  0.41&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;    0.000005&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;           5&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;         1&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;           set_tid_address&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;  0.41&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;    0.000005&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;           5&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;         1&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;           set_robust_list&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;------&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;----------&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;----------&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;--------&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;--------&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;---------------&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;100.00&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;    0.001230&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;                   130&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;        10&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; total&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;-c&lt;/code&gt; flag says we want the count.&lt;/p&gt;
&lt;h3 id=&quot;summary-2&quot;&gt;Summary&lt;a class=&quot;zola-anchor&quot; href=&quot;#summary-2&quot; aria-label=&quot;Anchor link for: summary-2&quot;&gt;🔗&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;strace&lt;/code&gt; can give the count and timing of each syscall.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>A Streaming Tool for Processing Large JSON/NDJSON Files</title>
        <published>2021-01-02T00:00:00+00:00</published>
        <updated>2021-01-02T00:00:00+00:00</updated>
        
        <author>
          <name>Boyd Johnson</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://boydjohnson.dev/blog/tool-for-processing-large-json/"/>
        <id>https://boydjohnson.dev/blog/tool-for-processing-large-json/</id>
        
        <content type="html" xml:base="https://boydjohnson.dev/blog/tool-for-processing-large-json/">&lt;p&gt;Over the last year or so I have been off-and-on working on two tools for processing NDJSON and turning JSON into NDJSON. I am &lt;a rel=&quot;external&quot; href=&quot;https://github.com/boydjohnson/ndjson-spatial/releases&quot;&gt;releasing&lt;/a&gt; one of them today, with &lt;a rel=&quot;external&quot; href=&quot;https://github.com/boydjohnson/ndjson-spatial/releases/download/ndjson-v0.1.0/ndjson_0.1.0_amd64.deb&quot;&gt;a deb package&lt;/a&gt;, and cross-compiled &lt;a rel=&quot;external&quot; href=&quot;https://github.com/boydjohnson/ndjson-spatial/releases/download/ndjson-v0.1.0/ndjson-x86_64-apple-darwin&quot;&gt;macOS&lt;/a&gt; and &lt;a rel=&quot;external&quot; href=&quot;https://github.com/boydjohnson/ndjson-spatial/releases/download/ndjson-v0.1.0/ndjson-x86_64-pc-windows-gnu.exe&quot;&gt;Windows&lt;/a&gt; executables.&lt;/p&gt;
&lt;p&gt;This blog post will explain the use case of this tool (short answer: you want to turn JSON into NDJSON or you want to do simple processing on NDJSON), give examples of usage and compare it to other tools that do similar things.&lt;/p&gt;
&lt;h1 id=&quot;summary-of-use-case&quot;&gt;Summary of Use Case&lt;a class=&quot;zola-anchor&quot; href=&quot;#summary-of-use-case&quot; aria-label=&quot;Anchor link for: summary-of-use-case&quot;&gt;🔗&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;There are very large JSON files out there. One of them happens to be &lt;a rel=&quot;external&quot; href=&quot;https://catalog.data.gov/dataset/consumer-complaint-database&quot;&gt;the Consumer Financial Protection Bureau’s Complaint Dataset on data.gov&lt;/a&gt;. It is 1.7 GB unzipped. It is possible to load this into memory on most computers, but why should you have to.&lt;/p&gt;
&lt;p&gt;Maybe you are wondering how many total complaints there are. Maybe you are wondering the number of complaints per state.&lt;/p&gt;
&lt;p&gt;You may think to reach for &lt;a rel=&quot;external&quot; href=&quot;https://stedolan.github.io/jq/&quot;&gt;jq&lt;/a&gt;. The default for &lt;code&gt;jq&lt;/code&gt; is to load everything into memory, but it has a streaming mode.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;WARNING&lt;/strong&gt;: these commands will be really slow&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Default: takes 1m18s on my workstation and uses a bunch of memory&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;cat&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; complaints.json&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt; |&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt; jq&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;.[]&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt; |&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt; wc&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;l&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;1833581&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Streaming: takes 7m6s on my workstation and uses less memory:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;cat&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; complaints.json&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt; |&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt; jq&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;cn&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;-stream&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;fromstream(1|truncate_stream(inputs))&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt; |&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt; wc&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;l&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;1833581&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h1 id=&quot;introducing-ndjson-tool&quot;&gt;Introducing ndjson tool&lt;a class=&quot;zola-anchor&quot; href=&quot;#introducing-ndjson-tool&quot; aria-label=&quot;Anchor link for: introducing-ndjson-tool&quot;&gt;🔗&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;Number of complaints in the dataset&lt;/p&gt;
&lt;p&gt;takes 26s on my workstation&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;cat&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; complaints.json&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt; |&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt; ndjson&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; from-json&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; d&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt; |&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt; wc&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;l&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;1833581&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Number of complaints per state, 2 ways&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;1st way&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;takes 44s on my workstation&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;cat&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; complaints.json&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt; |&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt; ndjson&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; from-json&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; d&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt; |&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt; ndjson&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; pick-field&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; d.state&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt; |&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt; sort&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt; |&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt; uniq&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt; |&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt; sort&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;n&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;243520&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;CA&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;2nd way&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;takes 2m56s on my workstation&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;cat&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; complaints.json&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt; |&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt; ndjson&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; from-json&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; d&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt; |&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt; ndjson&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; agg&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;-group-by&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; d.state&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;-agg&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; count&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; d&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;_count&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;:243520,&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;CA&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;_count&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;:1553,&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;WY&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&quot;in-depth-on-the-json-selector-patterns&quot;&gt;In-depth on the JSON selector patterns&lt;a class=&quot;zola-anchor&quot; href=&quot;#in-depth-on-the-json-selector-patterns&quot; aria-label=&quot;Anchor link for: in-depth-on-the-json-selector-patterns&quot;&gt;🔗&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Every command takes a selector to target a part of the JSON/NDJON. These selectors work very much like &lt;a rel=&quot;external&quot; href=&quot;https://d3js.org/&quot;&gt;d3&lt;/a&gt; JSON selectors. Each selector starts with &lt;code&gt;d&lt;/code&gt;, to refer to the whole document (or for NDJSON this refers to 1 NDJSON element). Next is each JSON key or an index in a JSON array (d.key[5] for the 5th item in the array given by the key &lt;code&gt;key&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;&lt;code&gt;d.key[5]&lt;/code&gt; refers to &lt;code&gt;5&lt;/code&gt;.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;1&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;2&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;3&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;4&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;5&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Suppose we have the following JSON document, and we want to process it into NDJSON:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;foo&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;bar&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-3&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; 1&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;bar&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-3&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; 2&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Selector&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;d&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-3&quot;&gt;.&lt;/span&gt;&lt;span&gt;foo&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Keys can be combined&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;company_name&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;: &lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;Dunder Mifflin&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;employees&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-3&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;Dwight&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;age&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-3&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; 37&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Selector targeting &lt;code&gt;37&lt;/code&gt;&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;d&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-3&quot;&gt;.&lt;/span&gt;&lt;span&gt;employees&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;0&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-4 z-d-3&quot;&gt;.&lt;/span&gt;&lt;span&gt;age&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h3 id=&quot;ndjson-tool-commands&quot;&gt;ndjson tool commands&lt;a class=&quot;zola-anchor&quot; href=&quot;#ndjson-tool-commands&quot; aria-label=&quot;Anchor link for: ndjson-tool-commands&quot;&gt;🔗&lt;/a&gt;&lt;/h3&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;SUBCOMMANDS:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;    agg&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;           Aggregatation&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; commands&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; on&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; a&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; grouped-by&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; key&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;    filter&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;        returns&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; only&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; json&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; that&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; matches&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; filter&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; expression&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;    from-json&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;     Converts&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; json&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; to&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; ndjson&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;    join&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;          joins&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; json&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; file&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; to&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; ndjson&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; stream&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;    pick-field&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;    picks&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; a&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; field&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; from&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; all&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; of&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; the&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; ndjson&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; objects&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&quot;caveats&quot;&gt;Caveats&lt;a class=&quot;zola-anchor&quot; href=&quot;#caveats&quot; aria-label=&quot;Anchor link for: caveats&quot;&gt;🔗&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;This tool can’t do everything &lt;code&gt;jq&lt;/code&gt; can do and it never will. It is supposed to be simple and fast. Also the &lt;code&gt;agg&lt;/code&gt; subcommand takes a lot of memory if the &lt;code&gt;group-by&lt;/code&gt; field is not already sorted.&lt;/p&gt;
&lt;h2 id=&quot;github-repo&quot;&gt;Github repo&lt;a class=&quot;zola-anchor&quot; href=&quot;#github-repo&quot; aria-label=&quot;Anchor link for: github-repo&quot;&gt;🔗&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;a rel=&quot;external&quot; href=&quot;https://github.com/boydjohnson/ndjson-spatial&quot;&gt;ndjson-spatial&lt;/a&gt; is the project that this tool is a part of.&lt;/p&gt;
&lt;p&gt;&lt;a rel=&quot;external&quot; href=&quot;https://github.com/boydjohnson/ndjson-spatial/tree/master/ndjson&quot;&gt;ndjson tool README.md&lt;/a&gt;&lt;/p&gt;
&lt;h1 id=&quot;conclusion&quot;&gt;Conclusion&lt;a class=&quot;zola-anchor&quot; href=&quot;#conclusion&quot; aria-label=&quot;Anchor link for: conclusion&quot;&gt;🔗&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;For gigantic JSON files there is a new way to process them into NDJSON. This NDJSON can be filtered and processed in relatively simple ways to find basic answers about the data.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Concurrent Rust: matrix multiplication.</title>
        <published>2020-03-30T00:00:00+00:00</published>
        <updated>2020-03-30T00:00:00+00:00</updated>
        
        <author>
          <name>Boyd Johnson</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://boydjohnson.dev/blog/concurrency-matrix-multiplication/"/>
        <id>https://boydjohnson.dev/blog/concurrency-matrix-multiplication/</id>
        
        <content type="html" xml:base="https://boydjohnson.dev/blog/concurrency-matrix-multiplication/">&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Warning&lt;/strong&gt; There was &lt;a rel=&quot;external&quot; href=&quot;https://github.com/boydjohnson/examples-multithreading-distributed-programming/issues/1&quot;&gt;an issue on Github&lt;/a&gt; pointing out that Rayon’s &lt;code&gt;collect&lt;/code&gt; method preserves
ordering. I want to thank Github user &lt;code&gt;@claudiosfr&lt;/code&gt; for pointing out my error.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Let’s write some concurrent programs with &lt;a rel=&quot;external&quot; href=&quot;https://web.archive.org/web/20180713235500/https://github.com/rust-lang/rust/&quot;&gt;A Safe, Concurrent, Practical Language&lt;/a&gt;, Rust!. Taking examples from the first chapter of &lt;a rel=&quot;external&quot; href=&quot;https://www.amazon.com/Foundations-Multithreaded-Parallel-Distributed-Programming/dp/0201357526&quot;&gt;Foundations of Multithreaded, Parallel, and Distributed Programming&lt;/a&gt;, we will explain several concurrent Rust programs to solve
matrix multiplication.&lt;/p&gt;
&lt;p&gt;My impetus for doing this project is that I find thinking carefully about concurrent programs fun and with the COVID-19 pandemic I’ve had more time.
Check out &lt;a rel=&quot;external&quot; href=&quot;https://github.com/boydjohnson/examples-multithreading-distributed-programming&quot;&gt;the github repo&lt;/a&gt; for the programs mentioned in this blog post and possibly future ones.&lt;/p&gt;
&lt;h2 id=&quot;requirements&quot;&gt;Requirements&lt;a class=&quot;zola-anchor&quot; href=&quot;#requirements&quot; aria-label=&quot;Anchor link for: requirements&quot;&gt;🔗&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Let’s start with some requirements: we will stick to the standard library as much as possible, but if a library has some signification traction and staying power, we may choose to use that library. To promote code-reuse, all of our matrix multiplication routines will conform to the same api:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;    Fn&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;Vec&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;f64&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;Vec&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;f64&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; Vec&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;Vec&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;f64&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Note: Fn is a trait that is implemented for both functions and closures. &lt;a rel=&quot;external&quot; href=&quot;https://doc.rust-lang.org/std/ops/trait.Fn.html&quot;&gt;Rust docs on Fn&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;We will be making command line executables so that we can easily test with the &lt;code&gt;time&lt;/code&gt; bash function. We could write more formal benchmarks but coarse timings are good enough for this exercise.&lt;/p&gt;
&lt;h2 id=&quot;background&quot;&gt;Background&lt;a class=&quot;zola-anchor&quot; href=&quot;#background&quot; aria-label=&quot;Anchor link for: background&quot;&gt;🔗&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Now, the problem space: matrix multiplication. I made the following diagram to get a better handle on what is going on with matrix multiplication. Note: Loop 1 and Loop 2 can be transposed.&lt;/p&gt;
&lt;figure class=&quot;image&quot;&gt;
&lt;p&gt;&lt;img src=&quot;https://boydjohnson.dev/blog/concurrency-matrix-multiplication/matrix-multiplication-good.gif&quot; alt=&quot;Matrix Multiplication Gif&quot; /&gt;&lt;/p&gt;
&lt;/figure&gt;
&lt;h2 id=&quot;sequential-program&quot;&gt;Sequential program&lt;a class=&quot;zola-anchor&quot; href=&quot;#sequential-program&quot; aria-label=&quot;Anchor link for: sequential-program&quot;&gt;🔗&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;One of the book’s pseudocode examples has a sequential program for square matrix multiplication, reproduced here with
the i-loop transposed with the j-loop to fit our diagram.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Square matrix multiplication pseudocode&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;for&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;j &lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; 0&lt;/span&gt;&lt;span&gt; to n-1&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    for&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;i &lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; 0&lt;/span&gt;&lt;span&gt; to n-1&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-l-i z-d-3 z-d-i&quot;&gt;        #&lt;/span&gt;&lt;span class=&quot;z-l-4 z-l-i z-d-3 z-d-i&quot;&gt; compute inner product of a [i, *] and b [* , j]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;        c[i,&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; j]&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; 0.0&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        for&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;k &lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; 0&lt;/span&gt;&lt;span&gt; to n-1&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;            c[i,&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; j]&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; c[i,&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; j]&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; +&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; a[i,&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; k]&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; *&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; b[k&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; ,&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; j]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A faithful translation of these nested loops into Rust looks like this.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-l-i z-d-3 z-d-i&quot;&gt;///&lt;/span&gt;&lt;span class=&quot;z-l-4 z-l-i z-d-3 z-d-i&quot;&gt; Sequentially multiply A and B square matrices.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt; simple_multiply_a_b&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;Vec&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;f64&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; b&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;Vec&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;f64&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; Vec&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;Vec&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;f64&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    if&lt;/span&gt;&lt;span&gt; b&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;len&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt; ==&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; 0&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        return&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt; vec!&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; else&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        if&lt;/span&gt;&lt;span&gt; b&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;0&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;len&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt; ==&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; 0&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;            return&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt; vec!&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    if&lt;/span&gt;&lt;span&gt; a&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;len&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt; ==&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; 0&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        return&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt; vec!&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; else&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        if&lt;/span&gt;&lt;span&gt; a&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;0&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;len&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt; ==&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; 0&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;            return&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt; vec!&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-l-i z-d-3 z-d-i&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-4 z-l-i z-d-3 z-d-i&quot;&gt; Assert that a and b are square matrices of the same size.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;    assert_eq!&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;len&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; a&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;0&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;len&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;    assert_eq!&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;len&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; b&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;len&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;    assert_eq!&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;b&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;len&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; b&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;0&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;len&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;    let&lt;/span&gt;&lt;span&gt; n&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt; =&lt;/span&gt;&lt;span&gt; b&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;len&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;    let&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt; mut&lt;/span&gt;&lt;span&gt; c&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt; vec!&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;vec!&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;0&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;0_&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;f64&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span&gt; n&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span&gt; n&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    for&lt;/span&gt;&lt;span&gt; j&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; in&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;..&lt;/span&gt;&lt;span&gt;n&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        for&lt;/span&gt;&lt;span&gt; i&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; in&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;..&lt;/span&gt;&lt;span&gt;n&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;            for&lt;/span&gt;&lt;span&gt; k&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; in&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;..&lt;/span&gt;&lt;span&gt;n&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                c&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;i&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;j&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt; +=&lt;/span&gt;&lt;span&gt; a&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;i&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;k&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt; *&lt;/span&gt;&lt;span&gt; b&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;k&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;j&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    c&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The early returns at the beginning are dealing with the possibilitity that a and b have no elements. After compiling this function into
a command line program, we can test it on several sizes of square matrices.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; time&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; ./target/release/sequential-mm&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; 100&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;	0m0.009s&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;	0m0.004s&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;sys&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;	0m0.006s&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; time&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; ./target/release/sequential-mm&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; 1000&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;	0m3.602s&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;	0m3.582s&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;sys&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;	0m0.020s&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; time&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; ./target/release/sequential-mm&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; 1500&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;	0m13.787s&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;	0m13.766s&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;sys&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;	0m0.020s&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Let’s pull out the loops section from the larger function but write it with iterator combinators. We will transpose the outer loops
so that the resulting Vec iterates over the rows of the matrix. This makes it so that we can compare results in tests without further
modification.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt; compute_matrix_combinators&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; usize&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; a&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;Vec&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;f64&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; b&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;Vec&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;f64&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; Vec&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;Vec&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;f64&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    (&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;..&lt;/span&gt;&lt;span&gt;n&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;        .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;map&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;|&lt;/span&gt;&lt;span&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;|&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            (&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;..&lt;/span&gt;&lt;span&gt;n&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;                .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;map&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;|&lt;/span&gt;&lt;span&gt;j&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;|&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;..&lt;/span&gt;&lt;span&gt;n&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;map&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;|&lt;/span&gt;&lt;span&gt;k&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;|&lt;/span&gt;&lt;span&gt; a&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;i&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;k&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt; *&lt;/span&gt;&lt;span&gt; b&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;k&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;j&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;sum&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;                .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;collect&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;::&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;Vec&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;f64&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;        .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;collect&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;::&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;Vec&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;Vec&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;f64&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A coding decision we can make to make it clearer that the k elements of the i-th row of a are being multiplied by the k elements of the j-th column
of b is create a variable binding that names the i-th row of a.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt; compute_matrix_combinators&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; usize&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; a&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;Vec&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;f64&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; b&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;Vec&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;f64&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; Vec&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;Vec&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;f64&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    (&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;..&lt;/span&gt;&lt;span&gt;n&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;        .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;map&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;|&lt;/span&gt;&lt;span&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;|&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;            let&lt;/span&gt;&lt;span&gt; a_row&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span&gt;a&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;i&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            (&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;..&lt;/span&gt;&lt;span&gt;n&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;                .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;map&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;|&lt;/span&gt;&lt;span&gt;j&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;|&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;..&lt;/span&gt;&lt;span&gt;n&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;map&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;|&lt;/span&gt;&lt;span&gt;k&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;|&lt;/span&gt;&lt;span&gt; a_row&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;k&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt; *&lt;/span&gt;&lt;span&gt; b&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;k&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;j&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;sum&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;                .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;collect&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;::&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;Vec&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;f64&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;        .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;collect&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;::&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;Vec&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;Vec&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;f64&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We ultimately won’t be able to do the same thing with the j-th column of b, because of the Vec of Vec datastructure we used.&lt;/p&gt;
&lt;p&gt;The function &lt;code&gt;compute_matrix_combinators&lt;/code&gt; can be broken up further so the j-loop is in a separate function.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt; compute_matrix_combinators_2&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; usize&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; a&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;Vec&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;f64&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; b&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;Vec&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;f64&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; Vec&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;Vec&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;f64&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    (&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;..&lt;/span&gt;&lt;span&gt;n&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;        .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;map&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;|&lt;/span&gt;&lt;span&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;|&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;            let&lt;/span&gt;&lt;span&gt; a_row&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span&gt;a&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;i&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;            compute_row_of_sums&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;a_row&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; b&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; n&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;        .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;collect&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;::&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;Vec&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;Vec&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;f64&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt; compute_row_of_sums&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;a_row&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;f64&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; b&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;Vec&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;f64&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; n&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; usize&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; Vec&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;f64&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    (&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;..&lt;/span&gt;&lt;span&gt;n&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;        .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;map&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;|&lt;/span&gt;&lt;span&gt;j&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;|&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;..&lt;/span&gt;&lt;span&gt;n&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;map&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;|&lt;/span&gt;&lt;span&gt;k&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;|&lt;/span&gt;&lt;span&gt; a_row&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;k&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt; *&lt;/span&gt;&lt;span&gt; b&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;k&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;j&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;sum&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;        .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;collect&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;::&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;Vec&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;f64&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&quot;parallel-matrix-multiplication&quot;&gt;Parallel matrix multiplication&lt;a class=&quot;zola-anchor&quot; href=&quot;#parallel-matrix-multiplication&quot; aria-label=&quot;Anchor link for: parallel-matrix-multiplication&quot;&gt;🔗&lt;/a&gt;&lt;/h2&gt;
&lt;h3 id=&quot;parallelize-the-i-loop&quot;&gt;Parallelize the i-loop&lt;a class=&quot;zola-anchor&quot; href=&quot;#parallelize-the-i-loop&quot; aria-label=&quot;Anchor link for: parallelize-the-i-loop&quot;&gt;🔗&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;The &lt;em&gt;Foundations&lt;/em&gt; pseudocode for parallelizing the outer loop looks like:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;co&lt;/span&gt;&lt;span&gt; [i&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; to&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; n-1]&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-4 z-l-i z-d-3 z-d-i&quot;&gt; #&lt;/span&gt;&lt;span class=&quot;z-l-4 z-l-i z-d-3 z-d-i&quot;&gt; rows in parallel then&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    for&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;j &lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; 0&lt;/span&gt;&lt;span&gt; to n-1&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;        c[i,&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; j]&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; 0.0&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;        for&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;k &lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; 0&lt;/span&gt;&lt;span&gt; to n-1&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;            c[i,&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; j]&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; c[i,&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; j]&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; +&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; a[i,&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; k]&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; *&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; b[k&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; ,&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; j]&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;which just means that each i-loop can happen in parallel without specifying how it should be done.&lt;/p&gt;
&lt;p&gt;Since we have iterators, let’s turn to &lt;a rel=&quot;external&quot; href=&quot;https://docs.rs/rayon/1.3.0/rayon/&quot;&gt;Rayon&lt;/a&gt; to make those iterators parallel.&lt;/p&gt;
&lt;p&gt;The api of Rayon is not a drop-in replacement of &lt;code&gt;Iterator&lt;/code&gt;, but is highly similar. There are methods &lt;code&gt;Map&lt;/code&gt;, &lt;code&gt;Filter&lt;/code&gt;, &lt;code&gt;Fold&lt;/code&gt;, etc with slight modifications from &lt;code&gt;Iterator&lt;/code&gt;, the most important of which is the methods take &lt;code&gt;Fn&lt;/code&gt; closures instead of &lt;code&gt;FnMut&lt;/code&gt; closures. &lt;del&gt;We should keep in mind there will
be a second step in our &lt;code&gt;compute_matrix_combinators*&lt;/code&gt; function: sorting the rows so that they are back in order. This is common in concurrent applications
where there needs to be special care or a new component that orders the items that were out of order due to the concurrency.&lt;/del&gt;&lt;/p&gt;
&lt;p&gt;So compute the rows in any order, and then order the rows into ascending order:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt; compute_matrix_combinators_rayon&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; usize&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; a&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;Vec&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;f64&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; b&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;Vec&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;f64&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; Vec&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;Vec&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;f64&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    (&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;..&lt;/span&gt;&lt;span&gt;n&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;        .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;into_par_iter&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;        .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;map&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;move&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt; |&lt;/span&gt;&lt;span&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;|&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;            let&lt;/span&gt;&lt;span&gt; a_row&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span&gt;a&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;i&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;            compute_row_of_sums&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;a_row&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; b&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; n&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;        .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;collect&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Compiling that into a command-line program we get a speedup.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; time&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; ./target/release/sequential-mm&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; 500&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;	0m0.422s&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;	0m0.414s&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;sys&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;	0m0.009s&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; time&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; ./target/release/rayon-parallel-mm&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; 500&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;	0m0.162s&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;	0m0.578s&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;sys&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;	0m0.008s&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Notice that eventhough we get a speedup in real time, the user time is greater than the sequential case, so there is a tradeoff to be made
between use of resources and speed.&lt;/p&gt;
&lt;h3 id=&quot;parallelize-the-j-loop&quot;&gt;Parallelize the j-loop&lt;a class=&quot;zola-anchor&quot; href=&quot;#parallelize-the-j-loop&quot; aria-label=&quot;Anchor link for: parallelize-the-j-loop&quot;&gt;🔗&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;So we will do the same thing to &lt;code&gt;compute_row_of_sums&lt;/code&gt; that we did with &lt;code&gt;compute_matrix_combinators&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt; compute_row_of_sums_rayon&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;a_row&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;f64&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; b&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;Vec&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;f64&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; n&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; usize&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; Vec&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;f64&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    (&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;..&lt;/span&gt;&lt;span&gt;n&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;        .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;into_par_iter&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;        .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;map&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;|&lt;/span&gt;&lt;span&gt;j&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;|&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;..&lt;/span&gt;&lt;span&gt;n&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;map&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;|&lt;/span&gt;&lt;span&gt;k&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;|&lt;/span&gt;&lt;span&gt; a_row&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;k&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt; *&lt;/span&gt;&lt;span&gt; b&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;k&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;j&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;sum&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;        .&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;collect&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Composing this into a &lt;code&gt;compute_matrix_*&lt;/code&gt; function and compiling it into a command-line executable we don’t get a speedup,
and perhaps get somewhat of a slowdown.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; time&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; ./target/release/rayon-parallel-mm&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; 500&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;	0m0.162s&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;	0m0.578s&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;sys&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;	0m0.008s&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; time&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; ./target/release/rayon-par-par-mm&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; 500&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;	0m0.177s&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;	0m0.653s&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;sys&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;	0m0.009s&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; time&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; ./target/release/rayon-parallel-mm&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; 1000&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;	0m2.867s&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;	0m11.301s&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;sys&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;	0m0.056s&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; time&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; ./target/release/rayon-par-par-mm&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; -&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt; 1000&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;	0m2.999s&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;	0m11.841s&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;sys&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;	0m0.026s&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&quot;thoughts-on-the-whole-exercise&quot;&gt;Thoughts on the whole exercise&lt;a class=&quot;zola-anchor&quot; href=&quot;#thoughts-on-the-whole-exercise&quot; aria-label=&quot;Anchor link for: thoughts-on-the-whole-exercise&quot;&gt;🔗&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Rayon made this &lt;em&gt;significantly&lt;/em&gt; easier than creating our own threadpool (Task for another day …) or using probably any other threadpool
implementation. I’m sure a person could create faster code with SIMD or a gpu library, but wouldn’t have fit my purposes. My thoughts on the lack of
a speed up when parallelizing the j-loop is that the cost of sorting at the end of each inner loop (the costs of parallelization), outwieghs the parallelism that we get. If you notice something out of sorts create an issue on &lt;a rel=&quot;external&quot; href=&quot;https://github.com/boydjohnson/examples-multithreading-distributed-programming&quot;&gt;the github repo&lt;/a&gt;.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Debug for Fn, FnOnce, FnMut</title>
        <published>2020-01-14T00:00:00+00:00</published>
        <updated>2020-01-14T00:00:00+00:00</updated>
        
        <author>
          <name>Boyd Johnson</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://boydjohnson.dev/blog/impl-debug-for-fn-type/"/>
        <id>https://boydjohnson.dev/blog/impl-debug-for-fn-type/</id>
        
        <content type="html" xml:base="https://boydjohnson.dev/blog/impl-debug-for-fn-type/">&lt;p&gt;A good rule in Rust is that every struct should have &lt;code&gt;std::debug::Debug&lt;/code&gt;. But what if that struct contains closures.&lt;/p&gt;
&lt;p&gt;I initially had a struct that looked like:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt; struct&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; Move&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    from&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; CardIndex&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    to&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; CardIndex&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    on_after&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; Option&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;DisplayFn&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    on_after_reverse&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; Option&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;DisplayFn&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt; type&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; DisplayFn&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; Box&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;dyn&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; FnOnce&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;mut&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; Card&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The usecase for this code is a cardgame that has an undo button. When a card moves, its display may change, but when undo happens its display needs to be reversed.&lt;/p&gt;
&lt;p&gt;If we try to derive Debug for it, we will get a compiler error because FnOnce does not impl Debug.&lt;/p&gt;
&lt;p&gt;The solution is to declare a trait that has FnOnce and impl it for all functions that have FnOnce, and then impl Debug for that trait.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt; trait&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; DisplayFnT&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt; FnOnce&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;mut&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; Card&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;impl&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;F&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; DisplayFnT&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; for&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; F&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; where&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; F&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt; FnOnce&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;mut&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; Card&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;impl&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-2&quot;&gt; std&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-2&quot;&gt;fmt&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;Debug&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt; for&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; DisplayFnT&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt; fmt&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; f&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;mut&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-2&quot;&gt; std&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-2&quot;&gt;fmt&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;Formatter&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;_&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-2&quot;&gt; std&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-2&quot;&gt;fmt&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;Result&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;        write!&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;f&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt;DisplayFunction&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt; type&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; DisplayFn&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; Box&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;dyn&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; DisplayFnT&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then the final struct looks like:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;#&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-5 z-d-5&quot;&gt;derive&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;Debug&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-8 z-d-7&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt; struct&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; Move&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    from&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; CardIndex&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    to&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; CardIndex&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    on_after&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; Option&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;DisplayFn&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    on_after_reverse&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-8&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt; Option&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-2 z-d-1&quot;&gt;DisplayFn&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I got the solution to this problem from &lt;a rel=&quot;external&quot; href=&quot;https://users.rust-lang.org/t/is-it-possible-to-implement-debug-for-fn-type/14824&quot;&gt;this users.rust-lang.org question and answer.&lt;/a&gt;&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Custom map making with tilemill</title>
        <published>2019-09-11T00:00:00+00:00</published>
        <updated>2019-09-11T00:00:00+00:00</updated>
        
        <author>
          <name>Boyd Johnson</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://boydjohnson.dev/blog/custom-maps/"/>
        <id>https://boydjohnson.dev/blog/custom-maps/</id>
        
        <content type="html" xml:base="https://boydjohnson.dev/blog/custom-maps/">&lt;p&gt;In my previous blog post, the map was made using R. I have now been exploring &lt;a rel=&quot;external&quot; href=&quot;https://github.com/tilemill-project/tilemill&quot;&gt;Tilemill&lt;/a&gt;. I have made around 5 maps and have two maps to share here.&lt;/p&gt;
&lt;p&gt;The first map is a map of Minneapolis, MN libraries and their relationship with bus lines. The map is designed to be useful to those who would like to explore libraries that they are not familiar with and would like to use the buslines.&lt;/p&gt;
&lt;figure class=&quot;image&quot;&gt;
&lt;p&gt;&lt;img src=&quot;https://boydjohnson.dev/blog/custom-maps/minneapolis-public-libraries.png&quot; alt=&quot;Minneapolis public libraries&quot; /&gt;&lt;/p&gt;
&lt;figcaption&gt;Minneapolis Public Libraries Map&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;The second map is a map of the United States and Territories in 1870 featuring the locations of railroad lines.&lt;/p&gt;
&lt;figure class=&quot;image&quot;&gt;
&lt;p&gt;&lt;img src=&quot;https://boydjohnson.dev/blog/custom-maps/1870-railroads-final.png&quot; alt=&quot;1870 Railroads image&quot; /&gt;&lt;/p&gt;
&lt;figcaption&gt;Railroads in 1870 - US&lt;/figcaption&gt;
&lt;/figure&gt;</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Improving my GIS data science workflow by building tools</title>
        <published>2019-05-03T00:00:00+00:00</published>
        <updated>2019-05-03T00:00:00+00:00</updated>
        
        <author>
          <name>Boyd Johnson</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://boydjohnson.dev/blog/making-a-choropleth-with-cli-tools/"/>
        <id>https://boydjohnson.dev/blog/making-a-choropleth-with-cli-tools/</id>
        
        <content type="html" xml:base="https://boydjohnson.dev/blog/making-a-choropleth-with-cli-tools/">&lt;p&gt;I set myself to a project of exploring some data sets and to quickly make a choropleth that shows that exploration. When I do that I usually use command line tools like &lt;a rel=&quot;external&quot; href=&quot;https://github.com/dwtkns/gdal-cheat-sheet&quot;&gt;ogrinfo&lt;/a&gt;. The project goals were to identify areas where I couldn’t find a command line tool to use, and make that tool. I am exploring Minnesota Block Group level Census data in combination with location of educational institutions.&lt;/p&gt;
&lt;p&gt;First up, queries that I know how to do:&lt;/p&gt;
&lt;h3 id=&quot;non-spatial-summary-statistics-on-a-single-table&quot;&gt;non-spatial summary statistics on a single table&lt;a class=&quot;zola-anchor&quot; href=&quot;#non-spatial-summary-statistics-on-a-single-table&quot; aria-label=&quot;Anchor link for: non-spatial-summary-statistics-on-a-single-table&quot;&gt;🔗&lt;/a&gt;&lt;/h3&gt;
&lt;h4 id=&quot;min-max-avg-2016-population-estimate-for-each-block-group-in-minnesota&quot;&gt;Min, Max, Avg 2016 Population Estimate for each Block Group in Minnesota&lt;a class=&quot;zola-anchor&quot; href=&quot;#min-max-avg-2016-population-estimate-for-each-block-group-in-minnesota&quot; aria-label=&quot;Anchor link for: min-max-avg-2016-population-estimate-for-each-block-group-in-minnesota&quot;&gt;🔗&lt;/a&gt;&lt;/h4&gt;
&lt;figure class=&quot;image&quot;&gt;
&lt;p&gt;&lt;img src=&quot;https://boydjohnson.dev/blog/making-a-choropleth-with-cli-tools/ogrinfo-summary-statistics.gif&quot; alt=&quot;ogrinfo gif&quot; /&gt;&lt;/p&gt;
&lt;/figure&gt;
&lt;p&gt;I wondered about population in combination with the distance to the nearest educational institution. I chose Junior Colleges from my data set to be specific. This is where I am missing a tool that I can quickly leverage to make a spatial join, so I worked on building it. Enter &lt;code&gt;ndjson-spatial&lt;/code&gt;:&lt;/p&gt;
&lt;h3 id=&quot;spatial-metrics-on-two-tables&quot;&gt;spatial metrics on two tables&lt;a class=&quot;zola-anchor&quot; href=&quot;#spatial-metrics-on-two-tables&quot; aria-label=&quot;Anchor link for: spatial-metrics-on-two-tables&quot;&gt;🔗&lt;/a&gt;&lt;/h3&gt;
&lt;h4 id=&quot;distance-to-nearest-junior-college-from-centroid-of-block-group-minnesota&quot;&gt;Distance to nearest Junior College from centroid of Block Group, Minnesota&lt;a class=&quot;zola-anchor&quot; href=&quot;#distance-to-nearest-junior-college-from-centroid-of-block-group-minnesota&quot; aria-label=&quot;Anchor link for: distance-to-nearest-junior-college-from-centroid-of-block-group-minnesota&quot;&gt;🔗&lt;/a&gt;&lt;/h4&gt;
&lt;figure class=&quot;image&quot;&gt;
&lt;p&gt;&lt;img src=&quot;https://boydjohnson.dev/blog/making-a-choropleth-with-cli-tools/ndjson-spatial-optimized.gif&quot; alt=&quot;ndjson spatial tool gif&quot; /&gt;&lt;/p&gt;
&lt;/figure&gt;
&lt;h3 id=&quot;thematic-map-showing-distance-to-nearest-junior-college-multiplied-by-population-estimate&quot;&gt;Thematic Map showing distance to nearest Junior College multiplied by population estimate&lt;a class=&quot;zola-anchor&quot; href=&quot;#thematic-map-showing-distance-to-nearest-junior-college-multiplied-by-population-estimate&quot; aria-label=&quot;Anchor link for: thematic-map-showing-distance-to-nearest-junior-college-multiplied-by-population-estimate&quot;&gt;🔗&lt;/a&gt;&lt;/h3&gt;
&lt;figure class=&quot;image&quot;&gt;
&lt;p&gt;&lt;img src=&quot;https://boydjohnson.dev/blog/making-a-choropleth-with-cli-tools/minnesota-distance-to-junior-colleges.png&quot; alt=&quot;choropleth of MN distance to nearest junior college&quot; /&gt;&lt;/p&gt;
&lt;/figure&gt;</content>
        
    </entry>
</feed>
