blog

Personal blog
git clone git://git.laack.co/blog.git
Log | Files | Refs

commit c50febe00c377ba670e571a6c2867c61be20aa45
parent d93f936e62bb50acac65a815a9523f29ad156891
Author: Andrew Laack <andrew@laack.co>
Date:   Sun, 11 Oct 2026 17:41:10 -0500

Continued work

Diffstat:
Mposts/wip/basic-software-profiling-cpu-usage.md | 29++++++++++++-----------------
1 file changed, 12 insertions(+), 17 deletions(-)

diff --git a/posts/wip/basic-software-profiling-cpu-usage.md b/posts/wip/basic-software-profiling-cpu-usage.md @@ -1,10 +1,6 @@ -TODOS: - -- review the ending stuff - # Basic Software Profiling: CPU Usage -Collectively, we waste trillions of CPU cycles every day because software isn't profiled frequently. I believe this is an educational problem. +We waste trillions of CPU cycles every day because software isn't profiled frequently. I believe this is an educational problem. This post will explain how you can find the parts of a program hogging the most CPU time. The post will focus on compiled programs, but the core ideas can be applied to interpreted programs. @@ -176,13 +172,15 @@ Both of these questions are about decreasing work, not making the work faster. M ### Amfora -Amfora is a fancy terminal-based Gemini protocol browser with syntax highlighting. When I start profiling software in a well-trodden domain, I find it useful to use an oracle to understand what's possible. An oracle in this context is a program that performs comparable tasks with sensible performance. +[Amfora](https://github.com/makew0rld/amfora) is a fancy terminal-based Gemini protocol browser with syntax highlighting. Gemini is a text-based protocol which primarily distributes [gemtext](https://geminiprotocol.net/docs/gemtext.gmi) documents. These documents use a similar syntax to markdown, but a bit simpler. I chose this program to profile because software in the Gemini ecosystem is generally quite simple to understand, making it great for educational purposes. + +When I start profiling software I often find it useful to use an oracle to understand what's possible. An oracle in this context is a program that performs comparable tasks with decent performance characteristics. -For Amfora I chose Foreseen (my Gemini protocol browser) as the oracle as it has adequate performance, and performs a sensible subset of the tasks performed by Amfora, notably without the inclusion of syntax highlighting or character addressable coloring. +I chose [Foreseen](https://git.laack.co/foreseen/), a Gemini protocol browser I developed, as the oracle as it has adequate performance, and performs a sensible subset of the tasks performed by Amfora. -I normally start by testing out what I think will be the worst cases. I've found worst cases are often amplifications of average cases, so by improving worst case performance you can improve average case performance as well. +I normally start by searching for worst case performance scenarios. Worst cases are often an amplification of an average case, so by improving worst case performance you can often improve average case performance. -Let's evaluating the worst case for just rendering. We'll load a large text file from disk into both browsers, ending execution once text is drawn. I used this code to generate the file: +Let's look for the worst case for the task of rendering a textual document. We'll load a large text file from disk using both browsers, killing the program once the first lines of text are drawn. Since neither browser supports text streaming, they will both have to load and parse the document, and then draw some text to the terminal. To generate the document I executed the following Python code: ```python with open("lf.txt", "w") as f: @@ -190,9 +188,7 @@ with open("lf.txt", "w") as f: f.write(str(i) + "\n") ``` -The resulting file is ~1.3MB in size, less than the ~2MB max file size for Amfora, and the 5MB truncation size for foreseen. - -I then ran both programs killing each program with Ctrl-C at first draw of the text from the file: +The resulting file was ~1.3MB in size. I then ran both programs killing each program with Ctrl-C at first draw of the text from the file: ``` perf stat amfora lf.txt @@ -243,9 +239,9 @@ perf stat foreseen lf.txt 0.021186000 seconds sys ``` -This is very unscientific, but that's how I like to start profiling. Given how unscientific this methodology is, we can't conclude anything from this as the timings are within margin of error. It might be worth looking into this further in the future if nothing obvious is found. +This is very unscientific, especially given I was eyeballing when to kill the programs, but that's how I normally start profiling. We can't conclude anything from these results yet as the timings are within margin of error. -Since Amfora can highlight code, let's see how the performance changes when we add our python code block from above to the top of the file and rename the file to lf.gmi: +Since Amfora can highlight code, let's see how that impacts performance. I pasted the Python code block above at the top of the text file and then renamed the file to `lf.gmi`. We then find: ``` perf stat foreseen lf.gmi @@ -295,11 +291,10 @@ perf stat amfora lf.gmi 8.207151000 seconds sys ``` -That's a lot slower for Amfora. Let's look at a flamegraph for this. +That's a lot slower for Amfora (TODO: How much?). Let's look at a flamegraph for this. <object data="/images/amfora-before.svg" type="image/svg+xml" width="100%"></object> - So 68.34% samples occurred while executing the GC, that's a bit surprising, even given how awful garbage collectors are. We also see that 25.47% of the time, most of the remaining time, is spent in the RenderGemini function. Of this, most of it is spent on concatstring3, a function under the hood that's used to concatenate strings. Here's where that'd be used in the function: @@ -321,7 +316,7 @@ rendered += ren buf += strings.TrimSuffix(lines[i], "\r") + "\r\n" ``` -Well, there's our problem; each of these seemingly innocuous appends is actually very costly. Unlike sensible OO / procedural programming languages, Go strings are immutable so each of these operations will create an entirely new string each time, and since we accumulate into `buf` until reaching a preformatted text switch line ("```" lines), the file we were trying to open has to re-create the full string 200,000 times, since we only have the tail 200,000 lines in the file don't have a format switcher. +Well, there's our problem; each of these seemingly innocuous appends is actually very costly. Go strings are immutable so each of these operations will create an entirely new string each time, and since we accumulate into `buf` until reaching a preformatted text switch line ("```" lines), the file we were trying to open has to re-create the full string 200,000 times, since we only have the tail 200,000 lines in the file don't have a format switcher. This is relatively trivial to solve by using a strings.Builder which doesn't have this issue, and that's [what I did](https://github.com/makew0rld/amfora/pull/370/changes)*. After this change, we can perform our profiling again to find: