Skip navigation
Ghostty is getting automatic scrollback compression, resulting in 70 to 90% less physical memory…
0:00 / 3:33

Ghostty is getting automatic scrollback compression, resulting in 70 to 90% less physical memory…

Mitchell Hashimoto

@mitchellh245K followers

Visit on X
3,027 likesDownloadOriginal post

167,996 views

Ghostty is getting automatic scrollback compression, resulting in 70 to 90% less physical memory usage. It happens incrementally when idle, so it had no measurable effect on IO throughput. I'm not aware of any other mainstream terminal that does this. Demo video below! The gains let us increase the default scrollback limit from 10MB to 50MB, because on average a full scrollback will still compress smaller than the prior limit. More history, for free. ("Unlimited", disk-paged history is on the roadmap too) Let's talk about cool implementation details, cause this was fun. First, the data structure and memory layout ("PageList") I wrote two years ago finally pays off! One of its traits is that screen memory is backed by a linked list of page-aligned, page-sized (or page-multiple-sized) blocks. Because each block is page-aligned and page-sized, we can use madvise to discard its physical backing while keeping the virtual address space reserved. Compressed pages therefore disappear from resident memory, but decompression is still guaranteed because the address space remains valid and we simply fault new pages back in as needed. We use the same trick for our memory pools, too. Unallocated pool pages don't count as resident memory, saving another couple of MB per terminal. This functionality is also available to libghostty-vt consumers via new `ghostty_terminal_compress` APIs. The consumer decides when the appropriate time to compress is and the APIs advise on compressability.

44 Comments

Comments from the original post on X (Twitter)

  • Behdad Esfahbod's profile picture

    @behdadesfahbod

    vte / gnome-terminal have been doing gzip compression on the scrollback for decades. :)

    32 likes

  • Mitchell Hashimoto's profile picture

    @mitchellh

    Good to know! Thank you.

    10 likes

  • Peter "Coder AI Optimist" Marreck's profile picture

    @pmarreck

    Assuming that the content of the history is more or less consistent, do you think you could get even more compression “for free” by training a zstd dictionary on the first N lines of history and then using that dictionary for the next N*M lines? (Tuning N and M up to you)

    19 likes

  • Mitchell Hashimoto's profile picture

    @mitchellh

    Yes absolutely. I actually tried this and got up to 95% compression. But libghostty-vt has a no C dependency policy, so we're going with something we can hand-maintain first.

    50 likes

  • Peter "Coder AI Optimist" Marreck's profile picture

    @pmarreck

    inb4 I pilot an LLM-driven port of zstd to Zig 😆… I’ve already wrapped it for my zig stuff, but obviously that’s still C. I’d do it fully if I had the time

    1 like

  • Omar 🍋's profile picture

    @ocornut

    Kudos, I love that you are you are being careful not wasting memory while managing data that's already relatively small. (vs practically any Electron apps using 900 MB after I opened it for 30 seconds to send two messages).

    9 likes

  • Florian Brand's profile picture

    @xeophon

    i absolutely love how you find all these tiny things and then go down an insane rabbit hole

    7 likes

  • Tonksthebear's profile picture

    @tonksthebear

    PageList also made my lossless binary snapshots much easier for syncing state between clients. Excited to see how portable this work is for the WASM version so that my remote clients can get some memory savings as well

    7 likes

  • Mitchell Hashimoto's profile picture

    @mitchellh

    Yep, thats part of the design is that we can encode the memory directly and transfer and its accurate. We aren't leveraging that, sounds like you are :) but we will!

    7 likes

  • Tonksthebear's profile picture

    @tonksthebear

    Nice, ya it was extremely helpful for my multiplexer + remote clients. Thinking out loud, I wonder if your secret multiplexer is what will push state transfer into libghostty 👀

  • T-bone's profile picture

    @tchon

    This is an excellent technical post. Is there a draft chnageset we can view of the new scroll back compression? Thanks for doing this.

    3 likes

  • Rasmus Andersson's profile picture

    @rsms

    Neat hack!

    3 likes

  • rabib's profile picture

    @rabizzzy

    you’re spoiling us 🥹

    3 likes

  • Ignazio De Santis's profile picture

    @ignaziodes

    The part I'd want to see benchmarked is fault latency on the first scroll after a long idle period, since you're going from zero resident pages to decompressing potentially tens of MB synchronously. Did that end up being imperceptible or did you have to tune the compression algorithm for fast decompress over ratio?

    1 like

  • Mitchell Hashimoto's profile picture

    @mitchellh

    Yeah I benchmarked this heavily and had to tune the compression as you said. Our decompression speeds are something like 10 GB/s and a page is roughly 400 KB so it decompresses more than fast enough so we don’t even skip a frame. (Note all the compressed pages are in memory still too so disk isn’t a problem yet)

    1 like

  • Ignazio De Santis's profile picture

    @ignaziodes

    10 GB/s on 400KB pages - that's tight. Keeping decompression that fast while compressing well is the real tuning challenge.

  • Alberto Nunez-Garcia's profile picture

    @Alberto8793

    Lol I did not know I could be excited about a TUI. Nonetheless, here we are.

    1 like

  • Tyler C. Laprade, CFA's profile picture

    @TylerCLaprade

    This is awesome, thank you!

    1 like

  • 胡安马奥 🇺🇸's profile picture

    @juanmaocn

    blinking cursor... Now I know @mitchellh is probably really not caring the battery life.

    1 like

  • Mitchell Hashimoto's profile picture

    @mitchellh

    Oooooh I’m actually working on something around this. :) (Moving to a CPU timer plus CPU rendering while on battery for the cursor blink). Should be basically idle. We already drop our framerate down on idle battery though

    6 likes