blog

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

commit 1714e7fc73c9b5239506caf3c3b422c8b3caebec
parent 03fd2ca12bb19466c8dfdd951e71a9d9461e899a
Author: Andrew Laack <andrew@laack.co>
Date:   Thu, 24 Sep 2026 10:27:26 -0500

Updated gemtext for pygamevraylib

Diffstat:
Mposts/gemini/pygame-vs-raylib.gmi | 54+++++++++++++++++++++++++++++++-----------------------
Aposts/wip/a-gemini-browser.md | 11+++++++++++
Aposts/wip/interesting-tests-ive-written.md | 31+++++++++++++++++++++++++++++++
3 files changed, 73 insertions(+), 23 deletions(-)

diff --git a/posts/gemini/pygame-vs-raylib.gmi b/posts/gemini/pygame-vs-raylib.gmi @@ -15,8 +15,8 @@ Prim's algorithm is worth understanding (albeit out of scope), and the problem i As it relates to the animation, the following two things should be apparent: -1. The first frame must draw every edge and vertex of the graph in a greyed-out color. -2. Each subsequent frame only has to draw one additional edge, the newly traversed edge, and one additional vertex, the vertex we are visiting. +* The first frame must draw every edge and vertex of the graph in a greyed-out color. +* Each subsequent frame only has to draw one additional edge, the newly traversed edge, and one additional vertex, the vertex we are visiting. While we could render each frame from scratch based on the graph's state, any reasonable rendering program has the ability to additively draw geometries to avoid this re-computation, and I care a bit about performance (even though this is written in Python*). Since PyGame and Raylib both support this, I'll be using additive rendering in both of my implementations. @@ -179,19 +179,21 @@ Despite this, the final `pygame.display.update()` is still using global state, w The benchmarks were executed under the following conditions: -- Vertex count: 2,500 -- Edge count: 25,000 -- Screen dimensions: 5120x1440 -- CPU: 11th Gen Intel(R) Core(TM) i5-1135G7 @ 2.40GHz (4 cores, 8 threads) -- iGPU: Intel Corporation TigerLake-LP GT2 \[Iris Xe Graphics\] -- OS: Arch Linux +* Vertex count: 2,500 +* Edge count: 25,000 +* Screen dimensions: 5120x1440 +* CPU: 11th Gen Intel(R) Core(TM) i5-1135G7 @ 2.40GHz (4 cores, 8 threads) +* iGPU: Intel Corporation TigerLake-LP GT2 \[Iris Xe Graphics\] +* OS: Arch Linux Additionally, the metrics are all **averages** across 10 full runs. +``` | Library | First frame time (ms) | Incremental frame time (ms) | | ------- | -------------------------- | --------------------------- | | Raylib | 29.87 ± 0.74 | 2.88 ± 1.11 | | PyGame | 323.39 ± 44.60 | 5.59 ± 0.65 | +``` In this benchmark Raylib's average incremental frame render time was ~2x faster than PyGame's, and its first frame render time was ~11x faster. @@ -278,10 +280,12 @@ if first: Running this updated code once we find: +``` | Library | Avg. Edge Draw Time (ns) | Avg. Vertex Draw Time (ns)| | ------- | ------------------------ | ------------------------- | | PyGame | 9504.15048 | 686.6508 | | Raylib | 944.15884 | 3016.1852 | +``` PyGame is faster at drawing vertices (circles), but slower at drawing edges (lines). Since we are drawing many more edges than we are vertices, this contributes to making PyGame slower. @@ -291,54 +295,58 @@ In the case of vertices, Raylib calls `DrawCircleSector()`, and this in turn app (There are ways to change the rendering pipeline for PyGame to make better use of GPUs, but these approaches are either complex or experimental) -#### Is that why PyGame is slower? +### Is that why PyGame is slower? That's not the fully story. Let's consider what the numbers above mean. The first frame renders 2,500 vertices, 25,000 edges. Each subsequent frame renders 1 vertex and 1 edge. If we multiply these numbers with the values we calculated for the amount of time each draw takes, we get the following expected frame times: +``` | Library | Expected First Frame Draw Time (ms)| Expected Subsequent Frame Draw Time (ms) | | ------- | ---------------------------------- | ---------------------------------------- | | PyGame | 239.3204 | 0.0101908 | | Raylib | 31.14443 | 0.003960344 | +``` Based on this, we see the first frame times are on par with what we actually saw in our benchmarking, but the subsequent frame draw times are orders of magnitude faster than what we benchmarked, and since most frames are subsequent frames, they are severely impacting performance. Let's see if we can figure out why this disparity exists. -##### PyGame Flamegraph +### PyGame Flamegraph => https://blog.laack.co/images/pygame-flamegraph.svg -##### Raylib Flamegraph +### Raylib Flamegraph => https://blog.laack.co/images/rlib-flamegraph.svg We see most of our time is being spent in `EndDrawing()` for Raylib and `pygame.display.update()` for Pygame. After instrumenting these calls we find the following times per invocation: +``` | Library | Function | Average Call Time (ms)| | ------- | ------------------------- | --------------------- | | PyGame | `EndDrawing()` | 5.677199 | | Raylib | `pygame.display.update()` | 2.680623 | +``` Based on our original frame time benchmarks, these calls make up almost all of the time spent rendering subsequent frames. -#### What Are These Functions Doing? +### What Are These Functions Doing? In the case of Raylib, EndDrawing does a few things: -1. Calls `rlDrawRenderBatchActive()` - - This clears the batched geometries queue, issuing the OpenGL calls -2. Calls `SwapScreenBuffer()` - - This swaps the buffer the display scans out. In effect, this ensures the frame we just drew becomes visible. - - This is done by with `glfwSwapBuffers()`, but this is beyond scope as it requires an understanding of OpenGL and double buffering. - - In the normal case (which is how our code uses it), this function returns after the instructions have been issued, not once the frame has been rendered. -3. Optionally waits to hit the target framerate in cases where rendering is happening faster than the target +Calls `rlDrawRenderBatchActive()` +* This clears the batched geometries queue, issuing the OpenGL calls +Calls `SwapScreenBuffer()` +* This swaps the buffer the display scans out. In effect, this ensures the frame we just drew becomes visible. +* This is done by with `glfwSwapBuffers()`, but this is beyond scope as it requires an understanding of OpenGL and double buffering. +* In the normal case (which is how our code uses it), this function returns after the instructions have been issued, not once the frame has been rendered. +Optionally waits to hit the target framerate in cases where rendering is happening faster than the target Most of the time spent here is on the second step. This is because issuing the instructions to swap will flush the driver's command stream, and once a few frames are in flight, this will block until the GPU retires one. Let's step through the PyGame `update()` function too: -1. Calls `pg_update()` in `src_c/display.c` - - This performs some basic checks to make sure a display is set -2. `pg_update()` calls `SDL_UpdateWindowSurfaceRects` - - This copies the buffer into a buffer owned by the windowing system (X11 in my case) +Calls `pg_update()` in `src_c/display.c` +* This performs some basic checks to make sure a display is set +`pg_update()` calls `SDL_UpdateWindowSurfaceRects` +* This copies the buffer into a buffer owned by the windowing system (X11 in my case) The main reason this is slower is PyGame copies the whole frame on the CPU for each frame while Raylib's frame is already in VRAM and the swap doesn't [blit](https://en.wikipedia.org/wiki/Bit_blit). diff --git a/posts/wip/a-gemini-browser.md b/posts/wip/a-gemini-browser.md @@ -0,0 +1,11 @@ +# A Gemini Browser (in C++) + +I've written gemini browsers before, but I never saw them through to completion; a silly proposition for such a simple task, but prior iterations lacked the structure necessary to see the project through. An earlier attempt, in Go, ended with a 500 line main file, some tests, and a bunch of weird state management. This time I'll be using TDD, a sensible object hierarchy, and general sensibilities. + +## Ncurses + +Ncurses is a great dependency (I know, dependency, yuck). It just makes sense without imposing stupid constraints. I can bind all of the keys I care about in the way I want, what's missing? Honestly, pretty much nothing (except maybe not great support for unicode, but I don't care about unicode, ascii ftw!!!). + +## Issues + +Redirects -- it's rather hard to find normal redirects. There's a stress test redirect thing, but that redirects to a redirect loop or smt. diff --git a/posts/wip/interesting-tests-ive-written.md b/posts/wip/interesting-tests-ive-written.md @@ -0,0 +1,31 @@ +# Interesting Tests I've Written + +## Prim's Algorithm + +### PBT Inadmissable + +Prim's algorithm. Created a comparison for random spanning tree inductions, starting at random vertex of connected graph, select random vertex that's already been visited, follow edges until all vertices have been visited. Assert rnd spanning tree weight <= mst weight. + +https://git.laack.co/abg/commit/4a265d34937ad339697218e4fd919b17b678ab6d.html + +The generalization of this is an algorithmic PBT where we assert an inadmissible heuristic is never better than an admissible heuristic. + +### Snapshots + +Defined toString() serialization functions for each of my objects, verifying that prior snapshot matches current snapshot. This was interesting because I realized converting from an unordered_map<int,Edge> => vector<Edge>, despite them representing the same data because the ints were from 0 -> n, this changed the ordering of my graph. Good reminder that unordered_map is not ordered, and led me down the rabit hole of figuring out how stable the ordering of unordered_maps actually are. This was interesting too because it reminded me I was relying on the structure of something that wasn't well defined, UB if you will. + +## Vet + +### E2E Time Test + +Time based e2e test for specific command. vet --help, and some other stuff was stupidly fucking slow because of imports of model providers so I wrote a test to assert this wasn't slow. This actually caught an issue with a PR someone submitted to me. + +This could've been a snapshot-type test too, but that'd have perhaps been too stringent / subject to variance. + +There is potential for issues w/ this type of test, but it seems to generally be within reason when the time deltas are very large, and your infra is pretty okay, even github action runners! + +## Watermelon Problem + +### Oracle + +Generation of inputs and outputs using a naive algorithm and then verifying io for the proper algorithm.