Hey,
as a small private project I’m thinking about building a TUI interface for managing my files (although there are already plenty out there). It should also be a good chance for learning some more Zig.
So far, I already have built some TUIs, but all with Rust and ratatui framework. And while its a great framework, its often heavy on CPU usage which might be due to stuff like allocating (just an unqualified guess)…
Now I’m wondering whats the best way to deal with that in a language like Zig with manual memory management. Since I’ve no experience with other such languages, it would be great to hear some hints/concepts/ideas etc.
I have not written any code yet, so there is nothing to show. But almost every TUI has a similar concept:
- a relativley fixed terminal window which represents kind of a grid (columns and rows). (I leave aside resizing the window for now)
- this grid renders chars/symbols and some escape codes (for coloring/cursor stuff etc.)
- this rendering is repeated constantly. E.g. on input, which changes the rendered interface, on tick, to automatically render changes happening through polling etc.
- rerendering is only fully stopped when the program ends (and all resources are released)
In the end this means the total size of bytes “allocated” by the grid has more or less an upper bound which can be calculated and is releases at the end of the program. Every rerendering should reuse/overwrite the former grid (or only parts of it, if only changed cells are written).
Normally, such a TUI rendering runs in a loop which tracks inputs and ticks. Thus, in my imagination it should be possible to pre-allocate some memory for the grid and reuse it on every loop-run instead of always allocating in-time and releasing before the next loop-round.
So far, I haven’t decided if I’ll use a TUI lib or if I should write the rendering code myself. This also depends on the thoughts regarding memory-architecure. I’ve read through some forum posts (e.g. this and this) and also looked through Zig docs.
But my experience is just too small to make a final decision. E.g. is StackFallbackAllocator or FixedBufferAllocator a valid option for such a case? Or should it be more flexible? And while I’m always willing to learn from mistakes, it would be hard to rewrite big portions only because the initial decision was wrong. Furthermore, I don’t want to use LLMs for that since I’m no big fan. I prefer to figer it out on my own with inter-human support by the community ![]()
I know this post is kind of unspecific (sorry for that). Nevertheless, I’m thankful for every hint. And be it only a link to a good explanation I’ve missed so far.