Re:Advice for the next 100 Rust GUIs
by kragen on Sunday November 22, 2026 @08:58PM
(#50606562)
I get 760 megapixels per second. 0.76 or 1.06 gigapixels per second is definitely slower than my ballpark of 5 gigapixels per second, both on a single core, although it's still enough to hit 400fps on my 1920×1080 laptop and close to 120fps at the 4K resolution you're testing. But there are easily fixable reasons why it's so slow.
It looks like GCC is failing to vectorize your blend() call in the inner loop, which is one key factor that limits your fill rate. In the listing file, I see `movzbl (%rdx,%rax), %r13d; imull $65793, %r13d, %r13d` generated from blend() with `gcc -Wa,-adhlns=codedokode.lst $(pkg-config --cflags freetype2) -O2 -g -c codedokode.c`, using Debian GCC 12.2.0-14. That multiplier is 0x010101, converting grayscale to BGRA. If you want to alpha-blend your pixels, as blend() suggests, you definitely need to vectorize that. That's not rocket science. It's more or less why we got MMX back in the Pentium Pro days. You can see my code for doing it, using GCC vector extensions rather than SSE intrinsics, at https://gitlab.com/kragen/bubbleos/-/blob/master/spikes/veca....
Also, as you can easily see, your code has 2× overdraw, visiting the whole framebuffer roughly from beginning to end twice:
/* Render the whole text grid into the framebuffer. Every cell is blended
* (no early-out on zero alpha) so the cost per letter is constant. */
static void render_text(uint32_t *buf)
{
for (size_t i = 0; i < BUF_PIXELS; i++) {
buf[i] = BG_COLOR;
}
for (int row = 0; row < ROWS; row++) {
for (int col = 0; col < COLS; col++) {
const int idx = text[row][col] - FIRST_CHAR;
const int x0 = col * CELL_W;
const int y0 = row * LINE_H;
for (int y = 0; y < CELL_H; y++) {
uint32_t *dst = buf + (size_t)(y0 + y) * SCREEN_W + x0;
const uint8_t *src = glyph_cache[idx][y];
for (int x = 0; x < CELL_W; x++)
dst[x] = blend(/* dst[x] */BG_COLOR, FG_COLOR, src[x]);
}
}
}
}
That means two cache misses per cache line in the framebuffer. So every pixel has to travel from the CPU cache out to SDRAM once when it's set, then a second time from SDRAM back into the cache when setting some other pixels in its cache line results in a cache miss, and then a third time back from cache to SDRAM.
I think that if you want to get pixel fill rates exceeding memcpy() on the CPU, you'll need to fix those two problems. You aren't going to get memcpy()-exceeding rates with that strategy! But, if you fix them, you probably will.
> First, the data is sent to the monitor from a framebuffer. With compositors like Wayland, every window has its own buffer, and when it is damaged (updated), the compositors copies (memcpy) the damaged part from window buffer into the framebuffer using CPU or GPU. As I understand, this part cannot be skipped and the application renders window content into a window buffer so there always will be a memcpy stage to have it actually displayed.
I don't know much about Wayland, but this is definitely not true when you're rendering fullscreen on the GPU. I've heard there are ways to allocate your memory buffers such that you get zero-copy scanout (GBM_BO_USE_SCANOUT | GBM_BO_USE_LINEAR or DRM) but I haven't tried Wayland.
> Furthermore, to render the text you need to memcpy the glyphs from glyph cache into their locations. So rendering whole window of text boils down to lot of small memcpys.
Yes, certainly you need to do memcpy or similar if you're copying pixels from the glyph cache into the framebuffer. But memcpy() is only slow when it misses cache. In this example, which is fairly typical in displaying only 95 distinct glyphs (though not in having 27000 of them on the screen at once), the glyph cache is less than 30KiB. That's a quarter the size of my L1d cache! So it doesn't have to pull data in from main-memory SDRAM (or, worse, VRAM!) to fill up the framebuffer the way it does if you're reusing previously rendered lines. It can use the main-memory bus's whole bandwidth to write to the framebuffer instead of having to also read it.
> Also, you mentioned display lists but that requires extra memory compared to my approach.
It looks like GCC is failing to vectorize your blend() call in the inner loop, which is one key factor that limits your fill rate. In the listing file, I see `movzbl (%rdx,%rax), %r13d; imull $65793, %r13d, %r13d` generated from blend() with `gcc -Wa,-adhlns=codedokode.lst $(pkg-config --cflags freetype2) -O2 -g -c codedokode.c`, using Debian GCC 12.2.0-14. That multiplier is 0x010101, converting grayscale to BGRA. If you want to alpha-blend your pixels, as blend() suggests, you definitely need to vectorize that. That's not rocket science. It's more or less why we got MMX back in the Pentium Pro days. You can see my code for doing it, using GCC vector extensions rather than SSE intrinsics, at https://gitlab.com/kragen/bubbleos/-/blob/master/spikes/veca....
Also, as you can easily see, your code has 2× overdraw, visiting the whole framebuffer roughly from beginning to end twice:
That means two cache misses per cache line in the framebuffer. So every pixel has to travel from the CPU cache out to SDRAM once when it's set, then a second time from SDRAM back into the cache when setting some other pixels in its cache line results in a cache miss, and then a third time back from cache to SDRAM.I think that if you want to get pixel fill rates exceeding memcpy() on the CPU, you'll need to fix those two problems. You aren't going to get memcpy()-exceeding rates with that strategy! But, if you fix them, you probably will.
> First, the data is sent to the monitor from a framebuffer. With compositors like Wayland, every window has its own buffer, and when it is damaged (updated), the compositors copies (memcpy) the damaged part from window buffer into the framebuffer using CPU or GPU. As I understand, this part cannot be skipped and the application renders window content into a window buffer so there always will be a memcpy stage to have it actually displayed.
I don't know much about Wayland, but this is definitely not true when you're rendering fullscreen on the GPU. I've heard there are ways to allocate your memory buffers such that you get zero-copy scanout (GBM_BO_USE_SCANOUT | GBM_BO_USE_LINEAR or DRM) but I haven't tried Wayland.
> Furthermore, to render the text you need to memcpy the glyphs from glyph cache into their locations. So rendering whole window of text boils down to lot of small memcpys.
Yes, certainly you need to do memcpy or similar if you're copying pixels from the glyph cache into the framebuffer. But memcpy() is only slow when it misses cache. In this example, which is fairly typical in displaying only 95 distinct glyphs (though not in having 27000 of them on the screen at once), the glyph cache is less than 30KiB. That's a quarter the size of my L1d cache! So it doesn't have to pull data in from main-memory SDRAM (or, worse, VRAM!) to fill up the framebuffer the way it does if you're reusing previously rendered lines. It can use the main-memory bus's whole bandwidth to write to the framebuffer instead of having to also read it.
> Also, you mentioned display lists but that requires extra memory compared to my approach.
Yes, tens of kilobytes.