News for hackers. Stuff that matters.
Re:Advice for the next 100 Rust GUIs by codedokode on Sunday November 22, 2026 @09:46AM (#50600195)
I think immediate-mode UI is awful (does lot of useless work, redraws whole window on every mouse move). Also I think GPU support is not that important:

- first, GPU requires shaders compilation which is slow so you need CPU renderer anyway

- second, not all GPUs are supported, not all have drivers, some are buggy and many machines, like VM, do not have a GPU

- third, it probably uses lot of memory

You don't need a GPU to add one letter to a text field.

I did some tests in a GPU-less VM, using one x86-64 core. The test simply rendered black and white 4K frames. Rendering to memory gave numbers like 360 fps (and ~8GiB/s of RAM writes). Rendering to X server via SHM produced something about 80 fps, the X server used 50% of the core and the program used other 50%. But many users have much smaller screens, and you typically do not need to redraw whole window every frame.

So you can render everything on a CPU and, if the user has a GPU, use it to accelerate drawing rounded rectangles which make probably 90% of GUI.

Re:Advice for the next 100 Rust GUIs by vinyl7 on Sunday November 22, 2026 @10:03AM (#50600348)
Immediate mode UIs don't need to be redrawn on every mouse move: https://blickeditor.com/blog/caching-an-immediate-mode-ui?la...
Re:Advice for the next 100 Rust GUIs by codedokode on Sunday November 22, 2026 @10:14AM (#50600424)
This wastes memory, and if you know which parts did not change, you could as well simply not re-render them. When using Wayland, you have 2 buffers that you render your window into, and they already contain previous window content so why make a cache?
Re:Advice for the next 100 Rust GUIs by ben-schaaf on Sunday November 22, 2026 @10:08AM (#50600386)
Pegging the CPU while scrolling - a common action that takes a full rerender - is not good enough in this day and age. Your best case scenario still doesn't achieve the minimum for a 4k120hz display. Take into account text with subpixel-antialiasing, and that resolutions scale to ok now and you'll quickly find CPU rendering unusably slow.

Shader compilation basically doesn't matter. The number of shaders needed for a GUI toolkit is tiny, and none are particularly complex. In my experience it doesn't appreciably add to the startup time.

I do agree there should still be CPU rendering for when the GPU doesn't work. It's a particularly useful feature for old or virtualised hardware.

Re:Advice for the next 100 Rust GUIs by codedokode on Sunday November 22, 2026 @10:11AM (#50600401)
Scrolling should not take a full render - you ideally only need to render a small stripe that becomes visible.

For example, if you are scrolling a PDF you could theoretically "scroll" (change start y offset) of the window buffer, render a newly visible stripe and re-render the toolbar which otherwise would scroll with the window.

Re:Advice for the next 100 Rust GUIs by kragen on Sunday November 22, 2026 @11:15AM (#50600960)
Surprisingly, it's often faster to do a full render than to copy pixels around in the framebuffer, for two reasons.

1. With discrete GPUs, which have their own VRAM (unlike integrated GPUs), reading the framebuffer is often much slower than reading main memory.

2. If you're copying pixels around in the framebuffer, the best you can do is memcpy speed, which spends half of the memory bus bandwidth on reading memory. But if you're computing fresh pixels to put in the framebuffer instead, you can go about twice that fast. Sometimes more than twice that fast. As a simple example, https://canonical.org/~kragen/sw/bgraify.c unpacks 1-bit-deep bitmaps with a palette of two 32-bit colors on one core at 26 gigabytes per second (on this Ryzen 5 3500U), while memcpy() on that same core can only reach about 10 gigabytes per second. (memcpy() on two cores reaches more like 18 gigabytes per second, so the naïve extrapolation is that you ought to be able to hit 36 gigabytes per second for something like a solid-color fill.)

Re:Advice for the next 100 Rust GUIs by codedokode on Sunday November 22, 2026 @11:27AM (#50601065)
I do not see where I suggested to copy the pixels with CPU. With compositors like Wayland, you have a buffer in memory that stores content of a window. Obviously writing a 6-pixel stripe into that buffer (and updating starting Y offset) is faster than over-writing the whole buffer. Now, if that buffer is directly accessibly by GPU, you do not need to copy nothing. And I assume that even poor discrete GPUs can copy that 6 rows with pixels from system RAM to their memory for compositing.
Re:Advice for the next 100 Rust GUIs by kragen on Sunday November 22, 2026 @11:39AM (#50601203)
I'm sorry about the URL; I've fixed it now. Let me know if it's still broken.

Yes, you can scroll by telling the GPU to use a different starting address for its framebuffer, but only if you're scrolling the whole screen. If you want to scroll the contents of a browser window but not the browser chrome, though, you start needing to tell the GPU to composite things from a few different textures, or needing to copy things around in system RAM. If you push this work off on a software compositor like Wayland, you aren't saving any work; you're just changing which process does the work.

Re:Advice for the next 100 Rust GUIs by codedokode on Sunday November 22, 2026 @11:51AM (#50601353)
> you start needing to tell the GPU to composite things from a few different textures,

Yes, correct, you need to have two buffers - one for the document, another for the toolbar. In this case you need to do compositing. The good thing is that compositing here is just memcpy, and it can be done on CPU and almost on any GPU starting from 90s, no need for Vulkan support or NVIDIA proprietary driver. And compositing (memcpy) is faster than rendering complicated document from scratch. So instead of sending one large buffer to Wayland, you send two smaller buffers ("send" doesn't imply copying here as Wayland uses shared memory).

URL works now.

Re:Advice for the next 100 Rust GUIs by kragen on Sunday November 22, 2026 @12:04PM (#50601501)
> And compositing (memcpy) is faster than rendering complicated document from scratch.

Again, I don't think this is generally true, and that was the specific point I was investigating with bgraify. You can potentially render a complicated document from scratch, on the CPU, at memset() speeds, which are much faster than memcpy() speeds. And on the GPU you can do it at texture-mapping speeds, which are much faster still. (The basic plan is that you put the glyphs of your font into a texture atlas which your fragment shader texture-maps, possibly using SDF thresholding.) https://github.com/rxi/microui is an example of a very small, simple IMGUI library whose standard backend works this way on top of OpenGL.

Re:Advice for the next 100 Rust GUIs by codedokode on Sunday November 22, 2026 @12:10PM (#50601575)
Let's say you scroll text. I believe rendering one line of text (that became visible) on each frame, and reusing previously rendered lines is faster than rendering all text on every frame. Text rendering is not an easy task.

And as I have written above, you can offload compositing (memcpy) to any GPU from 90s while rendering letters is a harder task. Even with glyph cache you need to manage it, upload updates to GPU etc. So much easier to not use GPU for this.

Re:Advice for the next 100 Rust GUIs by kragen on Sunday November 22, 2026 @12:34PM (#50601841)
I don't believe that reusing previously rendered lines is faster than rendering all text on every frame.

Text rendering can be made arbitrarily complex, but in most cases it is not. In Russian, most Spanish, most Japanese, Ukrainian, or English, code points map 1:1 to glyphs, and figuring out where each glyph belongs on a line is a matter of running a prefix sum over their advances. AVX-512 prefix sum on 32-element vectors is about 2 nanoseconds per 32-element vector, so you need about 5 nanoseconds of layout time per line and about 200 nanoseconds for the whole screen, say 250ns to include the time to check for exceptional cases and figure out where each line belongs on the screen. This gives you a display list of some 3000 glyphs at various places on the screen.

If you rerender 2 megapixels from that display list at, say, 5 gigapixels per second on the CPU, it's going to take 400μs, which is 400,000ns. (I think the evidence I've shown above strongly suggests that this is doable on my laptop here, although it isn't a proof.) If, instead, you copy 2 megapixels from one place to another in RAM, you can only do that at about 2.5 gigapixels per second, so reusing previously rendered lines will take more like 800μs. That is slower than 400μs. It is not faster than 400μs.

If you, instead, pass the display list to the GPU to texture-map from the font atlas, well, this GPU has a theoretical peak texture fill rate of 38.4 gigatexels per second, so it will take more like 52 μs. On the GPU, you can't really reuse the previously rendered lines. Again, 800μs is slower than 52 μs. It's not faster.

The GPU doesn't really mind rendering 3000 quads instead of two or three.

It's true that you need to manage the texture atlas, glyph cache, etc., although multichannel SDF fonts can provide reasonable antialiased results at a fairly wide range of pixel sizes, which can reduce this load quite a lot.

Re:Advice for the next 100 Rust GUIs by codedokode on Sunday November 22, 2026 @01:30PM (#50602419)
I believe your calculations have errors.

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.

For example, if the button changed its color, the program updates button image in window buffer, reports the damaged rectangle and the compositor copies that change into the framebuffer to become visible.

Actually compositors use two buffers to prevent tearing, and switch between them on every update so the things might be more complicated.

So with this model in mind, updating one line of text in a window buffer is faster than updating the whole buffer.

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.

Also, you mention AVX-512 but I don't think my CPU has it. And when doing lot of 256-bit SIMD the CPU starts throttling to 1GHz-level speed. Also, you mentioned display lists but that requires extra memory compared to my approach.

Also, I decided to try to actually benchmark rendering text from a glyph atlas. Luckily, LLMs can write such code in matter of minutes, so I wrote it: https://pastebin.com/7baP4tWv

Results in a VM without a GPU:

Buffer 3840 x 2160 px, grid 295 cols x 90 rows = 26550 letters/frame, 10 runs, avg per frame: 7.832 ms, time per letter: 295.0 ns, letters/second: 3390088, pixels/second: 1059.086 Mpx/s.

Re:Advice for the next 100 Rust GUIs by kragen on Sunday November 22, 2026 @02:29PM (#50603028)
Thanks! This is very interesting; I'll take a look.
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.

Yes, tens of kilobytes.

Re:Advice for the next 100 Rust GUIs by ben-schaaf on Sunday November 22, 2026 @06:52PM (#50605444)
This saves you some work of drawing glyphs, but you now need to blit that whole buffer. So you've gone from a single color background fill and rendering a full page of text, to a offset framebuffer copy and then a small partial render. In terms of memory bandwidth I don't think you're coming out on top here.
Re:Advice for the next 100 Rust GUIs by kragen on Sunday November 22, 2026 @11:08AM (#50600912)
Even without the GPU, you don't have to peg the CPU when scrolling a 4K 120Hz display. 3840 pixels/row × 2160 rows/frame × 120 frames/second × 4 bytes/pixel is 4 gigabytes per second. The laptop I'm typing this on can memcpy() to main memory at 10 gigabytes per second on one core and 17–19 gigabytes per second on two cores. So we're talking about more like a 25% CPU load if we can avoid extra copies, maybe better than that if most of the work is something like colormapping 8-bit grayscale font-glyph pixels.

Using a GPU is of course much better, but not necessary to reach the level of performance you're talking about.

Re:Advice for the next 100 Rust GUIs by grg0 on Sunday November 22, 2026 @05:45PM (#50604780)
4 bytes per pixel? You forgot HDR!

But jokes aside, the math is sound and people underestimate or simply do not understand how much a CPU can lift with the right data structures and vectorization.

Re:Advice for the next 100 Rust GUIs by ben-schaaf on Sunday November 22, 2026 @06:41PM (#50605345)
Real applications don't use 8 bit grayscale font glyphs; there's colour glyphs and subpixel Antialiasing, and subpixel positioning. Commonly you're storing 3 channel alpha (or color), and 6 copies of each glyphs at different subpixel positions. You're not blitting these glyphs, you're blending them with the background. Font atlases do not fit in cache, especially at 4k. You also need to not just write the framebuffer, but also send it to the GPU. On some systems that can be done quickly, on others you're going over PCIe. And on top of all that, you have the data structures that are telling you what text to render where, which further fill the cache.

All that to say, your example of blitting a single color is very, very far away from the practical reality of rendering a GUI.

Re:Advice for the next 100 Rust GUIs by kragen on Sunday November 22, 2026 @10:59AM (#50600834)
> I think immediate-mode UI is awful (does lot of useless work, redraws whole window on every mouse move). Also I think GPU support is not that important:

If you're concerned about efficiency, GPU support is very important. But IMGUI is not the efficiency enemy you think it is; while IMGUI libraries do need to traverse the hierarchy of visible objects on every mouse movement, there are generally only a few dozen rectangles and a few thousand letters and other graphical objects visible, so the work involved in traversing them is generally very small amount compared to copying a few million pixels into the framebuffer.

As an example, I currently have a browser viewing HN and an Emacs window open, and they total about 3000 characters, a couple dozen icons, and another dozen or so roundrects. But my screen is 2 million pixels. So the amount of per-drawable work has to be almost three orders of magnitude larger before the per-pixel work isn't the dominant factor.

IMGUI libraries do have to do a bit more work to avoid redrawing all those pixels on every mouse movement, but many popular IMGUI libraries, such as React, do in fact do that work. It's a somewhat less simple job than for retained-mode GUI libraries like Qt (or the browser DOM), but the added complexity in the IMGUI library often pays off in simpler and more reliable apps.

Re:Advice for the next 100 Rust GUIs by codedokode on Sunday November 22, 2026 @11:08AM (#50600910)
> there are generally only a few dozen rectangles

Only in a hello world application. Every professional editor like GIMP has tons of tiny widgets. And redrawing one tiny widget on mouse over is faster than running the render function.

Also, you are assuming that running render function costs nothing while in real-world apps there will be array iteration (for example, to display a sum and average of values from a 100K element array), calling helper functions, generating SVG for a 10K points graph, etc.

Yes, writing good code is harder than bad code, especially for a LLM.

Re:Advice for the next 100 Rust GUIs by kragen on Sunday November 22, 2026 @11:34AM (#50601143)
You say I'm talking about a hello-world application, but I'm talking about Firefox, Emacs, and my window manager. Firefox has 10 tabs visible, 21 icons in the toolbar, five bookmarks, and about 20 HTML elements. This still adds up to a few dozen rectangles, not thousands or even hundreds.

You say I'm assuming that the render function costs nothing. I thought it was clear that I was not assuming that; what I said in the comment that you're responding to is, "The amount of per-drawable work has to be almost three orders of magnitude larger before the per-pixel work isn't the dominant factor." But you may have responded before I added that; I forget if I added it in later. Here by "drawable" I mean talking about things like letters, not widgets that have their own render functions. Widgets that have their own render functions can take more than four orders of magnitude more time than the per-pixel work before the per-widget work becomes the dominant factor.

It's true, as you say, that people are capable of using up that margin by doing things like reading from a file, because writing good code is harder than writing bad code. But that's not really a question of immediate-mode vs. retained-mode. You can write bad code with retained-mode GUI libraries, too. In fact, my experience is that it's much harder to write good code with retained-mode GUI libraries.

As for layout calculations, the layout engine I elicited from Opus 5.5 for http://canonical.org/~kragen/sw/megilloom-sandbox lays out 60,000 lines of text per second, in JavaScript, and it supports table layout. (It caches the layout calculations so that it can remain responsive up to hundreds of thousands of lines.)

Some IMGUI libraries are capable of providing a dirty flag and damage rectangles without requiring every application to duplicate the error-prone cache-invalidation logic to do that. As Raph points out in this post, they're not a panacea. But they're not as bad as you are naïvely assuming them to be.

Re:Advice for the next 100 Rust GUIs by codedokode on Sunday November 22, 2026 @11:37AM (#50601176)
> the error-prone cache-invalidation logic

Because writing good code is harder than bad code.

Re:Advice for the next 100 Rust GUIs by kragen on Sunday November 22, 2026 @11:41AM (#50601228)
Generally speaking, what distinguishes good code from bad code is that it minimizes the number of error-prone things that must be done. As Frank Borman said, "A superior pilot uses his superior judgment to avoid situations which require the use of his superior skill."
Re:Advice for the next 100 Rust GUIs by codedokode on Sunday November 22, 2026 @12:15PM (#50601641)
I agree we need to minimize programming errors - by designing a good reactive retained mode framework.